-
Notifications
You must be signed in to change notification settings - Fork 1
/
Control_Adjustment.ino
290 lines (203 loc) · 11.5 KB
/
Control_Adjustment.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
// in this file there are the functions that modify the control strategy both in the strategy and in the shaping factors (N3) when required.
// take baseline for some of the controls during the plantarflexion state , i.e. 3
int take_baseline(int R_state_l, int R_state_old_l, steps* p_steps_l, int* p_flag_take_baseline_l) {
if ((R_state_l == 3) && (R_state_old_l == 1 || R_state_old_l == 2)) // I am in plantarflexion
{
// update the voltage peak
if (p_steps_l->curr_voltage > p_steps_l->peak)
p_steps_l->peak = p_steps_l->curr_voltage;
if (p_steps_l->flag_start_plant == false) // if it is true it means you started the step. Here I inizialize the parameters for speed adaption.
{
p_steps_l->plant_time = millis(); // start the plantarflexion
p_steps_l->dorsi_time = millis() - (p_steps_l->dorsi_time); // calculate the dorsiflexion that has just finished
if (p_steps_l->dorsi_time <= step_time_length / 4) // if <50ms probably it is noise
{
p_steps_l->peak = 0;
p_steps_l->flag_start_plant = false;
return 0;
} else {
p_steps_l->flag_start_plant = true; // Parameters inizialized Start a step
}
}
}
if (p_steps_l->flag_start_plant) {
// If a step has started i.e. the states have passed from 1 or 2 to 3
// if you transit from 3 to 1 plantar flexion is completed and start dorsiflexion
if ((R_state_old_l == 3) && (R_state_l == 1 || R_state_l == 2)) {
// start dorsiflexion
p_steps_l->dorsi_time = millis();
// calculate plantarflexion
p_steps_l->plant_time = millis() - (p_steps_l->plant_time);
if (p_steps_l->plant_time <= step_time_length)
{
p_steps_l->flag_start_plant = false;
return 0;
} else {
p_steps_l->flag_start_plant = false; // you have provided one plant
p_steps_l->count_plant_base++; // you have accomplished a step
}
if ((p_steps_l->count_plant_base) >= 2) { // avoid the first step just to be sure
// this is the time window of the filter for the dorsiflexion
if (((p_steps_l->count_plant_base) - 2) >= n_step_baseline) {
p_steps_l->dorsi_mean = 0;
p_steps_l->plant_mean = 0;
p_steps_l->plant_peak_mean_temp = 0;
for (int i = 0; i < n_step_baseline - 1; i++)
{
p_steps_l->four_step_dorsi_time[i] = p_steps_l->four_step_dorsi_time[i + 1];
p_steps_l->dorsi_mean += p_steps_l->four_step_dorsi_time[i];
p_steps_l->four_step_plant_time[i] = p_steps_l->four_step_plant_time[i + 1];
p_steps_l->plant_mean += p_steps_l->four_step_plant_time[i];
p_steps_l->four_step_plant_peak[i] = p_steps_l->four_step_plant_peak[i + 1];
p_steps_l->plant_peak_mean_temp += p_steps_l->four_step_plant_peak[i];
}
p_steps_l->four_step_dorsi_time[n_step_baseline - 1] = p_steps_l->dorsi_time;
p_steps_l->dorsi_mean += p_steps_l->dorsi_time;
p_steps_l->four_step_plant_time[n_step_baseline - 1] = p_steps_l->plant_time;
p_steps_l->plant_mean += p_steps_l->plant_time;
p_steps_l->four_step_plant_peak[n_step_baseline - 1] = p_steps_l->peak;
p_steps_l->plant_peak_mean_temp += p_steps_l->peak;
p_steps_l->dorsi_mean = (p_steps_l->dorsi_mean) / n_step_baseline;
p_steps_l->plant_mean = p_steps_l->plant_mean / n_step_baseline;
p_steps_l->plant_peak_mean_temp = 1.0 * (p_steps_l->plant_peak_mean_temp) / n_step_baseline; //Gain (1.0) was 0.9 2/25/2019 GO
//HERE
}
else {
p_steps_l->four_step_dorsi_time[p_steps_l->count_plant_base - 2] = p_steps_l->dorsi_time;
p_steps_l->four_step_plant_time[p_steps_l->count_plant_base - 2] = p_steps_l->plant_time;
p_steps_l->four_step_plant_peak[p_steps_l->count_plant_base - 2] = p_steps_l->peak;
for (int i = 0; i < n_step_baseline; i++) {
}
}
if (((p_steps_l->count_plant_base) - 2) >= n_step_baseline) {
}
if (((p_steps_l->count_plant_base) - 2) >= n_step_baseline) {
(p_steps_l->count_plant_base) = 0;
*p_flag_take_baseline_l = 0;
return (p_steps_l->count_plant_base);
} // return 1 activate a flag that stops the calc of the baseline
}// end if count_plant>2
}//end dorsiflexion
}// end start_step
if (((R_state_l == 1) || (R_state_l == 2)) && R_state_old_l == 3)
p_steps_l->peak = 0;
}// end take_baseline
//------------------------------------------------------------------------------
double Control_Adjustment(Leg* leg, int R_state_l, int R_state_old_l, steps* p_steps_l, double N3_l, double New_PID_Setpoint_l, double* p_Setpoint_Ankle_l, double * p_Setpoint_Ankle_Pctrl_l, int Control_Mode_l, double prop_gain_l, double taking_baseline_l, double *p_FSR_Ratio, double* p_Max_FSR_Ratio) {
// Control Mode 2: Balance control
// Control Mode 3: Joint Moment control, the torque is a percentage of the extimated Ankle moment. The mapping function that estimated the ankle moment use a ratio (p_FSR_Ratio) which depends on the current force of pressure
// and the baseline value. The baseline value can be updated several times also during the execution of the task
//otherwise Control Mode = 100 implies the classic bang bang whose shaping is based on N3
// Despite control mode 2 and 3 do not uses the N3 the function still returns a number associated to N3 which is not used.
if (taking_baseline_l) { // if I am taking the baseline adapt some parameters for the controls
//--------------------------------
if ((R_state_l == 3) && (R_state_old_l == 1 || R_state_old_l == 2))
{
if (Control_Mode_l == 3) { // JOINT MOMENT CONTROL also known as pivot proportional control while taking the baseline
*p_FSR_Ratio = fabs(p_steps_l->curr_voltage / p_steps_l->plant_peak_mean);
if (*p_FSR_Ratio > (*p_Max_FSR_Ratio))
(*p_Max_FSR_Ratio) = *p_FSR_Ratio; // update the max fsr Ratio
// while updating the ratio value still continue to provide the control
if ((p_steps_l->Setpoint ) > 0) { //depending on the leg the sign changes
*p_Setpoint_Ankle_Pctrl_l = max(Min_Prop, (p_steps_l->Setpoint ) * (p_prop[0] * pow(*p_FSR_Ratio, 2) + p_prop[1] * (*p_FSR_Ratio) + p_prop[2]) / (p_prop[0] + p_prop[1] + p_prop[2]));
*p_Setpoint_Ankle_Pctrl_l = min(Max_Prop, *p_Setpoint_Ankle_Pctrl_l);
}
else if ((p_steps_l->Setpoint ) < 0) {
*p_Setpoint_Ankle_Pctrl_l = max(-Max_Prop, (p_steps_l->Setpoint ) * (p_prop[0] * pow(*p_FSR_Ratio, 2) + p_prop[1] * (*p_FSR_Ratio) + p_prop[2]) / (p_prop[0] + p_prop[1] + p_prop[2]));
*p_Setpoint_Ankle_Pctrl_l = min(Min_Prop, *p_Setpoint_Ankle_Pctrl_l);
} else {
*p_Setpoint_Ankle_Pctrl_l = 0;
}
}
}
return N3_l; //return the previous N3 value whis is not used
}
if (taking_baseline_l == 0 && p_steps_l->plant_peak_mean_temp != p_steps_l->plant_peak_mean) {
p_steps_l->plant_peak_mean = p_steps_l->plant_peak_mean_temp;
}
// if you transit from state 1 to state 3 dorsiflexion is completed and start plantarflexion
if ((R_state_l == 3) && (R_state_old_l == 1 || R_state_old_l == 2))
{
// update the voltage peak to update torque in case of Bang Bang ctrl
if (p_steps_l->curr_voltage > p_steps_l->peak)
p_steps_l->peak = p_steps_l->curr_voltage;
*p_FSR_Ratio = fabs(p_steps_l->curr_voltage / p_steps_l->plant_peak_mean);
if (*p_FSR_Ratio > (*p_Max_FSR_Ratio))
(*p_Max_FSR_Ratio) = *p_FSR_Ratio;
if (Control_Mode_l == 2) { // Balance control
*p_Setpoint_Ankle_Pctrl_l = Balance_Torque_ref_based_on_Steady(leg);
return N3_l; // No modification in the shaping which is disabled
} else if (Control_Mode_l == 3) {
// JOINT MOMENT CONTROL also known as pivot proportional control
if ((p_steps_l->Setpoint ) > 0) {
*p_Setpoint_Ankle_Pctrl_l = max(Min_Prop, (p_steps_l->Setpoint ) * (p_prop[0] * pow(*p_FSR_Ratio, 2) + p_prop[1] * (*p_FSR_Ratio) + p_prop[2]) / (p_prop[0] + p_prop[1] + p_prop[2])); // the difference here is that we do it as a function of the FSR calibration
*p_Setpoint_Ankle_Pctrl_l = min(Max_Prop, *p_Setpoint_Ankle_Pctrl_l);
}
else if ((p_steps_l->Setpoint ) < 0) {
*p_Setpoint_Ankle_Pctrl_l = max(-Max_Prop, (p_steps_l->Setpoint ) * (p_prop[0] * pow(*p_FSR_Ratio, 2) + p_prop[1] * (*p_FSR_Ratio) + p_prop[2]) / (p_prop[0] + p_prop[1] + p_prop[2])); // the difference here is that we do it as a function of the FSR calibration
*p_Setpoint_Ankle_Pctrl_l = min(Min_Prop, *p_Setpoint_Ankle_Pctrl_l);
} else {
*p_Setpoint_Ankle_Pctrl_l = 0;
}
return N3_l; // No modification in the shaping function which is disabled
}
// Otherwise we need to calculate the time
// Parameters for speed adaption
if (p_steps_l->flag_start_plant == false) // if it is true it means you started the step. Here I inizialize the parameters for speed adaption.
{
p_steps_l->plant_time = millis(); // start the plantarflexion
p_steps_l->dorsi_time = millis() - (p_steps_l->dorsi_time); // calculate the dorsiflexion that has just finished
if (p_steps_l->dorsi_time <= step_time_length / 4) // if <50ms probably it is noise
{
p_steps_l->peak = 0;
p_steps_l->flag_start_plant = false;
// Serial.println(" SPD ADJ dorsi time too short ");
return N3_l;
}
p_steps_l->flag_start_plant = true; // Parameters inizialized Start a step
}
}// end if you enter in state 3 from state 2 or 1
// Hence here I am in state 2 or 1
if (p_steps_l->flag_start_plant) { // If a step has started i.e. the states have passed from 1 or 2 to 3
// if you transit from 3 to 1 plantar flexion is completed and start dorsiflexion
if ((R_state_old_l == 3) && (R_state_l == 1 || R_state_l == 2)) {
p_steps_l->count_plant++; // you have accomplished a step
// start dorsiflexion
p_steps_l->dorsi_time = millis();
// calculate plantarflexion
p_steps_l->plant_time = millis() - (p_steps_l->plant_time);
if (p_steps_l->plant_time <= step_time_length)
{
p_steps_l->flag_start_plant = false;
Serial.println(" SPD ADJ plant time too short ");
return N3_l;
}
p_steps_l->flag_start_plant = false; // you have provided one step
if (p_steps_l->count_plant >= 2) {
// this is the time window of the filter for the plantarflexion
if (p_steps_l->count_plant - 2 >= n_step_baseline) {
for (int i = 0; i < n_step_baseline - 1; i++)
{
p_steps_l->four_step_plant_time[i] = p_steps_l->four_step_plant_time[i + 1];
}
p_steps_l->four_step_plant_time[n_step_baseline - 1] = p_steps_l->plant_time;
}
else {
p_steps_l->four_step_plant_time[p_steps_l->count_plant - 2] = p_steps_l->plant_time;
}
// Update mean value
p_steps_l->plant_mean = 0;
for (int i = 0; i < n_step_baseline; i++) {
p_steps_l->plant_mean += p_steps_l->four_step_plant_time[i];
}
p_steps_l->plant_mean = p_steps_l->plant_mean / n_step_baseline;
}
}//end if R old 3 i.e. finish plantarflexion
}// end if flag_start_plant
// During the all dorsiflexion set the voltage peak to 0, probably we just need to do it one time
if (((R_state_l == 1) || (R_state_l == 2)) && R_state_old_l == 3) {
p_steps_l->peak = 0;
p_Max_FSR_Ratio = 0;
}
return N3_l;
}