Subversion Repositories NaviCtrl

Rev

Rev 426 | Rev 429 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 426 Rev 427
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 20000 // 20s   //##
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
                sprintf(filename, "LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
111
                sprintf(filename, "/LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
111
                filenum++;
112
                filenum++;
112
                return filename;
113
                return filename;
113
        }
114
        }
114
        else return NULL;
115
        else return NULL;
115
}
116
}
116
 
117
 
117
//----------------------------------------------------------------------------------------------------
118
//----------------------------------------------------------------------------------------------------
118
s8* GenerateGPXLogFileName(void)
119
s8* GenerateGPXLogFileName(void)
119
{
120
{
120
        static u16 filenum = 0; // file name counter
121
        static u16 filenum = 0; // file name counter
121
        static s8 filename[35];
122
        static s8 filename[35];
122
        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};
123
 
124
 
124
        if(SystemTime.Valid)
125
        if(SystemTime.Valid)
125
        {
126
        {
126
                // if the day has been changed
127
                // if the day has been changed
127
                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))
128
                {
129
                {
129
                        LastTime.Year = SystemTime.Year;
130
                        LastTime.Year = SystemTime.Year;
130
                        LastTime.Month = SystemTime.Month;
131
                        LastTime.Month = SystemTime.Month;
131
                        LastTime.Day = SystemTime.Day;
132
                        LastTime.Day = SystemTime.Day;
132
                        LastTime.Valid = 1;
133
                        LastTime.Valid = 1;
133
                        filenum = 0; // reset file counter
134
                        filenum = 0; // reset file counter
134
                }
135
                }
-
 
136
                // use filename with absolute path i.e. leading
135
                sprintf(filename, "LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
137
                sprintf(filename, "/LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
136
                filenum++;
138
                filenum++;
137
                return filename;
139
                return filename;
138
        }
140
        }
139
        else return NULL;
141
        else return NULL;
140
}
142
}
141
 
143
 
142
 
144
 
143
 
145
 
