Subversion Repositories NaviCtrl

Rev

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

Rev 357 Rev 360
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
-
 
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
-
 
7
// + FOR NON COMMERCIAL USE ONLY
-
 
8
// + www.MikroKopter.com
5
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
7
// + Software Nutzungsbedingungen (english version: see below)
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
15
// + Verkauf von Luftbildaufnahmen, usw.
-
 
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
22
// + eindeutig als Ursprung verlinkt werden
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
25
// + Benutzung auf eigene Gefahr
22
// + des Mitverschuldens offen.
-
 
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.
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
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
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + mit unserer Zustimmung zulässig
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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// + Software LICENSING TERMS
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + this list of conditions and the following disclaimer.
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
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// +     from this software without specific prior written permission.
37
// + The Software may only be used with the Licensor's products.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// +     for non-commercial use (directly or indirectly)
-
 
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// +     with our written permission
40
// + agreement shall be the property of the Licensor.
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// +     clearly linked as origin
-
 
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
44
//
-
 
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43
// + The customer shall be responsible for taking reasonable precautions
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
55
// +  POSSIBILITY OF SUCH DAMAGE.
53
// + #### END OF LICENSING TERMS ####
-
 
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <stdio.h>
56
#include <stdio.h>
58
#include "91x_lib.h"
57
#include "91x_lib.h"
59
#include "main.h"
58
#include "main.h"
60
#include "timer1.h"
59
#include "timer1.h"
61
#include "uart1.h"
60
#include "uart1.h"
62
#include "kml.h"
61
#include "kml.h"
63
#include "gpx.h"
62
#include "gpx.h"
64
#include "ssc.h"
63
#include "ssc.h"
65
#include "settings.h"
64
#include "settings.h"
66
 
65
 
67
u8 SD_LoggingError = 0;
66
u8 SD_LoggingError = 0;
68
 
67
 
69
#define LOG_FLUSH_INTERVAL 20000 // 20s
68
#define LOG_FLUSH_INTERVAL 20000 // 20s
70
 
69
 
71
typedef enum
70
typedef enum
72
{
71
{
73
        LOGFILE_IDLE,
72
        LOGFILE_IDLE,
74
        LOGFILE_START,
73
        LOGFILE_START,
75
        LOGFILE_CLOSED,
74
        LOGFILE_CLOSED,
76
        LOGFILE_OPENED,
75
        LOGFILE_OPENED,
77
        LOGFILE_ERROR
76
        LOGFILE_ERROR
78
} logfilestate_t;
77
} logfilestate_t;
79
 
78
 
80
// logger handler prototypes
79
// logger handler prototypes
81
logfilestate_t Logging_KML(u32 LogDelay);
80
logfilestate_t Logging_KML(u32 LogDelay);
82
logfilestate_t Logging_GPX(u32 LogDelay);
81
logfilestate_t Logging_GPX(u32 LogDelay);
83
 
82
 
84
typedef struct
83
typedef struct
85
{
84
{
86
        u32 KML_Interval;  // the kml-log interval (0 = off)
85
        u32 KML_Interval;  // the kml-log interval (0 = off)
87
        u32 GPX_Interval;  // the gpx-log interval (0 = off)
86
        u32 GPX_Interval;  // the gpx-log interval (0 = off)
88
} LogCfg_t;
87
} LogCfg_t;
89
 
88
 
90
LogCfg_t LogCfg = {500 , 1000};
89
LogCfg_t LogCfg = {500 , 1000};
91
 
90
 
92
 
91
 
