Subversion Repositories NaviCtrl

Rev

Rev 421 | Rev 427 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 421 Rev 426
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <stdio.h>
56
#include <stdio.h>
57
#include "91x_lib.h"
57
#include "91x_lib.h"
58
#include "main.h"
58
#include "main.h"
59
#include "timer1.h"
59
#include "timer1.h"
60
#include "uart1.h"
60
#include "uart1.h"
61
#include "kml.h"
61
#include "kml.h"
62
#include "gpx.h"
62
#include "gpx.h"
63
#include "ssc.h"
63
#include "ssc.h"
64
#include "settings.h"
64
#include "settings.h"
65
#include "led.h"
65
#include "led.h"
66
#include "logging.h"
66
#include "logging.h"
67
 
67
 
68
#define MIN_SD_INTERVAL_KML  200
68
#define MIN_SD_INTERVAL_KML  200
69
#define MIN_SD_INTERVAL_GPX  500
69
#define MIN_SD_INTERVAL_GPX  500
70
 
70
 
71
u8 SD_LoggingError = 0;
71
u8 SD_LoggingError = 0;
72
 
72
 
73
#define LOG_FLUSH_INTERVAL 20000 // 20s   //##
73
#define LOG_FLUSH_INTERVAL 20000 // 20s   //##
74
 
74
 
75
typedef enum
75
typedef enum
76
{
76
{
77
        LOGFILE_IDLE,
77
        LOGFILE_IDLE,
78
        LOGFILE_START,
78
        LOGFILE_START,
79
        LOGFILE_CLOSED,
79
        LOGFILE_CLOSED,
80
        LOGFILE_OPENED,
80
        LOGFILE_OPENED,
81
        LOGFILE_ERROR
81
        LOGFILE_ERROR
82
} logfilestate_t;
82
} logfilestate_t;
83
 
83
 
84
// logger handler prototypes
84
// logger handler prototypes
85
logfilestate_t Logging_KML(u32 LogDelay);
85
logfilestate_t Logging_KML(u32 LogDelay);
86
logfilestate_t Logging_GPX(u32 LogDelay);
86
logfilestate_t Logging_GPX(u32 LogDelay);
87
 
87
 
88
LogCfg_t LogCfg = {500 , 1000};
88
LogCfg_t LogCfg = {500 , 1000};
89
u32 Logged_GPX_Counter = 0, Logged_KML_Counter = 0;
89
u32 Logged_GPX_Counter = 0, Logged_KML_Counter = 0;
90
 
90
 
91
 
91
 
92
//----------------------------------------------------------------------------------------------------
92
//----------------------------------------------------------------------------------------------------
93
s8* GenerateKMLLogFileName(void)
93
s8* GenerateKMLLogFileName(void)
94
{
94
{
95
        static u16 filenum = 0; // file name counter
95
        static u16 filenum = 0; // file name counter
96
        static s8 filename[35];
96
        static s8 filename[35];
97
        static DateTime_t LastTime = {0,0,0,0,0,0,0,0};
97
        static DateTime_t LastTime = {0,0,0,0,0,0,0,0};
98
 
98
 
99
        if(SystemTime.Valid)
99
        if(SystemTime.Valid)
100
        {
100
        {
101
                // if the day has been changed
101
                // if the day has been changed
102
                if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day))
102
                if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day))
103
                {
103
                {
104
                        LastTime.Year = SystemTime.Year;
104
                        LastTime.Year = SystemTime.Year;
105
                        LastTime.Month = SystemTime.Month;
105
                        LastTime.Month = SystemTime.Month;
106
                        LastTime.Day = SystemTime.Day;
106
                        LastTime.Day = SystemTime.Day;
107
                        LastTime.Valid = 1;
107
                        LastTime.Valid = 1;
108
                        filenum = 0; // reset file counter
108
                        filenum = 0; // reset file counter
109
                }
109
                }
