Subversion Repositories NaviCtrl

Rev

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

Rev 310 Rev 345
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
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.
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
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
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
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
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"
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
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
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.
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
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
42
// +     clearly linked as origin
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
//
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
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
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
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
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <stdio.h>
57
#include <stdio.h>
58
#include "91x_lib.h"
58
#include "91x_lib.h"
59
#include "main.h"
59
#include "main.h"
60
#include "timer1.h"
60
#include "timer1.h"
61
#include "uart1.h"
61
#include "uart1.h"
62
#include "kml.h"
62
#include "kml.h"
63
#include "gpx.h"
63
#include "gpx.h"
64
#include "ssc.h"
64
#include "ssc.h"
65
#include "settings.h"
65
#include "settings.h"
66
 
66
 
67
 
67
 
68
#define LOG_FLUSH_INTERVAL 20000 // 20s
68
#define LOG_FLUSH_INTERVAL 20000 // 20s
69
 
69
 
70
typedef enum
70
typedef enum
71
{
71
{
72
        LOGFILE_IDLE,
72
        LOGFILE_IDLE,
73
        LOGFILE_START,
73
        LOGFILE_START,
74
        LOGFILE_CLOSED,
74
        LOGFILE_CLOSED,
75
        LOGFILE_OPENED,
75
        LOGFILE_OPENED,
76
        LOGFILE_ERROR
76
        LOGFILE_ERROR
77
} logfilestate_t;
77
} logfilestate_t;
78
 
78
 
79
// logger handler prototypes
79
// logger handler prototypes
80
logfilestate_t Logging_KML(u32 LogDelay);
80
logfilestate_t Logging_KML(u32 LogDelay);
81
logfilestate_t Logging_GPX(u32 LogDelay);
81
logfilestate_t Logging_GPX(u32 LogDelay);
82
 
82
 
83
typedef struct
83
typedef struct
84
{
84
{
85
        u32 KML_Interval;  // the kml-log interval (0 = off)
85
        u32 KML_Interval;  // the kml-log interval (0 = off)
86
        u32 GPX_Interval;  // the gpx-log interval (0 = off)
86
        u32 GPX_Interval;  // the gpx-log interval (0 = off)
87
} LogCfg_t;
87
} LogCfg_t;
88
 
88
 
89
LogCfg_t LogCfg = {500 , 1000};
89
LogCfg_t LogCfg = {500 , 1000};
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.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
                                                }
211
                                                }
212
                                        }
212
                                        }
213
                                        else
213
                                        else
214
                                        {
214
                                        {
215
                                                logfilestate = LOGFILE_ERROR;
215
                                                logfilestate = LOGFILE_ERROR;
216
                                                UART1_PutString("\r\nError getting free kml-file name\r\n");
216
                                                UART1_PutString("\r\nError getting free kml-file name\r\n");
217
                                        }
217
                                        }
218
                                        // else retry in next loop
218
                                        // else retry in next loop
219
                                        break;
219
                                        break;
220
                                case LOGFILE_OPENED:
220
                                case LOGFILE_OPENED:
221
                                        // append new gps log data
221
                                        // append new gps log data
222
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
222
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
223
                                        {
223
                                        {
224
                                                if(!KML_LoggGPSCoordinates(&logfile))
224
                                                if(!KML_LoggGPSCoordinates(&logfile))
225
                                                {       // error logging data
225
                                                {       // error logging data
226
                                                        UART1_PutString("\r\nError logging to kml-file\r\n");
226
                                                        UART1_PutString("\r\nError logging to kml-file\r\n");
227
                                                        KML_DocumentClose(&logfile);
227
                                                        KML_DocumentClose(&logfile);
228
                                                        logfilestate = LOGFILE_ERROR;
228
                                                        logfilestate = LOGFILE_ERROR;
229
                                                }
229
                                                }
230
                                                else // sucessfully logged
230
                                                else // sucessfully logged
231
                                                {
231
                                                {
232
                                                        if(CheckDelay(flushtimer))
232
                                                        if(CheckDelay(flushtimer))
233
                                                        {
233
                                                        {
234
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
234
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
235
                                                                fflush_(logfile.file);
235
                                                                fflush_(logfile.file);
236
                                                        }
236
                                                        }
237
                                                }
237
                                                }
238
 
238
 
239
                                        }
239
                                        }
