Rev 456 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
82 | killagreg | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
360 | holgerb | 7 | // + Software Nutzungsbedingungen (english version: see below) |
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 |
||
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. |
||
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
82 | killagreg | 13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 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. |
||
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. |
||
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. |
||
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 |
||
22 | // + des Mitverschuldens offen. |
||
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. |
||
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 |
||
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. |
||
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. |
||
82 | killagreg | 31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 32 | // + Software LICENSING TERMS |
82 | killagreg | 33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 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 |
||
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. |
||
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 |
||
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 |
||
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 |
||
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 |
||
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. |
||
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. |
||
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. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
82 | killagreg | 55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | #include <stdio.h> |
||
57 | #include "91x_lib.h" |
||
58 | #include "main.h" |
||
119 | killagreg | 59 | #include "timer1.h" |
82 | killagreg | 60 | #include "uart1.h" |
61 | #include "kml.h" |
||
85 | killagreg | 62 | #include "gpx.h" |
86 | killagreg | 63 | #include "ssc.h" |
88 | killagreg | 64 | #include "settings.h" |
378 | holgerb | 65 | #include "led.h" |
380 | holgerb | 66 | #include "logging.h" |
82 | killagreg | 67 | |
380 | holgerb | 68 | #define MIN_SD_INTERVAL_KML 200 |
456 | holgerb | 69 | #define MIN_SD_INTERVAL_GPX 200 |
436 | holgerb | 70 | #define APPEND_LOG_TIME_MS 2510 // log some more time |
378 | holgerb | 71 | |
351 | holgerb | 72 | u8 SD_LoggingError = 0; |
92 | killagreg | 73 | |
437 | holgerb | 74 | #define LOG_FLUSH_INTERVAL 4000 // 4s // -> and if an Error occures |
92 | killagreg | 75 | |
82 | killagreg | 76 | typedef enum |
77 | { |
||
78 | LOGFILE_IDLE, |
||
79 | LOGFILE_START, |
||
80 | LOGFILE_CLOSED, |
||
81 | LOGFILE_OPENED, |
||
82 | LOGFILE_ERROR |
||
83 | } logfilestate_t; |
||
84 | |||
85 | // logger handler prototypes |
||
86 | logfilestate_t Logging_KML(u32 LogDelay); |
||
87 | logfilestate_t Logging_GPX(u32 LogDelay); |
||
88 | |||
101 | holgerb | 89 | LogCfg_t LogCfg = {500 , 1000}; |
380 | holgerb | 90 | u32 Logged_GPX_Counter = 0, Logged_KML_Counter = 0; |
82 | killagreg | 91 | |
92 | |||
93 | //---------------------------------------------------------------------------------------------------- |
||
94 | s8* GenerateKMLLogFileName(void) |
||
95 | { |
||
96 | static u16 filenum = 0; // file name counter |
||
97 | static s8 filename[35]; |
||
98 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
||
99 | |||
100 | if(SystemTime.Valid) |
||
101 | { |
||
102 | // if the day has been changed |
||
103 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
||
104 | { |
||
105 | LastTime.Year = SystemTime.Year; |
||
106 | LastTime.Month = SystemTime.Month; |
||
107 | LastTime.Day = SystemTime.Day; |
||
108 | LastTime.Valid = 1; |
||
109 | filenum = 0; // reset file counter |
||
110 | } |
||
429 | holgerb | 111 | if(filenum < 100) sprintf(filename, "/LOG/%04i%02i%02i/KML/%02i%02i%02i%02i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, SystemTime.Year % 100, SystemTime.Month, SystemTime.Day, filenum); |
112 | else sprintf(filename, "/LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
||
82 | killagreg | 113 | filenum++; |
114 | return filename; |
||
115 | } |
||
116 | else return NULL; |
||
117 | } |
||
118 | |||
119 | //---------------------------------------------------------------------------------------------------- |
||
120 | s8* GenerateGPXLogFileName(void) |
||
121 | { |
||
122 | static u16 filenum = 0; // file name counter |
||
123 | static s8 filename[35]; |
||
124 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
||
125 | |||
126 | if(SystemTime.Valid) |
||
127 | { |
||
128 | // if the day has been changed |
||
129 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
||
130 | { |
||
131 | LastTime.Year = SystemTime.Year; |
||
132 | LastTime.Month = SystemTime.Month; |
||
133 | LastTime.Day = SystemTime.Day; |
||
134 | LastTime.Valid = 1; |
||
135 | filenum = 0; // reset file counter |
||
136 | } |
||
429 | holgerb | 137 | if(filenum < 100) sprintf(filename, "/LOG/%04i%02i%02i/GPX/%02i%02i%02i%02i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, SystemTime.Year % 100, SystemTime.Month, SystemTime.Day, filenum); |
138 | else sprintf(filename, "/LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
||
82 | killagreg | 139 | filenum++; |
140 | return filename; |
||
141 | } |
||
142 | else return NULL; |
||
143 | } |
||
144 | |||
145 | |||
146 | |||
147 | //---------------------------------------------------------------------------------------------------- |
||
148 | // logs the current gps position to a kml file |
||
149 | logfilestate_t Logging_KML(u32 LogDelay) |
||
150 | { |
||
151 | static logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate |
||
152 | static s8* logfilename = NULL; // the pointer to the logfilename |
||
92 | killagreg | 153 | static u32 logtimer = 0, flushtimer = 0; // the log update timer |
82 | killagreg | 154 | static KML_Document_t logfile; // the logfilehandle |
432 | holgerb | 155 | static u32 appendtimer = 0; // the log update timer |
156 | static u8 logging_active = 0; |
||
82 | killagreg | 157 | |
146 | killagreg | 158 | // initialize if LogDelay is zero |
82 | killagreg | 159 | if(!LogDelay) |
160 | { |
||
161 | switch(logfilestate) |
||
162 | { |
||
88 | killagreg | 163 | case LOGFILE_OPENED: |
82 | killagreg | 164 | KML_DocumentClose(&logfile); // try to close it |
165 | break; |
||
166 | default: |
||
167 | break; |
||
168 | } |
||
169 | logfilestate = LOGFILE_IDLE; |
||
170 | logfilename = NULL; |
||
171 | KML_DocumentInit(&logfile); |
||
172 | logtimer = SetDelay(0); // set logtimer to now |
||
88 | killagreg | 173 | return logfilestate; |
82 | killagreg | 174 | } |
175 | // no init |
||
176 | if(CheckDelay(logtimer)) |
||
177 | { |
||
178 | logtimer = SetDelay(LogDelay); // standard interval |
||
88 | killagreg | 179 | |
432 | holgerb | 180 | if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) |
181 | { |
||
182 | logging_active = 1; |
||
183 | appendtimer = SetDelay(APPEND_LOG_TIME_MS); |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | if(CheckDelay(appendtimer)) logging_active = 0; |
||
188 | } |
||
189 | |||
190 | if(logging_active) |
||
82 | killagreg | 191 | { |
192 | switch(logfilestate) |
||
193 | { |
||
194 | case LOGFILE_IDLE: |
||
195 | case LOGFILE_CLOSED: |
||
429 | holgerb | 196 | if((GPSData.Status != INVALID) && (SystemTime.Valid) && /*(GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)*/ (FC.StatusFlags & FC_STATUS_FLY)) |
82 | killagreg | 197 | { |
198 | logfilestate = LOGFILE_START; |
||
199 | } |
||
200 | break; |
||
201 | case LOGFILE_START: |
||
202 | // find unused logfile name |
||
203 | do |
||
204 | { // try to generate a new logfile name |
||
205 | logfilename = GenerateKMLLogFileName(); |
||
378 | holgerb | 206 | }while((logfilename != NULL) && fexist_(logfilename)); |
82 | killagreg | 207 | // if logfilename exist |
208 | if(logfilename != NULL) |
||
209 | { |
||
210 | // try to create the log file |
||
211 | if(KML_DocumentOpen(logfilename, &logfile)) |
||
212 | { |
||
92 | killagreg | 213 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
82 | killagreg | 214 | logfilestate = LOGFILE_OPENED; // goto next step |
110 | killagreg | 215 | UART1_PutString("\r\nOpening kml-file:"); |
216 | UART1_PutString(logfilename); |
||
217 | UART1_PutString("\r\n"); |
||
82 | killagreg | 218 | } |
219 | else // could not be openend |
||
220 | { |
||
221 | logfilestate = LOGFILE_ERROR; |
||
110 | killagreg | 222 | UART1_PutString("\r\nError opening kml-file: "); |
223 | UART1_PutString(logfilename); |
||
224 | UART1_PutString("\r\n"); |
||
82 | killagreg | 225 | logtimer = SetDelay(10); // try again in open logfile in 10 mili sec |
351 | holgerb | 226 | SD_LoggingError = 1; |
82 | killagreg | 227 | } |
228 | } |
||
229 | else |
||
230 | { |
||
231 | logfilestate = LOGFILE_ERROR; |
||
110 | killagreg | 232 | UART1_PutString("\r\nError getting free kml-file name\r\n"); |
351 | holgerb | 233 | SD_LoggingError = 2; |
82 | killagreg | 234 | } |
235 | // else retry in next loop |
||
236 | break; |
||
237 | case LOGFILE_OPENED: |
||
238 | // append new gps log data |
||
429 | holgerb | 239 | if((GPSData.Status != INVALID))// && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
82 | killagreg | 240 | { |
85 | killagreg | 241 | if(!KML_LoggGPSCoordinates(&logfile)) |
82 | killagreg | 242 | { // error logging data |
110 | killagreg | 243 | UART1_PutString("\r\nError logging to kml-file\r\n"); |
351 | holgerb | 244 | SD_LoggingError = 3; |
82 | killagreg | 245 | KML_DocumentClose(&logfile); |
246 | logfilestate = LOGFILE_ERROR; |
||
247 | } |
||
92 | killagreg | 248 | else // sucessfully logged |
249 | { |
||
351 | holgerb | 250 | SD_LoggingError = 0; |
92 | killagreg | 251 | if(CheckDelay(flushtimer)) |
252 | { |
||
253 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
||
254 | fflush_(logfile.file); |
||
255 | } |
||
256 | } |
||
257 | |||
82 | killagreg | 258 | } |
259 | break; |
||
88 | killagreg | 260 | |
82 | killagreg | 261 | case LOGFILE_ERROR: |
262 | break; |
||
88 | killagreg | 263 | |
82 | killagreg | 264 | default: |
265 | logfilestate = LOGFILE_IDLE; |
||
266 | break; |
||
267 | } |
||
92 | killagreg | 268 | } // EOF motors are not running |
82 | killagreg | 269 | else // model is not flying |
270 | { // close log file if opened |
||
271 | if(logfilestate == LOGFILE_OPENED) |
||
272 | { |
||
273 | if(KML_DocumentClose(&logfile)) |
||
274 | { |
||
110 | killagreg | 275 | UART1_PutString("\r\nClosing kml-file\r\n"); |
82 | killagreg | 276 | logfilestate = LOGFILE_CLOSED; |
277 | } |
||
278 | else // could not be closed |
||
279 | { |
||
110 | killagreg | 280 | UART1_PutString("\r\nError closing kml-file\r\n"); |
351 | holgerb | 281 | SD_LoggingError = 4; |
82 | killagreg | 282 | logfilestate = LOGFILE_ERROR; |
283 | } |
||
284 | } |
||
92 | killagreg | 285 | } //EOF motors are not running |
82 | killagreg | 286 | } // EOF Check LogTimer |
287 | |||
288 | return logfilestate; |
||
289 | } |
||
88 | killagreg | 290 | |
82 | killagreg | 291 | //---------------------------------------------------------------------------------------------------- |
292 | // logs gps and state info to a gpx file |
||
293 | logfilestate_t Logging_GPX(u32 LogDelay) |
||
294 | { |
||
85 | killagreg | 295 | static logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate |
296 | static s8* logfilename = NULL; // the pointer to the logfilename |
||
432 | holgerb | 297 | static u32 logtimer = 0, flushtimer = 0, appendtimer = 0; // the log update timer |
85 | killagreg | 298 | static GPX_Document_t logfile; // the logfilehandle |
437 | holgerb | 299 | static u8 part = 0, logging_active = 0, old_errorcode = 0; |
345 | holgerb | 300 | u32 measure_time; |
82 | killagreg | 301 | |
218 | killagreg | 302 | // initialize if LogDelay is zero |
85 | killagreg | 303 | if(!LogDelay) |
304 | { |
||
305 | switch(logfilestate) |
||
306 | { |
||
88 | killagreg | 307 | case LOGFILE_OPENED: |
85 | killagreg | 308 | GPX_DocumentClose(&logfile); // try to close it |
309 | break; |
||
310 | default: |
||
311 | break; |
||
312 | } |
||
313 | logfilestate = LOGFILE_IDLE; |
||
314 | logfilename = NULL; |
||
315 | GPX_DocumentInit(&logfile); |
||
316 | logtimer = SetDelay(0); // set logtimer to now |
||
88 | killagreg | 317 | return logfilestate; |
85 | killagreg | 318 | } |
319 | // no init |
||
345 | holgerb | 320 | if(CheckDelay(logtimer) || part) |
85 | killagreg | 321 | { |
378 | holgerb | 322 | if(!part) logtimer = SetDelay(LogDelay); // standard interval |
88 | killagreg | 323 | |
432 | holgerb | 324 | if(FC.StatusFlags & FC_STATUS_MOTOR_RUN) |
325 | { |
||
326 | logging_active = 1; |
||
327 | appendtimer = SetDelay(APPEND_LOG_TIME_MS); |
||
328 | } |
||
329 | else |
||
330 | { |
||
331 | if(CheckDelay(appendtimer)) logging_active = 0; |
||
332 | } |
||
333 | |||
334 | if(logging_active || part) |
||
85 | killagreg | 335 | { |
336 | switch(logfilestate) |
||
337 | { |
||
338 | case LOGFILE_IDLE: |
||
339 | case LOGFILE_CLOSED: |
||
429 | holgerb | 340 | if((GPSData.Status != INVALID) && (SystemTime.Valid) && /*(GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)*/ (FC.StatusFlags & FC_STATUS_FLY)) |
85 | killagreg | 341 | { |
342 | logfilestate = LOGFILE_START; |
||
343 | } |
||
345 | holgerb | 344 | part = 0; |
85 | killagreg | 345 | break; |
346 | case LOGFILE_START: |
||
347 | // find unused logfile name |
||
348 | do |
||
349 | { // try to generate a new logfile name |
||
350 | logfilename = GenerateGPXLogFileName(); |
||
378 | holgerb | 351 | }while((logfilename != NULL) && fexist_(logfilename)); |
85 | killagreg | 352 | // if logfilename exist |
353 | if(logfilename != NULL) |
||
354 | { |
||
355 | // try to create the log file |
||
356 | if(GPX_DocumentOpen(logfilename, &logfile)) |
||
357 | { |
||
92 | killagreg | 358 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
85 | killagreg | 359 | logfilestate = LOGFILE_OPENED; // goto next step |
110 | killagreg | 360 | UART1_PutString("\r\nOpening gpx-file:"); |
361 | UART1_PutString(logfilename); |
||
362 | UART1_PutString("\r\n"); |
||
85 | killagreg | 363 | } |
364 | else // could not be openend |
||
365 | { |
||
366 | logfilestate = LOGFILE_ERROR; |
||
110 | killagreg | 367 | UART1_PutString("\r\nError opening gpx-file: "); |
368 | UART1_PutString(logfilename); |
||
369 | UART1_PutString("\r\n"); |
||
351 | holgerb | 370 | SD_LoggingError = 11; |
85 | killagreg | 371 | logtimer = SetDelay(10); // try again in open logfile in 10 mili sec |
372 | } |
||
437 | holgerb | 373 | old_errorcode = ErrorCode; |
85 | killagreg | 374 | } |
375 | else |
||
376 | { |
||
377 | logfilestate = LOGFILE_ERROR; |
||
110 | killagreg | 378 | UART1_PutString("\r\nError getting free gpx-file name\r\n"); |
351 | holgerb | 379 | SD_LoggingError = 12; |
85 | killagreg | 380 | } |
381 | // else retry in next loop |
||
345 | holgerb | 382 | part = 0; |
85 | killagreg | 383 | break; |
384 | case LOGFILE_OPENED: |
||
385 | // append new gps log data |
||
421 | holgerb | 386 | measure_time = CountMilliseconds; |
429 | holgerb | 387 | if((GPSData.Status != INVALID))// && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
85 | killagreg | 388 | { |
345 | holgerb | 389 | if(!GPX_LoggGPSCoordinates(&logfile,part)) |
85 | killagreg | 390 | { // error logging data |
110 | killagreg | 391 | UART1_PutString("\r\nError logging to gpx-file\r\n"); |
85 | killagreg | 392 | GPX_DocumentClose(&logfile); |
351 | holgerb | 393 | SD_LoggingError = 13; |
85 | killagreg | 394 | logfilestate = LOGFILE_ERROR; |
395 | } |
||
92 | killagreg | 396 | else // successful log |
397 | { |
||
437 | holgerb | 398 | if(ErrorCode != old_errorcode || CheckDelay(flushtimer)) |
92 | killagreg | 399 | { |
437 | holgerb | 400 | old_errorcode = ErrorCode; |
92 | killagreg | 401 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
402 | fflush_(logfile.file); |
||
403 | } |
||
404 | } |
||
85 | killagreg | 405 | } |
345 | holgerb | 406 | if(++part == 9) part = 0; |
357 | holgerb | 407 | //DebugOut.Analog[19] = CountMilliseconds - measure_time; |
85 | killagreg | 408 | break; |
88 | killagreg | 409 | |
85 | killagreg | 410 | case LOGFILE_ERROR: |
345 | holgerb | 411 | part = 0; |
85 | killagreg | 412 | break; |
88 | killagreg | 413 | |
85 | killagreg | 414 | default: |
345 | holgerb | 415 | part = 0; |
85 | killagreg | 416 | logfilestate = LOGFILE_IDLE; |
417 | break; |
||
418 | } |
||
419 | } // EOF model is flying |
||
420 | else // model is not flying |
||
421 | { // close log file if opened |
||
422 | if(logfilestate == LOGFILE_OPENED) |
||
423 | { |
||
424 | if(GPX_DocumentClose(&logfile)) |
||
425 | { |
||
110 | killagreg | 426 | UART1_PutString("\r\nClosing gpx-file\r\n"); |
85 | killagreg | 427 | logfilestate = LOGFILE_CLOSED; |
345 | holgerb | 428 | part = 0; |
85 | killagreg | 429 | } |
430 | else // could not be closed |
||
431 | { |
||
110 | killagreg | 432 | UART1_PutString("\r\nError closing gpx-file\r\n"); |
351 | holgerb | 433 | SD_LoggingError = 14; |
85 | killagreg | 434 | logfilestate = LOGFILE_ERROR; |
345 | holgerb | 435 | part = 0; |
85 | killagreg | 436 | } |
437 | } |
||
438 | } //EOF model is not flying |
||
439 | } // EOF Check LogTimer |
||
440 | |||
82 | killagreg | 441 | return logfilestate; |
442 | } |
||
443 | |||
444 | //---------------------------------------------------------------------------------------------------- |
||
445 | // initialize logging |
||
446 | void Logging_Init(void) |
||
447 | { |
||
351 | holgerb | 448 | SD_LoggingError = 0; |
378 | holgerb | 449 | LogCfg.KML_Interval = 1000; //default |
88 | killagreg | 450 | Settings_GetParamValue(PID_KML_LOGGING, (u16*)&LogCfg.KML_Interval); // overwrite by settings value |
380 | holgerb | 451 | if(LogCfg.KML_Interval != 0 && LogCfg.KML_Interval < MIN_SD_INTERVAL_KML) LogCfg.KML_Interval = MIN_SD_INTERVAL_KML; |
310 | holgerb | 452 | |
92 | killagreg | 453 | Logging_KML(0); // initialize |
146 | killagreg | 454 | LogCfg.GPX_Interval = 0; //default |
88 | killagreg | 455 | Settings_GetParamValue(PID_GPX_LOGGING, (u16*)&LogCfg.GPX_Interval); // overwrite by settings value |
380 | holgerb | 456 | if(LogCfg.GPX_Interval != 0 && LogCfg.GPX_Interval < MIN_SD_INTERVAL_GPX) LogCfg.GPX_Interval = MIN_SD_INTERVAL_GPX; |
92 | killagreg | 457 | Logging_GPX(0); // initialize |
82 | killagreg | 458 | } |
459 | |||
460 | //---------------------------------------------------------------------------------------------------- |
||
461 | // gobal logging handler |
||
462 | void Logging_Update(void) |
||
463 | { |
||
115 | killagreg | 464 | static u8 logmodule = 0; |
82 | killagreg | 465 | static u32 logtimer = 0; |
466 | static logfilestate_t logstate = LOGFILE_IDLE; |
||
86 | killagreg | 467 | |
92 | killagreg | 468 | if(SD_SWITCH) // a card is in slot |
84 | killagreg | 469 | { |
351 | holgerb | 470 | if((logstate == LOGFILE_ERROR) && (FC.StatusFlags & FC_STATUS_START)) SD_LoggingError = 100; |
92 | killagreg | 471 | if(CheckDelay(logtimer)) |
84 | killagreg | 472 | { |
92 | killagreg | 473 | logtimer = SetDelay(10); // faster makes no sense |
474 | // call the logger handlers if no error has occured |
||
115 | killagreg | 475 | if(logstate != LOGFILE_ERROR) |
176 | killagreg | 476 | { // run only one logging handler per loop |
115 | killagreg | 477 | switch(logmodule++) |
478 | { |
||
479 | case 0: |
||
480 | logstate = Logging_KML(LogCfg.KML_Interval); |
||
481 | break; |
||
482 | case 1: |
||
483 | logstate = Logging_GPX(LogCfg.GPX_Interval); |
||
484 | logmodule = 0; // resart with first log module |
||
485 | break; |
||
486 | default: |
||
487 | logmodule = 0; |
||
488 | break; |
||
489 | } |
||
490 | } |
||
491 | else // a logging error has occured |
||
176 | killagreg | 492 | { |
351 | holgerb | 493 | // /* |
176 | killagreg | 494 | // try to reinitialize sd-card when motors are not running |
351 | holgerb | 495 | if(!(FC.StatusFlags & FC_STATUS_MOTOR_RUN)) |
92 | killagreg | 496 | { |
159 | killagreg | 497 | if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside |
498 | { |
||
499 | Logging_Init(); // initialize the logs |
||
500 | logstate = LOGFILE_IDLE; |
||
351 | holgerb | 501 | logtimer = SetDelay(100); // try next log in 100 mili sec |
159 | killagreg | 502 | } |
503 | else |
||
504 | { // retry in 5 seconds |
||
505 | logtimer = SetDelay(5000); // try again in 5 sec |
||
506 | } |
||
507 | } // EOF motors are not running |
||
351 | holgerb | 508 | // */ |
92 | killagreg | 509 | } //EOF logfile error |
510 | } // EOF CheckDelay |
||
511 | }// EOF Card in Slot |
||
88 | killagreg | 512 | } |