110
                sprintf(filename, "LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
110
                sprintf(filename, "LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
111
                filenum++;
111
                filenum++;
112
                return filename;
112
                return filename;
113
        }
113
        }
114
        else return NULL;
114
        else return NULL;
115
}
115
}
116
 
116
 
117
//----------------------------------------------------------------------------------------------------
117
//----------------------------------------------------------------------------------------------------
118
s8* GenerateGPXLogFileName(void)
118
s8* GenerateGPXLogFileName(void)
119
{
119
{
120
        static u16 filenum = 0; // file name counter
120
        static u16 filenum = 0; // file name counter
121
        static s8 filename[35];
121
        static s8 filename[35];
122
        static DateTime_t LastTime = {0,0,0,0,0,0,0,0};
122
        static DateTime_t LastTime = {0,0,0,0,0,0,0,0};
123
 
123
 
124
        if(SystemTime.Valid)
124
        if(SystemTime.Valid)
125
        {
125
        {
126
                // if the day has been changed
126
                // if the day has been changed
127
                if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day))
127
                if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day))
128
                {
128
                {
129
                        LastTime.Year = SystemTime.Year;
129
                        LastTime.Year = SystemTime.Year;
130
                        LastTime.Month = SystemTime.Month;
130
                        LastTime.Month = SystemTime.Month;
131
                        LastTime.Day = SystemTime.Day;
131
                        LastTime.Day = SystemTime.Day;
132
                        LastTime.Valid = 1;
132
                        LastTime.Valid = 1;
133
                        filenum = 0; // reset file counter
133
                        filenum = 0; // reset file counter
134
                }
134
                }
135
                sprintf(filename, "LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
135
                sprintf(filename, "LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
136
                filenum++;
136
                filenum++;
137
                return filename;
137
                return filename;
138
        }
138
        }
139
        else return NULL;
139
        else return NULL;
140
}
140
}
141
 
141
 
142
 
142
 
143
 
143
 