93
//----------------------------------------------------------------------------------------------------
92
//----------------------------------------------------------------------------------------------------
94
s8* GenerateKMLLogFileName(void)
93
s8* GenerateKMLLogFileName(void)
95
{
94
{
96
        static u16 filenum = 0; // file name counter
95
        static u16 filenum = 0; // file name counter
97
        static s8 filename[35];
96
        static s8 filename[35];
98
        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};
99
 
98
 
100
        if(SystemTime.Valid)
99
        if(SystemTime.Valid)
101
        {
100
        {
102
                // if the day has been changed
101
                // if the day has been changed
103
                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))
104
                {
103
                {
105
                        LastTime.Year = SystemTime.Year;
104
                        LastTime.Year = SystemTime.Year;
106
                        LastTime.Month = SystemTime.Month;
105
                        LastTime.Month = SystemTime.Month;
107
                        LastTime.Day = SystemTime.Day;
106
                        LastTime.Day = SystemTime.Day;
108
                        LastTime.Valid = 1;
107
                        LastTime.Valid = 1;
109
                        filenum = 0; // reset file counter
108
                        filenum = 0; // reset file counter
110
                }
109
                }
111
                sprintf(filename, "LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
110
                sprintf(filename, "LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
112
                filenum++;
111
                filenum++;
113
                return filename;
112
                return filename;
114
        }
113
        }
115
        else return NULL;
114
        else return NULL;
116
}
115
}
117
 
116
 
118
//----------------------------------------------------------------------------------------------------
117
//----------------------------------------------------------------------------------------------------
119
s8* GenerateGPXLogFileName(void)
118
s8* GenerateGPXLogFileName(void)
120
{
119
{
121
        static u16 filenum = 0; // file name counter
120
        static u16 filenum = 0; // file name counter
122
        static s8 filename[35];
121
        static s8 filename[35];
123
        static DateTime_t LastTime = {0,0,0,0,0,0,0,0};
122
        static DateTime_t LastTime = {0,0,0,0,0,0,0,0};
124
 
123
 
125
        if(SystemTime.Valid)
124
        if(SystemTime.Valid)
126
        {
125
        {
127
                // if the day has been changed
126
                // if the day has been changed
128
                if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day))
127
                if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day))
129
                {
128
                {
130
                        LastTime.Year = SystemTime.Year;
129
                        LastTime.Year = SystemTime.Year;
131
                        LastTime.Month = SystemTime.Month;
130
                        LastTime.Month = SystemTime.Month;
132
                        LastTime.Day = SystemTime.Day;
131
                        LastTime.Day = SystemTime.Day;
133
                        LastTime.Valid = 1;
132
                        LastTime.Valid = 1;
134
                        filenum = 0; // reset file counter
133
                        filenum = 0; // reset file counter
135
                }
134
                }
136
                sprintf(filename, "LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
135
                sprintf(filename, "LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum);
137
                filenum++;
136
                filenum++;
138
                return filename;
137
                return filename;
139
        }
138
        }
140
        else return NULL;
139
        else return NULL;
141
}
140
}
142
 
141
 
143
 
142
 
144
 
143
 
