Subversion Repositories FlightCtrl

Rev

Rev 1910 | Rev 2099 | 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.UserParams); i++) {
                staticParams.UserParams[i] = 0;
        }
        /*
         * While we are still using userparams for flight parameters, do set
         * some safe & meaningful default values.
         */

}

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.ServoRefresh = 7;
        staticParams.BitConfig = 0;
        staticParams.J16Bitmask = 95;
        staticParams.J17Bitmask = 243;
        staticParams.J16Timing = 15;
        staticParams.J17Timing = 15;
        staticParams.ControlSigns = 2;
}

void setDefaults(void) {
        setOtherDefaults();
        gyro_setDefaults();
        setDefaultUserParams();
        staticParams.J16Timing = 10;
        staticParams.J17Timing = 10;
}

/***************************************************/
/*       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() {
        eeprom_read_block((uint8_t *) &staticParams.ChannelAssignment[0],
                        &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN], PARAMSET_STRUCT_LEN);
        output_init();
}

/***************************************************/
/*        Write Parameter Set to EEPROM            */
/***************************************************/
// number [1..5]
void ParamSet_WriteToEEProm() {
        eeprom_write_block((uint8_t *) &staticParams.ChannelAssignment[0],
                        &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN], 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
        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);
}

/***************************************************/
/*          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, 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

        setDefaults(); // Fill staticParams Structure to default parameter set 1 (Sport)
               
        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();
   
                // update version info
                SetParamByte(PID_PARAM_REVISION, EEPARAM_REVISION);
        }
        // read active parameter set to staticParams stucture
        ParamSet_ReadFromEEProm();
}