Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1227 killagreg 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
 
52
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53
// + Contant Values
54
// + 0-250 -> normale Values
55
// + 251 -> Poti1
56
// + 252 -> Poti2
57
// + 253 -> Poti3
58
// + 254 -> Poti4
59
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
60
 
61
#ifndef EEMEM
62
#define EEMEM __attribute__ ((section (".eeprom")))
63
#endif
64
 
65
 
66
#include <avr/eeprom.h>
67
#include <string.h>
68
#include "eeprom.h"
69
#include "printf_P.h"
70
#include "led.h"
71
#include "main.h"
72
#include "fc.h"
73
 
74
 
75
// byte array in eeprom
76
uint8_t EEPromArray[E2END+1] EEMEM;
77
 
78
paramset_t              ParamSet;
79
MixerTable_t    Mixer;
80
 
81
 
82
/***************************************************/
83
/*    Default Values for parameter set 1           */
84
/***************************************************/
85
void ParamSet_DefaultSet1(void) // sport
86
{
87
        if(BoardRelease >= 20)
88
        {
89
                ParamSet.GyroD = 5;
90
                ParamSet.DriftComp = 0;
91
                ParamSet.GyroAccFactor = 27;
92
                ParamSet.AngleTurnOverNick = 78;
93
                ParamSet.AngleTurnOverRoll = 78;
94
        }
95
        else
96
        {
97
                ParamSet.GyroD = 3;
98
                ParamSet.DriftComp = 32;
99
                ParamSet.GyroAccFactor = 30;
100
                ParamSet.AngleTurnOverNick = 85;
101
                ParamSet.AngleTurnOverRoll = 85;
102
        }
103
        ParamSet.ChannelAssignment[CH_NICK]  = 1;
104
        ParamSet.ChannelAssignment[CH_ROLL]  = 2;
105
        ParamSet.ChannelAssignment[CH_GAS]   = 3;
106
        ParamSet.ChannelAssignment[CH_YAW]   = 4;
107
        ParamSet.ChannelAssignment[CH_POTI1] = 5;
108
        ParamSet.ChannelAssignment[CH_POTI2] = 6;
109
        ParamSet.ChannelAssignment[CH_POTI3] = 7;
110
        ParamSet.ChannelAssignment[CH_POTI4] = 8;
111
        ParamSet.GlobalConfig = CFG_AXIS_COUPLING_ACTIVE | CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
112
        ParamSet.HeightMinGas = 30;
113
        ParamSet.MaxHeight     = 251;
114
        ParamSet.HeightP      = 10;
115
        ParamSet.HeightD  = 30;
116
        ParamSet.Height_ACC_Effect = 30;
117
        ParamSet.Height_Gain = 4;
118
        ParamSet.StickP = 15;
119
        ParamSet.StickD = 30;
120
        ParamSet.StickYawP = 12;
121
        ParamSet.GasMin = 8;
122
        ParamSet.GasMax = 230;
123
        ParamSet.CompassYawEffect = 128;
124
        ParamSet.GyroP = 80;
125
        ParamSet.GyroI = 150;
126
        ParamSet.LowVoltageWarning = 94;
127
        ParamSet.EmergencyGas = 35;
128
        ParamSet.EmergencyGasDuration = 30;
129
        ParamSet.UfoArrangement = 0;
130
        ParamSet.IFactor = 32;
131
        ParamSet.UserParam1 = 0;
132
        ParamSet.UserParam2 = 0;
133
        ParamSet.UserParam3 = 0;
134
        ParamSet.UserParam4 = 0;
135
        ParamSet.UserParam5 = 0;
136
        ParamSet.UserParam6 = 0;
137
        ParamSet.UserParam7 = 0;
138
        ParamSet.UserParam8 = 0;
139
        ParamSet.ServoNickControl = 100;
140
        ParamSet.ServoNickComp = 40;
141
        ParamSet.ServoNickCompInvert = 0;
142
        ParamSet.ServoNickMin = 50;
143
        ParamSet.ServoNickMax = 150;
144
        ParamSet.ServoRefresh = 5;
145
        ParamSet.LoopGasLimit = 50;
146
        ParamSet.LoopThreshold = 90;
147
        ParamSet.LoopHysteresis = 50;
148
        ParamSet.BitConfig = 0;
149
        ParamSet.AxisCoupling1 = 90;
150
        ParamSet.AxisCoupling2 = 67;
151
        ParamSet.AxisCouplingYawCorrection = 0;
152
        ParamSet.GyroAccTrim = 16;
153
        ParamSet.DynamicStability = 100;
154
        ParamSet.J16Bitmask = 95;
155
        ParamSet.J17Bitmask = 243;
156
        ParamSet.J16Timing = 15;
157
        ParamSet.J17Timing = 15;
158
        ParamSet.NaviGpsModeControl = 253;
159
        ParamSet.NaviGpsGain = 100;
160
        ParamSet.NaviGpsP = 90;
161
        ParamSet.NaviGpsI = 90;
162
        ParamSet.NaviGpsD = 90;
163
        ParamSet.NaviGpsPLimit = 75;
164
        ParamSet.NaviGpsILimit = 75;
165
        ParamSet.NaviGpsDLimit = 75;
166
        ParamSet.NaviGpsACC = 0;
167
        ParamSet.NaviGpsMinSat = 6;
168
        ParamSet.NaviStickThreshold = 8;
169
        ParamSet.NaviWindCorrection = 90;
170
        ParamSet.NaviSpeedCompensation = 30;
171
        ParamSet.NaviOperatingRadius = 100;
172
        ParamSet.NaviAngleLimitation = 100;
173
        ParamSet.NaviPHLoginTime = 4;
174
        memcpy(ParamSet.Name, "Sport\0",6);
175
}
176
 
