Subversion Repositories FlightCtrl

Rev

Rev 2025 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Contant Values
// + 0-250 -> normale Values
// + 251 -> Poti1
// + 252 -> Poti2
// + 253 -> Poti3
// + 254 -> Poti4
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#ifndef EEMEM
#define EEMEM __attribute__ ((section (".eeprom")))
#endif

#include <avr/eeprom.h>
#include <string.h>
#include "eeprom.h"
#include "output.h"
// TODO: Get rid of these. They have nothing to do with eeprom.
#include "flight.h"
#include "rc.h"
#include "sensors.h"

// byte array in eeprom
uint8_t EEPromArray[E2END + 1] EEMEM;

paramset_t staticParams;
MixerTable_t Mixer;

/*
 * Default for your own experiments here, so you don't have to reset them
 * from MK-Tool all the time.
 */

void setDefaultUserParams(void) {
        uint8_t i;
        for (i = 0; i < sizeof(staticParams.UserParams1); i++) {
                staticParams.UserParams1[i] = 0;
        }
        for (i = 0; i < sizeof(staticParams.UserParams2); i++) {
                staticParams.UserParams2[i] = 0;
        }
        /*
         * While we are still using userparams for flight parameters, do set
         * some safe & meaningful default values.
         */

        staticParams.UserParams2[0] = 0xd5;  //0b11010101; // All gyro filter constants 2; acc. 4
        staticParams.UserParams2[1] = 0; // H&I motor smoothing.
        staticParams.UserParams2[2] = 120; // Yaw I factor
        staticParams.UserParams2[3] = 100; // Max Z acceleration for acc. correction of angles.
}

void setOtherDefaults(void) {
        /* Channel assignments were changed to the normal:
         * Aileron/roll=1, elevator/pitch=2, throttle=3, yaw/rudder=4
         */

        staticParams.ChannelAssignment[CH_ELEVATOR] = 2;
        staticParams.ChannelAssignment[CH_AILERONS] = 1;
        staticParams.ChannelAssignment[CH_THROTTLE] = 3;
        staticParams.ChannelAssignment[CH_RUDDER] = 4;
        staticParams.ChannelAssignment[CH_POTS + 0] = 5;
        staticParams.ChannelAssignment[CH_POTS + 1] = 6;
        staticParams.ChannelAssignment[CH_POTS + 2] = 7;
        staticParams.ChannelAssignment[CH_POTS + 3] = 8;
        staticParams.GlobalConfig = /* CFG_AXIS_COUPLING_ACTIVE | */ CFG_HEADING_HOLD; // CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
        staticParams.HeightMinGas = 30;
        staticParams.MaxHeight = 251;
        staticParams.HeightP = 10;
        staticParams.HeightD = 30;
        staticParams.Height_ACC_Effect = 30;
        staticParams.Height_Gain = 4;
        staticParams.CompassYawEffect = 128;

        staticParams.GyroPitchP = 0;
        staticParams.GyroRollP = 0;
        staticParams.GyroYawP = 0;

        staticParams.GyroPitchD = 0;
        staticParams.GyroRollD = 0;
        staticParams.GyroYawD = 0;

        staticParams.StickElevatorP = 10;
        staticParams.StickAileronsP = 10;
        staticParams.StickRudderP = 10;

        staticParams.LowVoltageWarning = 105;
        staticParams.IFactor = 32;
        staticParams.ServoRefresh = 7;
        staticParams.BitConfig = 0;
        staticParams.J16Bitmask = 95;
        staticParams.J17Bitmask = 243;
        staticParams.J16Timing = 15;
        staticParams.J17Timing = 15;

        staticParams.ControlSigns = 2;
}

/***************************************************/
/*    Default Values for parameter set 1           */
/***************************************************/
void ParamSet_DefaultSet1(void) { // sport
        setOtherDefaults();
        gyro_setDefaults();
        setDefaultUserParams();
        staticParams.J16Timing = 10;
        staticParams.J17Timing = 10;
        memcpy(staticParams.Name, "Sport\0", 6);
}

/***************************************************/
/*    Default Values for parameter set 2           */
/***************************************************/
void ParamSet_DefaultSet2(void) { // normal
        setOtherDefaults();
        gyro_setDefaults();
        setDefaultUserParams();
        staticParams.Height_Gain = 3;
        staticParams.J16Timing = 20;
        staticParams.J17Timing = 20;
        memcpy(staticParams.Name, "Normal\0", 7);
}

/***************************************************/
/*    Default Values for parameter set 3           */
/***************************************************/
void ParamSet_DefaultSet3(void) { // beginner
        setOtherDefaults();
        gyro_setDefaults();
        setDefaultUserParams();
        staticParams.Height_Gain = 3;
        staticParams.J16Timing = 30;
        staticParams.J17Timing = 30;
        memcpy(staticParams.Name, "Beginner\0", 9);
}

/***************************************************/
/*       Read Parameter from EEPROM as byte        */
/***************************************************/
uint8_t GetParamByte(uint16_t param_id) {
        return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
}

/***************************************************/
/*       Write Parameter to EEPROM as byte         */
/***************************************************/
void SetParamByte(uint16_t param_id, uint8_t value) {
        eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
}

/***************************************************/
/*       Read Parameter from EEPROM as word        */
/***************************************************/
uint16_t GetParamWord(uint16_t param_id) {
        return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
                        + param_id]);
}