240
                                        break;
240
                                        break;
241
 
241
 
242
                                case LOGFILE_ERROR:
242
                                case LOGFILE_ERROR:
243
                                        break;
243
                                        break;
244
 
244
 
245
                                default:
245
                                default:
246
                                        logfilestate = LOGFILE_IDLE;
246
                                        logfilestate = LOGFILE_IDLE;
247
                                        break;
247
                                        break;
248
                        }
248
                        }
249
                } // EOF motors are not running
249
                } // EOF motors are not running
250
                else // model is not flying
250
                else // model is not flying
251
                {   // close log file if opened
251
                {   // close log file if opened
252
                        if(logfilestate == LOGFILE_OPENED)
252
                        if(logfilestate == LOGFILE_OPENED)
253
                        {
253
                        {
254
                                if(KML_DocumentClose(&logfile))
254
                                if(KML_DocumentClose(&logfile))
255
                                {
255
                                {
256
                                        UART1_PutString("\r\nClosing kml-file\r\n");
256
                                        UART1_PutString("\r\nClosing kml-file\r\n");
257
                                        logfilestate = LOGFILE_CLOSED;
257
                                        logfilestate = LOGFILE_CLOSED;
258
                                }
258
                                }
259
                                else  // could not be closed
259
                                else  // could not be closed
260
                                {
260
                                {
261
                                        UART1_PutString("\r\nError closing kml-file\r\n");
261
                                        UART1_PutString("\r\nError closing kml-file\r\n");
262
                                        logfilestate =  LOGFILE_ERROR;
262
                                        logfilestate =  LOGFILE_ERROR;
263
                                }
263
                                }
264
                        }
264
                        }
265
                } //EOF motors are not running
265
                } //EOF motors are not running
266
        } // EOF Check LogTimer
266
        } // EOF Check LogTimer
267
 
267
 
268
        return logfilestate;
268
        return logfilestate;
269
}
269
}
270
 
270
 