177
 
178
/***************************************************/
179
/*    Default Values for parameter set 2           */
180
/***************************************************/
181
void ParamSet_DefaultSet2(void) // normal
182
{
183
        if(BoardRelease >= 20)
184
        {
185
                ParamSet.GyroD = 5;
186
                ParamSet.DriftComp = 0;
187
                ParamSet.GyroAccFactor = 27;
188
                ParamSet.AngleTurnOverNick = 78;
189
                ParamSet.AngleTurnOverRoll = 78;
190
        }
191
        else
192
        {
193
                ParamSet.GyroD = 3;
194
                ParamSet.DriftComp = 32;
195
                ParamSet.GyroAccFactor = 30;
196
                ParamSet.AngleTurnOverNick = 85;
197
                ParamSet.AngleTurnOverRoll = 85;
198
        }
199
        ParamSet.ChannelAssignment[CH_NICK]  = 1;
200
        ParamSet.ChannelAssignment[CH_ROLL]  = 2;
201
        ParamSet.ChannelAssignment[CH_GAS]   = 3;
202
        ParamSet.ChannelAssignment[CH_YAW]  = 4;
203
        ParamSet.ChannelAssignment[CH_POTI1] = 5;
204
        ParamSet.ChannelAssignment[CH_POTI2] = 6;
205
        ParamSet.ChannelAssignment[CH_POTI3] = 7;
206
        ParamSet.ChannelAssignment[CH_POTI4] = 8;
207
        ParamSet.GlobalConfig = CFG_AXIS_COUPLING_ACTIVE | CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
208
        ParamSet.HeightMinGas = 30;
209
        ParamSet.MaxHeight     = 251;
210
        ParamSet.HeightP      = 10;
211
        ParamSet.HeightD  = 30;
212
        ParamSet.Height_ACC_Effect = 30;
213
        ParamSet.Height_Gain = 3;
214
        ParamSet.StickP = 12;
215
        ParamSet.StickD = 16;
216
        ParamSet.StickYawP = 6;
217
        ParamSet.GasMin = 8;
218
        ParamSet.GasMax = 230;
219
        ParamSet.CompassYawEffect = 128;
220
        ParamSet.GyroP = 80;
221
        ParamSet.GyroI = 120;
222
        ParamSet.LowVoltageWarning = 94;
223
        ParamSet.EmergencyGas = 35;
224
        ParamSet.EmergencyGasDuration = 30;
225
        ParamSet.UfoArrangement = 0;
226
        ParamSet.IFactor = 32;
227
        ParamSet.UserParam1 = 0;
228
        ParamSet.UserParam2 = 0;
229
        ParamSet.UserParam3 = 0;
230
        ParamSet.UserParam4 = 0;
231
        ParamSet.UserParam5 = 0;
232
        ParamSet.UserParam6 = 0;
233
        ParamSet.UserParam7 = 0;
234
        ParamSet.UserParam8 = 0;
235
        ParamSet.ServoNickControl = 100;
236
        ParamSet.ServoNickComp = 40;
237
        ParamSet.ServoNickCompInvert = 0;
238
        ParamSet.ServoNickMin = 50;
239
        ParamSet.ServoNickMax = 150;
240
        ParamSet.ServoRefresh = 5;
241
        ParamSet.LoopGasLimit = 50;
242
        ParamSet.LoopThreshold = 90;
243
        ParamSet.LoopHysteresis = 50;
244
        ParamSet.BitConfig = 0;
245
        ParamSet.AxisCoupling1 = 90;
246
        ParamSet.AxisCoupling2 = 67;
247
        ParamSet.AxisCouplingYawCorrection = 60;
248
        ParamSet.GyroAccTrim = 32;
249
        ParamSet.DynamicStability = 75;
250
        ParamSet.J16Bitmask = 95;
251
        ParamSet.J17Bitmask = 243;
252
        ParamSet.J16Timing = 20;
253
        ParamSet.J17Timing = 20;
254
        ParamSet.NaviGpsModeControl = 253;
255
        ParamSet.NaviGpsGain = 100;
256
        ParamSet.NaviGpsP = 90;
257
        ParamSet.NaviGpsI = 90;
258
        ParamSet.NaviGpsD = 90;
259
        ParamSet.NaviGpsPLimit = 75;
260
        ParamSet.NaviGpsILimit = 75;
261
        ParamSet.NaviGpsDLimit = 75;
262
        ParamSet.NaviGpsACC = 0;
263
        ParamSet.NaviGpsMinSat = 6;
264
        ParamSet.NaviStickThreshold = 8;
265
        ParamSet.NaviWindCorrection = 90;
266
        ParamSet.NaviSpeedCompensation = 30;
267
        ParamSet.NaviOperatingRadius = 100;
268
        ParamSet.NaviAngleLimitation = 100;
269
        ParamSet.NaviPHLoginTime = 4;
270
        memcpy(ParamSet.Name, "Normal\0", 7);
271
}
272
 