/***************************************************/
/*       Write Parameter to EEPROM as word         */
/***************************************************/
void SetParamWord(uint16_t param_id, uint16_t value) {
        eeprom_write_word(
                        (uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
}

/***************************************************/
/*       Read Parameter Set from EEPROM            */
/***************************************************/
// number [1..5]
void ParamSet_ReadFromEEProm(uint8_t setnumber) {
        if ((1 > setnumber) || (setnumber > 5))
                setnumber = 3;
        eeprom_read_block((uint8_t *) &staticParams.ChannelAssignment[0],
                        &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN + PARAMSET_STRUCT_LEN * (setnumber
                                        - 1)], PARAMSET_STRUCT_LEN);
        output_init();
}

/***************************************************/
/*        Write Parameter Set to EEPROM            */
/***************************************************/
// number [1..5]
void ParamSet_WriteToEEProm(uint8_t setnumber) {
        if (setnumber > 5)
                setnumber = 5;
        if (setnumber < 1)
                return;
        eeprom_write_block((uint8_t *) &staticParams.ChannelAssignment[0],
                        &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN + PARAMSET_STRUCT_LEN * (setnumber
                                        - 1)], PARAMSET_STRUCT_LEN);
        eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAMSET_LENGTH],
                        PARAMSET_STRUCT_LEN);
        eeprom_write_block(&staticParams.ChannelAssignment[0],
                        &EEPromArray[EEPROM_ADR_CHANNELS], 8); // backup the first 8 bytes that is the rc channel mapping
        // set this parameter set to active set
        setActiveParamSet(setnumber);
        output_init();
}

/***************************************************/
/*       Get active parameter set                  */
/***************************************************/
uint8_t getActiveParamSet(void) {
        uint8_t setnumber;
        setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
        if (setnumber > 5) {
                setnumber = 3;
                eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
        }
        return (setnumber);
}

/***************************************************/
/*       Set active parameter set                  */
/***************************************************/
void setActiveParamSet(uint8_t setnumber) {
        if (setnumber > 5)
                setnumber = 5;
        if (setnumber < 1)
                setnumber = 1;
        eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
}

/***************************************************/
/*          Read MixerTable from EEPROM            */
/***************************************************/
uint8_t MixerTable_ReadFromEEProm(void) {
        if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE])
                        == EEMIXER_REVISION) {
                eeprom_read_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE],
                                sizeof(Mixer));
                return 1;
        } else
                return 0;
}

/***************************************************/
/*          Write Mixer Table to EEPROM            */
/***************************************************/
uint8_t MixerTable_WriteToEEProm(void) {
        if (Mixer.Revision == EEMIXER_REVISION) {
                eeprom_write_block((uint8_t *) &Mixer,
                                &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
                return 1;
        } else
                return 0;
}

/***************************************************/
/*    Default Values for Mixer Table               */
/***************************************************/
void MixerTable_Default(void) { // Quadro
        uint8_t i;
        Mixer.Revision = EEMIXER_REVISION;
        // clear mixer table (but preset throttle)
        for (i = 0; i < 16; i++) {
                Mixer.Motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
                Mixer.Motor[i][MIX_PITCH] = 0;
                Mixer.Motor[i][MIX_ROLL] = 0;
                Mixer.Motor[i][MIX_YAW] = 0;
        }
        // default = Quadro
        Mixer.Motor[0][MIX_PITCH] = +64;
        Mixer.Motor[0][MIX_YAW] = +64;
        Mixer.Motor[1][MIX_PITCH] = -64;
        Mixer.Motor[1][MIX_YAW] = +64;
        Mixer.Motor[2][MIX_ROLL] = -64;
        Mixer.Motor[2][MIX_YAW] = -64;
        Mixer.Motor[3][MIX_ROLL] = +64;
        Mixer.Motor[3][MIX_YAW] = -64;
        memcpy(Mixer.Name, "Quadro\0", 7);
}

/***************************************************/
/*       Initialize EEPROM Parameter Sets          */
/***************************************************/
void ParamSet_Init(void) {
        uint8_t Channel_Backup = 1, i, j;
        // parameter version  check
        if (eeprom_read_byte(&EEPromArray[PID_PARAM_REVISION]) != EEPARAM_REVISION) {
                // if version check faild
                eeprom_write_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE], 0xFF); // reset also mixer table
                // check if channel mapping backup is valid
                for (j = 0; j < 4 && Channel_Backup; j++) {
                        if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS + 0]) >= 12)
                                Channel_Backup = 0;
                }
                // fill all 5 parameter settings
                for (i = 1; i < 6; i++) {
                        switch (i) {
                        case 1:
                                ParamSet_DefaultSet1(); // Fill staticParams Structure to default parameter set 1 (Sport)
                                break;
                        case 2:
                                ParamSet_DefaultSet2(); // Kamera
                                break;
                        case 3:
                                ParamSet_DefaultSet3(); // Beginner
                                break;
                        default:
                                ParamSet_DefaultSet2(); // Kamera
                                break;
                        }
                        if (Channel_Backup) { // if we have a rc channel mapping backup in eeprom
                                // restore it
                                for (j = 0; j < 8; j++) {
                                        staticParams.ChannelAssignment[j] = eeprom_read_byte(
                                                        &EEPromArray[EEPROM_ADR_CHANNELS + j]);
                                }
                        }
                        ParamSet_WriteToEEProm(i);
                }
                // default-Setting is parameter set 3
                setActiveParamSet(1);
                // update version info
                SetParamByte(PID_PARAM_REVISION, EEPARAM_REVISION);
        }
        // read active parameter set to staticParams stucture
        ParamSet_ReadFromEEProm(getActiveParamSet());
}