Rev 380 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!! */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software Nutzungsbedingungen (english version: see below)
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
// + des Mitverschuldens offen.
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
// + 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.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software LICENSING TERMS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
// + 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
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
// + The Software may only be used with the Licensor's products.
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
// + agreement shall be the property of the Licensor.
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
// + features that can be used to identify the program may not be altered or defaced by the customer.
// + The customer shall be responsible for taking reasonable precautions
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
// + #### END OF LICENSING TERMS ####
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include "91x_lib.h"
#include "main.h"
#include "timer1.h"
#include "uart1.h"
#include "kml.h"
#include "gpx.h"
#include "ssc.h"
#include "settings.h"
#include "led.h"
#include "logging.h"
#define MIN_SD_INTERVAL_KML 200
#define MIN_SD_INTERVAL_GPX 500
u8 SD_LoggingError
= 0;
#define LOG_FLUSH_INTERVAL 20000 // 20s //##
typedef enum
{
LOGFILE_IDLE
,
LOGFILE_START
,
LOGFILE_CLOSED
,
LOGFILE_OPENED
,
LOGFILE_ERROR
} logfilestate_t
;
// logger handler prototypes
logfilestate_t Logging_KML
(u32 LogDelay
);
logfilestate_t Logging_GPX
(u32 LogDelay
);
LogCfg_t LogCfg
= {500 , 1000};
u32 Logged_GPX_Counter
= 0, Logged_KML_Counter
= 0;
//----------------------------------------------------------------------------------------------------
s8
* GenerateKMLLogFileName
(void)
{
static u16 filenum
= 0; // file name counter
static s8 filename
[35];
static DateTime_t LastTime
= {0,0,0,0,0,0,0,0};
if(SystemTime.
Valid)
{
// if the day has been changed
if((LastTime.
Year != SystemTime.
Year) || (LastTime.
Month != SystemTime.
Month) || (LastTime.
Day != SystemTime.
Day))
{
LastTime.
Year = SystemTime.
Year;
LastTime.
Month = SystemTime.
Month;
LastTime.
Day = SystemTime.
Day;
LastTime.
Valid = 1;
filenum
= 0; // reset file counter
}
sprintf(filename
, "LOG/%04i%02i%02i/KML/GPS%05i.KML", SystemTime.
Year, SystemTime.
Month, SystemTime.
Day, filenum
);
filenum
++;
return filename
;
}
else return NULL
;
}
//----------------------------------------------------------------------------------------------------
s8
* GenerateGPXLogFileName
(void)
{
static u16 filenum
= 0; // file name counter
static s8 filename
[35];
static DateTime_t LastTime
= {0,0,0,0,0,0,0,0};
if(SystemTime.
Valid)
{
// if the day has been changed
if((LastTime.
Year != SystemTime.
Year) || (LastTime.
Month != SystemTime.
Month) || (LastTime.
Day != SystemTime.
Day))
{
LastTime.
Year = SystemTime.
Year;
LastTime.
Month = SystemTime.
Month;
LastTime.
Day = SystemTime.
Day;
LastTime.
Valid = 1;
filenum
= 0; // reset file counter
}
sprintf(filename
, "LOG/%04i%02i%02i/GPX/GPS%05i.GPX", SystemTime.
Year, SystemTime.
Month, SystemTime.
Day, filenum
);
filenum
++;
return filename
;
}
else return NULL
;
}
//----------------------------------------------------------------------------------------------------
// logs the current gps position to a kml file
logfilestate_t Logging_KML
(u32 LogDelay
)
{
static logfilestate_t logfilestate
= LOGFILE_IDLE
; // the current logfilestate
static s8
* logfilename
= NULL
; // the pointer to the logfilename
static u32 logtimer
= 0, flushtimer
= 0; // the log update timer
static KML_Document_t logfile
; // the logfilehandle
// initialize if LogDelay is zero
if(!LogDelay
)
{
switch(logfilestate
)
{
case LOGFILE_OPENED
:
KML_DocumentClose
(&logfile
); // try to close it
break;
default:
break;
}
logfilestate
= LOGFILE_IDLE
;
logfilename
= NULL
;
KML_DocumentInit
(&logfile
);
logtimer
= SetDelay
(0); // set logtimer to now
return logfilestate
;
}
// no init
if(CheckDelay
(logtimer
))
{
logtimer
= SetDelay
(LogDelay
); // standard interval
if(FC.
StatusFlags & FC_STATUS_MOTOR_RUN
)
{
switch(logfilestate
)
{
case LOGFILE_IDLE
:
case LOGFILE_CLOSED
:
if((GPSData.
Status != INVALID
) && (GPSData.
Flags & FLAG_GPSFIXOK
) && (GPSData.
SatFix == SATFIX_3D
) && (FC.
StatusFlags & FC_STATUS_FLY
))
{
logfilestate
= LOGFILE_START
;
}
break;
case LOGFILE_START
:
// find unused logfile name
do
{ // try to generate a new logfile name
logfilename
= GenerateKMLLogFileName
();
}while((logfilename
!= NULL
) && fexist_
(logfilename
));
// if logfilename exist
if(logfilename
!= NULL
)
{
// try to create the log file
if(KML_DocumentOpen
(logfilename
, &logfile
))
{
flushtimer
= SetDelay
(LOG_FLUSH_INTERVAL
);
logfilestate
= LOGFILE_OPENED
; // goto next step
UART1_PutString
("\r\nOpening kml-file:");
UART1_PutString
(logfilename
);
UART1_PutString
("\r\n");
}
else // could not be openend
{
logfilestate
= LOGFILE_ERROR
;
UART1_PutString
("\r\nError opening kml-file: ");
UART1_PutString
(logfilename
);
UART1_PutString
("\r\n");
logtimer
= SetDelay
(10); // try again in open logfile in 10 mili sec
SD_LoggingError
= 1;
}
}
else
{
logfilestate
= LOGFILE_ERROR
;
UART1_PutString
("\r\nError getting free kml-file name\r\n");
SD_LoggingError
= 2;
}
// else retry in next loop
break;
case LOGFILE_OPENED
:
// append new gps log data
if((GPSData.
Status != INVALID
) && (GPSData.
Flags & FLAG_GPSFIXOK
) && (GPSData.
SatFix == SATFIX_3D
))
{
if(!KML_LoggGPSCoordinates
(&logfile
))
{ // error logging data
UART1_PutString
("\r\nError logging to kml-file\r\n");
SD_LoggingError
= 3;
KML_DocumentClose
(&logfile
);
logfilestate
= LOGFILE_ERROR
;
}
else // sucessfully logged
{
SD_LoggingError
= 0;
if(CheckDelay
(flushtimer
))
{
flushtimer
= SetDelay
(LOG_FLUSH_INTERVAL
);
fflush_
(logfile.
file);
}
}
}
break;
case LOGFILE_ERROR
:
break;
default:
logfilestate
= LOGFILE_IDLE
;
break;
}
} // EOF motors are not running
else // model is not flying
{ // close log file if opened
if(logfilestate
== LOGFILE_OPENED
)
{
if(KML_DocumentClose
(&logfile
))
{
UART1_PutString
("\r\nClosing kml-file\r\n");
logfilestate
= LOGFILE_CLOSED
;
}
else // could not be closed
{
UART1_PutString
("\r\nError closing kml-file\r\n");
SD_LoggingError
= 4;
logfilestate
= LOGFILE_ERROR
;
}
}
} //EOF motors are not running
} // EOF Check LogTimer
return logfilestate
;
}
//----------------------------------------------------------------------------------------------------
// logs gps and state info to a gpx file
logfilestate_t Logging_GPX
(u32 LogDelay
)
{
static logfilestate_t logfilestate
= LOGFILE_IDLE
; // the current logfilestate
static s8
* logfilename
= NULL
; // the pointer to the logfilename
static u32 logtimer
= 0, flushtimer
= 0; // the log update timer
static GPX_Document_t logfile
; // the logfilehandle
static u8 part
= 0;
u32 measure_time
;
// initialize if LogDelay is zero
if(!LogDelay
)
{
switch(logfilestate
)
{
case LOGFILE_OPENED
:
GPX_DocumentClose
(&logfile
); // try to close it
break;
default:
break;
}
logfilestate
= LOGFILE_IDLE
;
logfilename
= NULL
;
GPX_DocumentInit
(&logfile
);
logtimer
= SetDelay
(0); // set logtimer to now
return logfilestate
;
}
// no init
if(CheckDelay
(logtimer
) || part
)
{
if(!part
) logtimer
= SetDelay
(LogDelay
); // standard interval
if(FC.
StatusFlags & FC_STATUS_MOTOR_RUN
|| part
)
{
switch(logfilestate
)
{
case LOGFILE_IDLE
:
case LOGFILE_CLOSED
:
if((GPSData.
Status != INVALID
) && (GPSData.
Flags & FLAG_GPSFIXOK
) && (GPSData.
SatFix == SATFIX_3D
) && (FC.
StatusFlags & FC_STATUS_FLY
))
{
logfilestate
= LOGFILE_START
;
}
part
= 0;
break;
case LOGFILE_START
:
// find unused logfile name
do
{ // try to generate a new logfile name
logfilename
= GenerateGPXLogFileName
();
}while((logfilename
!= NULL
) && fexist_
(logfilename
));
// if logfilename exist
if(logfilename
!= NULL
)
{
// try to create the log file
if(GPX_DocumentOpen
(logfilename
, &logfile
))
{
flushtimer
= SetDelay
(LOG_FLUSH_INTERVAL
);
logfilestate
= LOGFILE_OPENED
; // goto next step
UART1_PutString
("\r\nOpening gpx-file:");
UART1_PutString
(logfilename
);
UART1_PutString
("\r\n");
}
else // could not be openend
{
logfilestate
= LOGFILE_ERROR
;
UART1_PutString
("\r\nError opening gpx-file: ");
UART1_PutString
(logfilename
);
UART1_PutString
("\r\n");
SD_LoggingError
= 11;
logtimer
= SetDelay
(10); // try again in open logfile in 10 mili sec
}
}
else
{
logfilestate
= LOGFILE_ERROR
;
UART1_PutString
("\r\nError getting free gpx-file name\r\n");
SD_LoggingError
= 12;
}
// else retry in next loop
part
= 0;
break;
case LOGFILE_OPENED
:
// append new gps log data
measure_time
= CountMilliseconds
;
if((GPSData.
Status != INVALID
) && (GPSData.
Flags & FLAG_GPSFIXOK
) && (GPSData.
SatFix == SATFIX_3D
))
{
if(!GPX_LoggGPSCoordinates
(&logfile
,part
))
{ // error logging data
UART1_PutString
("\r\nError logging to gpx-file\r\n");
GPX_DocumentClose
(&logfile
);
SD_LoggingError
= 13;
logfilestate
= LOGFILE_ERROR
;
}
else // successful log
{
if(CheckDelay
(flushtimer
))
{
flushtimer
= SetDelay
(LOG_FLUSH_INTERVAL
);
fflush_
(logfile.
file);
}
}
}
if(++part
== 9) part
= 0;
//DebugOut.Analog[19] = CountMilliseconds - measure_time;
break;
case LOGFILE_ERROR
:
part
= 0;
break;
default:
part
= 0;
logfilestate
= LOGFILE_IDLE
;
break;
}
} // EOF model is flying
else // model is not flying
{ // close log file if opened
if(logfilestate
== LOGFILE_OPENED
)
{
if(GPX_DocumentClose
(&logfile
))
{
UART1_PutString
("\r\nClosing gpx-file\r\n");
logfilestate
= LOGFILE_CLOSED
;
part
= 0;
}
else // could not be closed
{
UART1_PutString
("\r\nError closing gpx-file\r\n");
SD_LoggingError
= 14;
logfilestate
= LOGFILE_ERROR
;
part
= 0;
}
}
} //EOF model is not flying
} // EOF Check LogTimer
return logfilestate
;
}
//----------------------------------------------------------------------------------------------------
// initialize logging
void Logging_Init
(void)
{
SD_LoggingError
= 0;
LogCfg.
KML_Interval = 1000; //default
Settings_GetParamValue
(PID_KML_LOGGING
, (u16
*)&LogCfg.
KML_Interval); // overwrite by settings value
if(LogCfg.
KML_Interval != 0 && LogCfg.
KML_Interval < MIN_SD_INTERVAL_KML
) LogCfg.
KML_Interval = MIN_SD_INTERVAL_KML
;
Logging_KML
(0); // initialize
LogCfg.
GPX_Interval = 0; //default
Settings_GetParamValue
(PID_GPX_LOGGING
, (u16
*)&LogCfg.
GPX_Interval); // overwrite by settings value
if(LogCfg.
GPX_Interval != 0 && LogCfg.
GPX_Interval < MIN_SD_INTERVAL_GPX
) LogCfg.
GPX_Interval = MIN_SD_INTERVAL_GPX
;
Logging_GPX
(0); // initialize
}
//----------------------------------------------------------------------------------------------------
// gobal logging handler
void Logging_Update
(void)
{
static u8 logmodule
= 0;
static u32 logtimer
= 0;
static logfilestate_t logstate
= LOGFILE_IDLE
;
if(SD_SWITCH
) // a card is in slot
{
if((logstate
== LOGFILE_ERROR
) && (FC.
StatusFlags & FC_STATUS_START
)) SD_LoggingError
= 100;
if(CheckDelay
(logtimer
))
{
logtimer
= SetDelay
(10); // faster makes no sense
// call the logger handlers if no error has occured
if(logstate
!= LOGFILE_ERROR
)
{ // run only one logging handler per loop
switch(logmodule
++)
{
case 0:
logstate
= Logging_KML
(LogCfg.
KML_Interval);
break;
case 1:
logstate
= Logging_GPX
(LogCfg.
GPX_Interval);
logmodule
= 0; // resart with first log module
break;
default:
logmodule
= 0;
break;
}
}
else // a logging error has occured
{
// /*
// try to reinitialize sd-card when motors are not running
if(!(FC.
StatusFlags & FC_STATUS_MOTOR_RUN
))
{
if(Fat16_IsValid
()) // wait for reinizialization of fat16 from outside
{
Logging_Init
(); // initialize the logs
logstate
= LOGFILE_IDLE
;
logtimer
= SetDelay
(100); // try next log in 100 mili sec
}
else
{ // retry in 5 seconds
logtimer
= SetDelay
(5000); // try again in 5 sec
}
} // EOF motors are not running
// */
} //EOF logfile error
} // EOF CheckDelay
}// EOF Card in Slot
}