144
//----------------------------------------------------------------------------------------------------
144
//----------------------------------------------------------------------------------------------------
145
// logs the current gps position to a kml file
145
// logs the current gps position to a kml file
146
logfilestate_t Logging_KML(u32 LogDelay)
146
logfilestate_t Logging_KML(u32 LogDelay)
147
{
147
{
148
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
148
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
149
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
149
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
150
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
150
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
151
        static  KML_Document_t logfile;                                         // the logfilehandle
151
        static  KML_Document_t logfile;                                         // the logfilehandle
152
 
152
 
153
        // initialize if LogDelay is zero
153
        // initialize if LogDelay is zero
154
        if(!LogDelay)
154
        if(!LogDelay)
155
        {
155
        {
156
                switch(logfilestate)
156
                switch(logfilestate)
157
                {
157
                {
158
                        case LOGFILE_OPENED:
158
                        case LOGFILE_OPENED:
159
                                KML_DocumentClose(&logfile); // try to close it
159
                                KML_DocumentClose(&logfile); // try to close it
160
                                break;
160
                                break;
161
                        default:
161
                        default:
162
                                break;
162
                                break;
163
                }
163
                }
164
                logfilestate = LOGFILE_IDLE;
164
                logfilestate = LOGFILE_IDLE;
165
                logfilename = NULL;
165
                logfilename = NULL;
166
                KML_DocumentInit(&logfile);
166
                KML_DocumentInit(&logfile);
167
                logtimer = SetDelay(0); // set logtimer to now
167
                logtimer = SetDelay(0); // set logtimer to now
168
                return logfilestate;
168
                return logfilestate;
169
        }
169
        }
170
        // no init
170
        // no init
171
        if(CheckDelay(logtimer))
171
        if(CheckDelay(logtimer))
172
        {
172
        {
173
                logtimer = SetDelay(LogDelay);  // standard interval
173
                logtimer = SetDelay(LogDelay);  // standard interval
174
 
174
 
175
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN)
175
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN)
176
                {
176
                {
177
                        switch(logfilestate)
177
                        switch(logfilestate)
178
                        {
178
                        {
179
                                case LOGFILE_IDLE:
179
                                case LOGFILE_IDLE:
180
                                case LOGFILE_CLOSED:
180
                                case LOGFILE_CLOSED:
181
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.StatusFlags & FC_STATUS_FLY))
181
                                        if((GPSData.Status != INVALID) && (GPSData.NumOfSats > 2) && /*(GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)*/ (FC.StatusFlags & FC_STATUS_FLY))
182
                                        {
182
                                        {
183
                                                logfilestate = LOGFILE_START;
183
                                                logfilestate = LOGFILE_START;
184
                                        }
184
                                        }
185
                                        break;
185
                                        break;
186
                                case LOGFILE_START:
186
                                case LOGFILE_START:
187
                                        // find unused logfile name
187
                                        // find unused logfile name
188
                                        do
188
                                        do
189
                                        {        // try to generate a new logfile name
189
                                        {        // try to generate a new logfile name
190
                                                 logfilename = GenerateKMLLogFileName();
190
                                                 logfilename = GenerateKMLLogFileName();
191
                                        }while((logfilename != NULL) && fexist_(logfilename));
191
                                        }while((logfilename != NULL) && fexist_(logfilename));
192
                                        // if logfilename exist
192
                                        // if logfilename exist
193
                                        if(logfilename != NULL)
193
                                        if(logfilename != NULL)
194
                                        {
194
                                        {
195
                                                // try to create the log file
195
                                                // try to create the log file
196
                                                if(KML_DocumentOpen(logfilename, &logfile))
196
                                                if(KML_DocumentOpen(logfilename, &logfile))
197
                                                {
197
                                                {
198
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
198
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
199
                                                        logfilestate = LOGFILE_OPENED; // goto next step
199
                                                        logfilestate = LOGFILE_OPENED; // goto next step
200
                                                        UART1_PutString("\r\nOpening kml-file:");
200
                                                        UART1_PutString("\r\nOpening kml-file:");
201
                                                        UART1_PutString(logfilename);
201
                                                        UART1_PutString(logfilename);
202
                                                        UART1_PutString("\r\n");
202
                                                        UART1_PutString("\r\n");
203
                                                }
203
                                                }
204
                                                else // could not be openend
204
                                                else // could not be openend
205
                                                {
205
                                                {
206
                                                        logfilestate = LOGFILE_ERROR;
206
                                                        logfilestate = LOGFILE_ERROR;
207
                                                        UART1_PutString("\r\nError opening kml-file: ");
207
                                                        UART1_PutString("\r\nError opening kml-file: ");
208
                                                        UART1_PutString(logfilename);
208
                                                        UART1_PutString(logfilename);
209
                                                        UART1_PutString("\r\n");
209
                                                        UART1_PutString("\r\n");
210
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
210
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
211
                                                        SD_LoggingError = 1;
211
                                                        SD_LoggingError = 1;
212
                                                }
212
                                                }
213
                                        }
213
                                        }
214
                                        else
214
                                        else
215
                                        {
215
                                        {
216
                                                logfilestate = LOGFILE_ERROR;
216
                                                logfilestate = LOGFILE_ERROR;
217
                                                UART1_PutString("\r\nError getting free kml-file name\r\n");
217
                                                UART1_PutString("\r\nError getting free kml-file name\r\n");
218
                                                SD_LoggingError = 2;
218
                                                SD_LoggingError = 2;
219
                                        }
219
                                        }
220
                                        // else retry in next loop
220
                                        // else retry in next loop
221
                                        break;
221
                                        break;
222
                                case LOGFILE_OPENED:
222
                                case LOGFILE_OPENED:
223
                                        // append new gps log data
223
                                        // append new gps log data
224
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
224
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
225
                                        {
225
                                        {
226
                                                if(!KML_LoggGPSCoordinates(&logfile))
226
                                                if(!KML_LoggGPSCoordinates(&logfile))
227
                                                {       // error logging data
227
                                                {       // error logging data
228
                                                        UART1_PutString("\r\nError logging to kml-file\r\n");
228
                                                        UART1_PutString("\r\nError logging to kml-file\r\n");
229
                                                        SD_LoggingError = 3;
229
                                                        SD_LoggingError = 3;
230
                                                        KML_DocumentClose(&logfile);
230
                                                        KML_DocumentClose(&logfile);
231
                                                        logfilestate = LOGFILE_ERROR;
231
                                                        logfilestate = LOGFILE_ERROR;
232
                                                }
232
                                                }
233
                                                else // sucessfully logged
233
                                                else // sucessfully logged
234
                                                {
234
                                                {
235
                                                        SD_LoggingError = 0;
235
                                                        SD_LoggingError = 0;
236
                                                        if(CheckDelay(flushtimer))
236
                                                        if(CheckDelay(flushtimer))
237
                                                        {
237
                                                        {
238
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
238
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
239
                                                                fflush_(logfile.file);
239
                                                                fflush_(logfile.file);
240
                                                        }
240
                                                        }
241
                                                }
241
                                                }
242
 
242
 
243
                                        }
243
                                        }
244
                                        break;
244
                                        break;
245
 
245
 
246
                                case LOGFILE_ERROR:
246
                                case LOGFILE_ERROR:
247
                                        break;
247
                                        break;
248
 
248
 
249
                                default:
249
                                default:
250
                                        logfilestate = LOGFILE_IDLE;
250
                                        logfilestate = LOGFILE_IDLE;
251
                                        break;
251
                                        break;
252
                        }
252
                        }
253
                } // EOF motors are not running
253
                } // EOF motors are not running