144
//----------------------------------------------------------------------------------------------------
146
//----------------------------------------------------------------------------------------------------
145
// logs the current gps position to a kml file
147
// logs the current gps position to a kml file
146
logfilestate_t Logging_KML(u32 LogDelay)
148
logfilestate_t Logging_KML(u32 LogDelay)
147
{
149
{
148
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
150
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
149
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
151
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
150
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
152
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
151
        static  KML_Document_t logfile;                                         // the logfilehandle
153
        static  KML_Document_t logfile;                                         // the logfilehandle
152
 
154
 
153
        // initialize if LogDelay is zero
155
        // initialize if LogDelay is zero
154
        if(!LogDelay)
156
        if(!LogDelay)
155
        {
157
        {
156
                switch(logfilestate)
158
                switch(logfilestate)
157
                {
159
                {
158
                        case LOGFILE_OPENED:
160
                        case LOGFILE_OPENED:
159
                                KML_DocumentClose(&logfile); // try to close it
161
                                KML_DocumentClose(&logfile); // try to close it
160
                                break;
162
                                break;
161
                        default:
163
                        default:
162
                                break;
164
                                break;
163
                }
165
                }
164
                logfilestate = LOGFILE_IDLE;
166
                logfilestate = LOGFILE_IDLE;
165
                logfilename = NULL;
167
                logfilename = NULL;
166
                KML_DocumentInit(&logfile);
168
                KML_DocumentInit(&logfile);
167
                logtimer = SetDelay(0); // set logtimer to now
169
                logtimer = SetDelay(0); // set logtimer to now
168
                return logfilestate;
170
                return logfilestate;
169
        }
171
        }
170
        // no init
172
        // no init
171
        if(CheckDelay(logtimer))
173
        if(CheckDelay(logtimer))
172
        {
174
        {
173
                logtimer = SetDelay(LogDelay);  // standard interval
175
                logtimer = SetDelay(LogDelay);  // standard interval
174
 
176
 
175
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN)
177
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN)
176
                {
178
                {
177
                        switch(logfilestate)
179
                        switch(logfilestate)
178
                        {
180
                        {
179
                                case LOGFILE_IDLE:
181
                                case LOGFILE_IDLE:
180
                                case LOGFILE_CLOSED:
182
                                case LOGFILE_CLOSED:
181
                                        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) && (GPSData.NumOfSats > 2) && /*(GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)*/ (FC.StatusFlags & FC_STATUS_FLY))
182
                                        {
184
                                        {
183
                                                logfilestate = LOGFILE_START;
185
                                                logfilestate = LOGFILE_START;
184
                                        }
186
                                        }
185
                                        break;
187
                                        break;
186
                                case LOGFILE_START:
188
                                case LOGFILE_START:
187
                                        // find unused logfile name
189
                                        // find unused logfile name
188
                                        do
190
                                        do
189
                                        {        // try to generate a new logfile name
191
                                        {        // try to generate a new logfile name
190
                                                 logfilename = GenerateKMLLogFileName();
192
                                                 logfilename = GenerateKMLLogFileName();
191
                                        }while((logfilename != NULL) && fexist_(logfilename));
193
                                        }while((logfilename != NULL) && fexist_(logfilename));
192
                                        // if logfilename exist
194
                                        // if logfilename exist
193
                                        if(logfilename != NULL)
195
                                        if(logfilename != NULL)
194
                                        {
196
                                        {
195
                                                // try to create the log file
197
                                                // try to create the log file
196
                                                if(KML_DocumentOpen(logfilename, &logfile))
198
                                                if(KML_DocumentOpen(logfilename, &logfile))
197
                                                {
199
                                                {
198
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
200
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
199
                                                        logfilestate = LOGFILE_OPENED; // goto next step
201
                                                        logfilestate = LOGFILE_OPENED; // goto next step
200
                                                        UART1_PutString("\r\nOpening kml-file:");
202
                                                        UART1_PutString("\r\nOpening kml-file:");
201
                                                        UART1_PutString(logfilename);
203
                                                        UART1_PutString(logfilename);
202
                                                        UART1_PutString("\r\n");
204
                                                        UART1_PutString("\r\n");
203
                                                }
205
                                                }
204
                                                else // could not be openend
206
                                                else // could not be openend
205
                                                {
207
                                                {
206
                                                        logfilestate = LOGFILE_ERROR;
208
                                                        logfilestate = LOGFILE_ERROR;
207
                                                        UART1_PutString("\r\nError opening kml-file: ");
209
                                                        UART1_PutString("\r\nError opening kml-file: ");
208
                                                        UART1_PutString(logfilename);
210
                                                        UART1_PutString(logfilename);
209
                                                        UART1_PutString("\r\n");
211
                                                        UART1_PutString("\r\n");
210
                                                        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
211
                                                        SD_LoggingError = 1;
213
                                                        SD_LoggingError = 1;
212
                                                }
214
                                                }
213
                                        }
215
                                        }
214
                                        else
216
                                        else
215
                                        {
217
                                        {
216
                                                logfilestate = LOGFILE_ERROR;
218
                                                logfilestate = LOGFILE_ERROR;
217
                                                UART1_PutString("\r\nError getting free kml-file name\r\n");
219
                                                UART1_PutString("\r\nError getting free kml-file name\r\n");
218
                                                SD_LoggingError = 2;
220
                                                SD_LoggingError = 2;
219
                                        }
221
                                        }
220
                                        // else retry in next loop
222
                                        // else retry in next loop
221
                                        break;
223
                                        break;
222
                                case LOGFILE_OPENED:
224
                                case LOGFILE_OPENED:
223
                                        // append new gps log data
225
                                        // append new gps log data
224
                                        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))
225
                                        {
227
                                        {
226
                                                if(!KML_LoggGPSCoordinates(&logfile))
228
                                                if(!KML_LoggGPSCoordinates(&logfile))
227
                                                {       // error logging data
229
                                                {       // error logging data
228
                                                        UART1_PutString("\r\nError logging to kml-file\r\n");
230
                                                        UART1_PutString("\r\nError logging to kml-file\r\n");
229
                                                        SD_LoggingError = 3;
231
                                                        SD_LoggingError = 3;
230
                                                        KML_DocumentClose(&logfile);
232
                                                        KML_DocumentClose(&logfile);
231
                                                        logfilestate = LOGFILE_ERROR;
233
                                                        logfilestate = LOGFILE_ERROR;
232
                                                }
234
                                                }
233
                                                else // sucessfully logged
235
                                                else // sucessfully logged
234
                                                {
236
                                                {
235
                                                        SD_LoggingError = 0;
237
                                                        SD_LoggingError = 0;
236
                                                        if(CheckDelay(flushtimer))
238
                                                        if(CheckDelay(flushtimer))
237
                                                        {
239
                                                        {
238
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
240
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
239
                                                                fflush_(logfile.file);
241
                                                                fflush_(logfile.file);
240
                                                        }
242
                                                        }
241
                                                }
243
                                                }
242
 
244
 
243
                                        }
245
                                        }
244
                                        break;
246
                                        break;
245
 
247
 
246
                                case LOGFILE_ERROR:
248
                                case LOGFILE_ERROR:
247
                                        break;
249
                                        break;
248
 
250
 
249
                                default:
251
                                default:
250
                                        logfilestate = LOGFILE_IDLE;
252
                                        logfilestate = LOGFILE_IDLE;
251
                                        break;
253
                                        break;
252
                        }
254
                        }
