Subversion Repositories FlightCtrl

Rev

Rev 2025 | Rev 2102 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2025 Rev 2099
Line 1... Line -...
1
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
2
// + Contant Values
-
 
3
// + 0-250 -> normale Values
-
 
4
// + 251 -> Poti1
-
 
5
// + 252 -> Poti2
-
 
6
// + 253 -> Poti3
-
 
7
// + 254 -> Poti4
-
 
8
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
9
 
-
 
10
#ifndef EEMEM
1
#ifndef EEMEM
11
#define EEMEM __attribute__ ((section (".eeprom")))
2
#define EEMEM __attribute__ ((section (".eeprom")))
12
#endif
3
#endif
Line 13... Line -...
13
 
-
 
14
#include <avr/eeprom.h>
-
 
15
#include <string.h>
4
 
-
 
5
#include "eeprom.h"
16
#include "eeprom.h"
6
#include "printf_P.h"
17
#include "output.h"
-
 
18
// TODO: Get rid of these. They have nothing to do with eeprom.
7
#include "output.h"
19
#include "flight.h"
-
 
20
#include "rc.h"
8
#include <avr/wdt.h>
Line 21... Line 9...
21
#include "sensors.h"
9
#include <avr/eeprom.h>
22
 
10
 
Line 23... Line -...
23
// byte array in eeprom
-
 
24
uint8_t EEPromArray[E2END + 1] EEMEM;
-
 
25
 
-
 
26
paramset_t staticParams;
-
 
27
MixerTable_t Mixer;
-
 
28
 
-
 
29
/*
-
 
30
 * Default for your own experiments here, so you don't have to reset them
-
 
31
 * from MK-Tool all the time.
-
 
32
 */
-
 
33
void setDefaultUserParams(void) {
-
 
34
        uint8_t i;
-
 
35
        for (i = 0; i < sizeof(staticParams.UserParams); i++) {
-
 
36
                staticParams.UserParams[i] = 0;
-
 
37
        }
-
 
38
        /*
-
 
39
         * While we are still using userparams for flight parameters, do set
-
 
40
         * some safe & meaningful default values.
-
 
41
         */
-
 
42
}
-
 
43
 
-
 
44
void setOtherDefaults(void) {
-
 
45
        /* Channel assignments were changed to the normal:
-
 
46
         * Aileron/roll=1, elevator/pitch=2, throttle=3, yaw/rudder=4
-
 
47
         */
-
 
48
        staticParams.ChannelAssignment[CH_ELEVATOR] = 2;
-
 
49
        staticParams.ChannelAssignment[CH_AILERONS] = 1;
-
 
50
        staticParams.ChannelAssignment[CH_THROTTLE] = 3;
-
 
51
        staticParams.ChannelAssignment[CH_RUDDER] = 4;
-
 
52
        staticParams.ChannelAssignment[CH_POTS + 0] = 5;
-
 
53
        staticParams.ChannelAssignment[CH_POTS + 1] = 6;
-
 
54
        staticParams.ChannelAssignment[CH_POTS + 2] = 7;
-
 
55
        staticParams.ChannelAssignment[CH_POTS + 3] = 8;
-
 
56
        staticParams.GlobalConfig = /* CFG_AXIS_COUPLING_ACTIVE | */ CFG_HEADING_HOLD; // CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
-
 
57
        staticParams.HeightMinGas = 30;
-
 
58
        staticParams.MaxHeight = 251;
-
 
59
        staticParams.HeightP = 10;
-
 
60
        staticParams.HeightD = 30;
-
 
61
        staticParams.Height_ACC_Effect = 30;
-
 
62
        staticParams.Height_Gain = 4;
-
 
63
        staticParams.CompassYawEffect = 128;
-
 
64
 
-
 
65
        staticParams.GyroPitchP = 0;
-
 
66
        staticParams.GyroRollP = 0;
-
 
67
        staticParams.GyroYawP = 0;
-
 
68
 
-
 
69
        staticParams.GyroPitchD = 0;
-
 
70
        staticParams.GyroRollD = 0;
-
 
71
        staticParams.GyroYawD = 0;
-
 
72
 
-
 
73
        staticParams.StickElevatorP = 10;
-
 
74
        staticParams.StickAileronsP = 10;
-
 
75
        staticParams.StickRudderP = 10;
-
 
76
 
-
 
77
        staticParams.LowVoltageWarning = 105;
-
 
78
        staticParams.ServoRefresh = 7;
-
 
79
 
-
 
80
        staticParams.J16Bitmask = 95;
-
 
81
        staticParams.J17Bitmask = 243;
-
 
82
        staticParams.J16Timing = 15;
-
 
83
        staticParams.J17Timing = 15;
-
 
84
       
-
 
85
        staticParams.servoDirections = 2;
-
 
86
}
-
 
