Subversion Repositories NaviCtrl

Rev

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

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