Subversion Repositories NaviCtrl

Rev

Rev 767 | 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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "91x_lib.h"
#include "fat16.h"
#include "settings.h"
#include "uart1.h"
#include "main.h"
u16 WaypointAccelerationSetting = 100; // acceleration for flying waypoints in percent (0-100) or Poti -> 255 = Poti1, 254 = Poti2...
u16 WaypointAcceleration = 100; // the real value

typedef struct
{
        ParamId_t ParamId;
        s8  Name[17]; // 0 terminator is the last byte
        s8 Comment[60];
        u8 Group;   // 0 = reserved  1,2,3,4
        u16 Value;
        u16 Default;
        u16 Min;
        u16 Max;
} __attribute__((packed)) Parameter_t;




Parameter_t CFG_Parameter[] =
{
  //{PID                                , "1234567890123456" , Comment,                                                                                                           Group, Value, Default,   Min,         Max },
        {PID_KML_LOGGING                        , "KMLLOGGING      \0" ,"KML logging interval in ms (0 = disabled)                   ",   1,   200,     500,    0,      60000},   // the log interval for KML logging, 0 = off
        {PID_GPX_LOGGING                        , "GPXLOGGING      \0" ,"GPX logging interval in ms (0 = disabled)                   ",   1,   500,     500,    0,      60000},   // the log interval for GPX logging, 0 = off
        {PID_LOG_AT_MOTORRUN            , "LOG_AT_MOTOR_RUN\0" ,"Start GPX logfile as soon as motors start (0 = off, 1 = on) ",   1,     1,       1,    0,      1},
        {PID_SPEAK100M                          , "HOTT_SPEAK_100M \0" ,"HoTT Speaks '100m' when distance or altitude > 100m (1 = on)",   1,     1,       1,    0,      1},
//      {PID_ABSOLUTE_FLYING_ALT        , "MAX_FLYING_ALT  \0" ,"max. altitude in m                                          ",   1,     0,       0,    0,      30000},   // in [m]
//      {PID_ABSOLUTE_FLYING_RANGE      , "MAX_FLYING_RANGE\0" ,"max. range in m                                             ",   1,     0,       0,    0,      60000},   // in [m]
//      {PID_AUTO_DESCEND_RANGE         , "DESCEND_RANGE   \0" ,"Auto-descend range in m (0 = disabled) (only comm. License) ",   1,     0,       0,    0,      60000},   // in [m]
        {PID_GPS_SYSTEM_CFG                     , "GPS_SYSTEM_CFG  \0" ,"1:GPS+GLNAS 2:GPS+BEIDOU 3:GPS 4:GLNAS 5:BEIDOU             ",   1,     1,       1,    0,      5},
        {PID_GPS_SBAS_CONFIG            , "GPS_SBAS_DGPS_ON\0" ,"GPS SBAS mode (0 = off, 1 = on)                             ",   1,     1,       1,    0,      1},
        {PID_GPS_QZSS_CONFIG            , "GPS_QZSS_DGPS_ON\0" ,"GPS QZSS mode (0 = off, 1 = on) (Japan)                     ",   1,     1,       0,    0,      1},
        {PID_AUTO_WP_EVENT_VALUE        , "AUTO_WP_EVENT   \0" ,"value for the WP-Event Trigger at Auto-Distance [10ms]      ",   1,    10,      10,    1,    200},   // in 0,1sek
//      {PID_MIN_EVENT_TIME                     , "MIN_EVENT_TIME  \0" ,"minimum time of the Waypoint-Event value (seconds)          ",   1,     2,       2,    0,    600},   // in seconds
        {PID_WP_REACHED_TIMEOUT         , "WP_TIMEOUT      \0" ,"the MK increases the WP-target radius after this timeout [s]",   1,     5,       5,    0,   6000},   // in seconds
        {PID_WP_ACCELERATE                      , "WAYPOINT DYNAMIC\0" ,"dynamic for flying waypoints in percent (0-200)             ",   1,   100,     100,    0,    255},   // in percent or Poti
        {PID_WP_WAIT_FOR_LED            , "WAIT_FOR_OUT1   \0" ,"Wait on Waypoint until Out-Pattern is finished (1=on 0=off) ",   1,     1,       1,    0,      1},
        {PID_SEND_NMEA                          , "NMEA_INTERVAL   \0" ,"NMEA Output interval in ms (0 = disabled)                   ",   1,     0,       0,    0,      60000},   // the log interval for NMEA output, 0 = off
        {PID_CH_SPEED                           , "COMINGHOME_SPEED\0" ,"Maximum speed for coming home in 0,1m/sec (80 = 8,0 m/sec)  ",   1,    80,      80,   10,        150},
        {PID_DPH_SPEED                          , "DYNAMIC_PH_SPEED\0" ,"Maximum speed for dynamic position hold in 0,1m/sec         ",   1,   100,     100,   20,        150},
        {PID_POSITION_ACCURACY          , "POS.ACCURACY    \0" ,"Desired Accuracy of position in percent                     ",   1,   100,     100,    0,        255},
        {PID_IO1_FUNCTION                       , "IO1_FUNCTION    \0" ,"Function for IO1 input     9 = Parachute (license required) ",   1,     1,       0,    0,    255},
        {PID_GPS_AUTOCONFIG                     , "GPSAUTOCONFIG   \0" ,"GPS configmode (0 = off, 1 = on)                            ",   1,     1,       1,    0,      1}
};