87
 
-
 
88
void setDefaults(void) {
-
 
89
        setOtherDefaults();
-
 
90
        gyro_setDefaults();
-
 
91
        setDefaultUserParams();
-
 
92
        staticParams.J16Timing = 10;
-
 
93
        staticParams.J17Timing = 10;
11
// byte array in eeprom
94
}
12
uint8_t EEPromArray[E2END + 1] EEMEM;
95
 
13
 
96
/***************************************************/
14
/***************************************************/
97
/*       Read Parameter from EEPROM as byte        */
15
/*       Read Parameter from EEPROM as byte        */
98
/***************************************************/
16
/***************************************************/
Line 99... Line 17...
99
uint8_t GetParamByte(uint16_t param_id) {
17
uint8_t getParamByte(uint16_t param_id) {
100
        return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
18
  return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
101
}
19
}
102
 
20
 
103
/***************************************************/
21
/***************************************************/
104
/*       Write Parameter to EEPROM as byte         */
22
/*       Write Parameter to EEPROM as byte         */
Line 105... Line 23...
105
/***************************************************/
23
/***************************************************/
106
void SetParamByte(uint16_t param_id, uint8_t value) {
24
void setParamByte(uint16_t param_id, uint8_t value) {
107
        eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
25
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
-
 
26
}
108
}
27
 
109
 
28
/***************************************************/
110
/***************************************************/
29
/*       Read Parameter from EEPROM as word        */
111
/*       Read Parameter from EEPROM as word        */
30
/***************************************************/
-
 
31
/*
Line 112... Line 32...
112
/***************************************************/
32
uint16_t getParamWord(uint16_t param_id) {
113
uint16_t GetParamWord(uint16_t param_id) {
33
  return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
114
        return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
34
                                                    + param_id]);
-
 
35
}
115
                        + param_id]);
36
*/
-
 
37
 
-
 
38
/***************************************************/
-
 
39
/*       Write Parameter to EEPROM as word         */
-
 
40
/***************************************************/
-
 
41
/*
-
 
42
void setParamWord(uint16_t param_id, uint16_t value) {
-
 
43
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
-
 
44
}
-
 
45
*/
-
 
46
 
-
 
47
uint16_t CRC16(uint8_t* data, uint16_t length) {
-
 
48
  uint16_t crc = 0;
-
 
49
  for (uint16_t i=0; i<length; i++) {
116
}
50
    crc  = (uint8_t)(crc >> 8) | (crc << 8);
-
 
51
    crc ^= data[i];
-
 
52
    crc ^= (uint8_t)(crc & 0xff) >> 4;
-
 
53
    crc ^= (crc << 8) << 4;
-
 
54
    crc ^= ((crc & 0xff) << 4) << 1;
-
 
55
  }
-
 
56
  return crc;
-
 
57
}
-
 
58
 
-
 
59
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
-
 
60
// length is the length of the pure data not including checksum and revision number.
-
 