254
                else // model is not flying
254
                else // model is not flying
255
                {   // close log file if opened
255
                {   // close log file if opened
256
                        if(logfilestate == LOGFILE_OPENED)
256
                        if(logfilestate == LOGFILE_OPENED)
257
                        {
257
                        {
258
                                if(KML_DocumentClose(&logfile))
258
                                if(KML_DocumentClose(&logfile))
259
                                {
259
                                {
260
                                        UART1_PutString("\r\nClosing kml-file\r\n");
260
                                        UART1_PutString("\r\nClosing kml-file\r\n");
261
                                        logfilestate = LOGFILE_CLOSED;
261
                                        logfilestate = LOGFILE_CLOSED;
262
                                }
262
                                }
263
                                else  // could not be closed
263
                                else  // could not be closed
264
                                {
264
                                {
265
                                        UART1_PutString("\r\nError closing kml-file\r\n");
265
                                        UART1_PutString("\r\nError closing kml-file\r\n");
266
                                        SD_LoggingError = 4;
266
                                        SD_LoggingError = 4;
267
                                        logfilestate =  LOGFILE_ERROR;
267
                                        logfilestate =  LOGFILE_ERROR;
268
                                }
268
                                }
269
                        }
269
                        }
270
                } //EOF motors are not running
270
                } //EOF motors are not running
271
        } // EOF Check LogTimer
271
        } // EOF Check LogTimer
272
 
272
 
273
        return logfilestate;
273
        return logfilestate;
274
}
274
}
275
 
275
 