253
                } // EOF motors are not running
255
                } // EOF motors are not running
254
                else // model is not flying
256
                else // model is not flying
255
                {   // close log file if opened
257
                {   // close log file if opened
256
                        if(logfilestate == LOGFILE_OPENED)
258
                        if(logfilestate == LOGFILE_OPENED)
257
                        {
259
                        {
258
                                if(KML_DocumentClose(&logfile))
260
                                if(KML_DocumentClose(&logfile))
259
                                {
261
                                {
260
                                        UART1_PutString("\r\nClosing kml-file\r\n");
262
                                        UART1_PutString("\r\nClosing kml-file\r\n");
261
                                        logfilestate = LOGFILE_CLOSED;
263
                                        logfilestate = LOGFILE_CLOSED;
262
                                }
264
                                }
263
                                else  // could not be closed
265
                                else  // could not be closed
264
                                {
266
                                {
265
                                        UART1_PutString("\r\nError closing kml-file\r\n");
267
                                        UART1_PutString("\r\nError closing kml-file\r\n");
266
                                        SD_LoggingError = 4;
268
                                        SD_LoggingError = 4;
267
                                        logfilestate =  LOGFILE_ERROR;
269
                                        logfilestate =  LOGFILE_ERROR;
268
                                }
270
                                }
269
                        }
271
                        }
270
                } //EOF motors are not running
272
                } //EOF motors are not running
271
        } // EOF Check LogTimer
273
        } // EOF Check LogTimer
272
 
274
 
273
        return logfilestate;
275
        return logfilestate;
274
}
276
}
275
 
277
 