61
void writeChecksummedBlock(uint8_t revisionNumber, uint8_t* data, uint16_t offset, uint16_t length) {
-
 
62
  uint16_t CRC = CRC16(data, length);
-
 
63
  eeprom_write_byte(&EEPromArray[offset], CRC&0xff);
-
 
64
  eeprom_write_byte(&EEPromArray[offset+1], CRC>>8);
-
 
65
  eeprom_write_byte(&EEPromArray[offset+2], revisionNumber);
-
 
66
  eeprom_write_block(data, &EEPromArray[offset+3], length);
117
 
67
}
-
 
68
 
-
 
69
// offset is where the checksum is stored, offset+1 is the revision number, and offset+2... are the data.
-
 
70
// length is the length of the pure data not including checksum and revision number.
-
 
71
uint8_t readChecksummedBlock(uint8_t revisionNumber, uint8_t* target, uint16_t offset, uint16_t length) {
-
 
72
  uint16_t CRCRead = eeprom_read_byte(&EEPromArray[offset]) | (eeprom_read_byte(&EEPromArray[offset+1])<<8);
-
 
73
  uint8_t revisionNumberRead = eeprom_read_byte(&EEPromArray[offset+2]);
-
 
74
  eeprom_read_block(target, &EEPromArray[offset+3], length);
-
 
75
  uint16_t CRCCalculated = CRC16(target, length);
-
 
76
 
-
 
77
  uint8_t CRCError = (CRCRead != CRCCalculated);
118
/***************************************************/
78
  uint8_t revisionMismatch = (revisionNumber != revisionNumberRead);
Line 119... Line 79...
119
/*       Write Parameter to EEPROM as word         */
79
 
120
/***************************************************/
80
  if (CRCError && revisionMismatch) printf("\n\rEEPROM CRC error and revision mismatch; ");
121
void SetParamWord(uint16_t param_id, uint16_t value) {
81
  else if (CRCError) printf("\n\rEEPROM CRC error; ");
122
        eeprom_write_word(
82
  else if (revisionMismatch) printf("\n\rEEPROM revision mismatch; ");
123
                        (uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
83
  return (CRCError || revisionMismatch);
-
 
84
}
124
}
85
 
125
 
86
/***************************************************/
126
/***************************************************/
87
/*       Read Parameter Set from EEPROM            */
127
/*       Read Parameter Set from EEPROM            */
88
/***************************************************/
Line 128... Line 89...
128
/***************************************************/
89
// setnumber [1..5]
129
// number [1..5]
90
uint8_t paramSet_readFromEEProm(uint8_t setnumber) {
130
void ParamSet_ReadFromEEProm() {
91
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(ParamSet_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
131
        eeprom_read_block((uint8_t *) &staticParams.ChannelAssignment[0],
-
 
132
                        &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN], PARAMSET_STRUCT_LEN);
92
  uint8_t result = readChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(ParamSet_t));
133
        output_init();
-
 
134
}
93
  configuration_paramSetDidChange();
135
 
-
 
136
/***************************************************/
-
 
137
/*        Write Parameter Set to EEPROM            */
-
 
138
/***************************************************/
94
  return result;
139
// number [1..5]
95
}
140
void ParamSet_WriteToEEProm() {
-
 
141
        eeprom_write_block((uint8_t *) &staticParams.ChannelAssignment[0],
96
 
142
                        &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN], PARAMSET_STRUCT_LEN);
97
/***************************************************/
143
        eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAMSET_LENGTH],
98
/*        Write Parameter Set to EEPROM            */
144
                        PARAMSET_STRUCT_LEN);
99
/***************************************************/
-
 