276
//----------------------------------------------------------------------------------------------------
276
//----------------------------------------------------------------------------------------------------
277
// logs gps and state info to a gpx file
277
// logs gps and state info to a gpx file
278
logfilestate_t Logging_GPX(u32 LogDelay)
278
logfilestate_t Logging_GPX(u32 LogDelay)
279
{
279
{
280
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
280
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
281
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
281
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
282
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
282
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
283
        static  GPX_Document_t logfile;                                         // the logfilehandle
283
        static  GPX_Document_t logfile;                                         // the logfilehandle
284
        static  u8 part = 0;
284
        static  u8 part = 0;
285
        u32 measure_time;
285
        u32 measure_time;
286
 
286
 
287
        // initialize if LogDelay is zero
287
        // initialize if LogDelay is zero
288
        if(!LogDelay)
288
        if(!LogDelay)
289
        {
289
        {
290
                switch(logfilestate)
290
                switch(logfilestate)
291
                {
291
                {
292
                        case LOGFILE_OPENED:
292
                        case LOGFILE_OPENED:
293
                                GPX_DocumentClose(&logfile); // try to close it
293
                                GPX_DocumentClose(&logfile); // try to close it
294
                                break;
294
                                break;
295
                        default:
295
                        default:
296
                                break;
296
                                break;
297
                }
297
                }
298
                logfilestate = LOGFILE_IDLE;
298
                logfilestate = LOGFILE_IDLE;
299
                logfilename = NULL;
299
                logfilename = NULL;
300
                GPX_DocumentInit(&logfile);
300
                GPX_DocumentInit(&logfile);
301
                logtimer = SetDelay(0); // set logtimer to now
301
                logtimer = SetDelay(0); // set logtimer to now
302
                return logfilestate;
302
                return logfilestate;
303
        }
303
        }
304
        // no init
304
        // no init
305
        if(CheckDelay(logtimer) || part)
305
        if(CheckDelay(logtimer) || part)
306
        {
306
        {
307
                if(!part) logtimer = SetDelay(LogDelay);        // standard interval
307
                if(!part) logtimer = SetDelay(LogDelay);        // standard interval
308
 
308
 
309
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN || part)
309
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN || part)
310
                {
310
                {
311
                        switch(logfilestate)
311
                        switch(logfilestate)
312
                        {
312
                        {
313
                                case LOGFILE_IDLE:
313
                                case LOGFILE_IDLE:
314
                                case LOGFILE_CLOSED:
314
                                case LOGFILE_CLOSED:
315
                                        if((GPSData.Status != INVALID) && (GPSData.NumOfSats > 2) && /*(GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)*/ (FC.StatusFlags & FC_STATUS_FLY))
315
                                        if((GPSData.Status != INVALID) && (GPSData.NumOfSats > 2) && /*(GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)*/ (FC.StatusFlags & FC_STATUS_FLY))
316
                                        {
316
                                        {
317
                                                logfilestate = LOGFILE_START;
317
                                                logfilestate = LOGFILE_START;
318
                                        }
318
                                        }
319
                                        part = 0;
319
                                        part = 0;
320
                                        break;
320
                                        break;
321
                                case LOGFILE_START:
321
                                case LOGFILE_START:
322
                                        // find unused logfile name
322
                                        // find unused logfile name
323
                                        do
323
                                        do
324
                                        {        // try to generate a new logfile name
324
                                        {        // try to generate a new logfile name
325
                                                 logfilename = GenerateGPXLogFileName();
325
                                                 logfilename = GenerateGPXLogFileName();
326
                                        }while((logfilename != NULL) && fexist_(logfilename));
326
                                        }while((logfilename != NULL) && fexist_(logfilename));
327
                                        // if logfilename exist
327
                                        // if logfilename exist
328
                                        if(logfilename != NULL)
328
                                        if(logfilename != NULL)
329
                                        {
329
                                        {
330
                                                // try to create the log file
330
                                                // try to create the log file
331
                                                if(GPX_DocumentOpen(logfilename, &logfile))
331
                                                if(GPX_DocumentOpen(logfilename, &logfile))
332
                                                {
332
                                                {
333
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
333
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
334
                                                        logfilestate = LOGFILE_OPENED; // goto next step
334
                                                        logfilestate = LOGFILE_OPENED; // goto next step
335
                                                        UART1_PutString("\r\nOpening gpx-file:");
335
                                                        UART1_PutString("\r\nOpening gpx-file:");
336
                                                        UART1_PutString(logfilename);
336
                                                        UART1_PutString(logfilename);
337
                                                        UART1_PutString("\r\n");
337
                                                        UART1_PutString("\r\n");
338
                                                }
338
                                                }
339
                                                else // could not be openend
339
                                                else // could not be openend
340
                                                {
340
                                                {
341
                                                        logfilestate = LOGFILE_ERROR;
341
                                                        logfilestate = LOGFILE_ERROR;
342
                                                        UART1_PutString("\r\nError opening gpx-file: ");
342
                                                        UART1_PutString("\r\nError opening gpx-file: ");
343
                                                        UART1_PutString(logfilename);
343
                                                        UART1_PutString(logfilename);
344
                                                        UART1_PutString("\r\n");
344
                                                        UART1_PutString("\r\n");
345
                                                        SD_LoggingError = 11;
345
                                                        SD_LoggingError = 11;
346
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
346
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
347
                                                }
347
                                                }
348
                                        }
348
                                        }
349
                                        else
349
                                        else
350
                                        {
350
                                        {
351
                                                logfilestate = LOGFILE_ERROR;
351
                                                logfilestate = LOGFILE_ERROR;
352
                                                UART1_PutString("\r\nError getting free gpx-file name\r\n");
352
                                                UART1_PutString("\r\nError getting free gpx-file name\r\n");
353
                                                SD_LoggingError = 12;
353
                                                SD_LoggingError = 12;
354
                                        }
354
                                        }
355
                                        // else retry in next loop
355
                                        // else retry in next loop
356
                                        part = 0;
356
                                        part = 0;
357
                                        break;
357
                                        break;
358
                                case LOGFILE_OPENED:
358
                                case LOGFILE_OPENED:
359
                                        // append new gps log data
359
                                        // append new gps log data
360
                                        measure_time = CountMilliseconds;
360
                                        measure_time = CountMilliseconds;
361
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
361
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
362
                                        {
362
                                        {
363
                                                if(!GPX_LoggGPSCoordinates(&logfile,part))
363
                                                if(!GPX_LoggGPSCoordinates(&logfile,part))
364
                                                {       // error logging data
364
                                                {       // error logging data
365
                                                        UART1_PutString("\r\nError logging to gpx-file\r\n");
365
                                                        UART1_PutString("\r\nError logging to gpx-file\r\n");
366
                                                        GPX_DocumentClose(&logfile);
366
                                                        GPX_DocumentClose(&logfile);
367
                                                        SD_LoggingError = 13;
367
                                                        SD_LoggingError = 13;
368
                                                        logfilestate = LOGFILE_ERROR;
368
                                                        logfilestate = LOGFILE_ERROR;
369
                                                }
369
                                                }
370
                                                else // successful log
370
                                                else // successful log
371
                                                {
371
                                                {
372
                                                        if(CheckDelay(flushtimer))
372
                                                        if(CheckDelay(flushtimer))
373
                                                        {
373
                                                        {
374
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
374
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
375
                                                                fflush_(logfile.file);
375
                                                                fflush_(logfile.file);
376
                                                        }
376
                                                        }
377
                                                }
377
                                                }
378
                                        }
378
                                        }
379
                                        if(++part == 9) part = 0;
379
                                        if(++part == 9) part = 0;
380
//DebugOut.Analog[19] = CountMilliseconds - measure_time;
380
//DebugOut.Analog[19] = CountMilliseconds - measure_time;
381
                                        break;
381
                                        break;
382
 
382
 
383
                                case LOGFILE_ERROR:
383
                                case LOGFILE_ERROR:
384
                                        part = 0;
384
                                        part = 0;
385
                                        break;
385
                                        break;
386
 
386
 
387
                                default:
387
                                default:
388
                                        part = 0;
388
                                        part = 0;
389
                                        logfilestate = LOGFILE_IDLE;
389
                                        logfilestate = LOGFILE_IDLE;
390
                                        break;
390
                                        break;
391
                        }
391
                        }
392
                } // EOF model is flying
392
                } // EOF model is flying
393
                else // model is not flying
393
                else // model is not flying
394
                {   // close log file if opened
394
                {   // close log file if opened
395
                        if(logfilestate == LOGFILE_OPENED)
395
                        if(logfilestate == LOGFILE_OPENED)
396
                        {
396
                        {
397
                                if(GPX_DocumentClose(&logfile))
397
                                if(GPX_DocumentClose(&logfile))
398
                                {
398
                                {
399
                                        UART1_PutString("\r\nClosing gpx-file\r\n");
399
                                        UART1_PutString("\r\nClosing gpx-file\r\n");
400
                                        logfilestate = LOGFILE_CLOSED;
400
                                        logfilestate = LOGFILE_CLOSED;
401
                                        part = 0;
401
                                        part = 0;
402
                                }
402
                                }
403
                                else  // could not be closed
403
                                else  // could not be closed
404
                                {
404
                                {
405
                                        UART1_PutString("\r\nError closing gpx-file\r\n");
405
                                        UART1_PutString("\r\nError closing gpx-file\r\n");
406
                                        SD_LoggingError = 14;
406
                                        SD_LoggingError = 14;
407
                                        logfilestate =  LOGFILE_ERROR;
407
                                        logfilestate =  LOGFILE_ERROR;
408
                                        part = 0;
408
                                        part = 0;
409
                                }
409
                                }
410
                        }
410
                        }
411
                } //EOF model is not flying
411
                } //EOF model is not flying
412
        } // EOF Check LogTimer
412
        } // EOF Check LogTimer