276
//----------------------------------------------------------------------------------------------------
278
//----------------------------------------------------------------------------------------------------
277
// logs gps and state info to a gpx file
279
// logs gps and state info to a gpx file
278
logfilestate_t Logging_GPX(u32 LogDelay)
280
logfilestate_t Logging_GPX(u32 LogDelay)
279
{
281
{
280
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
282
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
281
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
283
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
282
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
284
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
283
        static  GPX_Document_t logfile;                                         // the logfilehandle
285
        static  GPX_Document_t logfile;                                         // the logfilehandle
284
        static  u8 part = 0;
286
        static  u8 part = 0;
285
        u32 measure_time;
287
        u32 measure_time;
286
 
288
 
287
        // initialize if LogDelay is zero
289
        // initialize if LogDelay is zero
288
        if(!LogDelay)
290
        if(!LogDelay)
289
        {
291
        {
290
                switch(logfilestate)
292
                switch(logfilestate)
291
                {
293
                {
292
                        case LOGFILE_OPENED:
294
                        case LOGFILE_OPENED:
293
                                GPX_DocumentClose(&logfile); // try to close it
295
                                GPX_DocumentClose(&logfile); // try to close it
294
                                break;
296
                                break;
295
                        default:
297
                        default:
296
                                break;
298
                                break;
297
                }
299
                }
298
                logfilestate = LOGFILE_IDLE;
300
                logfilestate = LOGFILE_IDLE;
299
                logfilename = NULL;
301
                logfilename = NULL;
300
                GPX_DocumentInit(&logfile);
302
                GPX_DocumentInit(&logfile);
301
                logtimer = SetDelay(0); // set logtimer to now
303
                logtimer = SetDelay(0); // set logtimer to now
302
                return logfilestate;
304
                return logfilestate;
303
        }
305
        }
304
        // no init
306
        // no init
305
        if(CheckDelay(logtimer) || part)
307
        if(CheckDelay(logtimer) || part)
306
        {
308
        {
307
                if(!part) logtimer = SetDelay(LogDelay);        // standard interval
309
                if(!part) logtimer = SetDelay(LogDelay);        // standard interval
308
 
310
 
309
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN || part)
311
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN || part)
310
                {
312
                {
311
                        switch(logfilestate)
313
                        switch(logfilestate)
312
                        {
314
                        {
313
                                case LOGFILE_IDLE:
315
                                case LOGFILE_IDLE:
314
                                case LOGFILE_CLOSED:
316
                                case LOGFILE_CLOSED:
315
                                        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) && (GPSData.NumOfSats > 2) && /*(GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)*/ (FC.StatusFlags & FC_STATUS_FLY))
316
                                        {
318
                                        {
317
                                                logfilestate = LOGFILE_START;
319
                                                logfilestate = LOGFILE_START;
318
                                        }
320
                                        }
319
                                        part = 0;
321
                                        part = 0;
320
                                        break;
322
                                        break;
321
                                case LOGFILE_START:
323
                                case LOGFILE_START:
322
                                        // find unused logfile name
324
                                        // find unused logfile name
323
                                        do
325
                                        do
324
                                        {        // try to generate a new logfile name
326
                                        {        // try to generate a new logfile name
325
                                                 logfilename = GenerateGPXLogFileName();
327
                                                 logfilename = GenerateGPXLogFileName();
326
                                        }while((logfilename != NULL) && fexist_(logfilename));
328
                                        }while((logfilename != NULL) && fexist_(logfilename));
327
                                        // if logfilename exist
329
                                        // if logfilename exist
328
                                        if(logfilename != NULL)
330
                                        if(logfilename != NULL)
329
                                        {
331
                                        {
330
                                                // try to create the log file
332
                                                // try to create the log file
331
                                                if(GPX_DocumentOpen(logfilename, &logfile))
333
                                                if(GPX_DocumentOpen(logfilename, &logfile))
332
                                                {
334
                                                {
333
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
335
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
334
                                                        logfilestate = LOGFILE_OPENED; // goto next step
336
                                                        logfilestate = LOGFILE_OPENED; // goto next step
335
                                                        UART1_PutString("\r\nOpening gpx-file:");
337
                                                        UART1_PutString("\r\nOpening gpx-file:");
336
                                                        UART1_PutString(logfilename);
338
                                                        UART1_PutString(logfilename);
337
                                                        UART1_PutString("\r\n");
339
                                                        UART1_PutString("\r\n");
338
                                                }
340
                                                }
339
                                                else // could not be openend
341
                                                else // could not be openend
340
                                                {
342
                                                {
341
                                                        logfilestate = LOGFILE_ERROR;
343
                                                        logfilestate = LOGFILE_ERROR;
342
                                                        UART1_PutString("\r\nError opening gpx-file: ");
344
                                                        UART1_PutString("\r\nError opening gpx-file: ");
343
                                                        UART1_PutString(logfilename);
345
                                                        UART1_PutString(logfilename);
344
                                                        UART1_PutString("\r\n");
346
                                                        UART1_PutString("\r\n");
345
                                                        SD_LoggingError = 11;
347
                                                        SD_LoggingError = 11;
346
                                                        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
347
                                                }
349
                                                }
348
                                        }
350
                                        }
349
                                        else
351
                                        else
350
                                        {
352
                                        {
351
                                                logfilestate = LOGFILE_ERROR;
353
                                                logfilestate = LOGFILE_ERROR;
352
                                                UART1_PutString("\r\nError getting free gpx-file name\r\n");
354
                                                UART1_PutString("\r\nError getting free gpx-file name\r\n");
353
                                                SD_LoggingError = 12;
355
                                                SD_LoggingError = 12;
354
                                        }
356
                                        }
355
                                        // else retry in next loop
357
                                        // else retry in next loop
356
                                        part = 0;
358
                                        part = 0;
357
                                        break;
359
                                        break;
358
                                case LOGFILE_OPENED:
360
                                case LOGFILE_OPENED:
359
                                        // append new gps log data
361
                                        // append new gps log data
360
                                        measure_time = CountMilliseconds;
362
                                        measure_time = CountMilliseconds;
361
                                        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))
362
                                        {
364
                                        {
363
                                                if(!GPX_LoggGPSCoordinates(&logfile,part))
365
                                                if(!GPX_LoggGPSCoordinates(&logfile,part))
364
                                                {       // error logging data
366
                                                {       // error logging data
365
                                                        UART1_PutString("\r\nError logging to gpx-file\r\n");
367
                                                        UART1_PutString("\r\nError logging to gpx-file\r\n");
366
                                                        GPX_DocumentClose(&logfile);
368
                                                        GPX_DocumentClose(&logfile);
367
                                                        SD_LoggingError = 13;
369
                                                        SD_LoggingError = 13;
368
                                                        logfilestate = LOGFILE_ERROR;
370
                                                        logfilestate = LOGFILE_ERROR;
369
                                                }
371
                                                }
370
                                                else // successful log
372
                                                else // successful log
371
                                                {
373
                                                {
372
                                                        if(CheckDelay(flushtimer))
374
                                                        if(CheckDelay(flushtimer))
373
                                                        {
375
                                                        {
374
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
376
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
375
                                                                fflush_(logfile.file);
377
                                                                fflush_(logfile.file);
376
                                                        }
378
                                                        }
377
                                                }
379
                                                }
378
                                        }
380
                                        }
379
                                        if(++part == 9) part = 0;
381
                                        if(++part == 9) part = 0;
380
//DebugOut.Analog[19] = CountMilliseconds - measure_time;
382
//DebugOut.Analog[19] = CountMilliseconds - measure_time;
381
                                        break;
383
                                        break;
382
 
384
 
383
                                case LOGFILE_ERROR:
385
                                case LOGFILE_ERROR:
384
                                        part = 0;
386
                                        part = 0;
385
                                        break;
387
                                        break;
386
 
388
 
387
                                default:
389
                                default:
388
                                        part = 0;
390
                                        part = 0;
389
                                        logfilestate = LOGFILE_IDLE;
391
                                        logfilestate = LOGFILE_IDLE;
390
                                        break;
392
                                        break;
391
                        }
393
                        }