273
 
274
/***************************************************/
275
/*    Default Values for parameter set 3           */
276
/***************************************************/
277
void ParamSet_DefaultSet3(void) // beginner
278
{
279
        if(BoardRelease >= 20)
280
        {
281
                ParamSet.GyroD = 5;
282
                ParamSet.DriftComp = 0;
283
                ParamSet.GyroAccFactor = 27;         // Wert : 1-64
284
                ParamSet.AngleTurnOverNick = 78;
285
                ParamSet.AngleTurnOverRoll = 78;
286
        }
287
        else
288
        {
289
                ParamSet.GyroD = 3;
290
                ParamSet.DriftComp = 32;
291
                ParamSet.GyroAccFactor = 30;         // Wert : 1-64
292
                ParamSet.AngleTurnOverNick = 85;
293
                ParamSet.AngleTurnOverRoll = 85;
294
        }
295
        ParamSet.ChannelAssignment[CH_NICK]  = 1;
296
        ParamSet.ChannelAssignment[CH_ROLL]  = 2;
297
        ParamSet.ChannelAssignment[CH_GAS]   = 3;
298
        ParamSet.ChannelAssignment[CH_YAW]  = 4;
299
        ParamSet.ChannelAssignment[CH_POTI1] = 5;
300
        ParamSet.ChannelAssignment[CH_POTI2] = 6;
301
        ParamSet.ChannelAssignment[CH_POTI3] = 7;
302
        ParamSet.ChannelAssignment[CH_POTI4] = 8;
303
        ParamSet.GlobalConfig = CFG_ROTARY_RATE_LIMITER | CFG_AXIS_COUPLING_ACTIVE | CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
304
        ParamSet.HeightMinGas = 30;
305
        ParamSet.MaxHeight     = 251;
306
        ParamSet.HeightP      = 10;
307
        ParamSet.HeightD  = 30;
308
        ParamSet.Height_ACC_Effect = 30;
309
        ParamSet.Height_Gain = 3;
310
        ParamSet.StickP = 8;
311
        ParamSet.StickD = 16;
312
        ParamSet.StickYawP  = 6;
313
        ParamSet.GasMin = 8;
314
        ParamSet.GasMax = 230;
315
        ParamSet.CompassYawEffect = 128;
316
        ParamSet.GyroP = 100;
317
        ParamSet.GyroI = 120;
318
        ParamSet.LowVoltageWarning = 94;
319
        ParamSet.EmergencyGas = 35;
320
        ParamSet.EmergencyGasDuration = 20;
321
        ParamSet.UfoArrangement = 0;
322
        ParamSet.IFactor = 16;
323
        ParamSet.UserParam1 = 0;
324
        ParamSet.UserParam2 = 0;
325
        ParamSet.UserParam3 = 0;
326
        ParamSet.UserParam4 = 0;
327
        ParamSet.UserParam5 = 0;
328
        ParamSet.UserParam6 = 0;
329
        ParamSet.UserParam7 = 0;
330
        ParamSet.UserParam8 = 0;
331
        ParamSet.ServoNickControl = 100;
332
        ParamSet.ServoNickComp = 40;
333
        ParamSet.ServoNickCompInvert = 0;
334
        ParamSet.ServoNickMin = 50;
335
        ParamSet.ServoNickMax = 150;
336
        ParamSet.ServoRefresh = 5;
337
        ParamSet.LoopGasLimit = 50;
338
        ParamSet.LoopThreshold = 90;
339
        ParamSet.LoopHysteresis = 50;
340
        ParamSet.BitConfig = 0;
341
        ParamSet.AxisCoupling1 = 90;
342
        ParamSet.AxisCoupling2 = 67;
343
        ParamSet.AxisCouplingYawCorrection = 70;
344
        ParamSet.GyroAccTrim = 32;
345
        ParamSet.DynamicStability = 50;
346
        ParamSet.J16Bitmask = 95;
347
        ParamSet.J17Bitmask = 243;
348
        ParamSet.J16Timing = 30;
349
        ParamSet.J17Timing = 30;
350
        ParamSet.NaviGpsModeControl = 253;
351
        ParamSet.NaviGpsGain = 100;
352
        ParamSet.NaviGpsP = 90;
353
        ParamSet.NaviGpsI = 90;
354
        ParamSet.NaviGpsD = 90;
355
        ParamSet.NaviGpsPLimit = 75;
356
        ParamSet.NaviGpsILimit = 75;
357
        ParamSet.NaviGpsDLimit = 75;
358
        ParamSet.NaviGpsACC = 0;
359
        ParamSet.NaviGpsMinSat = 6;
360
        ParamSet.NaviStickThreshold = 8;
361
        ParamSet.NaviWindCorrection = 90;
362
        ParamSet.NaviSpeedCompensation = 30;
363
        ParamSet.NaviOperatingRadius = 100;
364
        ParamSet.NaviAngleLimitation = 100;
365
        ParamSet.NaviPHLoginTime = 4;
366
        memcpy(ParamSet.Name, "Beginner\0", 9);
367
}
368
 