//----------------------------------------------------------------------------------
// initialize all parameters by its default value
void Settings_SetDefaultValues(void)
{
        u8 i;
        // run thrue all parameters and set value to default
        for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++)
        {
                CFG_Parameter[i].Value  = CFG_Parameter[i].Default;
        }
}

//----------------------------------------------------------------------------------
// set parameter from string based name and value
u8 Settings_SetParameterFromString(s8 *name, s8 *value)
{
        u8 i, retval = 0;
        s8 string[] = "                \0"; // null terminated string of 16 characters

        if((name == NULL) || (value == NULL)) return(retval);

        i = strlen(name);                                       // lenght of the parameter name
        if (i > 16) i = 16;                                     // cut off at 16

        memcpy(string, name, i);                        // copy name to local buffer

    // search name in parameter list
        for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++)
        {
                if(strncmp(string, CFG_Parameter[i].Name, 16) == 0) // names are matching
                {
                        CFG_Parameter[i].Value = (u16)atoi(value); // convert string to number and set value
                        // out of range ?
                        if((CFG_Parameter[i].Value < CFG_Parameter[i].Min) || (CFG_Parameter[i].Value > CFG_Parameter[i].Max))
                        {       // print a warning
                                sprintf(text,"\r\n%s <-- %d is out of range [%d...%d]", string, CFG_Parameter[i].Value, CFG_Parameter[i].Min, CFG_Parameter[i].Max);
                                UART1_PutString(text);
                                CFG_Parameter[i].Value = CFG_Parameter[i].Default;  // fallback to default
                        }
                        retval = 1; // value in range
                        break; // end loop
                }
        }
        if(!retval)
        {
                sprintf(text,"\r\n%s <-- unknown parameter\r\n", string);
                UART1_PutString(text);
        }
        return(retval);
}

//----------------------------------------------------------------------------------
// read settings from file on sd-card
void Settings_Init(void)
{
        #define LINE_MAX 70
        File_t *fp;
        s8 settingsline[LINE_MAX];
        s8 *name, *value;
        u8 i;
        // user absolute path, i.e. leading /
        s8 filename[] = "/settings.ini";

        UART1_PutString("\r\n Settings init...");
        Settings_SetDefaultValues();

        if(Fat16_IsValid())
        {       // check if settings file is existing
                if(fexist_(filename))
                {
                        fp = fopen_(filename, 'r');             // try to open the file
                        if (fp == NULL)                                                 // could not open the file
                        {
                                UART1_PutString("ERROR: Opening settings file!");
                                return;
                        }
                        // read all lines from file
                        while(fgets_(settingsline, LINE_MAX, fp) != 0 && (SD_WatchDog))
                        {
                                if ( // ignorelines starting with \r,\n,' ',';','#'
                                        (settingsline[0] != '\n') &&
                                        (settingsline[0] != '\r') &&
                                        (settingsline[0] != ' ' ) &&
                                        (settingsline[0] != ';' ) &&
                                        (settingsline[0] != '#' )
                                        )
                                {
                                        name  = strtok(settingsline, "="); // get name
                                        value = strtok(NULL, "="); // get value
                                        if ((name != NULL) && (value != NULL))
                                        {
                                                u8 i;
                                                for(i=0; name[i]; i++) name[i] = toupper(name[i]);
                                                Settings_SetParameterFromString(name, value);
                                        }
                                }
                        }
                        fclose_(fp);
                        UART1_PutString("ok");
                        return;
                }
                else // settings file does not exist
                {       // create default settings file
                        fp = fopen_("settings.ini", 'w');               // try to open the file
                        if(fp == NULL)
                        {
                                UART1_PutString("ERROR: Creating default settings file!");
                                return;
                        }
                        // run thrue all parameters and set value to default
                        fputs_("########    NaviControl configuration file     ########\r\n", fp); // write to file
                        fputs_("#------------------------------------------------------\r\n\r\n", fp); // write to file
                        for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++)
                        {
                                s8 tmp_comment[61];
                                tmp_comment[60] = '\0';

                                memcpy(tmp_comment,CFG_Parameter[i].Comment , 60);

                                sprintf(settingsline, "\r\n# %s\r\n", tmp_comment);
                                fputs_(settingsline, fp); // write to file

                                sprintf(settingsline, "%s = %d\r\n", CFG_Parameter[i].Name, CFG_Parameter[i].Default);
                                fputs_(settingsline, fp); // write to file
                        }
                        fputs_("\r\n", fp); // newline at the end of file
                        fclose_(fp);
                        UART1_PutString("Default settings file created!");
                        return;
                }
        }
        else // no acces to fat 16 filesystem
        {
                UART1_PutString("Using default values!");
                return;
        }
}


//----------------------------------------------------------------------------------
// read value of a paramter identified by its id
// returns 1 on success and 0 on error
u8 Settings_GetParamValue(ParamId_t Pid, u16 * pValue)
{
        u8 i, retval = 0;

        for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++)
        {
                if(CFG_Parameter[i].ParamId == Pid)
                {
                        *pValue = CFG_Parameter[i].Value;
                        retval = 1;
                        break;
                }
        }
        return retval;
}