Rev 1927 | Rev 2099 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1910 | - | 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 |
||
11 | #define EEMEM __attribute__ ((section (".eeprom"))) |
||
12 | #endif |
||
13 | |||
14 | #include <avr/eeprom.h> |
||
15 | #include <string.h> |
||
16 | #include "eeprom.h" |
||
17 | #include "output.h" |
||
18 | // TODO: Get rid of these. They have nothing to do with eeprom. |
||
19 | #include "flight.h" |
||
20 | #include "rc.h" |
||
21 | #include "sensors.h" |
||
22 | |||
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; |
||
1927 | - | 35 | for (i = 0; i < sizeof(staticParams.UserParams); i++) { |
36 | staticParams.UserParams[i] = 0; |
||
1910 | - | 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; |
||
2025 | - | 79 | |
1910 | - | 80 | staticParams.J16Bitmask = 95; |
81 | staticParams.J17Bitmask = 243; |
||
82 | staticParams.J16Timing = 15; |
||
83 | staticParams.J17Timing = 15; |
||
2025 | - | 84 | |
85 | staticParams.servoDirections = 2; |
||
1910 | - | 86 | } |
87 | |||
1927 | - | 88 | void setDefaults(void) { |
1910 | - | 89 | setOtherDefaults(); |
90 | gyro_setDefaults(); |
||
91 | setDefaultUserParams(); |
||
92 | staticParams.J16Timing = 10; |
||
93 | staticParams.J17Timing = 10; |
||
94 | } |
||
95 | |||
96 | /***************************************************/ |
||
97 | /* Read Parameter from EEPROM as byte */ |
||
98 | /***************************************************/ |
||
99 | uint8_t GetParamByte(uint16_t param_id) { |
||
100 | return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]); |
||
101 | } |
||
102 | |||
103 | /***************************************************/ |
||
104 | /* Write Parameter to EEPROM as byte */ |
||
105 | /***************************************************/ |
||
106 | void SetParamByte(uint16_t param_id, uint8_t value) { |
||
107 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value); |
||
108 | } |
||
109 | |||
110 | /***************************************************/ |
||
111 | /* Read Parameter from EEPROM as word */ |
||
112 | /***************************************************/ |
||
113 | uint16_t GetParamWord(uint16_t param_id) { |
||
114 | return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN |
||
115 | + param_id]); |
||
116 | } |
||
117 | |||
118 | /***************************************************/ |
||
119 | /* Write Parameter to EEPROM as word */ |
||
120 | /***************************************************/ |
||
121 | void SetParamWord(uint16_t param_id, uint16_t value) { |
||
122 | eeprom_write_word( |
||
123 | (uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value); |
||
124 | } |
||
125 | |||
126 | /***************************************************/ |
||
127 | /* Read Parameter Set from EEPROM */ |
||
128 | /***************************************************/ |
||
129 | // number [1..5] |
||
1927 | - | 130 | void ParamSet_ReadFromEEProm() { |
1910 | - | 131 | eeprom_read_block((uint8_t *) &staticParams.ChannelAssignment[0], |
1927 | - | 132 | &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN], PARAMSET_STRUCT_LEN); |
1910 | - | 133 | output_init(); |
134 | } |
||
135 | |||
136 | /***************************************************/ |
||
137 | /* Write Parameter Set to EEPROM */ |
||
138 | /***************************************************/ |
||
139 | // number [1..5] |
||
1927 | - | 140 | void ParamSet_WriteToEEProm() { |
1910 | - | 141 | eeprom_write_block((uint8_t *) &staticParams.ChannelAssignment[0], |
1927 | - | 142 | &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN], PARAMSET_STRUCT_LEN); |
1910 | - | 143 | eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAMSET_LENGTH], |
144 | PARAMSET_STRUCT_LEN); |
||
145 | eeprom_write_block(&staticParams.ChannelAssignment[0], |
||
146 | &EEPromArray[EEPROM_ADR_CHANNELS], 8); // backup the first 8 bytes that is the rc channel mapping |
||
147 | // set this parameter set to active set |
||
148 | output_init(); |
||
149 | } |
||
150 | |||
151 | /***************************************************/ |
||
152 | /* Get active parameter set */ |
||
153 | /***************************************************/ |
||
154 | uint8_t getActiveParamSet(void) { |
||
155 | uint8_t setnumber; |
||
156 | setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]); |
||
157 | if (setnumber > 5) { |
||
158 | setnumber = 3; |
||
159 | eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber); |
||
160 | } |
||
161 | return (setnumber); |
||
162 | } |
||
163 | |||
164 | /***************************************************/ |
||
165 | /* Read MixerTable from EEPROM */ |
||
166 | /***************************************************/ |
||
167 | uint8_t MixerTable_ReadFromEEProm(void) { |
||
168 | if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE]) |
||
169 | == EEMIXER_REVISION) { |
||
170 | eeprom_read_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], |
||
171 | sizeof(Mixer)); |
||
172 | return 1; |
||
173 | } else |
||
174 | return 0; |
||
175 | } |
||
176 | |||
177 | /***************************************************/ |
||
178 | /* Write Mixer Table to EEPROM */ |
||
179 | /***************************************************/ |
||
180 | uint8_t MixerTable_WriteToEEProm(void) { |
||
181 | if (Mixer.Revision == EEMIXER_REVISION) { |
||
182 | eeprom_write_block((uint8_t *) &Mixer, |
||
183 | &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer)); |
||
184 | return 1; |
||
185 | } else |
||
186 | return 0; |
||
187 | } |
||
188 | |||
189 | /***************************************************/ |
||
190 | /* Default Values for Mixer Table */ |
||
191 | /***************************************************/ |
||
192 | void MixerTable_Default(void) { // Quadro |
||
193 | uint8_t i; |
||
194 | Mixer.Revision = EEMIXER_REVISION; |
||
195 | // clear mixer table (but preset throttle) |
||
196 | for (i = 0; i < 16; i++) { |
||
197 | Mixer.Motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0; |
||
198 | Mixer.Motor[i][MIX_PITCH] = 0; |
||
199 | Mixer.Motor[i][MIX_ROLL] = 0; |
||
200 | Mixer.Motor[i][MIX_YAW] = 0; |
||
201 | } |
||
202 | // default = Quadro |
||
203 | Mixer.Motor[0][MIX_PITCH] = +64; |
||
204 | Mixer.Motor[0][MIX_YAW] = +64; |
||
205 | Mixer.Motor[1][MIX_PITCH] = -64; |
||
206 | Mixer.Motor[1][MIX_YAW] = +64; |
||
207 | Mixer.Motor[2][MIX_ROLL] = -64; |
||
208 | Mixer.Motor[2][MIX_YAW] = -64; |
||
209 | Mixer.Motor[3][MIX_ROLL] = +64; |
||
210 | Mixer.Motor[3][MIX_YAW] = -64; |
||
211 | memcpy(Mixer.Name, "Quadro\0", 7); |
||
212 | } |
||
213 | |||
214 | /***************************************************/ |
||
215 | /* Initialize EEPROM Parameter Sets */ |
||
216 | /***************************************************/ |
||
217 | void ParamSet_Init(void) { |
||
1927 | - | 218 | uint8_t Channel_Backup = 1, j; |
1910 | - | 219 | // parameter version check |
220 | if (eeprom_read_byte(&EEPromArray[PID_PARAM_REVISION]) != EEPARAM_REVISION) { |
||
221 | // if version check faild |
||
222 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE], 0xFF); // reset also mixer table |
||
223 | // check if channel mapping backup is valid |
||
224 | for (j = 0; j < 4 && Channel_Backup; j++) { |
||
225 | if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS + 0]) >= 12) |
||
226 | Channel_Backup = 0; |
||
227 | } |
||
228 | // fill all 5 parameter settings |
||
1927 | - | 229 | |
230 | setDefaults(); // Fill staticParams Structure to default parameter set 1 (Sport) |
||
231 | |||
232 | if (Channel_Backup) { // if we have a rc channel mapping backup in eeprom |
||
233 | // restore it |
||
234 | for (j = 0; j < 8; j++) { |
||
235 | staticParams.ChannelAssignment[j] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS + j]); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | ParamSet_WriteToEEProm(); |
||
240 | |||
1910 | - | 241 | // update version info |
242 | SetParamByte(PID_PARAM_REVISION, EEPARAM_REVISION); |
||
243 | } |
||
244 | // read active parameter set to staticParams stucture |
||
1927 | - | 245 | ParamSet_ReadFromEEProm(); |
1910 | - | 246 | } |