145
//----------------------------------------------------------------------------------------------------
144
//----------------------------------------------------------------------------------------------------
146
// logs the current gps position to a kml file
145
// logs the current gps position to a kml file
147
logfilestate_t Logging_KML(u32 LogDelay)
146
logfilestate_t Logging_KML(u32 LogDelay)
148
{
147
{
149
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
148
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
150
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
149
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
151
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
150
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
152
        static  KML_Document_t logfile;                                         // the logfilehandle
151
        static  KML_Document_t logfile;                                         // the logfilehandle
153
 
152
 
154
        // initialize if LogDelay is zero
153
        // initialize if LogDelay is zero
155
        if(!LogDelay)
154
        if(!LogDelay)
156
        {
155
        {
157
                switch(logfilestate)
156
                switch(logfilestate)
158
                {
157
                {
159
                        case LOGFILE_OPENED:
158
                        case LOGFILE_OPENED:
160
                                KML_DocumentClose(&logfile); // try to close it
159
                                KML_DocumentClose(&logfile); // try to close it
161
                                break;
160
                                break;
162
                        default:
161
                        default:
163
                                break;
162
                                break;
164
                }
163
                }
165
                logfilestate = LOGFILE_IDLE;
164
                logfilestate = LOGFILE_IDLE;
166
                logfilename = NULL;
165
                logfilename = NULL;
167
                KML_DocumentInit(&logfile);
166
                KML_DocumentInit(&logfile);
168
                logtimer = SetDelay(0); // set logtimer to now
167
                logtimer = SetDelay(0); // set logtimer to now
169
                return logfilestate;
168
                return logfilestate;
170
        }
169
        }
171
        // no init
170
        // no init
172
        if(CheckDelay(logtimer))
171
        if(CheckDelay(logtimer))
173
        {
172
        {
174
                logtimer = SetDelay(LogDelay);  // standard interval
173
                logtimer = SetDelay(LogDelay);  // standard interval
175
 
174
 
176
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN)
175
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN)
177
                {
176
                {
178
                        switch(logfilestate)
177
                        switch(logfilestate)
179
                        {
178
                        {
180
                                case LOGFILE_IDLE:
179
                                case LOGFILE_IDLE:
181
                                case LOGFILE_CLOSED:
180
                                case LOGFILE_CLOSED:
182
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.StatusFlags & FC_STATUS_FLY))
181
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.StatusFlags & FC_STATUS_FLY))
183
                                        {
182
                                        {
184
                                                logfilestate = LOGFILE_START;
183
                                                logfilestate = LOGFILE_START;
185
                                        }
184
                                        }
186
                                        break;
185
                                        break;
187
                                case LOGFILE_START:
186
                                case LOGFILE_START:
188
                                        // find unused logfile name
187
                                        // find unused logfile name
189
                                        do
188
                                        do
190
                                        {        // try to generate a new logfile name
189
                                        {        // try to generate a new logfile name
191
                                                 logfilename = GenerateKMLLogFileName();
190
                                                 logfilename = GenerateKMLLogFileName();
192
                                        }while((logfilename != NULL) && fexist_(logfilename));
191
                                        }while((logfilename != NULL) && fexist_(logfilename));
193
                                        // if logfilename exist
192
                                        // if logfilename exist
194
                                        if(logfilename != NULL)
193
                                        if(logfilename != NULL)
195
                                        {
194
                                        {
196
                                                // try to create the log file
195
                                                // try to create the log file
197
                                                if(KML_DocumentOpen(logfilename, &logfile))
196
                                                if(KML_DocumentOpen(logfilename, &logfile))
198
                                                {
197
                                                {
199
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
198
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
200
                                                        logfilestate = LOGFILE_OPENED; // goto next step
199
                                                        logfilestate = LOGFILE_OPENED; // goto next step
201
                                                        UART1_PutString("\r\nOpening kml-file:");
200
                                                        UART1_PutString("\r\nOpening kml-file:");
202
                                                        UART1_PutString(logfilename);
201
                                                        UART1_PutString(logfilename);
203
                                                        UART1_PutString("\r\n");
202
                                                        UART1_PutString("\r\n");
204
                                                }
203
                                                }
205
                                                else // could not be openend
204
                                                else // could not be openend
206
                                                {
205
                                                {
207
                                                        logfilestate = LOGFILE_ERROR;
206
                                                        logfilestate = LOGFILE_ERROR;
208
                                                        UART1_PutString("\r\nError opening kml-file: ");
207
                                                        UART1_PutString("\r\nError opening kml-file: ");
209
                                                        UART1_PutString(logfilename);
208
                                                        UART1_PutString(logfilename);
210
                                                        UART1_PutString("\r\n");
209
                                                        UART1_PutString("\r\n");
211
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
210
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
212
                                                        SD_LoggingError = 1;
211
                                                        SD_LoggingError = 1;
213
                                                }
212
                                                }
214
                                        }
213
                                        }
215
                                        else
214
                                        else
216
                                        {
215
                                        {
217
                                                logfilestate = LOGFILE_ERROR;
216
                                                logfilestate = LOGFILE_ERROR;
218
                                                UART1_PutString("\r\nError getting free kml-file name\r\n");
217
                                                UART1_PutString("\r\nError getting free kml-file name\r\n");
219
                                                SD_LoggingError = 2;
218
                                                SD_LoggingError = 2;
220
                                        }
219
                                        }
221
                                        // else retry in next loop
220
                                        // else retry in next loop
222
                                        break;
221
                                        break;
223
                                case LOGFILE_OPENED:
222
                                case LOGFILE_OPENED:
224
                                        // append new gps log data
223
                                        // append new gps log data
225
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
224
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
226
                                        {
225
                                        {
227
                                                if(!KML_LoggGPSCoordinates(&logfile))
226
                                                if(!KML_LoggGPSCoordinates(&logfile))
228
                                                {       // error logging data
227
                                                {       // error logging data
229
                                                        UART1_PutString("\r\nError logging to kml-file\r\n");
228
                                                        UART1_PutString("\r\nError logging to kml-file\r\n");
230
                                                        SD_LoggingError = 3;
229
                                                        SD_LoggingError = 3;
231
                                                        KML_DocumentClose(&logfile);
230
                                                        KML_DocumentClose(&logfile);
232
                                                        logfilestate = LOGFILE_ERROR;
231
                                                        logfilestate = LOGFILE_ERROR;
233
                                                }
232
                                                }
234
                                                else // sucessfully logged
233
                                                else // sucessfully logged
235
                                                {
234
                                                {
236
                                                        SD_LoggingError = 0;
235
                                                        SD_LoggingError = 0;
237
                                                        if(CheckDelay(flushtimer))
236
                                                        if(CheckDelay(flushtimer))
238
                                                        {
237
                                                        {
239
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
238
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
240
                                                                fflush_(logfile.file);
239
                                                                fflush_(logfile.file);
241
                                                        }
240
                                                        }
242
                                                }
241
                                                }
243
 
242
 
244
                                        }
243
                                        }
245
                                        break;
244
                                        break;
246
 
245
 
247
                                case LOGFILE_ERROR:
246
                                case LOGFILE_ERROR:
248
                                        break;
247
                                        break;
249
 
248
 
250
                                default:
249
                                default:
251
                                        logfilestate = LOGFILE_IDLE;
250
                                        logfilestate = LOGFILE_IDLE;
252
                                        break;
251
                                        break;
253
                        }
252
                        }
