Rev 436 | Go to most recent revision | Details | 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" |
||
62 | //#include "gpx.h" |
||
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 | //---------------------------------------------------------------------------------------------------- |
||
118 | /*int8_t* GenerateGPXLogFileName(void) |
||
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; |
||
140 | }*/ |
||
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 | |||
152 | // initialize if LogDelay os zero |
||
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 | /* |
||
269 | // logs gps and state info to a gpx file |
||
270 | logfilestate_t Logging_GPX(uint16_t LogDelay) |
||
271 | { |
||
272 | static logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate |
||
273 | static int8_t* logfilename = NULL; // the pointer to the logfilename |
||
274 | static uint16_t logtimer = 0, flushtimer = 0; // the log update timer |
||
275 | static GPX_Document_t logfile; // the logfilehandle |
||
276 | |||
277 | // initialize if LogDelay os zero |
||
278 | if(!LogDelay) |
||
279 | { |
||
280 | switch(logfilestate) |
||
281 | { |
||
282 | case LOGFILE_OPENED: |
||
283 | GPX_DocumentClose(&logfile); // try to close it |
||
284 | break; |
||
285 | default: |
||
286 | break; |
||
287 | } |
||
288 | logfilestate = LOGFILE_IDLE; |
||
289 | logfilename = NULL; |
||
290 | GPX_DocumentInit(&logfile); |
||
291 | logtimer = SetDelay(0); // set logtimer to now |
||
292 | return logfilestate; |
||
293 | } |
||
294 | // no init |
||
295 | if(CheckDelay(logtimer)) |
||
296 | { |
||
297 | logtimer = SetDelay(LogDelay); // standard interval |
||
298 | |||
299 | if(SysState == STATE_SEND_FOLLOWME) |
||
300 | { |
||
301 | switch(logfilestate) |
||
302 | { |
||
303 | case LOGFILE_IDLE: |
||
304 | case LOGFILE_CLOSED: |
||
305 | //if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) ) |
||
306 | { |
||
307 | logfilestate = LOGFILE_START; |
||
308 | } |
||
309 | break; |
||
310 | case LOGFILE_START: |
||
311 | // find unused logfile name |
||
312 | do |
||
313 | { // try to generate a new logfile name |
||
314 | logfilename = GenerateGPXLogFileName(); |
||
315 | }while((logfilename != NULL) && fexist_(logfilename)); |
||
316 | // if logfilename exist |
||
317 | if(logfilename != NULL) |
||
318 | { |
||
319 | // try to create the log file |
||
320 | if(GPX_DocumentOpen(logfilename, &logfile)) |
||
321 | { |
||
322 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
||
323 | logfilestate = LOGFILE_OPENED; // goto next step |
||
324 | printf("\r\nOpening gpx-file: %s\r\n", logfilename); |
||
325 | } |
||
326 | else // could not be openend |
||
327 | { |
||
328 | logfilestate = LOGFILE_ERROR; |
||
329 | printf("\r\nError opening gpx-file: %s\r\n", logfilename); |
||
330 | logtimer = SetDelay(10); // try again in open logfile in 10 mili sec |
||
331 | } |
||
332 | } |
||
333 | else |
||
334 | { |
||
335 | logfilestate = LOGFILE_ERROR; |
||
336 | printf("\r\nError getting free gpx-file name\r\n"); |
||
337 | } |
||
338 | // else retry in next loop |
||
339 | break; |
||
340 | case LOGFILE_OPENED: |
||
341 | // append new gps log data |
||
342 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
||
343 | { |
||
344 | if(!GPX_LoggGPSCoordinates(&logfile)) |
||
345 | { // error logging data |
||
346 | printf("\r\nError logging to gpx-file\r\n"); |
||
347 | GPX_DocumentClose(&logfile); |
||
348 | logfilestate = LOGFILE_ERROR; |
||
349 | } |
||
350 | else // successful log |
||
351 | { |
||
352 | if(CheckDelay(flushtimer)) |
||
353 | { |
||
354 | flushtimer = SetDelay(LOG_FLUSH_INTERVAL); |
||
355 | fflush_(logfile.file); |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | break; |
||
360 | |||
361 | case LOGFILE_ERROR: |
||
362 | break; |
||
363 | |||
364 | default: |
||
365 | logfilestate = LOGFILE_IDLE; |
||
366 | break; |
||
367 | } |
||
368 | } // EOF follow me on |
||
369 | else // follow me off |
||
370 | { // close log file if opened |
||
371 | if(logfilestate == LOGFILE_OPENED) |
||
372 | { |
||
373 | if(GPX_DocumentClose(&logfile)) |
||
374 | { |
||
375 | printf("\r\nClosing gpx-file\r\n"); |
||
376 | logfilestate = LOGFILE_CLOSED; |
||
377 | } |
||
378 | else // could not be closed |
||
379 | { |
||
380 | printf("\r\nError closing gpx-file\r\n"); |
||
381 | logfilestate = LOGFILE_ERROR; |
||
382 | } |
||
383 | } |
||
384 | } //EOF follow me off |
||
385 | } // EOF Check LogTimer |
||
386 | |||
387 | return logfilestate; |
||
388 | } |
||
389 | */ |
||
390 | |||
391 | //---------------------------------------------------------------------------------------------------- |
||
392 | // initialize logging |
||
393 | void Logging_Init(void) |
||
394 | { |
||
395 | LogCfg.KML_Interval = 500; //default |
||
396 | Settings_GetParamValue(PID_KML_LOGGING, (uint16_t*)&LogCfg.KML_Interval); // overwrite by settings value |
||
397 | Logging_KML(0); // initialize |
||
398 | //LogCfg.GPX_Interval = 1000; //default |
||
399 | //Settings_GetParamValue(PID_GPX_LOGGING, (uint16_t*)&LogCfg.GPX_Interval); // overwrite by settings value |
||
400 | //Logging_GPX(0); // initialize |
||
401 | } |
||
402 | |||
403 | //---------------------------------------------------------------------------------------------------- |
||
404 | // gobal logging handler |
||
405 | void Logging_Update(void) |
||
406 | { |
||
407 | static uint16_t logtimer = 0; |
||
408 | static logfilestate_t logstate = LOGFILE_IDLE; |
||
409 | |||
410 | |||
411 | if(SD_SWITCH) // a card is in slot |
||
412 | { |
||
413 | if(CheckDelay(logtimer)) |
||
414 | { |
||
415 | logtimer = SetDelay(10); // faster makes no sense |
||
416 | // call the logger handlers if no error has occured |
||
417 | if(logstate != LOGFILE_ERROR) logstate = Logging_KML(LogCfg.KML_Interval); |
||
418 | //if(logstate != LOGFILE_ERROR) logstate = Logging_GPX(LogCfg.GPX_Interval); |
||
419 | |||
420 | // a logging error has occured |
||
421 | if(logstate == LOGFILE_ERROR) |
||
422 | { |
||
423 | if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside |
||
424 | { |
||
425 | Logging_Init(); // initialize the logs |
||
426 | logstate = LOGFILE_IDLE; |
||
427 | logtimer = SetDelay(10); // try next log in 10 mili sec |
||
428 | } |
||
429 | else |
||
430 | { // retry in 5 seconds |
||
431 | logtimer = SetDelay(5000); // try again in 5 sec |
||
432 | } |
||
433 | } //EOF logfile error |
||
434 | } // EOF CheckDelay |
||
435 | }// EOF Card in Slot |
||
436 | } |