392
                } // EOF model is flying
394
                } // EOF model is flying
393
                else // model is not flying
395
                else // model is not flying
394
                {   // close log file if opened
396
                {   // close log file if opened
395
                        if(logfilestate == LOGFILE_OPENED)
397
                        if(logfilestate == LOGFILE_OPENED)
396
                        {
398
                        {
397
                                if(GPX_DocumentClose(&logfile))
399
                                if(GPX_DocumentClose(&logfile))
398
                                {
400
                                {
399
                                        UART1_PutString("\r\nClosing gpx-file\r\n");
401
                                        UART1_PutString("\r\nClosing gpx-file\r\n");
400
                                        logfilestate = LOGFILE_CLOSED;
402
                                        logfilestate = LOGFILE_CLOSED;
401
                                        part = 0;
403
                                        part = 0;
402
                                }
404
                                }
403
                                else  // could not be closed
405
                                else  // could not be closed
404
                                {
406
                                {
405
                                        UART1_PutString("\r\nError closing gpx-file\r\n");
407
                                        UART1_PutString("\r\nError closing gpx-file\r\n");
406
                                        SD_LoggingError = 14;
408
                                        SD_LoggingError = 14;
407
                                        logfilestate =  LOGFILE_ERROR;
409
                                        logfilestate =  LOGFILE_ERROR;
408
                                        part = 0;
410
                                        part = 0;
409
                                }
411
                                }
410
                        }
412
                        }
411
                } //EOF model is not flying
413
                } //EOF model is not flying
412
        } // EOF Check LogTimer
414
        } // EOF Check LogTimer
413
 
415
 
414
        return logfilestate;
416
        return logfilestate;
415
}
417
}
416
 
418
 