254
                } // EOF motors are not running
253
                } // EOF motors are not running
255
                else // model is not flying
254
                else // model is not flying
256
                {   // close log file if opened
255
                {   // close log file if opened
257
                        if(logfilestate == LOGFILE_OPENED)
256
                        if(logfilestate == LOGFILE_OPENED)
258
                        {
257
                        {
259
                                if(KML_DocumentClose(&logfile))
258
                                if(KML_DocumentClose(&logfile))
260
                                {
259
                                {
261
                                        UART1_PutString("\r\nClosing kml-file\r\n");
260
                                        UART1_PutString("\r\nClosing kml-file\r\n");
262
                                        logfilestate = LOGFILE_CLOSED;
261
                                        logfilestate = LOGFILE_CLOSED;
263
                                }
262
                                }
264
                                else  // could not be closed
263
                                else  // could not be closed
265
                                {
264
                                {
266
                                        UART1_PutString("\r\nError closing kml-file\r\n");
265
                                        UART1_PutString("\r\nError closing kml-file\r\n");
267
                                        SD_LoggingError = 4;
266
                                        SD_LoggingError = 4;
268
                                        logfilestate =  LOGFILE_ERROR;
267
                                        logfilestate =  LOGFILE_ERROR;
269
                                }
268
                                }
270
                        }
269
                        }
271
                } //EOF motors are not running
270
                } //EOF motors are not running
272
        } // EOF Check LogTimer
271
        } // EOF Check LogTimer
273
 
272
 
274
        return logfilestate;
273
        return logfilestate;
275
}
274
}
276
 
275
 