271
//----------------------------------------------------------------------------------------------------
271
//----------------------------------------------------------------------------------------------------
272
// logs gps and state info to a gpx file
272
// logs gps and state info to a gpx file
273
logfilestate_t Logging_GPX(u32 LogDelay)
273
logfilestate_t Logging_GPX(u32 LogDelay)
274
{
274
{
275
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
275
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
276
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
276
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
277
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
277
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
278
        static  GPX_Document_t logfile;                                         // the logfilehandle
278
        static  GPX_Document_t logfile;                                         // the logfilehandle
-
 
279
        static  u8 part = 0;
-
 
280
        u32 measure_time;
279
 
281
 
280
        // initialize if LogDelay is zero
282
        // initialize if LogDelay is zero
281
        if(!LogDelay)
283
        if(!LogDelay)
282
        {
284
        {
283
                switch(logfilestate)
285
                switch(logfilestate)
284
                {
286
                {
285
                        case LOGFILE_OPENED:
287
                        case LOGFILE_OPENED:
286
                                GPX_DocumentClose(&logfile); // try to close it
288
                                GPX_DocumentClose(&logfile); // try to close it
287
                                break;
289
                                break;
288
                        default:
290
                        default:
289
                                break;
291
                                break;
290
                }
292
                }
291
                logfilestate = LOGFILE_IDLE;
293
                logfilestate = LOGFILE_IDLE;
292
                logfilename = NULL;
294
                logfilename = NULL;
293
                GPX_DocumentInit(&logfile);
295
                GPX_DocumentInit(&logfile);
294
                logtimer = SetDelay(0); // set logtimer to now
296
                logtimer = SetDelay(0); // set logtimer to now
295
                return logfilestate;
297
                return logfilestate;
296
        }
298
        }
297
        // no init
299
        // no init
298
        if(CheckDelay(logtimer))
300
        if(CheckDelay(logtimer) || part)
299
        {
301
        {
300
                logtimer = SetDelay(LogDelay);  // standard interval
302
                logtimer = SetDelay(LogDelay);  // standard interval
301
 
303
 
302
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN)
304
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN || part)
303
                {
305
                {
304
                        switch(logfilestate)
306
                        switch(logfilestate)
305
                        {
307
                        {
306
                                case LOGFILE_IDLE:
308
                                case LOGFILE_IDLE:
307
                                case LOGFILE_CLOSED:
309
                                case LOGFILE_CLOSED:
308
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.StatusFlags & FC_STATUS_FLY))
310
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.StatusFlags & FC_STATUS_FLY))
309
                                        {
311
                                        {
310
                                                logfilestate = LOGFILE_START;
312
                                                logfilestate = LOGFILE_START;
311
                                        }
313
                                        }
-
 
314
                                        part = 0;
312
                                        break;
315
                                        break;
313
                                case LOGFILE_START:
316
                                case LOGFILE_START:
314
                                        // find unused logfile name
317
                                        // find unused logfile name
315
                                        do
318
                                        do
316
                                        {        // try to generate a new logfile name
319
                                        {        // try to generate a new logfile name
317
                                                 logfilename = GenerateGPXLogFileName();
320
                                                 logfilename = GenerateGPXLogFileName();
318
                                        }while((logfilename != NULL) && fexist_(logfilename));
321
                                        }while((logfilename != NULL) && fexist_(logfilename));
319
                                        // if logfilename exist
322
                                        // if logfilename exist
320
                                        if(logfilename != NULL)
323
                                        if(logfilename != NULL)
321
                                        {
324
                                        {
322
                                                // try to create the log file
325
                                                // try to create the log file
323
                                                if(GPX_DocumentOpen(logfilename, &logfile))
326
                                                if(GPX_DocumentOpen(logfilename, &logfile))
324
                                                {
327
                                                {
325
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
328
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
326
                                                        logfilestate = LOGFILE_OPENED; // goto next step
329
                                                        logfilestate = LOGFILE_OPENED; // goto next step
327
                                                        UART1_PutString("\r\nOpening gpx-file:");
330
                                                        UART1_PutString("\r\nOpening gpx-file:");
328
                                                        UART1_PutString(logfilename);
331
                                                        UART1_PutString(logfilename);
329
                                                        UART1_PutString("\r\n");
332
                                                        UART1_PutString("\r\n");
330
                                                }
333
                                                }
331
                                                else // could not be openend
334
                                                else // could not be openend
332
                                                {
335
                                                {
333
                                                        logfilestate = LOGFILE_ERROR;
336
                                                        logfilestate = LOGFILE_ERROR;
334
                                                        UART1_PutString("\r\nError opening gpx-file: ");
337
                                                        UART1_PutString("\r\nError opening gpx-file: ");
335
                                                        UART1_PutString(logfilename);
338
                                                        UART1_PutString(logfilename);
336
                                                        UART1_PutString("\r\n");
339
                                                        UART1_PutString("\r\n");
337
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
340
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
338
                                                }
341
                                                }
339
                                        }
342
                                        }
340
                                        else
343
                                        else
341
                                        {
344
                                        {
342
                                                logfilestate = LOGFILE_ERROR;
345
                                                logfilestate = LOGFILE_ERROR;
343
                                                UART1_PutString("\r\nError getting free gpx-file name\r\n");
346
                                                UART1_PutString("\r\nError getting free gpx-file name\r\n");
344
                                        }
347
                                        }
345
                                        // else retry in next loop
348
                                        // else retry in next loop
-
 
349
                                        part = 0;
346
                                        break;
350
                                        break;
347
                                case LOGFILE_OPENED:
351
                                case LOGFILE_OPENED:
348
                                        // append new gps log data
352
                                        // append new gps log data
-
 
353
measure_time = CountMilliseconds;
349
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
354
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
350
                                        {
355
                                        {
351
                                                if(!GPX_LoggGPSCoordinates(&logfile))
356
                                                if(!GPX_LoggGPSCoordinates(&logfile,part))
352
                                                {       // error logging data
357
                                                {       // error logging data
353
                                                        UART1_PutString("\r\nError logging to gpx-file\r\n");
358
                                                        UART1_PutString("\r\nError logging to gpx-file\r\n");
354
                                                        GPX_DocumentClose(&logfile);
359
                                                        GPX_DocumentClose(&logfile);
355
                                                        logfilestate = LOGFILE_ERROR;
360
                                                        logfilestate = LOGFILE_ERROR;
356
                                                }
361
                                                }
357
                                                else // successful log
362
                                                else // successful log
358
                                                {
363
                                                {
359
                                                        if(CheckDelay(flushtimer))
364
                                                        if(CheckDelay(flushtimer))
360
                                                        {
365
                                                        {
361
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
366
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
362
                                                                fflush_(logfile.file);
367
                                                                fflush_(logfile.file);
363
                                                        }
368
                                                        }
364
                                                }
369
                                                }
365
                                        }
370
                                        }
-
 
371
                                        if(++part == 9) part = 0;
-
 
372
DebugOut.Analog[19] = CountMilliseconds - measure_time;
366
                                        break;
373
                                        break;
367
 
374
 
368
                                case LOGFILE_ERROR:
375
                                case LOGFILE_ERROR:
-
 
376
                                        part = 0;
369
                                        break;
377
                                        break;
370
 
378
 
371
                                default:
379
                                default:
-
 
380
                                        part = 0;
372
                                        logfilestate = LOGFILE_IDLE;
381
                                        logfilestate = LOGFILE_IDLE;
373
                                        break;
382
                                        break;
374
                        }
383
                        }