369
/***************************************************/
370
/*       Read Parameter from EEPROM as byte        */
371
/***************************************************/
372
uint8_t GetParamByte(uint16_t param_id)
373
{
374
        return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
375
}
376
 
377
/***************************************************/
378
/*       Write Parameter to EEPROM as byte         */
379
/***************************************************/
380
void SetParamByte(uint16_t param_id, uint8_t value)
381
{
382
        eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
383
}
384
 
385
/***************************************************/
386
/*       Read Parameter from EEPROM as word        */
387
/***************************************************/
388
uint16_t GetParamWord(uint16_t param_id)
389
{
390
        return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
391
}
392
 
393
/***************************************************/
394
/*       Write Parameter to EEPROM as word         */
395
/***************************************************/
396
void SetParamWord(uint16_t param_id, uint16_t value)
397
{
398
        eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
399
}
400
 
401
/***************************************************/
402
/*       Read Parameter Set from EEPROM            */
403
/***************************************************/
404
// number [1..5]
405
void ParamSet_ReadFromEEProm(uint8_t setnumber)
406
{
407
        if((1 > setnumber) || (setnumber > 5)) setnumber = 3;
408
        eeprom_read_block((uint8_t *) &ParamSet.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN + PARAMSET_STRUCT_LEN * (setnumber - 1)], PARAMSET_STRUCT_LEN);