277
//----------------------------------------------------------------------------------------------------
276
//----------------------------------------------------------------------------------------------------
278
// logs gps and state info to a gpx file
277
// logs gps and state info to a gpx file
279
logfilestate_t Logging_GPX(u32 LogDelay)
278
logfilestate_t Logging_GPX(u32 LogDelay)
280
{
279
{
281
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
280
        static  logfilestate_t logfilestate = LOGFILE_IDLE; // the current logfilestate
282
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
281
        static  s8* logfilename = NULL;                                         // the pointer to the logfilename
283
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
282
        static  u32 logtimer = 0, flushtimer = 0;               // the log update timer
284
        static  GPX_Document_t logfile;                                         // the logfilehandle
283
        static  GPX_Document_t logfile;                                         // the logfilehandle
285
        static  u8 part = 0;
284
        static  u8 part = 0;
286
        u32 measure_time;
285
        u32 measure_time;
287
 
286
 
288
        // initialize if LogDelay is zero
287
        // initialize if LogDelay is zero
289
        if(!LogDelay)
288
        if(!LogDelay)
290
        {
289
        {
291
                switch(logfilestate)
290
                switch(logfilestate)
292
                {
291
                {
293
                        case LOGFILE_OPENED:
292
                        case LOGFILE_OPENED:
294
                                GPX_DocumentClose(&logfile); // try to close it
293
                                GPX_DocumentClose(&logfile); // try to close it
295
                                break;
294
                                break;
296
                        default:
295
                        default:
297
                                break;
296
                                break;
298
                }
297
                }
299
                logfilestate = LOGFILE_IDLE;
298
                logfilestate = LOGFILE_IDLE;
300
                logfilename = NULL;
299
                logfilename = NULL;
301
                GPX_DocumentInit(&logfile);
300
                GPX_DocumentInit(&logfile);
302
                logtimer = SetDelay(0); // set logtimer to now
301
                logtimer = SetDelay(0); // set logtimer to now
303
                return logfilestate;
302
                return logfilestate;
304
        }
303
        }
305
        // no init
304
        // no init
306
        if(CheckDelay(logtimer) || part)
305
        if(CheckDelay(logtimer) || part)
307
        {
306
        {
308
                logtimer = SetDelay(LogDelay);  // standard interval
307
                logtimer = SetDelay(LogDelay);  // standard interval
309
 
308
 
310
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN || part)
309
                if(FC.StatusFlags & FC_STATUS_MOTOR_RUN || part)
311
                {
310
                {
312
                        switch(logfilestate)
311
                        switch(logfilestate)
313
                        {
312
                        {
314
                                case LOGFILE_IDLE:
313
                                case LOGFILE_IDLE:
315
                                case LOGFILE_CLOSED:
314
                                case LOGFILE_CLOSED:
316
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.StatusFlags & FC_STATUS_FLY))
315
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D) && (FC.StatusFlags & FC_STATUS_FLY))
317
                                        {
316
                                        {
318
                                                logfilestate = LOGFILE_START;
317
                                                logfilestate = LOGFILE_START;
319
                                        }
318
                                        }
320
                                        part = 0;
319
                                        part = 0;
321
                                        break;
320
                                        break;
322
                                case LOGFILE_START:
321
                                case LOGFILE_START:
323
                                        // find unused logfile name
322
                                        // find unused logfile name
324
                                        do
323
                                        do
325
                                        {        // try to generate a new logfile name
324
                                        {        // try to generate a new logfile name
326
                                                 logfilename = GenerateGPXLogFileName();
325
                                                 logfilename = GenerateGPXLogFileName();
327
                                        }while((logfilename != NULL) && fexist_(logfilename));
326
                                        }while((logfilename != NULL) && fexist_(logfilename));
328
                                        // if logfilename exist
327
                                        // if logfilename exist
329
                                        if(logfilename != NULL)
328
                                        if(logfilename != NULL)
330
                                        {
329
                                        {
331
                                                // try to create the log file
330
                                                // try to create the log file
332
                                                if(GPX_DocumentOpen(logfilename, &logfile))
331
                                                if(GPX_DocumentOpen(logfilename, &logfile))
333
                                                {
332
                                                {
334
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
333
                                                        flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
335
                                                        logfilestate = LOGFILE_OPENED; // goto next step
334
                                                        logfilestate = LOGFILE_OPENED; // goto next step
336
                                                        UART1_PutString("\r\nOpening gpx-file:");
335
                                                        UART1_PutString("\r\nOpening gpx-file:");
337
                                                        UART1_PutString(logfilename);
336
                                                        UART1_PutString(logfilename);
338
                                                        UART1_PutString("\r\n");
337
                                                        UART1_PutString("\r\n");
339
                                                }
338
                                                }
340
                                                else // could not be openend
339
                                                else // could not be openend
341
                                                {
340
                                                {
342
                                                        logfilestate = LOGFILE_ERROR;
341
                                                        logfilestate = LOGFILE_ERROR;
343
                                                        UART1_PutString("\r\nError opening gpx-file: ");
342
                                                        UART1_PutString("\r\nError opening gpx-file: ");
344
                                                        UART1_PutString(logfilename);
343
                                                        UART1_PutString(logfilename);
345
                                                        UART1_PutString("\r\n");
344
                                                        UART1_PutString("\r\n");
346
                                                        SD_LoggingError = 11;
345
                                                        SD_LoggingError = 11;
347
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
346
                                                        logtimer = SetDelay(10);  // try again in open logfile in 10 mili sec
348
                                                }
347
                                                }
349
                                        }
348
                                        }
350
                                        else
349
                                        else
351
                                        {
350
                                        {
352
                                                logfilestate = LOGFILE_ERROR;
351
                                                logfilestate = LOGFILE_ERROR;
353
                                                UART1_PutString("\r\nError getting free gpx-file name\r\n");
352
                                                UART1_PutString("\r\nError getting free gpx-file name\r\n");
354
                                                SD_LoggingError = 12;
353
                                                SD_LoggingError = 12;
355
                                        }
354
                                        }
356
                                        // else retry in next loop
355
                                        // else retry in next loop
357
                                        part = 0;
356
                                        part = 0;
358
                                        break;
357
                                        break;
359
                                case LOGFILE_OPENED:
358
                                case LOGFILE_OPENED:
360
                                        // append new gps log data
359
                                        // append new gps log data
361
measure_time = CountMilliseconds;
360
measure_time = CountMilliseconds;
362
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
361
                                        if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D))
363
                                        {
362
                                        {
364
                                                if(!GPX_LoggGPSCoordinates(&logfile,part))
363
                                                if(!GPX_LoggGPSCoordinates(&logfile,part))
365
                                                {       // error logging data
364
                                                {       // error logging data
366
                                                        UART1_PutString("\r\nError logging to gpx-file\r\n");
365
                                                        UART1_PutString("\r\nError logging to gpx-file\r\n");
367
                                                        GPX_DocumentClose(&logfile);
366
                                                        GPX_DocumentClose(&logfile);
368
                                                        SD_LoggingError = 13;
367
                                                        SD_LoggingError = 13;
369
                                                        logfilestate = LOGFILE_ERROR;
368
                                                        logfilestate = LOGFILE_ERROR;
370
                                                }
369
                                                }
371
                                                else // successful log
370
                                                else // successful log
372
                                                {
371
                                                {
373
                                                        if(CheckDelay(flushtimer))
372
                                                        if(CheckDelay(flushtimer))
374
                                                        {
373
                                                        {
375
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
374
                                                                flushtimer = SetDelay(LOG_FLUSH_INTERVAL);
376
                                                                fflush_(logfile.file);
375
                                                                fflush_(logfile.file);
377
                                                        }
376
                                                        }
378
                                                }
377
                                                }
379
                                        }
378
                                        }
380
                                        if(++part == 9) part = 0;
379
                                        if(++part == 9) part = 0;
381
//DebugOut.Analog[19] = CountMilliseconds - measure_time;
380
//DebugOut.Analog[19] = CountMilliseconds - measure_time;
382
                                        break;
381
                                        break;
383
 
382
 
384
                                case LOGFILE_ERROR:
383
                                case LOGFILE_ERROR:
385
                                        part = 0;
384
                                        part = 0;
386
                                        break;
385
                                        break;
387
 
386
 
388
                                default:
387
                                default:
389
                                        part = 0;
388
                                        part = 0;
390
                                        logfilestate = LOGFILE_IDLE;
389
                                        logfilestate = LOGFILE_IDLE;
391
                                        break;
390
                                        break;
392
                        }
