forked from naubiomech/ExoCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead_and_write_EEPROM.ino
234 lines (205 loc) · 5.69 KB
/
Read_and_write_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
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
// Functions to check the memory values and to read them
int check_torque_bias(int address_torque_l)
{
byte value = EEPROM.read(address_torque_l);
if (char(value) == 'y')
{
return 1;
}
return 0;
};
int write_torque_bias(int address_torque_l, double torque_adj)
{
EEPROM.put(address_torque_l, 'y');
address_torque_l++;
EEPROM.put(address_torque_l, torque_adj);
return 1;
}
double read_torque_bias(int address_torque_l)
{
address_torque_l += 1;
double val_t = 0;
byte array_values[8];
byte* p_array = array_values;
for (int i = 0; i < sizeof(double); i++)
{
*(p_array + i) = EEPROM.read(address_torque_l + i);
}
memcpy(&val_t, &array_values, 8);
return val_t;
}
int clean_torque_bias(int address_torque_l)
{
for ( int i = address_torque_l ; i < (sizeof(double) + sizeof(char) + address_torque_l) ; i++ )
{
EEPROM.write(i, 0);
}
return 1;
}
int check_FSR_values(int address_FSR_l)
{
byte value = EEPROM.read(address_FSR_l);
if (char(value) == 'y') {
return 1;
}
return 0;
}
int write_FSR_values(int address_FSR_l, double FSR_adj)
{
EEPROM.put(address_FSR_l, 'y');
address_FSR_l++;
EEPROM.put(address_FSR_l, FSR_adj);
return 1;
}
double read_FSR_values(int address_FSR_l)
{
address_FSR_l += 1;
double val_t = 0;
byte array_values[8];
byte* p_array = array_values;
for (int i = 0; i < sizeof(double); i++)
{
*(p_array + i) = EEPROM.read(address_FSR_l + i);
}
memcpy(&val_t, &array_values, 8);
return val_t;
}
int clean_FSR_values(int address_FSR_l)
{
for ( int i = address_FSR_l ; i < (sizeof(double) + sizeof(char) + address_FSR_l) ; i++ )
{
EEPROM.write(i, 0);
}
return 1;
}
// wirte and read values in EEPROM also per KF, smoothing ,PID and FSR params
// Since it is really big update I would suggest to do it before the trial not in the middle
int write_EXP_parameters(int address_params_l)
{ Serial.println();
EEPROM.put(address_params_l, 'y');
address_params_l++;
EEPROM.put(address_params_l, left_leg->KF);
address_params_l += 8;
Serial.println(left_leg->KF);
EEPROM.put(address_params_l, right_leg->KF);
address_params_l += 8;
Serial.println(right_leg->KF);
EEPROM.put(address_params_l, N1);
address_params_l += 8;
Serial.println(N1);
EEPROM.put(address_params_l, N2);
address_params_l += 8;
Serial.println(N2);
EEPROM.put(address_params_l, N3);
address_params_l += 8;
Serial.println(N3);
EEPROM.put(address_params_l, left_leg->kp);
address_params_l += 8;
EEPROM.put(address_params_l, left_leg->kd);
address_params_l += 8;
EEPROM.put(address_params_l, left_leg->ki);
address_params_l += 8;
EEPROM.put(address_params_l, right_leg->kp);
address_params_l += 8;
EEPROM.put(address_params_l, right_leg->kd);
address_params_l += 8;
EEPROM.put(address_params_l, right_leg->ki);
address_params_l += 8;
EEPROM.put(address_params_l, left_leg->fsr_percent_thresh_Toe);
address_params_l += 8;
EEPROM.put(address_params_l, right_leg->fsr_percent_thresh_Toe);
address_params_l += 8;
Serial.println("Completed Saving EXP Parameters");
return 1;
}
int check_EXP_parameters(int address_params_l)
{
// Serial.print("Checking at pos ");
// Serial.println(address_params_l);
byte value = EEPROM.read(address_params_l);
Serial.println(char(value));
if (char(value) == 'y') {
Serial.println(" EXP parameters are present in memory ");
return 1;
}
Serial.println(" EXP parameters are not present in memory ");
return 0;
}
double read_param(int address_l)
{
double val_t = 0;
byte array_values[8];
byte* p_array = array_values;
for (int i = 0; i < sizeof(double); i++)
{
*(p_array + i) = EEPROM.read(address_l + i);
}
memcpy(&val_t, &array_values, 8);
return val_t;
}
int read_all_params(int address_params_l) {
address_params_l++;
left_leg->KF = read_param(address_params_l);
address_params_l += 8;
right_leg->KF = read_param(address_params_l);
address_params_l += 8;
N1 = read_param(address_params_l);
address_params_l += 8;
N2 = read_param(address_params_l);
address_params_l += 8;
N3 = read_param(address_params_l);
address_params_l += 8;
left_leg->N1 = N1;
left_leg->N2 = N2;
left_leg->N3 = N3;
right_leg->N1 = N1;
right_leg->N2 = N2;
right_leg->N3 = N3;
left_leg->kp = read_param(address_params_l);
address_params_l += 8;
left_leg->kd = read_param(address_params_l);
address_params_l += 8;
left_leg->ki = read_param(address_params_l);
address_params_l += 8;
right_leg->kp = read_param(address_params_l);
address_params_l += 8;
right_leg->kd = read_param(address_params_l);
address_params_l += 8;
right_leg->ki = read_param(address_params_l);
address_params_l += 8;
left_leg->fsr_percent_thresh_Toe = read_param(address_params_l);
address_params_l += 8;
right_leg->fsr_percent_thresh_Toe = read_param(address_params_l);
address_params_l += 8;
left_leg->p_steps->fsr_percent_thresh_Toe = left_leg->fsr_percent_thresh_Toe;
right_leg->p_steps->fsr_percent_thresh_Toe = right_leg->fsr_percent_thresh_Toe;
return 1;
}
int clean_EXP_Parameters(int address_params_l)
{
for ( int i = address_params_l ; i < (sizeof(double) * 13 + sizeof(char) + address_params_l) ; i++ )
{
EEPROM.write(i, 0);
}
return 1;
}
int write_baseline(int address_baseline_l, double baseline_val)
{
EEPROM.put(address_baseline_l, 'y');
address_baseline_l++;
EEPROM.put(address_baseline_l, baseline_val);
return 1;
}
double read_baseline(int address_baseline_l)
{
address_baseline_l += 1;
double val_t = 0;
byte array_values[8];
byte* p_array = array_values;
for (int i = 0; i < sizeof(double); i++)
{
*(p_array + i) = EEPROM.read(address_baseline_l + i);
}
memcpy(&val_t, &array_values, 8);
return val_t;
}