forked from naubiomech/ExoCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calibrate_and_Read_Sensors.ino
133 lines (104 loc) · 4.29 KB
/
Calibrate_and_Read_Sensors.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
// Calibrate the torque sensors, FSR sensors and get the torque and FSR voltage
void torque_calibration()
{
long torque_calibration_value_time = millis();
int torq_cal_count = 0;
left_leg->torque_calibration_value = 0;
right_leg->torque_calibration_value = 0;
while (millis() - torque_calibration_value_time < 1000)
{ //Calibrates the LL for a total time of 1 second,
left_leg->torque_calibration_value += analogRead(TORQUE_SENSOR_LEFT_ANKLE_PIN) * (3.3 / 4096); //Sums the torque read in and sums it with all previous red values
right_leg->torque_calibration_value += analogRead(TORQUE_SENSOR_RIGHT_ANKLE_PIN) * (3.3 / 4096);
torq_cal_count ++; //Increments count
}
left_leg->torque_calibration_value = left_leg->torque_calibration_value / torq_cal_count; // Averages torque over a second
right_leg->torque_calibration_value = right_leg->torque_calibration_value / torq_cal_count; // Averages torque over a second
// Serial.println(left_leg->torque_calibration_value);
// Serial.println(right_leg->torque_calibration_value);
}
void FSR_calibration()
{
if (FSR_FIRST_Cycle) {
FSR_FIRST_Cycle = 0;
startTime = millis();
// Serial.println("First time");
right_leg->Curr_Combined = 0;
left_leg->Curr_Combined = 0;
right_leg->fsr_Combined_peak_ref = 0;
left_leg->fsr_Combined_peak_ref = 0;
left_leg->fsr_Toe_peak_ref = 0;
right_leg->fsr_Toe_peak_ref = 0;
left_leg->fsr_Heel_peak_ref = 0;
right_leg->fsr_Heel_peak_ref = 0;
}
if (millis() - startTime < 5000)
{
left_leg->Curr_Toe = fsr(left_leg->fsr_sense_Toe);
right_leg->Curr_Toe = fsr(right_leg->fsr_sense_Toe);
left_leg->Curr_Heel = fsr(left_leg->fsr_sense_Heel);
right_leg->Curr_Heel = fsr(right_leg->fsr_sense_Heel);
left_leg->Curr_Combined = left_leg->Curr_Toe + left_leg->Curr_Heel;
right_leg->Curr_Combined = right_leg->Curr_Toe + right_leg->Curr_Heel;
if (left_leg->Curr_Combined > left_leg->fsr_Combined_peak_ref)
{
left_leg->fsr_Combined_peak_ref = left_leg->Curr_Combined;
}
if (right_leg->Curr_Combined > right_leg->fsr_Combined_peak_ref)
{
right_leg->fsr_Combined_peak_ref = right_leg->Curr_Combined;
}
// Toe
if (left_leg->Curr_Toe > left_leg->fsr_Toe_peak_ref)
{
left_leg->fsr_Toe_peak_ref = left_leg->Curr_Toe;
}
if (right_leg->Curr_Toe > right_leg->fsr_Toe_peak_ref)
{
right_leg->fsr_Toe_peak_ref = right_leg->Curr_Toe;
}
// Heel
if (left_leg->Curr_Heel > left_leg->fsr_Heel_peak_ref)
{
left_leg->fsr_Heel_peak_ref = left_leg->Curr_Heel;
}
if (right_leg->Curr_Heel > right_leg->fsr_Heel_peak_ref)
{
right_leg->fsr_Heel_peak_ref = right_leg->Curr_Heel;
}
}
else {
FSR_FIRST_Cycle = 1;
FSR_CAL_FLAG = 0;
}
}
double get_torq(Leg* leg) {
double Torq = 56.5 / (2.1) * (analogRead(leg->torque_sensor_ankle_pin) * (3.3 / 4096) - leg->torque_calibration_value);
return -Torq; //neg is here for right leg, returns the torque value of the right leg (Newton-Meters)
}
double get_LL_torq()
{ //flexion is positive 8.10.16, gets the torque of the right leg
return get_torq(left_leg);
}
double get_RL_torq()
{ //flexion is positive 8.10.16, gets the torque of the right leg
return get_torq(right_leg);
}
/*FSR Code
This code is very basic but is kept as an outside function for clarity. The FSR readings are used to control state based actions based on the part of the gait cycle the patient
is in.
*/
double fsr(const unsigned int pin) {
//using voltage divider: 3.3 V -- >FSR<-- Vo -- >R< (1000) -- ground
//Vo from analog read: 3.3* analog read/ max read (4096) || I = Vo/R || FSR resistance = (3.3V - Vo)/I
double Vo = 10 * 3.3 * analogRead(pin) / 4096; //ZL Added in the 10* to scale the output
if ( FSR_Sensors_type == 10) {
// This to return the force instead of the Voltage
Vo = max(0, Vo); // add the max cause cannot be negative force
}
else {
if (FSR_Sensors_type == 40)
// This to return the force instead of the Voltage
Vo = max(0, p[0] * pow(Vo, 3) + p[1] * pow(Vo, 2) + p[2] * Vo + p[3]); // add the max cause cannot be negative force
}
return Vo;
}