100
void paramSet_writeToEEProm(uint8_t setnumber) {
-
 
101
  uint16_t offset = EEPROM_ADR_PARAMSET_BEGIN + (setnumber-1)*(sizeof(ParamSet_t)+EEPROM_CHECKSUMMED_BLOCK_OVERHEAD);
145
        eeprom_write_block(&staticParams.ChannelAssignment[0],
102
  writeChecksummedBlock(EEPARAM_REVISION, (uint8_t*)&staticParams, offset, sizeof(ParamSet_t));
146
                        &EEPromArray[EEPROM_ADR_CHANNELS], 8); // backup the first 8 bytes that is the rc channel mapping
103
  // set this parameter set to active set
147
        // set this parameter set to active set
104
}
148
        output_init();
105
 
-
 
106
void paramSet_readOrDefault() {
-
 
107
  // parameter version  check
149
}
108
  if (paramSet_readFromEEProm(1)) {
-
 
109
    // if version check faild
150
 
110
    printf("\n\rwriting default parameter sets");
151
/***************************************************/
111
    for (uint8_t i=5; i>0; i--) {
152
/*       Get active parameter set                  */
112
      paramSet_default(i);
153
/***************************************************/
113
      paramSet_writeToEEProm(i);
154
uint8_t getActiveParamSet(void) {
114
    }
155
        uint8_t setnumber;
115
    // default-Setting is parameter set 1
156
        setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
116
    paramSet_readFromEEProm(1);
157
        if (setnumber > 5) {
117
    // For some strange reason, the read will have no effect.
158
                setnumber = 3;
118
    // Lets reset...
159
                eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
119
    // wdt_enable(WDTO_500MS);
160
        }
-
 
161
        return (setnumber);
-
 
162
}
120
  }
163
 
-
 
164
/***************************************************/
-
 
165
/*          Read MixerTable from EEPROM            */
-
 
166
/***************************************************/
-
 
167
uint8_t MixerTable_ReadFromEEProm(void) {
121
  printf("\n\r\rUsing Parameter Set %d", 1);
168
        if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE])
122
}
169
                        == EEMIXER_REVISION) {
123
 
170
                eeprom_read_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE],
124
/***************************************************/
171
                                sizeof(Mixer));
125
/*          Read IMU Config from EEPROM            */
172
                return 1;
126
/***************************************************/
-
 
127
uint8_t IMUConfig_readFromEEprom(void) {
-
 
128
  return readChecksummedBlock(IMUCONFIG_REVISION, (uint8_t*)&IMUConfig, EEPROM_ADR_IMU_CONFIG, sizeof(IMUConfig_t));
-
 
129
}
173
        } else
130
 
174
                return 0;
131
/***************************************************/
175
}
132
/*          Write IMU Config to EEPROM             */
176
 
133
/***************************************************/
-
 