391
                        }
393
                } // EOF model is flying
392
                } // EOF model is flying
394
                else // model is not flying
393
                else // model is not flying
395
                {   // close log file if opened
394
                {   // close log file if opened
396
                        if(logfilestate == LOGFILE_OPENED)
395
                        if(logfilestate == LOGFILE_OPENED)
397
                        {
396
                        {
398
                                if(GPX_DocumentClose(&logfile))
397
                                if(GPX_DocumentClose(&logfile))
399
                                {
398
                                {
400
                                        UART1_PutString("\r\nClosing gpx-file\r\n");
399
                                        UART1_PutString("\r\nClosing gpx-file\r\n");
401
                                        logfilestate = LOGFILE_CLOSED;
400
                                        logfilestate = LOGFILE_CLOSED;
402
                                        part = 0;
401
                                        part = 0;
403
                                }
402
                                }
404
                                else  // could not be closed
403
                                else  // could not be closed
405
                                {
404
                                {
406
                                        UART1_PutString("\r\nError closing gpx-file\r\n");
405
                                        UART1_PutString("\r\nError closing gpx-file\r\n");
407
                                        SD_LoggingError = 14;
406
                                        SD_LoggingError = 14;
408
                                        logfilestate =  LOGFILE_ERROR;
407
                                        logfilestate =  LOGFILE_ERROR;
409
                                        part = 0;
408
                                        part = 0;
410
                                }
409
                                }
411
                        }
410
                        }
412
                } //EOF model is not flying
411
                } //EOF model is not flying
