forked from multiwii/multiwii-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEPROM.ino
149 lines (137 loc) · 5.45 KB
/
EEPROM.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <avr/eeprom.h>
uint8_t calculate_sum(uint8_t *cb , uint8_t siz) {
uint8_t sum=0x55; // checksum init
while(--siz) sum += *cb++; // calculate checksum (without checksum byte)
return sum;
}
void readGlobalSet() {
eeprom_read_block((void*)&global_conf, (void*)0, sizeof(global_conf));
if(calculate_sum((uint8_t*)&global_conf, sizeof(global_conf)) != global_conf.checksum) {
global_conf.currentSet = 0;
global_conf.accZero[ROLL] = 5000; // for config error signalization
}
}
void readEEPROM() {
uint8_t i;
if(global_conf.currentSet>2) global_conf.currentSet=0;
eeprom_read_block((void*)&conf, (void*)(global_conf.currentSet * sizeof(conf) + sizeof(global_conf)), sizeof(conf));
if(calculate_sum((uint8_t*)&conf, sizeof(conf)) != conf.checksum) {
blinkLED(6,100,3);
LoadDefaults(); // force load defaults
}
for(i=0;i<6;i++) {
lookupPitchRollRC[i] = (2500+conf.rcExpo8*(i*i-25))*i*(int32_t)conf.rcRate8/2500;
}
for(i=0;i<11;i++) {
int16_t tmp = 10*i-conf.thrMid8;
uint8_t y = 1;
if (tmp>0) y = 100-conf.thrMid8;
if (tmp<0) y = conf.thrMid8;
lookupThrottleRC[i] = 10*conf.thrMid8 + tmp*( 100-conf.thrExpo8+(int32_t)conf.thrExpo8*(tmp*tmp)/(y*y) )/10; // [0;1000]
lookupThrottleRC[i] = MINTHROTTLE + (int32_t)(MAXTHROTTLE-MINTHROTTLE)* lookupThrottleRC[i]/1000; // [0;1000] -> [MINTHROTTLE;MAXTHROTTLE]
}
#if defined(POWERMETER)
pAlarm = (uint32_t) conf.powerTrigger1 * (uint32_t) PLEVELSCALE * (uint32_t) conf.pleveldiv; // need to cast before multiplying
#endif
#ifdef FLYING_WING
#ifdef LCD_CONF
conf.wing_left_mid = constrain(conf.wing_left_mid, WING_LEFT_MIN, WING_LEFT_MAX); //LEFT
conf.wing_right_mid = constrain(conf.wing_right_mid, WING_RIGHT_MIN, WING_RIGHT_MAX); //RIGHT
#else // w.o LCD support user may not find this value stored in eeprom, so always use the define value
conf.wing_left_mid = WING_LEFT_MID;
conf.wing_right_mid = WING_RIGHT_MID;
#endif
#endif
#ifdef TRI
#ifdef LCD_CONF
conf.tri_yaw_middle = constrain(conf.tri_yaw_middle, TRI_YAW_CONSTRAINT_MIN, TRI_YAW_CONSTRAINT_MAX); //REAR
#else // w.o LCD support user may not find this value stored in eeprom, so always use the define value
conf.tri_yaw_middle = TRI_YAW_MIDDLE;
#endif
#endif
#if GPS
if (f.I2C_INIT_DONE) GPS_set_pids();
#endif
#ifdef POWERMETER_HARD
conf.pleveldivsoft = PLEVELDIVSOFT;
#endif
#ifdef POWERMETER_SOFT
conf.pleveldivsoft = conf.pleveldiv;
#endif
}
void writeGlobalSet(uint8_t b) {
global_conf.checksum = calculate_sum((uint8_t*)&global_conf, sizeof(global_conf));
eeprom_write_block((const void*)&global_conf, (void*)0, sizeof(global_conf));
if (b == 1) blinkLED(15,20,1);
}
void writeParams(uint8_t b) {
if(global_conf.currentSet>2) global_conf.currentSet=0;
conf.checksum = calculate_sum((uint8_t*)&conf, sizeof(conf));
eeprom_write_block((const void*)&conf, (void*)(global_conf.currentSet * sizeof(conf) + sizeof(global_conf)), sizeof(conf));
readEEPROM();
if (b == 1) blinkLED(15,20,1);
}
void LoadDefaults() {
conf.P8[ROLL] = 40; conf.I8[ROLL] = 30; conf.D8[ROLL] = 23;
conf.P8[PITCH] = 40; conf.I8[PITCH] = 30; conf.D8[PITCH] = 23;
conf.P8[YAW] = 85; conf.I8[YAW] = 45; conf.D8[YAW] = 0;
conf.P8[PIDALT] = 50; conf.I8[PIDALT] = 20; conf.D8[PIDALT] = 30;
conf.P8[PIDPOS] = POSHOLD_P * 100; conf.I8[PIDPOS] = POSHOLD_I * 100; conf.D8[PIDPOS] = 0;
conf.P8[PIDPOSR] = POSHOLD_RATE_P * 10; conf.I8[PIDPOSR] = POSHOLD_RATE_I * 100; conf.D8[PIDPOSR] = POSHOLD_RATE_D * 1000;
conf.P8[PIDNAVR] = NAV_P * 10; conf.I8[PIDNAVR] = NAV_I * 100; conf.D8[PIDNAVR] = NAV_D * 1000;
conf.P8[PIDLEVEL] = 70; conf.I8[PIDLEVEL] = 10; conf.D8[PIDLEVEL] = 100;
conf.P8[PIDMAG] = 40;
conf.P8[PIDVEL] = 0; conf.I8[PIDVEL] = 0; conf.D8[PIDVEL] = 0;
conf.rcRate8 = 90; conf.rcExpo8 = 65;
conf.rollPitchRate = 0;
conf.yawRate = 0;
conf.dynThrPID = 0;
conf.thrMid8 = 50; conf.thrExpo8 = 0;
for(uint8_t i=0;i<CHECKBOXITEMS;i++) {conf.activate[i] = 0;}
conf.angleTrim[0] = 0; conf.angleTrim[1] = 0;
conf.powerTrigger1 = 0;
#ifdef FLYING_WING
conf.wing_left_mid = WING_LEFT_MID;
conf.wing_right_mid = WING_RIGHT_MID;
#endif
#ifdef FIXEDWING
conf.dynThrPID = 50;
conf.rcExpo8 = 0;
#endif
#ifdef TRI
conf.tri_yaw_middle = TRI_YAW_MIDDLE;
#endif
#if defined HELICOPTER || defined(AIRPLANE)|| defined(SINGLECOPTER)|| defined(DUALCOPTER)
{
int16_t s[8] = SERVO_OFFSET;
for(uint8_t i=0;i<8;i++) conf.servoTrim[i] = s[i];
}
#endif
#if defined(GYRO_SMOOTHING)
{
uint8_t s[3] = GYRO_SMOOTHING;
for(uint8_t i=0;i<3;i++) conf.Smoothing[i] = s[i];
}
#endif
#if defined (FAILSAFE)
conf.failsafe_throttle = FAILSAFE_THROTTLE;
#endif
#ifdef VBAT
conf.vbatscale = VBATSCALE;
conf.vbatlevel1_3s = VBATLEVEL1_3S;
conf.vbatlevel2_3s = VBATLEVEL2_3S;
conf.vbatlevel3_3s = VBATLEVEL3_3S;
conf.vbatlevel4_3s = VBATLEVEL4_3S;
conf.no_vbat = NO_VBAT;
#endif
#ifdef POWERMETER
conf.psensornull = PSENSORNULL;
//conf.pleveldivsoft = PLEVELDIVSOFT; // not neccessary; this gets set in the eeprom read function
conf.pleveldiv = PLEVELDIV;
conf.pint2ma = PINT2mA;
#endif
#ifdef CYCLETIME_FIXATED
conf.cycletime_fixated = CYCLETIME_FIXATED;
#endif
writeParams(0); // this will also (p)reset checkNewConf with the current version number again.
}