-
Notifications
You must be signed in to change notification settings - Fork 25
/
EEPROM.ino
207 lines (191 loc) · 7.04 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#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;
#ifdef MULTIPLE_CONFIGURATION_PROFILES
if(global_conf.currentSet>2) global_conf.currentSet=0;
#else
global_conf.currentSet=0;
#endif
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);
#if defined(BUZZER)
alarmArray[7] = 3;
#endif
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] = conf.minthrottle + (int32_t)(MAXTHROTTLE-conf.minthrottle)* lookupThrottleRC[i]/1000; // [0;1000] -> [conf.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
#if defined(ARMEDTIMEWARNING)
ArmedTimeWarningMicroSeconds = (conf.armedtimewarning *1000000);
#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);
#if defined(BUZZER)
alarmArray[7] = 1;
#endif
}
void writeParams(uint8_t b) {
#ifdef MULTIPLE_CONFIGURATION_PROFILES
if(global_conf.currentSet>2) global_conf.currentSet=0;
#else
global_conf.currentSet=0;
#endif
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);
#if defined(BUZZER)
alarmArray[7] = 1; //beep if loaded from gui or android
#endif
}
void LoadDefaults() {
conf.P8[ROLL] = 33; conf.I8[ROLL] = 30; conf.D8[ROLL] = 23;
conf.P8[PITCH] = 33; conf.I8[PITCH] = 30; conf.D8[PITCH] = 23;
conf.P8[YAW] = 68; conf.I8[YAW] = 45; conf.D8[YAW] = 0;
conf.P8[PIDALT] = 64; conf.I8[PIDALT] = 25; conf.D8[PIDALT] = 24;
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] = 90; 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 = 0;
conf.rollPitchRate = 0;
conf.yawRate = 0;
conf.dynThrPID = 0;
conf.thrMid8 = 50; conf.thrExpo8 = 50;
for(uint8_t i=0;i<CHECKBOXITEMS;i++) {conf.activate[i] = 0;}
conf.activate[BOXHORIZON] = 1 << 0 | 1 << 1 | 1 << 2;
//conf.activate[BOXHEADFREE] = 1 << 2;
conf.activate[BOXBARO] = 1 << 5;
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.vbatlevel_warn1 = VBATLEVEL_WARN1;
conf.vbatlevel_warn2 = VBATLEVEL_WARN2;
conf.vbatlevel_crit = VBATLEVEL_CRIT;
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
#ifdef MMGYRO
conf.mmgyro = MMGYRO;
#endif
#if defined(ARMEDTIMEWARNING)
conf.armedtimewarning = ARMEDTIMEWARNING;
#endif
conf.minthrottle = MINTHROTTLE;
#ifdef GOVERNOR_P
conf.governorP = GOVERNOR_P;
conf.governorD = GOVERNOR_D;
conf.governorR = GOVERNOR_R;
#endif
writeParams(0); // this will also (p)reset checkNewConf with the current version number again.
}
#ifdef LOG_PERMANENT
void readPLog() {
eeprom_read_block((void*)&plog, (void*)(LOG_PERMANENT - sizeof(plog)), sizeof(plog));
if(calculate_sum((uint8_t*)&plog, sizeof(plog)) != plog.checksum) {
blinkLED(9,100,3);
#if defined(BUZZER)
alarmArray[7] = 3;
#endif
// force load defaults
plog.arm = plog.disarm = plog.start = plog.failsafe = plog.i2c = 0;
plog.running = 1;
plog.lifetime = plog.armed_time = 0;
writePLog();
}
}
void writePLog() {
plog.checksum = calculate_sum((uint8_t*)&plog, sizeof(plog));
eeprom_write_block((const void*)&plog, (void*)(LOG_PERMANENT - sizeof(plog)), sizeof(plog));
}
#endif