-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEEPROM_operations.ino
326 lines (282 loc) · 8.09 KB
/
EEPROM_operations.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//*********************************************************************
// EEPROM management fuctions
//
// Parameter data structure
// Address Data Length value range
// 0 FLBThresholdPWM int 0-400
// 1 debounceCount byte 0-10
// 2 pumpMinPWM int 0-400
// 3 pumpMaxPWM int 0-400
// 4 mlPerFlowMeterPulse float
// 8 unionThreshold
// 12 Kpp double (4 bytes)
// 16 Kpi double (4 bytes)
// 20 Kpd double (4 bytes)
// 24 Kfp double (4 bytes)
// 28 Kfi double (4 bytes)
// 32 Kfd double (4 bytes)
// 36 slayerPIFlowRate byte
// 41 slayerMainPWM int
// 43 slayerPIPeriod byte
// 39 slayerMaxPWM int
//
// Profile data structure
// 97 Size of PWMprofile byte
// 98 Size of Pressure p. byte
// 99 Size of Flow prof. byte
// 100-301 PWM Profile bytes (2 per second)
// 302-503 Pressure Profile bytes (2 per second)
// 504-705 Flow Profile bytes (2 per second)
//
//*********************************************************************
// Store PWM, pressure and flow Profiles in EEPROM
void storeProfilesinEEPROM()
{
EEPROM.update(97, (byte)sizeof(g_PWMProfile)); // Should be MAX_PROFILE_INDEX + 1
EEPROM.update(98, (byte)sizeof(g_pressureProfile));
EEPROM.update(99, (byte)sizeof(g_flowProfile));
int shiftAddr = 100; // Start writing arrays at 100th byte
int shiftPP = shiftAddr + sizeof(g_PWMProfile);
int shiftFP = shiftPP + sizeof(g_pressureProfile);
// Write PWM Profile
for (int addr = 0; addr < sizeof(g_PWMProfile); addr++)
{
EEPROM.update(addr + shiftAddr, g_PWMProfile[addr]);
EEPROM.update(addr + shiftPP, g_pressureProfile[addr]);
EEPROM.update(addr + shiftFP, g_flowProfile[addr]);
}
}
// Retrieve PWM, pressure and flow Profiles from EEPROM
void readProfilesfromEEPROM()
{
int PWMProfileSize = EEPROM.read(97);
int pressureProfileSize = EEPROM.read(98);
int flowProfileSize = EEPROM.read(99);
int shiftAddr = 100; // Start reading arrays at 10th byte (should match similar decleration at storeProfilesinEEPROM()
int shiftPP = shiftAddr + PWMProfileSize;
int shiftFP = shiftPP + pressureProfileSize;
for (int addr = 0; addr < PWMProfileSize; addr++)
{
g_PWMProfile[addr] = EEPROM.read(addr + shiftAddr);
g_pressureProfile[addr] = EEPROM.read(addr + shiftPP);
g_flowProfile[addr] = EEPROM.read(addr + shiftFP);
}
}
// Retrieve software parameters from EEPROM
void readSWParametersfromEEPROM()
{
Serial.println("Retrieving system parameters from EEPROM");
EEPROM.get(0, FLBThresholdPWM);
debounceCount = EEPROM.read(2);
EEPROM.get(3, pumpMinPWM);
EEPROM.get(5, pumpMaxPWM);
EEPROM.get(8, mlPerFlowMeterPulse);
EEPROM.get(12, unionThreshold);
EEPROM.get(16, Kpp);
EEPROM.get(20, Kpi);
EEPROM.get(24, Kpd);
EEPROM.get(28, Kfp);
EEPROM.get(32, Kfi);
EEPROM.get(36, Kfd);
slayerPIFlowRate = EEPROM.read(40);
EEPROM.get(41, slayerMainPWM);
slayerPIPeriod = EEPROM.read(43);
EEPROM.get(44, slayerMaxPWM);
}
// Update software parameters in EEPROM
void writeSWParameterstoEEPROM()
{
Serial.println("Saving system parameters to EEPROM");
EEPROM.put(0, FLBThresholdPWM);
EEPROM.update(2, debounceCount);
EEPROM.put(3, pumpMinPWM);
EEPROM.put(5, pumpMaxPWM);
EEPROM.put(8, mlPerFlowMeterPulse);
EEPROM.put(12, unionThreshold);
EEPROM.put(16, Kpp);
EEPROM.put(20, Kpi);
EEPROM.put(24, Kpd);
EEPROM.put(28, Kfp);
EEPROM.put(32, Kfi);
EEPROM.put(36, Kfd);
EEPROM.update(40, slayerPIFlowRate);
EEPROM.put(41, slayerMainPWM);
EEPROM.update(43, slayerPIPeriod);
EEPROM.put(44, slayerMaxPWM); //const
}
void writeSlayerParameterstoEEPROM()
{
Serial.println("Writing Slayer-like mode parameters to EEPROM");
EEPROM.update(40, slayerPIFlowRate);
EEPROM.put(41, slayerMainPWM);
EEPROM.update(43, slayerPIPeriod);
EEPROM.put(44, slayerMaxPWM);
}
/*
* EEPROM Read
*
* Reads the value of each byte of the EEPROM and prints it
*
*/
void readEEPROMtoSerial()
{
byte value;
float fpvalue;
int intValue;
Serial.println("");
Serial.println("*****************************************");
Serial.println("* System Parameters *");
Serial.println("*****************************************");
EEPROM.get(0, intValue);
Serial.print("a: FLBThresholdPWM");
Serial.print("\t");
Serial.print(intValue, DEC);
Serial.print("/400 (");
Serial.print(intValue/4, DEC);
Serial.println("%)");
value = EEPROM.read(2);
Serial.print("b: debounceCount");
Serial.print("\t");
Serial.println(value, DEC);
EEPROM.get(3, intValue);
Serial.print("c: pumpMinPWM");
Serial.print("\t\t");
Serial.println(intValue, DEC);
Serial.print("/400 (");
Serial.print(intValue/4, DEC);
Serial.println("%)");
EEPROM.get(5, intValue);
Serial.print("d: pumpMaxPWM");
Serial.print("\t\t");
Serial.println(intValue, DEC);
Serial.print("/400 (");
Serial.print(intValue/4, DEC);
Serial.println("%)");
EEPROM.get(8 , fpvalue);
Serial.print("e: mlPerFlowMeterPulse");
Serial.print("\t");
Serial.println(fpvalue, 3);
EEPROM.get(12, fpvalue);
Serial.print("f: unionThreshold");
Serial.print("\t");
Serial.println(fpvalue, 2);
EEPROM.get(16, fpvalue);
Serial.print("g: Kpp");
Serial.print("\t\t\t");
Serial.println(fpvalue, 4);
EEPROM.get(20, fpvalue);
Serial.print("h: Kpi");
Serial.print("\t\t\t");
Serial.println(fpvalue, 4);
EEPROM.get(24, fpvalue);
Serial.print("i: Kpd");
Serial.print("\t\t\t");
Serial.println(fpvalue, 4);
EEPROM.get(28, fpvalue);
Serial.print("j: Kfp");
Serial.print("\t\t\t");
Serial.println(fpvalue, 4);
EEPROM.get(32, fpvalue);
Serial.print("k: Kfi");
Serial.print("\t\t\t");
Serial.println(fpvalue, 4);
EEPROM.get(36, fpvalue);
Serial.print("l. Kfd");
Serial.print("\t\t\t");
Serial.println(fpvalue, 4);
EEPROM.get(41, intValue);
Serial.print("m: slayerMainPWM");
Serial.print("\t");
Serial.println(intValue, DEC);
Serial.print("/400 (");
Serial.print(intValue/4, DEC);
Serial.println("%)");
value = EEPROM.read(43);
Serial.print("n: slayerPIPeriod");
Serial.print("\t");
Serial.println(value, DEC);
value = EEPROM.read(40);
Serial.print("slayerPIFlowRate");
Serial.print("\t");
Serial.println(value, DEC);
Serial.print("/400 (");
Serial.print(value/4, DEC);
Serial.println("%)");
value = EEPROM.read(44);
Serial.print("slayerMaxPWM");
Serial.print("\t");
Serial.println(value, DEC);
Serial.print("/400 (");
Serial.print(value/4, DEC);
Serial.println("%)");
Serial.println("*****************************************");
Serial.println("");
}
void readProfilestoSerial()
{
byte value;
int PWMProfileSize = EEPROM.read(97);
int pressureProfileSize = EEPROM.read(98);
int flowProfileSize = EEPROM.read(99);
int shiftAddr = 100; // Start reading arrays at 10th byte (should match similar decleration at storeProfilesinEEPROM()
int shiftPP = shiftAddr + PWMProfileSize;
int shiftFP = shiftPP + pressureProfileSize;
Serial.print("Sizeof PWM Profile");
Serial.print("\t");
Serial.print(PWMProfileSize, DEC);
Serial.println();
Serial.print("Sizeof Pressure Profile");
Serial.print("\t");
Serial.print(pressureProfileSize, DEC);
Serial.println();
Serial.print("Sizeof Flow Profile");
Serial.print("\t");
Serial.print(flowProfileSize, DEC);
Serial.println();
Serial.println("No. PWM Pressure Flow");
for (int addr = 0; addr < PWMProfileSize; addr++)
{
Serial.print(addr);
Serial.print("\t");
value = EEPROM.read(addr + shiftAddr);
Serial.print(value, DEC);
Serial.print("\t");
value = EEPROM.read(addr + shiftPP);
Serial.print(value, DEC);
Serial.print("\t");
EEPROM.read(addr + shiftFP);
Serial.print(value, DEC);
Serial.println();
}
/* for (int i = 100; i < 301; i++)
{
value = EEPROM.read(i);
Serial.print("PWM");
Serial.print("\t");
Serial.print(i);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();
}
for (int i = 302 ; i < 503; i++)
{
value = EEPROM.read(i);
Serial.print("Pressure");
Serial.print("\t");
Serial.print(i);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();
}
for (int i = 504; i < 705; i++)
{
value = EEPROM.read(i);
Serial.print("Flow");
Serial.print("\t");
Serial.print(i);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();
}
*/
}