413
        } // EOF Check LogTimer
412
        } // EOF Check LogTimer
414
 
413
 
415
        return logfilestate;
414
        return logfilestate;
416
}
415
}
417
 
416
 
418
//----------------------------------------------------------------------------------------------------
417
//----------------------------------------------------------------------------------------------------
419
// initialize logging
418
// initialize logging
420
void Logging_Init(void)
419
void Logging_Init(void)
421
{
420
{
422
        SD_LoggingError = 0;
421
        SD_LoggingError = 0;
423
        LogCfg.KML_Interval = 500; //default
422
        LogCfg.KML_Interval = 500; //default
424
        Settings_GetParamValue(PID_KML_LOGGING, (u16*)&LogCfg.KML_Interval); // overwrite by settings value
423
        Settings_GetParamValue(PID_KML_LOGGING, (u16*)&LogCfg.KML_Interval); // overwrite by settings value
425
        if(LogCfg.KML_Interval != 0 && LogCfg.KML_Interval < 500) LogCfg.KML_Interval = 500;
424
        if(LogCfg.KML_Interval != 0 && LogCfg.KML_Interval < 500) LogCfg.KML_Interval = 500;
426
 
425
 
427
        Logging_KML(0); // initialize
426
        Logging_KML(0); // initialize
428
        LogCfg.GPX_Interval = 0; //default
427
        LogCfg.GPX_Interval = 0; //default
429
        Settings_GetParamValue(PID_GPX_LOGGING, (u16*)&LogCfg.GPX_Interval); // overwrite by settings value
428
        Settings_GetParamValue(PID_GPX_LOGGING, (u16*)&LogCfg.GPX_Interval); // overwrite by settings value
430
        if(LogCfg.GPX_Interval != 0 && LogCfg.GPX_Interval < 500) LogCfg.GPX_Interval = 500;
429
        if(LogCfg.GPX_Interval != 0 && LogCfg.GPX_Interval < 500) LogCfg.GPX_Interval = 500;
431
        Logging_GPX(0); // initialize
430
        Logging_GPX(0); // initialize
432
}
431
}
433
 
432
 
