Rev 436 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
426 | killagreg | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
||
6 | // + Nur für den privaten Gebrauch |
||
7 | // + FOR NON COMMERCIAL USE ONLY |
||
8 | // + www.MikroKopter.com |
||
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
15 | // + Verkauf von Luftbildaufnahmen, usw. |
||
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
22 | // + eindeutig als Ursprung verlinkt werden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
25 | // + Benutzung auf eigene Gefahr |
||
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
29 | // + mit unserer Zustimmung zulässig |
||
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
34 | // + this list of conditions and the following disclaimer. |
||
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
36 | // + from this software without specific prior written permission. |
||
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
||
38 | // + for non-commercial use (directly or indirectly) |
||
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
40 | // + with our written permission |
||
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
42 | // + clearly linked as origin |
||
43 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
||
44 | // |
||
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
55 | // + POSSIBILITY OF SUCH DAMAGE. |
||
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
57 | #include <stdio.h> |
||
58 | #include "main.h" |
||
59 | #include "timer0.h" |
||
60 | #include "uart1.h" |
||
61 | #include "kml.h" |
||
436 | killagreg | 62 | #include "gpx.h" |
426 | killagreg | 63 | #include "ssc.h" |
64 | #include "settings.h" |
||
65 | #include "printf_P.h" |
||
66 | |||
67 | |||
68 | #define LOG_FLUSH_INTERVAL 20000 // 20s |
||
69 | |||
70 | typedef enum |
||
71 | { |
||
72 | LOGFILE_IDLE, |
||
73 | LOGFILE_START, |
||
74 | LOGFILE_CLOSED, |
||
75 | LOGFILE_OPENED, |
||
76 | LOGFILE_ERROR |
||
77 | } logfilestate_t; |
||
78 | |||
79 | // logger handler prototypes |
||
80 | logfilestate_t Logging_KML(uint16_t LogDelay); |
||
81 | //logfilestate_t Logging_GPX(uint16_t LogDelay); |
||
82 | |||
83 | typedef struct |
||
84 | { |
||
85 | uint16_t KML_Interval; // the kml-log interval (0 = off) |
||
86 | uint16_t GPX_Interval; // the gpx-log interval (0 = off) |
||
87 | } LogCfg_t; |
||
88 | |||
89 | LogCfg_t LogCfg = {500 , 1000}; |
||
90 | |||
91 | |||
92 | //---------------------------------------------------------------------------------------------------- |
||
93 | int8_t* GenerateKMLLogFileName(void) |
||
94 | { |
||
95 | static uint16_t filenum = 0; // file name counter |
||
96 | static int8_t filename[35]; |
||
97 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
||
98 | |||
99 | if(SystemTime.Valid) |
||
100 | { |
||
101 | // if the day has been changed |
||
102 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
||
103 | { |
||
104 | LastTime.Year = SystemTime.Year; |
||
105 | LastTime.Month = SystemTime.Month; |
||
106 | LastTime.Day = SystemTime.Day; |
||
107 | LastTime.Valid = 1; |
||
108 | filenum = 0; // reset file counter |
||
109 | } |
||
110 | sprintf(filename, "LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
||
111 | filenum++; |
||
112 | return filename; |
||
113 | } |
||
114 | else return NULL; |
||
115 | } |
||
116 | |||
117 | //---------------------------------------------------------------------------------------------------- |
||
436 | killagreg | 118 | int8_t* GenerateGPXLogFileName(void) |
426 | killagreg | 119 | { |
120 | static uint16_t filenum = 0; // file name counter |
||
121 | static int8_t filename[35]; |
||
122 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
||
123 | |||
124 | if(SystemTime.Valid) |
||
125 | { |
||
126 | // if the day has been changed |
||
127 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
||
128 | { |
||
129 | LastTime.Year = SystemTime.Year; |
||
130 | LastTime.Month = SystemTime.Month; |
||
131 | LastTime.Day = SystemTime.Day; |
||
132 | LastTime.Valid = 1; |
||
133 | filenum = 0; // reset file counter |
||
134 | } |
||
135 | sprintf(filename, "LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
||
136 | filenum++; |
||
137 | return filename; |
||
138 | } |
||
139 | else return NULL; |
||
436 | killagreg | 140 | } |
426 | killagreg | 141 | |
142 | |||
143 | //---------------------------------------------------------------------------------------------------- |
||
144 | // logs the current gps position to a kml file |
||
145 | logfilestate_t Logging_KML(uint16_t LogDelay) |
||
146 | { |
||
147 | static logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate |
||
148 | static int8_t* logfilename = NULL; // the pointer to the logfilename |
||
149 | static uint16_t logtimer = 0, flushtimer = 0; // the log update timer |
||
150 | static KML_Document_t logfile; // the logfilehandle |
||
151 | |||
436 | killagreg | 152 | // initialize if LogDelay is zero |
426 | killagreg | 153 | if(!LogDelay) |
154 | { |
||
155 | switch(logfilestate) |
||
156 | { |
||
157 | case LOGFILE_OPENED: |
||
158 | KML_DocumentClose(&logfile); // try to close it |
||
159 | break; |
||
160 | default: |
||
161 | break; |
||
162 | } |
||
163 | logfilestate = LOGFILE_IDLE; |
||
164 | logfilename = NULL; |
||
165 | KML_DocumentInit(&logfile); |
||
166 | logtimer = SetDelay(0); // set logtimer to now |
||
167 | return logfilestate; |
||
168 | } |
||
169 | // no init |
||
170 | if(CheckDelay(logtimer)) |
||
171 | { |
||
172 | logtimer = SetDelay(LogDelay); // standard interval |
||
173 | |||
174 | if(SysState == STATE_SEND_FOLLOWME) |
||
175 | { |
||
176 | switch(logfilestate) |
||
177 | { |
||
178 | case LOGFILE_IDLE: |
||
179 | case LOGFILE_CLOSED: |
||
180 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
||
181 | { |
||
182 | logfilestate = LOGFILE_START; |
||
183 | } |
||
184 | break; |
||
185 | case LOGFILE_START: |
||
186 | // find unused logfile name |
||
187 | do |
||
188 | { // try to generate a new logfile name |
||
189 | logfilename = GenerateKMLLogFileName(); |
||
190 | }while((logfilename != NULL) && fexist_(logfilename)); |
||
191 | // if logfilename exist |
||
192 | if(logfilename != NULL) |
||
193 | { |
||
194 | // try to create the log file |
||
195 | if(KML_DocumentOpen(logfilename, &logfile)) |
||
196 | { |
||
197 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
||
198 | logfilestate = LOGFILE_OPENED; // goto next step |
||
199 | printf("\r\nOpening kml-file: %s\r\n",logfilename); |
||
200 | } |
||
201 | else // could not be openend |
||
202 | { |
||
203 | logfilestate = LOGFILE_ERROR; |
||
204 | printf("\r\nError opening kml-file: %s\r\n", logfilename); |
||
205 | logtimer = SetDelay(10); // try again in open logfile in 10 mili sec |
||
206 | } |
||
207 | } |
||
208 | else |
||
209 | { |
||
210 | logfilestate = LOGFILE_ERROR; |
||
211 | printf("\r\nError getting free kml-file name\r\n"); |
||
212 | } |
||
213 | // else retry in next loop |
||
214 | break; |
||
215 | case LOGFILE_OPENED: |
||
216 | // append new gps log data |
||
217 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
||
218 | { |
||
219 | if(!KML_LoggGPSCoordinates(&logfile)) |
||
220 | { // error logging data |
||
221 | printf("\r\nError logging to kml-file\r\n"); |
||
222 | KML_DocumentClose(&logfile); |
||
223 | logfilestate = LOGFILE_ERROR; |
||
224 | } |
||
225 | else // sucessfully logged |
||
226 | { |
||
227 | if(CheckDelay(flushtimer)) |
||
228 | { |
||
229 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
||
230 | fflush_(logfile.file); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | } |
||
235 | break; |
||
236 | |||
237 | case LOGFILE_ERROR: |
||
238 | break; |
||
239 | |||
240 | default: |
||
241 | logfilestate = LOGFILE_IDLE; |
||
242 | break; |
||
243 | } |
||
244 | } // EOF follow me on |
||
245 | else // follow me off |
||
246 | { // close log file if opened |
||
247 | if(logfilestate == LOGFILE_OPENED) |
||
248 | { |
||
249 | if(KML_DocumentClose(&logfile)) |
||
250 | { |
||
251 | printf("\r\nClosing kml-file\r\n"); |
||
252 | logfilestate = LOGFILE_CLOSED; |
||
253 | } |
||
254 | else // could not be closed |
||
255 | { |
||
256 | printf("\r\nError closing kml-file\r\n"); |
||
257 | logfilestate = LOGFILE_ERROR; |
||
258 | } |
||
259 | } |
||
260 | } //EOF follow me off |
||
261 | |||
262 | } // EOF Check LogTimer |
||
263 | |||
264 | return logfilestate; |
||
265 | } |
||
266 | |||
267 | //---------------------------------------------------------------------------------------------------- |
||
268 | // logs gps and state info to a gpx file |
||
269 | logfilestate_t Logging_GPX(uint16_t LogDelay) |
||
270 | { |
||
271 | static logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate |
||
272 | static int8_t* logfilename = NULL; // the pointer to the logfilename |
||
273 | static uint16_t logtimer = 0, flushtimer = 0; // the log update timer |
||
274 | static GPX_Document_t logfile; // the logfilehandle |
||
275 | |||
276 | // initialize if LogDelay os zero |
||
277 | if(!LogDelay) |
||
278 | { |
||
279 | switch(logfilestate) |
||
280 | { |
||
281 | case LOGFILE_OPENED: |
||
282 | GPX_DocumentClose(&logfile); // try to close it |
||
283 | break; |
||
284 | default: |
||
285 | break; |
||
286 | } |
||
287 | logfilestate = LOGFILE_IDLE; |
||
288 | logfilename = NULL; |
||
289 | GPX_DocumentInit(&logfile); |
||
290 | logtimer = SetDelay(0); // set logtimer to now |
||
291 | return logfilestate; |
||
292 | } |
||
293 | // no init |
||
294 | if(CheckDelay(logtimer)) |
||
295 | { |
||
296 | logtimer = SetDelay(LogDelay); // standard interval |
||
297 | |||
298 | if(SysState == STATE_SEND_FOLLOWME) |
||
299 | { |
||
300 | switch(logfilestate) |
||
301 | { |
||
302 | case LOGFILE_IDLE: |
||
303 | case LOGFILE_CLOSED: |
||
304 | //if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) ) |
||
305 | { |
||
306 | logfilestate = LOGFILE_START; |
||
307 | } |
||
308 | break; |
||
309 | case LOGFILE_START: |
||
310 | // find unused logfile name |
||
311 | do |
||
312 | { // try to generate a new logfile name |
||
313 | logfilename = GenerateGPXLogFileName(); |
||
314 | }while((logfilename != NULL) && fexist_(logfilename)); |
||
315 | // if logfilename exist |
||
316 | if(logfilename != NULL) |
||
317 | { |
||
318 | // try to create the log file |
||
319 | if(GPX_DocumentOpen(logfilename, &logfile)) |
||
320 | { |
||
321 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
||
322 | logfilestate = LOGFILE_OPENED; // goto next step |
||
323 | printf("\r\nOpening gpx-file: %s\r\n", logfilename); |
||
324 | } |
||
325 | else // could not be openend |
||
326 | { |
||
327 | logfilestate = LOGFILE_ERROR; |
||
328 | printf("\r\nError opening gpx-file: %s\r\n", logfilename); |
||
329 | logtimer = SetDelay(10); // try again in open logfile in 10 mili sec |
||
330 | } |
||
331 | } |
||
332 | else |
||
333 | { |
||
334 | logfilestate = LOGFILE_ERROR; |
||
335 | printf("\r\nError getting free gpx-file name\r\n"); |
||
336 | } |
||
337 | // else retry in next loop |
||
338 | break; |
||
339 | case LOGFILE_OPENED: |
||
340 | // append new gps log data |
||
341 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
||
342 | { |
||
343 | if(!GPX_LoggGPSCoordinates(&logfile)) |
||
344 | { // error logging data |
||
345 | printf("\r\nError logging to gpx-file\r\n"); |
||
346 | GPX_DocumentClose(&logfile); |
||
347 | logfilestate = LOGFILE_ERROR; |
||
348 | } |
||
349 | else // successful log |
||
350 | { |
||
351 | if(CheckDelay(flushtimer)) |
||
352 | { |
||
353 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
||
354 | fflush_(logfile.file); |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | break; |
||
359 | |||
360 | case LOGFILE_ERROR: |
||
361 | break; |
||
362 | |||
363 | default: |
||
364 | logfilestate = LOGFILE_IDLE; |
||
365 | break; |
||
366 | } |
||
367 | } // EOF follow me on |
||
368 | else // follow me off |
||
369 | { // close log file if opened |
||
370 | if(logfilestate == LOGFILE_OPENED) |
||
371 | { |
||
372 | if(GPX_DocumentClose(&logfile)) |
||
373 | { |
||
374 | printf("\r\nClosing gpx-file\r\n"); |
||
375 | logfilestate = LOGFILE_CLOSED; |
||
376 | } |
||
377 | else // could not be closed |
||
378 | { |
||
379 | printf("\r\nError closing gpx-file\r\n"); |
||
380 | logfilestate = LOGFILE_ERROR; |
||
381 | } |
||
382 | } |
||
383 | } //EOF follow me off |
||
384 | } // EOF Check LogTimer |
||
385 | |||
386 | return logfilestate; |
||
387 | } |
||
388 | |||
389 | //---------------------------------------------------------------------------------------------------- |
||
390 | // initialize logging |
||
391 | void Logging_Init(void) |
||
392 | { |
||
393 | LogCfg.KML_Interval = 500; //default |
||
436 | killagreg | 394 | Settings_GetParamValue(PID_KML_LOGGING, &(LogCfg.KML_Interval)); // overwrite by settings value |
438 | killagreg | 395 | Logging_KML(0); // initialize |
436 | killagreg | 396 | LogCfg.GPX_Interval = 1000; //default |
397 | Settings_GetParamValue(PID_GPX_LOGGING, &(LogCfg.GPX_Interval)); // overwrite by settings value |
||
398 | Logging_GPX(0); // initialize |
||
426 | killagreg | 399 | } |
400 | |||
401 | //---------------------------------------------------------------------------------------------------- |
||
402 | // gobal logging handler |
||
403 | void Logging_Update(void) |
||
404 | { |
||
405 | static uint16_t logtimer = 0; |
||
406 | static logfilestate_t logstate = LOGFILE_IDLE; |
||
407 | |||
408 | |||
409 | if(SD_SWITCH) // a card is in slot |
||
410 | { |
||
411 | if(CheckDelay(logtimer)) |
||
412 | { |
||
413 | logtimer = SetDelay(10); // faster makes no sense |
||
414 | // call the logger handlers if no error has occured |
||
415 | if(logstate != LOGFILE_ERROR) logstate = Logging_KML(LogCfg.KML_Interval); |
||
436 | killagreg | 416 | if(logstate != LOGFILE_ERROR) logstate = Logging_GPX(LogCfg.GPX_Interval); |
426 | killagreg | 417 | |
418 | // a logging error has occured |
||
419 | if(logstate == LOGFILE_ERROR) |
||
420 | { |
||
421 | if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside |
||
422 | { |
||
423 | Logging_Init(); // initialize the logs |
||
424 | logstate = LOGFILE_IDLE; |
||
425 | logtimer = SetDelay(10); // try next log in 10 mili sec |
||
426 | } |
||
427 | else |
||
428 | { // retry in 5 seconds |
||
429 | logtimer = SetDelay(5000); // try again in 5 sec |
||
430 | } |
||
431 | } //EOF logfile error |
||
432 | } // EOF CheckDelay |
||
433 | }// EOF Card in Slot |
||
434 | } |