375
                } // EOF model is flying
384
                } // EOF model is flying
376
                else // model is not flying
385
                else // model is not flying
377
                {   // close log file if opened
386
                {   // close log file if opened
378
                        if(logfilestate == LOGFILE_OPENED)
387
                        if(logfilestate == LOGFILE_OPENED)
379
                        {
388
                        {
380
                                if(GPX_DocumentClose(&logfile))
389
                                if(GPX_DocumentClose(&logfile))
381
                                {
390
                                {
382
                                        UART1_PutString("\r\nClosing gpx-file\r\n");
391
                                        UART1_PutString("\r\nClosing gpx-file\r\n");
383
                                        logfilestate = LOGFILE_CLOSED;
392
                                        logfilestate = LOGFILE_CLOSED;
-
 
393
                                        part = 0;
384
                                }
394
                                }
385
                                else  // could not be closed
395
                                else  // could not be closed
386
                                {
396
                                {
387
                                        UART1_PutString("\r\nError closing gpx-file\r\n");
397
                                        UART1_PutString("\r\nError closing gpx-file\r\n");
388
                                        logfilestate =  LOGFILE_ERROR;
398
                                        logfilestate =  LOGFILE_ERROR;
-
 
399
                                        part = 0;
389
                                }
400
                                }
390
                        }
401
                        }
391
                } //EOF model is not flying
402
                } //EOF model is not flying
392
        } // EOF Check LogTimer
403
        } // EOF Check LogTimer
393
 
404
 
394
        return logfilestate;
405
        return logfilestate;
395
}
406
}
396
 
407
 
397
//----------------------------------------------------------------------------------------------------
408
//----------------------------------------------------------------------------------------------------
398
// initialize logging
409
// initialize logging
399
void Logging_Init(void)
410
void Logging_Init(void)
400
{
411
{
401
        LogCfg.KML_Interval = 500; //default
412
        LogCfg.KML_Interval = 500; //default
402
        Settings_GetParamValue(PID_KML_LOGGING, (u16*)&LogCfg.KML_Interval); // overwrite by settings value
413
        Settings_GetParamValue(PID_KML_LOGGING, (u16*)&LogCfg.KML_Interval); // overwrite by settings value
403
        if(LogCfg.KML_Interval < 500) LogCfg.KML_Interval = 500;
414
        if(LogCfg.KML_Interval < 500) LogCfg.KML_Interval = 500;
404
 
415
 
405
        Logging_KML(0); // initialize
416
        Logging_KML(0); // initialize
406
        LogCfg.GPX_Interval = 0; //default
417
        LogCfg.GPX_Interval = 0; //default
407
        Settings_GetParamValue(PID_GPX_LOGGING, (u16*)&LogCfg.GPX_Interval); // overwrite by settings value
418
        Settings_GetParamValue(PID_GPX_LOGGING, (u16*)&LogCfg.GPX_Interval); // overwrite by settings value
408
        if(LogCfg.GPX_Interval < 500) LogCfg.GPX_Interval = 500;
419
        if(LogCfg.GPX_Interval < 500) LogCfg.GPX_Interval = 500;
409
        Logging_GPX(0); // initialize
420
        Logging_GPX(0); // initialize
410
}
421
}
411
 