409
        LED_Init();
410
}
411
 
412
/***************************************************/
413
/*        Write Parameter Set to EEPROM            */
414
/***************************************************/
415
// number [1..5]
416
void ParamSet_WriteToEEProm(uint8_t setnumber)
417
{
418
        if(setnumber > 5) setnumber = 5;
419
        if(setnumber < 1) return;
420
        eeprom_write_block((uint8_t *) &ParamSet.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN + PARAMSET_STRUCT_LEN * (setnumber - 1)], PARAMSET_STRUCT_LEN);
421
        eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAMSET_LENGTH], PARAMSET_STRUCT_LEN);
422
        eeprom_write_block( &ParamSet.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_CHANNELS], 8); // backup the first 8 bytes that is the rc channel mapping
423
        // set this parameter set to active set
424
        SetActiveParamSet(setnumber);
425
        LED_Init();
426
}
427
 
428
/***************************************************/
429
/*          Read MixerTable from EEPROM            */
430
/***************************************************/
431
uint8_t MixerTable_ReadFromEEProm(void)
432
{
433
        if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE]) == EEMIXER_REVISION)
434
        {
435
                eeprom_read_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
436
                return 1;
437
        }
438
        else return 0;
439
}
440
 
441
/***************************************************/
442
/*          Write Mixer Table to EEPROM            */
443
/***************************************************/
444
uint8_t MixerTable_WriteToEEProm(void)
445
{
446
        if(Mixer.Revision == EEMIXER_REVISION)
447
        {
448
                eeprom_write_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
449
                return 1;
450
        }
451
        else return 0;
452
}
453
 
454
/***************************************************/
455
/*    Default Values for Mixer Table               */
456
/***************************************************/
457
void MixerTable_Default(void) // Quadro
458
{
459
        uint8_t i;
460
 
461
        Mixer.Revision = EEMIXER_REVISION;
462
        // clear mixer table
463
        for(i = 0; i < 16; i++)
464
        {
465
                Mixer.Motor[i][MIX_GAS]  = 0;
466
                Mixer.Motor[i][MIX_NICK] = 0;
467
                Mixer.Motor[i][MIX_ROLL] = 0;
468
                Mixer.Motor[i][MIX_YAW]  = 0;
469
        }
470
        // default = Quadro
471
        Mixer.Motor[0][MIX_GAS] = 64; Mixer.Motor[0][MIX_NICK] = +64; Mixer.Motor[0][MIX_ROLL] =   0; Mixer.Motor[0][MIX_YAW] = +64;
472
        Mixer.Motor[1][MIX_GAS] = 64; Mixer.Motor[1][MIX_NICK] = -64; Mixer.Motor[1][MIX_ROLL] =   0; Mixer.Motor[1][MIX_YAW] = +64;
473
        Mixer.Motor[2][MIX_GAS] = 64; Mixer.Motor[2][MIX_NICK] =   0; Mixer.Motor[2][MIX_ROLL] = -64; Mixer.Motor[2][MIX_YAW] = -64;
474
        Mixer.Motor[3][MIX_GAS] = 64; Mixer.Motor[3][MIX_NICK] =   0; Mixer.Motor[3][MIX_ROLL] = +64; Mixer.Motor[3][MIX_YAW] = -64;
475
        memcpy(Mixer.Name, "Quadro\0", 7);
476
}
477
 