413
 
413
 
414
        return logfilestate;
414
        return logfilestate;
415
}
415
}
416
 
416
 
417
//----------------------------------------------------------------------------------------------------
417
//----------------------------------------------------------------------------------------------------
418
// initialize logging
418
// initialize logging
419
void Logging_Init(void)
419
void Logging_Init(void)
420
{
420
{
421
        SD_LoggingError = 0;
421
        SD_LoggingError = 0;
422
        LogCfg.KML_Interval = 1000; //default
422
        LogCfg.KML_Interval = 1000; //default
423
        Settings_GetParamValue(PID_KML_LOGGING, (u16*)&LogCfg.KML_Interval); // overwrite by settings value
423
        Settings_GetParamValue(PID_KML_LOGGING, (u16*)&LogCfg.KML_Interval); // overwrite by settings value
424
        if(LogCfg.KML_Interval != 0 && LogCfg.KML_Interval < MIN_SD_INTERVAL_KML) LogCfg.KML_Interval = MIN_SD_INTERVAL_KML;
424
        if(LogCfg.KML_Interval != 0 && LogCfg.KML_Interval < MIN_SD_INTERVAL_KML) LogCfg.KML_Interval = MIN_SD_INTERVAL_KML;
425
 
425
 
426
        Logging_KML(0); // initialize
426
        Logging_KML(0); // initialize
427
        LogCfg.GPX_Interval = 0; //default
427
        LogCfg.GPX_Interval = 0; //default
428
        Settings_GetParamValue(PID_GPX_LOGGING, (u16*)&LogCfg.GPX_Interval); // overwrite by settings value
428
        Settings_GetParamValue(PID_GPX_LOGGING, (u16*)&LogCfg.GPX_Interval); // overwrite by settings value
429
        if(LogCfg.GPX_Interval != 0 && LogCfg.GPX_Interval < MIN_SD_INTERVAL_GPX) LogCfg.GPX_Interval = MIN_SD_INTERVAL_GPX;
429
        if(LogCfg.GPX_Interval != 0 && LogCfg.GPX_Interval < MIN_SD_INTERVAL_GPX) LogCfg.GPX_Interval = MIN_SD_INTERVAL_GPX;
430
        Logging_GPX(0); // initialize
430
        Logging_GPX(0); // initialize
431
}
431
}
432
 
