![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mag niet van hendrik D:
Dependencies: mbed MatrixMath QEI HIDScope Matrix biquadFilter MODSERIAL FastPWM
main.cpp@24:a9ec9b836fd9, 2019-10-21 (annotated)
- Committer:
- Hendrikvg
- Date:
- Mon Oct 21 15:11:14 2019 +0000
- Revision:
- 24:a9ec9b836fd9
- Parent:
- 23:78898ddfb103
- Child:
- 25:832b26afbe0b
EMG calibratie error vrij, nog testen!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hendrikvg | 17:cacf9e75eda7 | 1 | #include "QEI.h" |
Hendrikvg | 15:80b3ac2b8448 | 2 | #include "mbed.h" |
Hendrikvg | 20:ac1b4ffa3323 | 3 | #include "BiQuad.h" |
Hendrikvg | 14:20f11bb58244 | 4 | #include "FastPWM.h" |
Hendrikvg | 17:cacf9e75eda7 | 5 | #include "HIDScope.h" |
Hendrikvg | 16:40183eeadb6d | 6 | #include "MODSERIAL.h" |
Hendrikvg | 9:12b9865e7373 | 7 | |
Hendrikvg | 21:394a7a1deb73 | 8 | // Pins |
Hendrikvg | 21:394a7a1deb73 | 9 | MODSERIAL pc(USBTX, USBRX); |
Hendrikvg | 21:394a7a1deb73 | 10 | InterruptIn stepresponse(D0); |
Hendrikvg | 24:a9ec9b836fd9 | 11 | |
Hendrikvg | 21:394a7a1deb73 | 12 | FastPWM PWM_motor_1(D6); |
Hendrikvg | 23:78898ddfb103 | 13 | FastPWM PWM_motor_2(D5); |
Hendrikvg | 24:a9ec9b836fd9 | 14 | |
Hendrikvg | 20:ac1b4ffa3323 | 15 | DigitalOut direction_motor_1(D7); |
Hendrikvg | 23:78898ddfb103 | 16 | DigitalOut direction_motor_2(D4); |
Hendrikvg | 24:a9ec9b836fd9 | 17 | DigitalOut led_red(LED1); |
Hendrikvg | 24:a9ec9b836fd9 | 18 | DigitalOut led_green(LED2); |
Hendrikvg | 24:a9ec9b836fd9 | 19 | DigitalOut led_blue(LED3); |
Hendrikvg | 24:a9ec9b836fd9 | 20 | |
Hendrikvg | 24:a9ec9b836fd9 | 21 | AnalogIn emg_bl( A0 ); |
Hendrikvg | 24:a9ec9b836fd9 | 22 | AnalogIn emg_br( A1 ); |
Hendrikvg | 24:a9ec9b836fd9 | 23 | AnalogIn emg_leg( A2 ); |
Hendrikvg | 24:a9ec9b836fd9 | 24 | |
Hendrikvg | 21:394a7a1deb73 | 25 | InterruptIn button_1(SW2); |
Hendrikvg | 21:394a7a1deb73 | 26 | InterruptIn button_2(SW3); |
Hendrikvg | 17:cacf9e75eda7 | 27 | |
Hendrikvg | 24:a9ec9b836fd9 | 28 | |
Hendrikvg | 15:80b3ac2b8448 | 29 | // variables |
Hendrikvg | 21:394a7a1deb73 | 30 | Ticker TickerStateMachine; |
Hendrikvg | 23:78898ddfb103 | 31 | Ticker motor_control; |
Hendrikvg | 23:78898ddfb103 | 32 | Ticker write_scope; |
Hendrikvg | 21:394a7a1deb73 | 33 | Timer sinus_time; |
Hendrikvg | 24:a9ec9b836fd9 | 34 | Timeout rest_timeout; |
Hendrikvg | 24:a9ec9b836fd9 | 35 | Timeout mvc_timeout; |
Hendrikvg | 21:394a7a1deb73 | 36 | enum states {start, motor_calibration, demo_mode, emg_calibration, vertical_movement, horizontal_movement}; |
Hendrikvg | 21:394a7a1deb73 | 37 | states CurrentState = start; |
Hendrikvg | 21:394a7a1deb73 | 38 | bool StateChanged = true; |
Hendrikvg | 24:a9ec9b836fd9 | 39 | enum substates {rest_biceps_left, mvc_biceps_left, rest_biceps_right, mvc_biceps_right, rest_biceps_leg, mvc_biceps_leg}; |
Hendrikvg | 24:a9ec9b836fd9 | 40 | substates CurrentSubstate = rest_biceps_left; |
Hendrikvg | 24:a9ec9b836fd9 | 41 | bool SubstateChanged = true; |
Hendrikvg | 24:a9ec9b836fd9 | 42 | bool pressed_1 = false; |
Hendrikvg | 24:a9ec9b836fd9 | 43 | bool pressed_2 = false; |
Hendrikvg | 21:394a7a1deb73 | 44 | HIDScope scope(3); |
Hendrikvg | 23:78898ddfb103 | 45 | QEI encoder_1(D10,D11,NC,8400,QEI::X4_ENCODING); |
Hendrikvg | 23:78898ddfb103 | 46 | QEI encoder_2(D12,D13,NC,8400,QEI::X4_ENCODING); |
Hendrikvg | 22:6cc93216b323 | 47 | volatile double theta_1; |
Hendrikvg | 23:78898ddfb103 | 48 | //volatile float theta_error_1; |
Hendrikvg | 22:6cc93216b323 | 49 | volatile float theta_reference_1; |
Hendrikvg | 23:78898ddfb103 | 50 | volatile double theta_2; |
Hendrikvg | 23:78898ddfb103 | 51 | //volatile float theta_error_2; |
Hendrikvg | 23:78898ddfb103 | 52 | volatile float theta_reference_2; |
Hendrikvg | 21:394a7a1deb73 | 53 | float Ts = 0.001; |
Hendrikvg | 23:78898ddfb103 | 54 | float Kp; |
Hendrikvg | 23:78898ddfb103 | 55 | float Ki; |
Hendrikvg | 23:78898ddfb103 | 56 | float Kd; |
Hendrikvg | 20:ac1b4ffa3323 | 57 | |
Hendrikvg | 24:a9ec9b836fd9 | 58 | BiQuadChain highnotch; |
Hendrikvg | 24:a9ec9b836fd9 | 59 | BiQuadChain low; |
Hendrikvg | 24:a9ec9b836fd9 | 60 | BiQuad Lowpass1 (3.73938e-07, 7.47876e-07, 3.73938e-07, -1.90886e+00, 9.11279e-01); |
Hendrikvg | 24:a9ec9b836fd9 | 61 | BiQuad Lowpass2 (1.00000e+00, 2.00000e+00, 1.00000e+00, -1.95979e+00, 9.62270e-01); |
Hendrikvg | 24:a9ec9b836fd9 | 62 | BiQuad Highpass (9.69531e-01, -9.69531e-01, 0.00000e+00, -9.39063e-01, 0.00000e+00 ); |
Hendrikvg | 24:a9ec9b836fd9 | 63 | BiQuad Notch (9.98432e-01, -1.89913e+00, 9.98432e-01, -1.89913e+00, 9.96863e-01); |
Hendrikvg | 24:a9ec9b836fd9 | 64 | int n = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 65 | |
Hendrikvg | 24:a9ec9b836fd9 | 66 | double emgFiltered_bl; |
Hendrikvg | 24:a9ec9b836fd9 | 67 | double emgFiltered_br; |
Hendrikvg | 24:a9ec9b836fd9 | 68 | double emgFiltered_leg; |
Hendrikvg | 24:a9ec9b836fd9 | 69 | double emg; |
Hendrikvg | 24:a9ec9b836fd9 | 70 | double xmvc_value = 1e-11; |
Hendrikvg | 24:a9ec9b836fd9 | 71 | |
Hendrikvg | 24:a9ec9b836fd9 | 72 | int muscle; |
Hendrikvg | 24:a9ec9b836fd9 | 73 | float sum = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 74 | float rest_value; |
Hendrikvg | 24:a9ec9b836fd9 | 75 | float rest_value_bl; |
Hendrikvg | 24:a9ec9b836fd9 | 76 | float rest_value_br; |
Hendrikvg | 24:a9ec9b836fd9 | 77 | float rest_value_leg; |
Hendrikvg | 24:a9ec9b836fd9 | 78 | |
Hendrikvg | 24:a9ec9b836fd9 | 79 | float mvc_value_bl; |
Hendrikvg | 24:a9ec9b836fd9 | 80 | float mvc_value_br; |
Hendrikvg | 24:a9ec9b836fd9 | 81 | float mvc_value_leg; |
Hendrikvg | 24:a9ec9b836fd9 | 82 | |
Hendrikvg | 21:394a7a1deb73 | 83 | // functies |
Hendrikvg | 22:6cc93216b323 | 84 | float CalculateError(float theta_reference,float theta) |
Hendrikvg | 21:394a7a1deb73 | 85 | { |
Hendrikvg | 22:6cc93216b323 | 86 | float theta_error = theta_reference-theta; |
Hendrikvg | 21:394a7a1deb73 | 87 | return theta_error; |
Hendrikvg | 21:394a7a1deb73 | 88 | } |
Hendrikvg | 20:ac1b4ffa3323 | 89 | |
Hendrikvg | 23:78898ddfb103 | 90 | float Controller(float theta_error, bool motor) |
Hendrikvg | 21:394a7a1deb73 | 91 | { |
Hendrikvg | 24:a9ec9b836fd9 | 92 | if (motor == false) { |
Hendrikvg | 23:78898ddfb103 | 93 | float K = 1; |
Hendrikvg | 23:78898ddfb103 | 94 | float ti = 0.1; |
Hendrikvg | 23:78898ddfb103 | 95 | float td = 10; |
Hendrikvg | 23:78898ddfb103 | 96 | Kp = K*(1+td/ti); |
Hendrikvg | 23:78898ddfb103 | 97 | Ki = K/ti; |
Hendrikvg | 23:78898ddfb103 | 98 | Kd = K*td; |
Hendrikvg | 24:a9ec9b836fd9 | 99 | } else { |
Hendrikvg | 23:78898ddfb103 | 100 | float K = 1; |
Hendrikvg | 23:78898ddfb103 | 101 | float ti = 0.1; |
Hendrikvg | 23:78898ddfb103 | 102 | float td = 10; |
Hendrikvg | 23:78898ddfb103 | 103 | Kp = K*(1+td/ti); |
Hendrikvg | 23:78898ddfb103 | 104 | Ki = K/ti; |
Hendrikvg | 23:78898ddfb103 | 105 | Kd = K*td; |
Hendrikvg | 23:78898ddfb103 | 106 | } |
Hendrikvg | 21:394a7a1deb73 | 107 | static float error_integral = 0; |
Hendrikvg | 21:394a7a1deb73 | 108 | static float error_prev = 0; |
Hendrikvg | 21:394a7a1deb73 | 109 | static BiQuad LowPassFilter(0.0640, 0.1279, 0.0640, -1.1683, 0.4241); |
Hendrikvg | 24:a9ec9b836fd9 | 110 | |
Hendrikvg | 20:ac1b4ffa3323 | 111 | // Proportional part: |
Hendrikvg | 20:ac1b4ffa3323 | 112 | float torque_p = Kp * theta_error; |
Hendrikvg | 24:a9ec9b836fd9 | 113 | |
Hendrikvg | 20:ac1b4ffa3323 | 114 | // Integral part: |
Hendrikvg | 21:394a7a1deb73 | 115 | error_integral = error_integral + theta_error * Ts; |
Hendrikvg | 21:394a7a1deb73 | 116 | float torque_i = Ki * error_integral; |
Hendrikvg | 24:a9ec9b836fd9 | 117 | |
Hendrikvg | 20:ac1b4ffa3323 | 118 | // Derivative part: |
Hendrikvg | 21:394a7a1deb73 | 119 | float error_derivative = (theta_error - error_prev)/Ts; |
Hendrikvg | 21:394a7a1deb73 | 120 | float filtered_error_derivative = LowPassFilter.step(error_derivative); |
Hendrikvg | 21:394a7a1deb73 | 121 | float torque_d = Kd * filtered_error_derivative; |
Hendrikvg | 21:394a7a1deb73 | 122 | error_prev = theta_error; |
Hendrikvg | 24:a9ec9b836fd9 | 123 | |
Hendrikvg | 20:ac1b4ffa3323 | 124 | // Sum all parts and return it |
Hendrikvg | 21:394a7a1deb73 | 125 | float torque = torque_p + torque_i + torque_d; |
Hendrikvg | 21:394a7a1deb73 | 126 | return torque; |
Hendrikvg | 21:394a7a1deb73 | 127 | } |
Hendrikvg | 16:40183eeadb6d | 128 | |
Hendrikvg | 24:a9ec9b836fd9 | 129 | void CalculateDirectionMotor() |
Hendrikvg | 21:394a7a1deb73 | 130 | { |
Hendrikvg | 23:78898ddfb103 | 131 | direction_motor_1 = Controller(CalculateError(theta_reference_1,theta_1),0) <= 0.0f; |
Hendrikvg | 23:78898ddfb103 | 132 | direction_motor_2 = Controller(CalculateError(theta_reference_2,theta_2),1) <= 0.0f; |
Hendrikvg | 21:394a7a1deb73 | 133 | } |
Hendrikvg | 20:ac1b4ffa3323 | 134 | |
Hendrikvg | 23:78898ddfb103 | 135 | void ReadEncoder() |
Hendrikvg | 23:78898ddfb103 | 136 | { |
Hendrikvg | 23:78898ddfb103 | 137 | theta_1 = ((360.0f/64.0f)*(float)encoder_1.getPulses())/131.25f; // 360/64 voor de 64 CPR Encoder, 131.25 omdat de gear ratio 131.25:1 is. Hiermee is 1 omwenteling 360. |
Hendrikvg | 23:78898ddfb103 | 138 | theta_2 = ((360.0f/64.0f)*(float)encoder_2.getPulses())/131.25f; |
Hendrikvg | 23:78898ddfb103 | 139 | } |
Hendrikvg | 16:40183eeadb6d | 140 | |
Hendrikvg | 24:a9ec9b836fd9 | 141 | void MotorControl() |
Hendrikvg | 23:78898ddfb103 | 142 | { |
Hendrikvg | 21:394a7a1deb73 | 143 | ReadEncoder(); |
Hendrikvg | 23:78898ddfb103 | 144 | theta_reference_1 = 360*sin(0.1f*sinus_time.read()*2*3.14f); // voor test, moet weg in eindscript |
Hendrikvg | 22:6cc93216b323 | 145 | CalculateDirectionMotor(); |
Hendrikvg | 23:78898ddfb103 | 146 | PWM_motor_1.write(fabs(Controller(CalculateError(theta_reference_1,theta_1),0)/360.0f)); |
Hendrikvg | 23:78898ddfb103 | 147 | PWM_motor_2.write(fabs(Controller(CalculateError(theta_reference_2,theta_2),1)/360.0f)); |
Hendrikvg | 21:394a7a1deb73 | 148 | } |
Hendrikvg | 21:394a7a1deb73 | 149 | |
Hendrikvg | 21:394a7a1deb73 | 150 | void go_next_1() |
Hendrikvg | 21:394a7a1deb73 | 151 | { |
Hendrikvg | 24:a9ec9b836fd9 | 152 | pressed_1 = !pressed_1; |
Hendrikvg | 21:394a7a1deb73 | 153 | } |
Hendrikvg | 21:394a7a1deb73 | 154 | |
Hendrikvg | 21:394a7a1deb73 | 155 | void go_next_2() |
Hendrikvg | 21:394a7a1deb73 | 156 | { |
Hendrikvg | 24:a9ec9b836fd9 | 157 | pressed_2 = !pressed_2; |
Hendrikvg | 24:a9ec9b836fd9 | 158 | } |
Hendrikvg | 24:a9ec9b836fd9 | 159 | |
Hendrikvg | 24:a9ec9b836fd9 | 160 | float EmgCalibration(double emgFiltered, float mvc_value, float rest_value) |
Hendrikvg | 24:a9ec9b836fd9 | 161 | { |
Hendrikvg | 24:a9ec9b836fd9 | 162 | float emgCalibrated; |
Hendrikvg | 24:a9ec9b836fd9 | 163 | if (emgFiltered <= rest_value) { |
Hendrikvg | 24:a9ec9b836fd9 | 164 | emgCalibrated = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 165 | } |
Hendrikvg | 24:a9ec9b836fd9 | 166 | if (emgFiltered >= mvc_value) { |
Hendrikvg | 24:a9ec9b836fd9 | 167 | emgCalibrated = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 168 | } else { |
Hendrikvg | 24:a9ec9b836fd9 | 169 | emgCalibrated = (emgFiltered-rest_value)/(mvc_value-rest_value); |
Hendrikvg | 24:a9ec9b836fd9 | 170 | } |
Hendrikvg | 24:a9ec9b836fd9 | 171 | return emgCalibrated; |
Hendrikvg | 24:a9ec9b836fd9 | 172 | } |
Hendrikvg | 24:a9ec9b836fd9 | 173 | |
Hendrikvg | 24:a9ec9b836fd9 | 174 | void emgsample() |
Hendrikvg | 24:a9ec9b836fd9 | 175 | { |
Hendrikvg | 24:a9ec9b836fd9 | 176 | emgFiltered_bl = highnotch.step(emg_bl.read()); |
Hendrikvg | 24:a9ec9b836fd9 | 177 | emgFiltered_bl = fabs(emgFiltered_bl); |
Hendrikvg | 24:a9ec9b836fd9 | 178 | emgFiltered_bl = low.step(emgFiltered_bl); |
Hendrikvg | 24:a9ec9b836fd9 | 179 | |
Hendrikvg | 24:a9ec9b836fd9 | 180 | emgFiltered_br = highnotch.step(emg_br.read()); |
Hendrikvg | 24:a9ec9b836fd9 | 181 | emgFiltered_br = fabs(emgFiltered_br); |
Hendrikvg | 24:a9ec9b836fd9 | 182 | emgFiltered_br = low.step(emgFiltered_br); |
Hendrikvg | 24:a9ec9b836fd9 | 183 | |
Hendrikvg | 24:a9ec9b836fd9 | 184 | emgFiltered_leg = highnotch.step(emg_leg.read()); |
Hendrikvg | 24:a9ec9b836fd9 | 185 | emgFiltered_leg = fabs(emgFiltered_leg); |
Hendrikvg | 24:a9ec9b836fd9 | 186 | emgFiltered_leg = low.step(emgFiltered_leg); |
Hendrikvg | 24:a9ec9b836fd9 | 187 | } |
Hendrikvg | 24:a9ec9b836fd9 | 188 | |
Hendrikvg | 24:a9ec9b836fd9 | 189 | void rest() |
Hendrikvg | 24:a9ec9b836fd9 | 190 | { |
Hendrikvg | 24:a9ec9b836fd9 | 191 | if (muscle == 0) |
Hendrikvg | 24:a9ec9b836fd9 | 192 | { |
Hendrikvg | 24:a9ec9b836fd9 | 193 | emg = emgFiltered_bl; |
Hendrikvg | 24:a9ec9b836fd9 | 194 | } |
Hendrikvg | 24:a9ec9b836fd9 | 195 | if (muscle == 2) |
Hendrikvg | 24:a9ec9b836fd9 | 196 | { |
Hendrikvg | 24:a9ec9b836fd9 | 197 | emg = emgFiltered_br; |
Hendrikvg | 24:a9ec9b836fd9 | 198 | } |
Hendrikvg | 24:a9ec9b836fd9 | 199 | if (muscle == 4) |
Hendrikvg | 24:a9ec9b836fd9 | 200 | { |
Hendrikvg | 24:a9ec9b836fd9 | 201 | emg = emgFiltered_leg; |
Hendrikvg | 24:a9ec9b836fd9 | 202 | } |
Hendrikvg | 24:a9ec9b836fd9 | 203 | if (n < 50) |
Hendrikvg | 24:a9ec9b836fd9 | 204 | { |
Hendrikvg | 24:a9ec9b836fd9 | 205 | sum = sum + emg; |
Hendrikvg | 24:a9ec9b836fd9 | 206 | n++; |
Hendrikvg | 24:a9ec9b836fd9 | 207 | rest_value = float (sum/n); |
Hendrikvg | 24:a9ec9b836fd9 | 208 | rest_timeout.attach(rest,0.01f); |
Hendrikvg | 24:a9ec9b836fd9 | 209 | } |
Hendrikvg | 24:a9ec9b836fd9 | 210 | if (n == 50) |
Hendrikvg | 24:a9ec9b836fd9 | 211 | { |
Hendrikvg | 24:a9ec9b836fd9 | 212 | sum = sum + emg; |
Hendrikvg | 24:a9ec9b836fd9 | 213 | n++; |
Hendrikvg | 24:a9ec9b836fd9 | 214 | rest_value = float (sum/n); |
Hendrikvg | 24:a9ec9b836fd9 | 215 | n = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 216 | sum = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 217 | if (muscle == 0) |
Hendrikvg | 24:a9ec9b836fd9 | 218 | { |
Hendrikvg | 24:a9ec9b836fd9 | 219 | rest_value_bl = rest_value; |
Hendrikvg | 24:a9ec9b836fd9 | 220 | CurrentSubstate = mvc_biceps_left; |
Hendrikvg | 24:a9ec9b836fd9 | 221 | SubstateChanged = true; |
Hendrikvg | 24:a9ec9b836fd9 | 222 | led_red = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 223 | } |
Hendrikvg | 24:a9ec9b836fd9 | 224 | if (muscle == 2) |
Hendrikvg | 24:a9ec9b836fd9 | 225 | { |
Hendrikvg | 24:a9ec9b836fd9 | 226 | rest_value_br = rest_value; |
Hendrikvg | 24:a9ec9b836fd9 | 227 | CurrentSubstate = mvc_biceps_right; |
Hendrikvg | 24:a9ec9b836fd9 | 228 | SubstateChanged = true; |
Hendrikvg | 24:a9ec9b836fd9 | 229 | led_red = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 230 | } |
Hendrikvg | 24:a9ec9b836fd9 | 231 | if (muscle == 4) |
Hendrikvg | 24:a9ec9b836fd9 | 232 | { |
Hendrikvg | 24:a9ec9b836fd9 | 233 | rest_value_leg = rest_value; |
Hendrikvg | 24:a9ec9b836fd9 | 234 | CurrentSubstate = mvc_biceps_leg; |
Hendrikvg | 24:a9ec9b836fd9 | 235 | SubstateChanged = true; |
Hendrikvg | 24:a9ec9b836fd9 | 236 | led_red = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 237 | } |
Hendrikvg | 24:a9ec9b836fd9 | 238 | } |
Hendrikvg | 24:a9ec9b836fd9 | 239 | } |
Hendrikvg | 24:a9ec9b836fd9 | 240 | |
Hendrikvg | 24:a9ec9b836fd9 | 241 | void mvc() |
Hendrikvg | 24:a9ec9b836fd9 | 242 | { |
Hendrikvg | 24:a9ec9b836fd9 | 243 | if (muscle == 1) |
Hendrikvg | 24:a9ec9b836fd9 | 244 | { |
Hendrikvg | 24:a9ec9b836fd9 | 245 | emg = emgFiltered_bl; |
Hendrikvg | 24:a9ec9b836fd9 | 246 | } |
Hendrikvg | 24:a9ec9b836fd9 | 247 | if (muscle == 3) |
Hendrikvg | 24:a9ec9b836fd9 | 248 | { |
Hendrikvg | 24:a9ec9b836fd9 | 249 | emg = emgFiltered_br; |
Hendrikvg | 24:a9ec9b836fd9 | 250 | } |
Hendrikvg | 24:a9ec9b836fd9 | 251 | if (muscle == 5) |
Hendrikvg | 24:a9ec9b836fd9 | 252 | { |
Hendrikvg | 24:a9ec9b836fd9 | 253 | emg = emgFiltered_leg; |
Hendrikvg | 24:a9ec9b836fd9 | 254 | } |
Hendrikvg | 24:a9ec9b836fd9 | 255 | if (emg >= xmvc_value) |
Hendrikvg | 24:a9ec9b836fd9 | 256 | { |
Hendrikvg | 24:a9ec9b836fd9 | 257 | xmvc_value = emg; |
Hendrikvg | 24:a9ec9b836fd9 | 258 | } |
Hendrikvg | 24:a9ec9b836fd9 | 259 | n++; |
Hendrikvg | 24:a9ec9b836fd9 | 260 | if (n < 100) |
Hendrikvg | 24:a9ec9b836fd9 | 261 | { |
Hendrikvg | 24:a9ec9b836fd9 | 262 | mvc_timeout.attach(mvc,0.01f); |
Hendrikvg | 24:a9ec9b836fd9 | 263 | } |
Hendrikvg | 24:a9ec9b836fd9 | 264 | if (n == 100) |
Hendrikvg | 24:a9ec9b836fd9 | 265 | { |
Hendrikvg | 24:a9ec9b836fd9 | 266 | n = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 267 | if (muscle == 1) |
Hendrikvg | 24:a9ec9b836fd9 | 268 | { |
Hendrikvg | 24:a9ec9b836fd9 | 269 | mvc_value_bl = xmvc_value; |
Hendrikvg | 24:a9ec9b836fd9 | 270 | CurrentSubstate = rest_biceps_right; |
Hendrikvg | 24:a9ec9b836fd9 | 271 | SubstateChanged = true; |
Hendrikvg | 24:a9ec9b836fd9 | 272 | led_red = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 273 | } |
Hendrikvg | 24:a9ec9b836fd9 | 274 | if (muscle == 3) |
Hendrikvg | 24:a9ec9b836fd9 | 275 | { |
Hendrikvg | 24:a9ec9b836fd9 | 276 | mvc_value_br = xmvc_value; |
Hendrikvg | 24:a9ec9b836fd9 | 277 | CurrentSubstate = rest_biceps_leg; |
Hendrikvg | 24:a9ec9b836fd9 | 278 | SubstateChanged = true; |
Hendrikvg | 24:a9ec9b836fd9 | 279 | led_red = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 280 | } |
Hendrikvg | 24:a9ec9b836fd9 | 281 | if (muscle == 5) |
Hendrikvg | 24:a9ec9b836fd9 | 282 | { |
Hendrikvg | 24:a9ec9b836fd9 | 283 | mvc_value_leg = xmvc_value; |
Hendrikvg | 24:a9ec9b836fd9 | 284 | CurrentState = vertical_movement; |
Hendrikvg | 24:a9ec9b836fd9 | 285 | StateChanged = true; |
Hendrikvg | 24:a9ec9b836fd9 | 286 | led_red = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 287 | } |
Hendrikvg | 24:a9ec9b836fd9 | 288 | xmvc_value = 1e-11; |
Hendrikvg | 24:a9ec9b836fd9 | 289 | led_red = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 290 | } |
Hendrikvg | 24:a9ec9b836fd9 | 291 | } |
Hendrikvg | 24:a9ec9b836fd9 | 292 | |
Hendrikvg | 24:a9ec9b836fd9 | 293 | void WriteScope() |
Hendrikvg | 24:a9ec9b836fd9 | 294 | { |
Hendrikvg | 24:a9ec9b836fd9 | 295 | //scope.set(0,theta_1); |
Hendrikvg | 24:a9ec9b836fd9 | 296 | // scope.set(1,CalculateError(theta_reference_1,theta_1)); |
Hendrikvg | 24:a9ec9b836fd9 | 297 | // scope.set(2,theta_reference_1); |
Hendrikvg | 24:a9ec9b836fd9 | 298 | scope.set(0, EmgCalibration(emgFiltered_bl, mvc_value_bl, rest_value_bl)); |
Hendrikvg | 24:a9ec9b836fd9 | 299 | scope.set(1, EmgCalibration(emgFiltered_br, mvc_value_br, rest_value_br) ); |
Hendrikvg | 24:a9ec9b836fd9 | 300 | scope.set(2, EmgCalibration(emgFiltered_leg, mvc_value_leg, rest_value_leg)); |
Hendrikvg | 24:a9ec9b836fd9 | 301 | scope.send(); |
Hendrikvg | 24:a9ec9b836fd9 | 302 | } |
Hendrikvg | 24:a9ec9b836fd9 | 303 | |
Hendrikvg | 24:a9ec9b836fd9 | 304 | void SubstateTransition() |
Hendrikvg | 24:a9ec9b836fd9 | 305 | { |
Hendrikvg | 24:a9ec9b836fd9 | 306 | pressed_1 = false; |
Hendrikvg | 24:a9ec9b836fd9 | 307 | pressed_2 = false; |
Hendrikvg | 24:a9ec9b836fd9 | 308 | SubstateChanged = false; |
Hendrikvg | 21:394a7a1deb73 | 309 | } |
Hendrikvg | 21:394a7a1deb73 | 310 | |
Hendrikvg | 21:394a7a1deb73 | 311 | void while_start() |
Hendrikvg | 21:394a7a1deb73 | 312 | { |
Hendrikvg | 21:394a7a1deb73 | 313 | // Do startup stuff |
Hendrikvg | 21:394a7a1deb73 | 314 | CurrentState = motor_calibration; |
Hendrikvg | 21:394a7a1deb73 | 315 | StateChanged = true; |
Hendrikvg | 21:394a7a1deb73 | 316 | } |
Hendrikvg | 21:394a7a1deb73 | 317 | |
Hendrikvg | 21:394a7a1deb73 | 318 | void while_motor_calibration() |
Hendrikvg | 21:394a7a1deb73 | 319 | { |
Hendrikvg | 21:394a7a1deb73 | 320 | // Do motor calibration stuff |
Hendrikvg | 24:a9ec9b836fd9 | 321 | if (pressed_1) { // bool aanmaken voor demo (switch oid aanmaken) |
Hendrikvg | 21:394a7a1deb73 | 322 | CurrentState = demo_mode; |
Hendrikvg | 21:394a7a1deb73 | 323 | StateChanged = true; |
Hendrikvg | 21:394a7a1deb73 | 324 | } |
Hendrikvg | 24:a9ec9b836fd9 | 325 | if (pressed_2) { // bool aanmaken voor EMG (switch oid aanmaken) |
Hendrikvg | 21:394a7a1deb73 | 326 | CurrentState = emg_calibration; |
Hendrikvg | 21:394a7a1deb73 | 327 | StateChanged = true; |
Hendrikvg | 21:394a7a1deb73 | 328 | } |
Hendrikvg | 21:394a7a1deb73 | 329 | } |
Hendrikvg | 21:394a7a1deb73 | 330 | |
Hendrikvg | 21:394a7a1deb73 | 331 | void while_demo_mode() |
Hendrikvg | 21:394a7a1deb73 | 332 | { |
Hendrikvg | 21:394a7a1deb73 | 333 | // Do demo mode stuff |
Hendrikvg | 24:a9ec9b836fd9 | 334 | if (pressed_1 || pressed_2) { |
Hendrikvg | 21:394a7a1deb73 | 335 | CurrentState = emg_calibration; |
Hendrikvg | 21:394a7a1deb73 | 336 | StateChanged = true; |
Hendrikvg | 21:394a7a1deb73 | 337 | } |
Hendrikvg | 21:394a7a1deb73 | 338 | } |
Hendrikvg | 21:394a7a1deb73 | 339 | |
Hendrikvg | 21:394a7a1deb73 | 340 | void while_emg_calibration() |
Hendrikvg | 21:394a7a1deb73 | 341 | { |
Hendrikvg | 21:394a7a1deb73 | 342 | // Do emg calibration stuff |
Hendrikvg | 24:a9ec9b836fd9 | 343 | switch (CurrentSubstate) { |
Hendrikvg | 24:a9ec9b836fd9 | 344 | case rest_biceps_left: |
Hendrikvg | 24:a9ec9b836fd9 | 345 | SubstateTransition(); |
Hendrikvg | 24:a9ec9b836fd9 | 346 | muscle = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 347 | led_green = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 348 | if (pressed_1 || pressed_2) |
Hendrikvg | 24:a9ec9b836fd9 | 349 | { |
Hendrikvg | 24:a9ec9b836fd9 | 350 | led_green = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 351 | led_red = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 352 | rest(); |
Hendrikvg | 24:a9ec9b836fd9 | 353 | } |
Hendrikvg | 24:a9ec9b836fd9 | 354 | break; |
Hendrikvg | 24:a9ec9b836fd9 | 355 | case mvc_biceps_left: |
Hendrikvg | 24:a9ec9b836fd9 | 356 | SubstateTransition(); |
Hendrikvg | 24:a9ec9b836fd9 | 357 | muscle = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 358 | led_blue = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 359 | if (pressed_1 || pressed_2) |
Hendrikvg | 24:a9ec9b836fd9 | 360 | { |
Hendrikvg | 24:a9ec9b836fd9 | 361 | led_blue = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 362 | led_red = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 363 | mvc(); |
Hendrikvg | 24:a9ec9b836fd9 | 364 | } |
Hendrikvg | 24:a9ec9b836fd9 | 365 | break; |
Hendrikvg | 24:a9ec9b836fd9 | 366 | case rest_biceps_right: |
Hendrikvg | 24:a9ec9b836fd9 | 367 | SubstateTransition(); |
Hendrikvg | 24:a9ec9b836fd9 | 368 | muscle = 2; |
Hendrikvg | 24:a9ec9b836fd9 | 369 | led_red = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 370 | led_green = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 371 | if (pressed_1 || pressed_2) |
Hendrikvg | 24:a9ec9b836fd9 | 372 | { |
Hendrikvg | 24:a9ec9b836fd9 | 373 | led_green = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 374 | rest(); |
Hendrikvg | 24:a9ec9b836fd9 | 375 | } |
Hendrikvg | 24:a9ec9b836fd9 | 376 | break; |
Hendrikvg | 24:a9ec9b836fd9 | 377 | case mvc_biceps_right: |
Hendrikvg | 24:a9ec9b836fd9 | 378 | SubstateTransition(); |
Hendrikvg | 24:a9ec9b836fd9 | 379 | muscle = 3; |
Hendrikvg | 24:a9ec9b836fd9 | 380 | led_red = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 381 | led_blue = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 382 | if (pressed_1 || pressed_2) |
Hendrikvg | 24:a9ec9b836fd9 | 383 | { |
Hendrikvg | 24:a9ec9b836fd9 | 384 | led_blue = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 385 | mvc(); |
Hendrikvg | 24:a9ec9b836fd9 | 386 | } |
Hendrikvg | 24:a9ec9b836fd9 | 387 | break; |
Hendrikvg | 24:a9ec9b836fd9 | 388 | case rest_biceps_leg: |
Hendrikvg | 24:a9ec9b836fd9 | 389 | SubstateTransition(); |
Hendrikvg | 24:a9ec9b836fd9 | 390 | muscle = 4; |
Hendrikvg | 24:a9ec9b836fd9 | 391 | led_green = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 392 | led_blue = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 393 | if (pressed_1 || pressed_2) |
Hendrikvg | 24:a9ec9b836fd9 | 394 | { |
Hendrikvg | 24:a9ec9b836fd9 | 395 | led_green = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 396 | led_blue = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 397 | led_red = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 398 | rest(); |
Hendrikvg | 24:a9ec9b836fd9 | 399 | } |
Hendrikvg | 24:a9ec9b836fd9 | 400 | break; |
Hendrikvg | 24:a9ec9b836fd9 | 401 | case mvc_biceps_leg: |
Hendrikvg | 24:a9ec9b836fd9 | 402 | SubstateTransition(); |
Hendrikvg | 24:a9ec9b836fd9 | 403 | muscle = 5; |
Hendrikvg | 24:a9ec9b836fd9 | 404 | led_red = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 405 | led_green = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 406 | led_blue = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 407 | if (pressed_1 || pressed_2) |
Hendrikvg | 24:a9ec9b836fd9 | 408 | { |
Hendrikvg | 24:a9ec9b836fd9 | 409 | led_green = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 410 | led_blue = 1; |
Hendrikvg | 24:a9ec9b836fd9 | 411 | led_red = 0; |
Hendrikvg | 24:a9ec9b836fd9 | 412 | mvc(); |
Hendrikvg | 24:a9ec9b836fd9 | 413 | } |
Hendrikvg | 24:a9ec9b836fd9 | 414 | break; |
Hendrikvg | 24:a9ec9b836fd9 | 415 | default: |
Hendrikvg | 24:a9ec9b836fd9 | 416 | pc.printf("Unknown or unimplemented state reached!\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 417 | } |
Hendrikvg | 21:394a7a1deb73 | 418 | } |
Hendrikvg | 21:394a7a1deb73 | 419 | |
Hendrikvg | 21:394a7a1deb73 | 420 | void while_vertical_movement() |
Hendrikvg | 21:394a7a1deb73 | 421 | { |
Hendrikvg | 21:394a7a1deb73 | 422 | // Do vertical movement stuff |
Hendrikvg | 24:a9ec9b836fd9 | 423 | if (pressed_1 || pressed_2) { // EMG gebaseerde threshold aanmaken |
Hendrikvg | 21:394a7a1deb73 | 424 | CurrentState = horizontal_movement; |
Hendrikvg | 21:394a7a1deb73 | 425 | StateChanged = true; |
Hendrikvg | 21:394a7a1deb73 | 426 | } |
Hendrikvg | 21:394a7a1deb73 | 427 | } |
Hendrikvg | 17:cacf9e75eda7 | 428 | |
Hendrikvg | 21:394a7a1deb73 | 429 | void while_horizontal_movement() |
Hendrikvg | 21:394a7a1deb73 | 430 | { |
Hendrikvg | 21:394a7a1deb73 | 431 | // Do horizontal movement stuff |
Hendrikvg | 24:a9ec9b836fd9 | 432 | if (pressed_1 || pressed_2) { // EMG gebaseerde threshold aanmaken |
Hendrikvg | 21:394a7a1deb73 | 433 | CurrentState = vertical_movement; |
Hendrikvg | 21:394a7a1deb73 | 434 | StateChanged = true; |
Hendrikvg | 21:394a7a1deb73 | 435 | } |
Hendrikvg | 21:394a7a1deb73 | 436 | } |
Hendrikvg | 21:394a7a1deb73 | 437 | |
Hendrikvg | 21:394a7a1deb73 | 438 | void StateTransition() |
Hendrikvg | 21:394a7a1deb73 | 439 | { |
Hendrikvg | 24:a9ec9b836fd9 | 440 | pressed_1 = false; |
Hendrikvg | 24:a9ec9b836fd9 | 441 | pressed_2 = false; |
Hendrikvg | 24:a9ec9b836fd9 | 442 | if (StateChanged) { |
Hendrikvg | 24:a9ec9b836fd9 | 443 | if (CurrentState == start) { |
Hendrikvg | 21:394a7a1deb73 | 444 | pc.printf("Initiating start.\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 445 | } |
Hendrikvg | 24:a9ec9b836fd9 | 446 | if (CurrentState == motor_calibration) { |
Hendrikvg | 21:394a7a1deb73 | 447 | pc.printf("Initiating motor_calibration.\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 448 | } |
Hendrikvg | 24:a9ec9b836fd9 | 449 | if (CurrentState == demo_mode) { |
Hendrikvg | 21:394a7a1deb73 | 450 | pc.printf("Initiating demo_mode.\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 451 | } |
Hendrikvg | 24:a9ec9b836fd9 | 452 | if (CurrentState == emg_calibration) { |
Hendrikvg | 21:394a7a1deb73 | 453 | pc.printf("Initiating emg_calibration.\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 454 | } |
Hendrikvg | 24:a9ec9b836fd9 | 455 | if (CurrentState == vertical_movement) { |
Hendrikvg | 21:394a7a1deb73 | 456 | pc.printf("Initiating vertical_movement.\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 457 | } |
Hendrikvg | 24:a9ec9b836fd9 | 458 | if (CurrentState == horizontal_movement) { |
Hendrikvg | 21:394a7a1deb73 | 459 | pc.printf("Initiating horizontal_movement.\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 460 | } |
Hendrikvg | 21:394a7a1deb73 | 461 | StateChanged = false; |
Hendrikvg | 21:394a7a1deb73 | 462 | } |
Hendrikvg | 21:394a7a1deb73 | 463 | } |
Hendrikvg | 21:394a7a1deb73 | 464 | |
Hendrikvg | 21:394a7a1deb73 | 465 | void StateMachine() |
Hendrikvg | 21:394a7a1deb73 | 466 | { |
Hendrikvg | 24:a9ec9b836fd9 | 467 | switch(CurrentState) { |
Hendrikvg | 21:394a7a1deb73 | 468 | case start: |
Hendrikvg | 21:394a7a1deb73 | 469 | StateTransition(); |
Hendrikvg | 21:394a7a1deb73 | 470 | while_start(); |
Hendrikvg | 21:394a7a1deb73 | 471 | break; |
Hendrikvg | 21:394a7a1deb73 | 472 | case motor_calibration: |
Hendrikvg | 21:394a7a1deb73 | 473 | StateTransition(); |
Hendrikvg | 21:394a7a1deb73 | 474 | while_motor_calibration(); |
Hendrikvg | 21:394a7a1deb73 | 475 | break; |
Hendrikvg | 21:394a7a1deb73 | 476 | case demo_mode: |
Hendrikvg | 21:394a7a1deb73 | 477 | StateTransition(); |
Hendrikvg | 21:394a7a1deb73 | 478 | while_demo_mode(); |
Hendrikvg | 21:394a7a1deb73 | 479 | break; |
Hendrikvg | 21:394a7a1deb73 | 480 | case emg_calibration: |
Hendrikvg | 21:394a7a1deb73 | 481 | StateTransition(); |
Hendrikvg | 21:394a7a1deb73 | 482 | while_emg_calibration(); |
Hendrikvg | 21:394a7a1deb73 | 483 | break; |
Hendrikvg | 21:394a7a1deb73 | 484 | case vertical_movement: |
Hendrikvg | 21:394a7a1deb73 | 485 | StateTransition(); |
Hendrikvg | 21:394a7a1deb73 | 486 | while_vertical_movement(); |
Hendrikvg | 21:394a7a1deb73 | 487 | break; |
Hendrikvg | 21:394a7a1deb73 | 488 | case horizontal_movement: |
Hendrikvg | 21:394a7a1deb73 | 489 | StateTransition(); |
Hendrikvg | 21:394a7a1deb73 | 490 | while_horizontal_movement(); |
Hendrikvg | 21:394a7a1deb73 | 491 | break; |
Hendrikvg | 21:394a7a1deb73 | 492 | default: |
Hendrikvg | 21:394a7a1deb73 | 493 | pc.printf("Unknown or unimplemented state reached!\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 494 | } |
Hendrikvg | 21:394a7a1deb73 | 495 | } |
Hendrikvg | 21:394a7a1deb73 | 496 | |
Hendrikvg | 15:80b3ac2b8448 | 497 | // main |
Hendrikvg | 24:a9ec9b836fd9 | 498 | int main() |
Hendrikvg | 24:a9ec9b836fd9 | 499 | { |
RobertoO | 0:67c50348f842 | 500 | pc.baud(115200); |
Hendrikvg | 17:cacf9e75eda7 | 501 | pc.printf("Hello World!\n\r"); |
Hendrikvg | 21:394a7a1deb73 | 502 | button_1.fall(go_next_1); |
Hendrikvg | 21:394a7a1deb73 | 503 | button_2.fall(go_next_2); |
Hendrikvg | 21:394a7a1deb73 | 504 | sinus_time.start(); |
Hendrikvg | 21:394a7a1deb73 | 505 | PWM_motor_1.period_ms(10); |
Hendrikvg | 23:78898ddfb103 | 506 | motor_control.attach(&MotorControl, Ts); |
Hendrikvg | 23:78898ddfb103 | 507 | write_scope.attach(&WriteScope, 0.1); |
Hendrikvg | 21:394a7a1deb73 | 508 | //TickerStateMachine.attach(StateMachine,1.00f); |
Hendrikvg | 21:394a7a1deb73 | 509 | while(true) { |
Hendrikvg | 21:394a7a1deb73 | 510 | StateMachine(); |
Hendrikvg | 21:394a7a1deb73 | 511 | } |
Hendrikvg | 2:d9b0ebf3fcca | 512 | } |