478
 
479
/***************************************************/
480
/*       Get active parameter set                  */
481
/***************************************************/
482
uint8_t GetActiveParamSet(void)
483
{
484
        uint8_t setnumber;
485
        setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
486
        if(setnumber > 5)
487
        {
488
                setnumber = 3;
489
                eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
490
        }
491
        return(setnumber);
492
}
493
 
494
/***************************************************/
495
/*       Set active parameter set                  */
496
/***************************************************/
497
void SetActiveParamSet(uint8_t setnumber)
498
{
499
        if(setnumber > 5) setnumber = 5;
500
        if(setnumber < 1) setnumber = 1;
501
        eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
502
}
503
 
504
/***************************************************/
505
/*       Initialize EEPROM Parameter Sets          */
506
/***************************************************/
507
void ParamSet_Init(void)
508
{
509
        uint8_t Channel_Backup  = 0, i;
510
        // parameter version  check
511
        if(eeprom_read_byte(&EEPromArray[PID_PARAM_REVISION]) != EEPARAM_REVISION)
512
        {
513
                // if version check faild
514
                printf("\n\rInit Parameter in EEPROM");
515
                eeprom_write_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE], 0xFF); // reset also mixer table
516
                // check if channel mapping backup is valid
517
                if(             (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]) < 12)
518
                    &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]) < 12)
519
                    &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]) < 12)
520
                    &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]) < 12)
521
                   )
522
                {
523
                        Channel_Backup = 1;
524
                }
525
                // fill all 5 parameter settings
526
                for (i = 1;i < 6; i++)
527
                {
528
                        switch(i)
529
                        {
530
                                case 1:
531
                                        ParamSet_DefaultSet1(); // Fill ParamSet Structure to default parameter set 1 (Sport)
532
                                        break;
533
                                case 2:
534
                                        ParamSet_DefaultSet2(); // Kamera
535
                                        break;
536
                                case 3:
537
                                        ParamSet_DefaultSet3(); // Beginner
538
                                        break;
539
                                default:
540
                                        ParamSet_DefaultSet2(); // Kamera
541
                                        break;
542
                        }
543
                        if(Channel_Backup) // if we have a rc channel mapping backup in eeprom
544
                        {
545
                                // restore it
546
                                ParamSet.ChannelAssignment[0] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]);
547
                                ParamSet.ChannelAssignment[1] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]);
548
                                ParamSet.ChannelAssignment[2] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]);
549
                                ParamSet.ChannelAssignment[3] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]);
550
                                ParamSet.ChannelAssignment[4] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+4]);
551
                                ParamSet.ChannelAssignment[5] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+5]);
552
                                ParamSet.ChannelAssignment[6] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+6]);
553
                                ParamSet.ChannelAssignment[7] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+7]);
554
                        }
555
                        ParamSet_WriteToEEProm(i);
556
                }
557
                // default-Setting is parameter set 3
558
                SetActiveParamSet(3);
559
                // update version info
560
                SetParamByte(PID_PARAM_REVISION, EEPARAM_REVISION);
561
        }
562
        // read active parameter set to ParamSet stucture
563
        ParamSet_ReadFromEEProm(GetActiveParamSet());
564
        printf("\n\rUsing Parameter Set %d", GetActiveParamSet());
565
 
566
        // load mixer table
567
        if(!MixerTable_ReadFromEEProm() )
568
        {
569
                printf("\n\rGenerating default Mixer Table");
570
                MixerTable_Default(); // Quadro
571
                MixerTable_WriteToEEProm();
572
        }
573
        // determine motornumber
574
        RequiredMotors = 0;
575
        for(i = 0; i < 16; i++)
576
        {
577
                if(Mixer.Motor[i][MIX_GAS] > 0) RequiredMotors++;
578
        }
579
 
580
        printf("\n\rMixer-Config: '%s' (%u Motors)",Mixer.Name, RequiredMotors);
581
        printf("\n\r==============================");
582
}