434
//----------------------------------------------------------------------------------------------------
433
//----------------------------------------------------------------------------------------------------
435
// gobal logging handler
434
// gobal logging handler
436
void Logging_Update(void)
435
void Logging_Update(void)
437
{
436
{
438
        static u8 logmodule = 0;
437
        static u8 logmodule = 0;
439
        static u32 logtimer = 0;
438
        static u32 logtimer = 0;
440
        static logfilestate_t logstate = LOGFILE_IDLE;
439
        static logfilestate_t logstate = LOGFILE_IDLE;
441
 
440
 
442
        if(SD_SWITCH) // a card is in slot
441
        if(SD_SWITCH) // a card is in slot
443
        {
442
        {
444
        if((logstate == LOGFILE_ERROR) && (FC.StatusFlags & FC_STATUS_START)) SD_LoggingError = 100;
443
        if((logstate == LOGFILE_ERROR) && (FC.StatusFlags & FC_STATUS_START)) SD_LoggingError = 100;
445
                if(CheckDelay(logtimer))
444
                if(CheckDelay(logtimer))
446
                {
445
                {
447
                        logtimer = SetDelay(10);  // faster makes no sense
446
                        logtimer = SetDelay(10);  // faster makes no sense
448
                        // call the logger handlers if no error has occured
447
                        // call the logger handlers if no error has occured
449
                        if(logstate != LOGFILE_ERROR)
448
                        if(logstate != LOGFILE_ERROR)
450
                        {  // run only one logging handler per loop
449
                        {  // run only one logging handler per loop
451
                                switch(logmodule++)
450
                                switch(logmodule++)
452
                                {
451
                                {
453
                                        case 0:
452
                                        case 0:
454
                                                logstate = Logging_KML(LogCfg.KML_Interval);
453
                                                logstate = Logging_KML(LogCfg.KML_Interval);
455
                                                break;
454
                                                break;
456
                                        case 1:
455
                                        case 1:
457
                                                logstate = Logging_GPX(LogCfg.GPX_Interval);
456
                                                logstate = Logging_GPX(LogCfg.GPX_Interval);
458
                                                logmodule = 0; // resart with first log module
457
                                                logmodule = 0; // resart with first log module
459
                                                break;
458
                                                break;
460
                                        default:
459
                                        default:
461
                                                logmodule = 0;
460
                                                logmodule = 0;
462
                                                break;
461
                                                break;
463
                                }
462
                                }
464
                        }
463
                        }
465
                        else // a logging error has occured
464
                        else // a logging error has occured
466
                        {      
465
                        {      
467
//                              /*
466
//                              /*
468
                                // try to reinitialize sd-card when motors are not running
467
                                // try to reinitialize sd-card when motors are not running
469
                                if(!(FC.StatusFlags & FC_STATUS_MOTOR_RUN))
468
                                if(!(FC.StatusFlags & FC_STATUS_MOTOR_RUN))
470
                                {
469
                                {
471
                                        if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside
470
                                        if(Fat16_IsValid()) // wait for reinizialization of fat16 from outside
472
                                        {
471
                                        {
473
                                                Logging_Init(); // initialize the logs
472
                                                Logging_Init(); // initialize the logs
474
                                                logstate = LOGFILE_IDLE;
473
                                                logstate = LOGFILE_IDLE;
475
                                                logtimer = SetDelay(100);       // try next log in 100 mili sec
474
                                                logtimer = SetDelay(100);       // try next log in 100 mili sec
476
                                        }
475
                                        }
477
                                        else
476
                                        else
478
                                        {   // retry in 5 seconds
477
                                        {   // retry in 5 seconds
479
                                                logtimer = SetDelay(5000);  // try again in 5 sec
478
                                                logtimer = SetDelay(5000);  // try again in 5 sec
480
                                        }
479
                                        }
481
                                } // EOF motors are not running
480
                                } // EOF motors are not running
482
//                              */
481
//                              */
483
                        } //EOF logfile error
482
                        } //EOF logfile error
484
                }  // EOF CheckDelay
483
                }  // EOF CheckDelay
485
        }// EOF Card in Slot
484
        }// EOF Card in Slot
486
}
485
}
487
 
486