422
 
412
//----------------------------------------------------------------------------------------------------
423
//----------------------------------------------------------------------------------------------------
413
// gobal logging handler
424
// gobal logging handler
414
void Logging_Update(void)
425
void Logging_Update(void)
415
{
426
{
416
        static u8 logmodule = 0;
427
        static u8 logmodule = 0;
417
        static u32 logtimer = 0;
428
        static u32 logtimer = 0;
418
        static logfilestate_t logstate = LOGFILE_IDLE;
429
        static logfilestate_t logstate = LOGFILE_IDLE;
419
 
430
 
420
        if(SD_SWITCH) // a card is in slot
431
        if(SD_SWITCH) // a card is in slot
421
        {
432
        {
422
                if(CheckDelay(logtimer))
433
                if(CheckDelay(logtimer))
423
                {
434
                {
424
                        logtimer = SetDelay(10);  // faster makes no sense
435
                        logtimer = SetDelay(10);  // faster makes no sense
425
                        // call the logger handlers if no error has occured
436
                        // call the logger handlers if no error has occured
426
                        if(logstate != LOGFILE_ERROR)
437
                        if(logstate != LOGFILE_ERROR)
427
                        {  // run only one logging handler per loop
438
                        {  // run only one logging handler per loop
428
                                switch(logmodule++)
439
                                switch(logmodule++)
429
                                {
440
                                {
430
                                        case 0:
441
                                        case 0:
431
                                                logstate = Logging_KML(LogCfg.KML_Interval);
442
                                                logstate = Logging_KML(LogCfg.KML_Interval);
432
                                                break;
443
                                                break;
433
                                        case 1:
444
                                        case 1:
434
                                                logstate = Logging_GPX(LogCfg.GPX_Interval);
445
                                                logstate = Logging_GPX(LogCfg.GPX_Interval);
435
                                                logmodule = 0; // resart with first log module
446
                                                logmodule = 0; // resart with first log module
436
                                                break;
447
                                                break;
437
                                        default:
448
                                        default:
438
                                                logmodule = 0;
449
                                                logmodule = 0;
439
                                                break;
450
                                                break;
440
                                }
451
                                }
441
                        }
452
                        }
442
                        else // a logging error has occured
453
                        else // a logging error has occured
443
                        {      
454
                        {      
444
                                /*
455
                                /*
445
                                // try to reinitialize sd-card when motors are not running
456
                                // try to reinitialize sd-card when motors are not running
446
                                if(!(FC.Flags & FCFLAG_MOTOR_RUN))
457
                                if(!(FC.Flags & FCFLAG_MOTOR_RUN))
447
                                {
458
                                {
448
                                        if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside
459
                                        if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside
449
                                        {
460
                                        {
450
                                                Logging_Init(); // initialize the logs
461
                                                Logging_Init(); // initialize the logs
451
                                                logstate = LOGFILE_IDLE;
462
                                                logstate = LOGFILE_IDLE;
452
                                                logtimer = SetDelay(10);        // try next log in 10 mili sec
463
                                                logtimer = SetDelay(10);        // try next log in 10 mili sec
453
                                        }
464
                                        }
454
                                        else
465
                                        else
455
                                        {   // retry in 5 seconds
466
                                        {   // retry in 5 seconds
456
                                                logtimer = SetDelay(5000);  // try again in 5 sec
467
                                                logtimer = SetDelay(5000);  // try again in 5 sec
457
                                        }
468
                                        }
458
                                } // EOF motors are not running
469
                                } // EOF motors are not running
459
                                */
470
                                */
460
                        } //EOF logfile error
471
                        } //EOF logfile error
461
                }  // EOF CheckDelay
472
                }  // EOF CheckDelay
462
        }// EOF Card in Slot
473
        }// EOF Card in Slot
463
}
474
}
464
 
475