417
//----------------------------------------------------------------------------------------------------
419
//----------------------------------------------------------------------------------------------------
418
// initialize logging
420
// initialize logging
419
void Logging_Init(void)
421
void Logging_Init(void)
420
{
422
{
421
        SD_LoggingError = 0;
423
        SD_LoggingError = 0;
422
        LogCfg.KML_Interval = 1000; //default
424
        LogCfg.KML_Interval = 1000; //default
423
        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
424
        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;
425
 
427
 
426
        Logging_KML(0); // initialize
428
        Logging_KML(0); // initialize
427
        LogCfg.GPX_Interval = 0; //default
429
        LogCfg.GPX_Interval = 0; //default
428
        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
429
        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;
430
        Logging_GPX(0); // initialize
432
        Logging_GPX(0); // initialize
431
}
433
}
432
 
434
 
433
//----------------------------------------------------------------------------------------------------
435
//----------------------------------------------------------------------------------------------------
434
// gobal logging handler
436
// gobal logging handler
435
void Logging_Update(void)
437
void Logging_Update(void)
436
{
438
{
437
        static u8 logmodule = 0;
439
        static u8 logmodule = 0;
438
        static u32 logtimer = 0;
440
        static u32 logtimer = 0;
439
        static logfilestate_t logstate = LOGFILE_IDLE;
441
        static logfilestate_t logstate = LOGFILE_IDLE;
440
 
442
 
441
        if(SD_SWITCH) // a card is in slot
443
        if(SD_SWITCH) // a card is in slot
442
        {
444
        {
443
        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;
444
                if(CheckDelay(logtimer))
446
                if(CheckDelay(logtimer))
445
                {
447
                {
446
                        logtimer = SetDelay(10);  // faster makes no sense
448
                        logtimer = SetDelay(10);  // faster makes no sense
447
                        // call the logger handlers if no error has occured
449
                        // call the logger handlers if no error has occured
448
                        if(logstate != LOGFILE_ERROR)
450
                        if(logstate != LOGFILE_ERROR)
449
                        {  // run only one logging handler per loop
451
                        {  // run only one logging handler per loop
450
                                switch(logmodule++)
452
                                switch(logmodule++)
451
                                {
453
                                {
452
                                        case 0:
454
                                        case 0:
453
                                                logstate = Logging_KML(LogCfg.KML_Interval);
455
                                                logstate = Logging_KML(LogCfg.KML_Interval);
454
                                                break;
456
                                                break;
455
                                        case 1:
457
                                        case 1:
456
                                                logstate = Logging_GPX(LogCfg.GPX_Interval);
458
                                                logstate = Logging_GPX(LogCfg.GPX_Interval);
457
                                                logmodule = 0; // resart with first log module
459
                                                logmodule = 0; // resart with first log module
458
                                                break;
460
                                                break;
459
                                        default:
461
                                        default:
460
                                                logmodule = 0;
462
                                                logmodule = 0;
461
                                                break;
463
                                                break;
462
                                }
464
                                }
463
                        }
465
                        }
464
                        else // a logging error has occured
466
                        else // a logging error has occured
465
                        {      
467
                        {      
466
//                              /*
468
//                              /*
467
                                // try to reinitialize sd-card when motors are not running
469
                                // try to reinitialize sd-card when motors are not running
468
                                if(!(FC.StatusFlags & FC_STATUS_MOTOR_RUN))
470
                                if(!(FC.StatusFlags & FC_STATUS_MOTOR_RUN))
469
                                {
471
                                {
470
                                        if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside
472
                                        if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside
471
                                        {
473
                                        {
472
                                                Logging_Init(); // initialize the logs
474
                                                Logging_Init(); // initialize the logs
473
                                                logstate = LOGFILE_IDLE;
475
                                                logstate = LOGFILE_IDLE;
474
                                                logtimer = SetDelay(100);       // try next log in 100 mili sec
476
                                                logtimer = SetDelay(100);       // try next log in 100 mili sec
475
                                        }
477
                                        }
476
                                        else
478
                                        else
477
                                        {   // retry in 5 seconds
479
                                        {   // retry in 5 seconds
478
                                                logtimer = SetDelay(5000);  // try again in 5 sec
480
                                                logtimer = SetDelay(5000);  // try again in 5 sec
479
                                        }
481
                                        }
480
                                } // EOF motors are not running
482
                                } // EOF motors are not running
481
//                              */
483
//                              */
482
                        } //EOF logfile error
484
                        } //EOF logfile error
483
                }  // EOF CheckDelay
485
                }  // EOF CheckDelay
484
        }// EOF Card in Slot
486
        }// EOF Card in Slot
485
}
487
}
486
 
488