Subversion Repositories FlightCtrl

Rev

Rev 1927 | Rev 2102 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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