432
 
433
//----------------------------------------------------------------------------------------------------
433
//----------------------------------------------------------------------------------------------------
434
// gobal logging handler
434
// gobal logging handler
435
void Logging_Update(void)
435
void Logging_Update(void)
436
{
436
{
437
        static u8 logmodule = 0;
437
        static u8 logmodule = 0;
438
        static u32 logtimer = 0;
438
        static u32 logtimer = 0;
439
        static logfilestate_t logstate = LOGFILE_IDLE;
439
        static logfilestate_t logstate = LOGFILE_IDLE;
440
 
440
 
441
        if(SD_SWITCH) // a card is in slot
441
        if(SD_SWITCH) // a card is in slot
442
        {
442
        {
443
        if((logstate == LOGFILE_ERROR) && (FC.StatusFlags & FC_STATUS_START)) SD_LoggingError = 100;
443
        if((logstate == LOGFILE_ERROR) && (FC.StatusFlags & FC_STATUS_START)) SD_LoggingError = 100;
444
                if(CheckDelay(logtimer))
444
                if(CheckDelay(logtimer))
445
                {
445
                {
446
                        logtimer = SetDelay(10);  // faster makes no sense
446
                        logtimer = SetDelay(10);  // faster makes no sense
447
                        // call the logger handlers if no error has occured
447
                        // call the logger handlers if no error has occured
448
                        if(logstate != LOGFILE_ERROR)
448
                        if(logstate != LOGFILE_ERROR)
449
                        {  // run only one logging handler per loop
449
                        {  // run only one logging handler per loop
450
                                switch(logmodule++)
450
                                switch(logmodule++)
451
                                {
451
                                {
452
                                        case 0:
452
                                        case 0:
453
                                                logstate = Logging_KML(LogCfg.KML_Interval);
453
                                                logstate = Logging_KML(LogCfg.KML_Interval);
454
                                                break;
454
                                                break;
455
                                        case 1:
455
                                        case 1:
456
                                                logstate = Logging_GPX(LogCfg.GPX_Interval);
456
                                                logstate = Logging_GPX(LogCfg.GPX_Interval);
457
                                                logmodule = 0; // resart with first log module
457
                                                logmodule = 0; // resart with first log module
458
                                                break;
458
                                                break;
459
                                        default:
459
                                        default:
460
                                                logmodule = 0;
460
                                                logmodule = 0;
461
                                                break;
461
                                                break;
462
                                }
462
                                }
463
                        }
463
                        }
464
                        else // a logging error has occured
464
                        else // a logging error has occured
465
                        {      
465
                        {      
466
//                              /*
466
//                              /*
467
                                // try to reinitialize sd-card when motors are not running
467
                                // try to reinitialize sd-card when motors are not running
468
                                if(!(FC.StatusFlags & FC_STATUS_MOTOR_RUN))
468
                                if(!(FC.StatusFlags & FC_STATUS_MOTOR_RUN))
469
                                {
469
                                {
470
                                        if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside
470
                                        if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside
471
                                        {
471
                                        {
472
                                                Logging_Init(); // initialize the logs
472
                                                Logging_Init(); // initialize the logs
473
                                                logstate = LOGFILE_IDLE;
473
                                                logstate = LOGFILE_IDLE;
474
                                                logtimer = SetDelay(100);       // try next log in 100 mili sec
474
                                                logtimer = SetDelay(100);       // try next log in 100 mili sec
475
                                        }
475
                                        }
476
                                        else
476
                                        else
477
                                        {   // retry in 5 seconds
477
                                        {   // retry in 5 seconds
478
                                                logtimer = SetDelay(5000);  // try again in 5 sec
478
                                                logtimer = SetDelay(5000);  // try again in 5 sec
479
                                        }
479
                                        }
480
                                } // EOF motors are not running
480
                                } // EOF motors are not running
481
//                              */
481
//                              */
482
                        } //EOF logfile error
482
                        } //EOF logfile error
483
                }  // EOF CheckDelay
483
                }  // EOF CheckDelay
484
        }// EOF Card in Slot
484
        }// EOF Card in Slot
485
}
485
}
486
 
486