134
void IMUConfig_writeToEEprom(void) {
177
/***************************************************/
135
  writeChecksummedBlock(IMUCONFIG_REVISION, (uint8_t*)&IMUConfig, EEPROM_ADR_IMU_CONFIG, sizeof(IMUConfig_t));
178
/*          Write Mixer Table to EEPROM            */
-
 
179
/***************************************************/
136
}
180
uint8_t MixerTable_WriteToEEProm(void) {
137
 
181
        if (Mixer.Revision == EEMIXER_REVISION) {
138
void IMUConfig_readOrDefault(void) {
182
                eeprom_write_block((uint8_t *) &Mixer,
139
    if(IMUConfig_readFromEEprom()) {
183
                                &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
140
      printf("\n\rwriting default IMU config");
184
                return 1;
141
      IMUConfig_default();
185
        } else
-
 
186
                return 0;
-
 
187
}
-
 
188
 
-
 
189
/***************************************************/
142
      IMUConfig_writeToEEprom();
190
/*    Default Values for Mixer Table               */
-
 
191
/***************************************************/
-
 
192
void MixerTable_Default(void) { // Quadro 
-
 
193
        uint8_t i;
143
    }
-
 
144
}
194
        Mixer.Revision = EEMIXER_REVISION;
145
 
195
        // clear mixer table (but preset throttle)
146
/***************************************************/
196
        for (i = 0; i < 16; i++) {
-
 
197
                Mixer.Motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
147
/* ChannelMap                                      */
198
                Mixer.Motor[i][MIX_PITCH] = 0;
148
/***************************************************/
199
                Mixer.Motor[i][MIX_ROLL] = 0;
-
 
200
                Mixer.Motor[i][MIX_YAW] = 0;
149
void channelMap_writeToEEProm(void) {
201
        }
150
  writeChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(channelMap_t));
202
        // default = Quadro
151
}
203
        Mixer.Motor[0][MIX_PITCH] = +64;
-
 
204
        Mixer.Motor[0][MIX_YAW] = +64;
152
 
205
        Mixer.Motor[1][MIX_PITCH] = -64;
153
void channelMap_readOrDefault(void) {
206
        Mixer.Motor[1][MIX_YAW] = +64;
154
  if (readChecksummedBlock(CHANNELMAP_REVISION, (uint8_t*)&channelMap, EEPROM_ADR_CHANNELMAP, sizeof(channelMap_t))) {
207
        Mixer.Motor[2][MIX_ROLL] = -64;
155
    printf("\n\rwriting default channel map");
208
        Mixer.Motor[2][MIX_YAW] = -64;
156
    channelMap_default();
209
        Mixer.Motor[3][MIX_ROLL] = +64;
157
    channelMap_writeToEEProm();
210
        Mixer.Motor[3][MIX_YAW] = -64;
-
 
211
        memcpy(Mixer.Name, "Quadro\0", 7);
-
 
212
}
-
 
213
 
-
 
214
/***************************************************/
158
        wdt_enable(WDTO_500MS);
215
/*       Initialize EEPROM Parameter Sets          */
-
 
216
/***************************************************/
-
 
217
void ParamSet_Init(void) {
-
 
218
        uint8_t Channel_Backup = 1, j;
-
 
219
        // parameter version  check
159
  }
220
        if (eeprom_read_byte(&EEPromArray[PID_PARAM_REVISION]) != EEPARAM_REVISION) {
-
 
221
                // if version check faild
160
}
-
 
161
 
222
                eeprom_write_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE], 0xFF); // reset also mixer table
162
/***************************************************/
-
 
163
/* Sensor offsets                                  */
223
                // check if channel mapping backup is valid
164
/***************************************************/
224
                for (j = 0; j < 4 && Channel_Backup; j++) {
-
 
225
                        if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS + 0]) >= 12)
-
 
226
                                Channel_Backup = 0;
165
uint8_t gyroAmplifierOffset_readFromEEProm(void) {
227
                }
166
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroAmplifierOffset, EEPROM_ADR_GYROAMPLIFIER, sizeof(sensorOffset_t));
228
                // fill all 5 parameter settings
-
 
229
 
167
}
230
        setDefaults(); // Fill staticParams Structure to default parameter set 1 (Sport)
168
 
231
               
169
void gyroAmplifierOffset_writeToEEProm(void) {
-
 
170
  return writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroAmplifierOffset, EEPROM_ADR_GYROAMPLIFIER, sizeof(sensorOffset_t));
-
 
171
}
232
        if (Channel_Backup) { // if we have a rc channel mapping backup in eeprom
172
 
-
 
173
uint8_t gyroOffset_readFromEEProm(void) {
233
                        // restore it
174
  return readChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
234
                        for (j = 0; j < 8; j++) {
175
}
235
                                staticParams.ChannelAssignment[j] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS + j]);
176
 
-
 
177
void gyroOffset_writeToEEProm(void) {
236
            }
178
  writeChecksummedBlock(SENSOROFFSET_REVISION, (uint8_t*)&gyroOffset, EEPROM_ADR_GYROOFFSET, sizeof(sensorOffset_t));
237
        }
179
}
238
               
180
 
-
 
181
/*