Totale script, 2 rotationele joints met middelpunt van swiffer als end-effector die verticaal en horizontaal wordt bestuurd middels EMG-signalen. Automatische kalibratie, grenshoeken

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Committer:
willem_hoitzing
Date:
Tue Nov 01 09:23:01 2016 +0000
Revision:
2:6523e21391e5
Parent:
1:078e96685ed3
Initialize; beide armen bewegen tegelijk; Lengte 2e arm aangepast; Grenzen aangepast; Nog niet getest

Who changed what in which revision?

UserRevisionLine numberNew contents of line
willem_hoitzing 0:e03285f8a410 1 #include "stdio.h"
willem_hoitzing 0:e03285f8a410 2 #include "math.h"
willem_hoitzing 0:e03285f8a410 3 #include "mbed.h"
willem_hoitzing 0:e03285f8a410 4 #include "QEI.h"
willem_hoitzing 0:e03285f8a410 5 #include "MODSERIAL.h"
willem_hoitzing 0:e03285f8a410 6 #include "BiQuad.h"
willem_hoitzing 0:e03285f8a410 7 #include "HIDScope.h"
willem_hoitzing 0:e03285f8a410 8
willem_hoitzing 0:e03285f8a410 9 MODSERIAL pc(USBTX, USBRX);
willem_hoitzing 0:e03285f8a410 10 QEI wheel_M1 (D13, D12, NC, 32);
willem_hoitzing 0:e03285f8a410 11 QEI wheel_M2 (D10, D11, NC, 32);
willem_hoitzing 0:e03285f8a410 12 PwmOut pwm_M1 (D6);
willem_hoitzing 0:e03285f8a410 13 PwmOut pwm_M2 (D5);
willem_hoitzing 0:e03285f8a410 14 DigitalOut dir_M1 (D7);
willem_hoitzing 0:e03285f8a410 15 DigitalOut dir_M2 (D4);
willem_hoitzing 0:e03285f8a410 16
willem_hoitzing 0:e03285f8a410 17 Ticker emgticker;
willem_hoitzing 0:e03285f8a410 18 AnalogIn emgB(A0);
willem_hoitzing 0:e03285f8a410 19 AnalogIn emgT(A1);
willem_hoitzing 0:e03285f8a410 20 AnalogIn emgS(A2);
willem_hoitzing 1:078e96685ed3 21 HIDScope scope(4);
willem_hoitzing 0:e03285f8a410 22
willem_hoitzing 0:e03285f8a410 23 DigitalOut ledg (LED_GREEN);
willem_hoitzing 0:e03285f8a410 24 DigitalOut ledr (LED_RED);
willem_hoitzing 0:e03285f8a410 25 DigitalOut ledb (LED_BLUE);
willem_hoitzing 0:e03285f8a410 26 InterruptIn knop_biceps(SW2);
willem_hoitzing 0:e03285f8a410 27 InterruptIn knop_triceps(SW3);
willem_hoitzing 0:e03285f8a410 28 InterruptIn knop_switch(D9);
willem_hoitzing 1:078e96685ed3 29 InterruptIn knop_calibrate(PTC12);
willem_hoitzing 0:e03285f8a410 30 BiQuadChain filter1b;
willem_hoitzing 0:e03285f8a410 31 BiQuadChain filter2b;
willem_hoitzing 0:e03285f8a410 32 BiQuadChain filter1t;
willem_hoitzing 0:e03285f8a410 33 BiQuadChain filter2t;
willem_hoitzing 0:e03285f8a410 34 BiQuadChain filter1s;
willem_hoitzing 0:e03285f8a410 35 BiQuadChain filter2s;
willem_hoitzing 0:e03285f8a410 36
willem_hoitzing 0:e03285f8a410 37 BiQuad bq1b(8.5977e-01, -1.7195e+00, 8.5977e-01, -1.7347e+00, 7.6601e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 38 BiQuad bq2b(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.5933e+00, 9.8217e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 39 BiQuad bq3b(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.6143e+00 , 9.8260e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 40 BiQuad bq4b(3.4604e-04, 6.9208e-04, 3.4604e-04, -1.9467e+00, 9.4808e-01); // LP
willem_hoitzing 0:e03285f8a410 41
willem_hoitzing 0:e03285f8a410 42 BiQuad bq1t(8.5977e-01, -1.7195e+00, 8.5977e-01, -1.7347e+00, 7.6601e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 43 BiQuad bq2t(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.5933e+00, 9.8217e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 44 BiQuad bq3t(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.6143e+00 , 9.8260e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 45 BiQuad bq4t(3.4604e-04, 6.9208e-04, 3.4604e-04, -1.9467e+00, 9.4808e-01); // LP
willem_hoitzing 0:e03285f8a410 46
willem_hoitzing 0:e03285f8a410 47 BiQuad bq1s(8.5977e-01, -1.7195e+00, 8.5977e-01, -1.7347e+00, 7.6601e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 48 BiQuad bq2s(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.5933e+00, 9.8217e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 49 BiQuad bq3s(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.6143e+00 , 9.8260e-01); // Notch + HP
willem_hoitzing 0:e03285f8a410 50 BiQuad bq4s(3.4604e-04, 6.9208e-04, 3.4604e-04, -1.9467e+00, 9.4808e-01); // LP
willem_hoitzing 0:e03285f8a410 51
willem_hoitzing 1:078e96685ed3 52 volatile double bEMG_max = 0;
willem_hoitzing 1:078e96685ed3 53 volatile double tEMG_max = 0;
willem_hoitzing 1:078e96685ed3 54 volatile double sEMG_max = 0;
willem_hoitzing 1:078e96685ed3 55 const double percentage_threshold_biceps = 0.09/0.171536; // 0.171536 is max aanspanning
willem_hoitzing 1:078e96685ed3 56 const double percentage_threshold_triceps = 0.07/0.203654; // 0.203654 is max aanspanning
willem_hoitzing 1:078e96685ed3 57 const double percentage_threshold_switch = 0.09/0.171536; // gekopieerd van andere biceps
willem_hoitzing 1:078e96685ed3 58 volatile double threshold_biceps = 0;
willem_hoitzing 1:078e96685ed3 59 volatile double threshold_triceps = 0;
willem_hoitzing 1:078e96685ed3 60 volatile double threshold_switch = 0;
willem_hoitzing 1:078e96685ed3 61 volatile bool calibrate_biceps = false;
willem_hoitzing 1:078e96685ed3 62 volatile bool calibrate_triceps = false;
willem_hoitzing 1:078e96685ed3 63 volatile bool calibrate_switch = false;
willem_hoitzing 1:078e96685ed3 64 volatile bool calibration_finished = false;
willem_hoitzing 0:e03285f8a410 65
willem_hoitzing 2:6523e21391e5 66 const double pi = 3.14159265359;
willem_hoitzing 2:6523e21391e5 67
willem_hoitzing 1:078e96685ed3 68 volatile double q1 = 0;
willem_hoitzing 1:078e96685ed3 69 volatile double q2 = 0;
willem_hoitzing 1:078e96685ed3 70 const double l1 = 0.3626;
willem_hoitzing 2:6523e21391e5 71 const double l2 = (0.420-0.065); // middelpunt swiffer
willem_hoitzing 1:078e96685ed3 72 volatile double q1_v;
willem_hoitzing 1:078e96685ed3 73 volatile double q2_v;
willem_hoitzing 1:078e96685ed3 74 volatile double q1_ref = 0;
willem_hoitzing 1:078e96685ed3 75 volatile double q2_ref = 0;
willem_hoitzing 1:078e96685ed3 76 volatile double q1_ref_prev = 0;
willem_hoitzing 1:078e96685ed3 77 volatile double q2_ref_prev = 0;
willem_hoitzing 1:078e96685ed3 78 volatile double q1_error = 0;
willem_hoitzing 1:078e96685ed3 79 volatile double q2_error = 0;
willem_hoitzing 1:078e96685ed3 80 volatile double q1_error_prev = 0;
willem_hoitzing 1:078e96685ed3 81 volatile double q2_error_prev = 0;
willem_hoitzing 1:078e96685ed3 82 volatile double q1DerivativeError = 0;
willem_hoitzing 1:078e96685ed3 83 volatile double q2DerivativeError = 0;
willem_hoitzing 1:078e96685ed3 84 volatile double q1IntError = 0;
willem_hoitzing 1:078e96685ed3 85 volatile double q2IntError = 0;
willem_hoitzing 1:078e96685ed3 86 volatile double q1_total_error= 0;
willem_hoitzing 1:078e96685ed3 87 volatile double q2_total_error= 0;
willem_hoitzing 1:078e96685ed3 88 double ctrlOutput_M1 = 0;
willem_hoitzing 1:078e96685ed3 89 double ctrlOutput_M2 = 0;
willem_hoitzing 1:078e96685ed3 90 volatile double vx;
willem_hoitzing 1:078e96685ed3 91 volatile double vy;
willem_hoitzing 0:e03285f8a410 92 volatile bool translatie_richting = true; //true is verticaal, false is horizontaal
willem_hoitzing 0:e03285f8a410 93
willem_hoitzing 1:078e96685ed3 94 const double TS = 0.02;
willem_hoitzing 1:078e96685ed3 95 const double MotorGain_M1 = 4.3; // bij pwm = 1 draait (losse) motor met 4.3 rad/s -> gemeten
willem_hoitzing 1:078e96685ed3 96 const double MotorGain_M2 = 4.7; // gemeten
willem_hoitzing 0:e03285f8a410 97
willem_hoitzing 0:e03285f8a410 98 Ticker update_encoder_ticker;
willem_hoitzing 0:e03285f8a410 99 volatile bool go_flag_update_encoder = false;
willem_hoitzing 0:e03285f8a410 100 void flag_update_encoder()
willem_hoitzing 0:e03285f8a410 101 {
willem_hoitzing 0:e03285f8a410 102 go_flag_update_encoder = true;
willem_hoitzing 0:e03285f8a410 103 }
willem_hoitzing 0:e03285f8a410 104
willem_hoitzing 0:e03285f8a410 105 void update_encoder()
willem_hoitzing 0:e03285f8a410 106 {
willem_hoitzing 0:e03285f8a410 107 //q1 = wheel_M1.getPulses()/(1334.355/2);
willem_hoitzing 0:e03285f8a410 108 //q2 = wheel_M2.getPulses()/(1334.355/2);
willem_hoitzing 1:078e96685ed3 109 //pc.printf("q1 = %f \tq1_ref = %f \tq2 = %f \tq2_ref = %f \ttotalerr1 = %f \ttotalerr2 = %f\n\r",q1, q1_ref,q2,q2_ref,q1_total_error,q2_total_error);
willem_hoitzing 0:e03285f8a410 110 pc.printf("vx = %f \tvy = %f \tq1_r = %f \tq2_r = %f \tq1 = %f \tq2 = %f \tpwm_M1 = %f \tpwm_M2 = %f\n\r",vx,vy,q1_ref,q2_ref,q1,q2,pwm_M1.read(),pwm_M2.read());
willem_hoitzing 1:078e96685ed3 111 //pc.printf("q1_err = %0.9f \tq2_err = %0.9f \tq1IntErr = %0.9f \tq2IntErr = %0.9f \tTotErr1 = %0.9f \tTotErr2 = %0.9f\n\r",q1_error,q2_error,q1IntError,q2IntError,q1_total_error,q2_total_error);
willem_hoitzing 1:078e96685ed3 112 }
willem_hoitzing 1:078e96685ed3 113
willem_hoitzing 1:078e96685ed3 114 Ticker end_calibration_biceps_ticker;
willem_hoitzing 1:078e96685ed3 115 void end_calibration_biceps()
willem_hoitzing 1:078e96685ed3 116 {
willem_hoitzing 1:078e96685ed3 117 ledr = 1;
willem_hoitzing 1:078e96685ed3 118 calibrate_biceps = false;
willem_hoitzing 1:078e96685ed3 119 end_calibration_biceps_ticker.detach();
willem_hoitzing 1:078e96685ed3 120 }
willem_hoitzing 1:078e96685ed3 121
willem_hoitzing 1:078e96685ed3 122 Ticker end_calibration_triceps_ticker;
willem_hoitzing 1:078e96685ed3 123 void end_calibration_triceps()
willem_hoitzing 1:078e96685ed3 124 {
willem_hoitzing 1:078e96685ed3 125 ledg = 1;
willem_hoitzing 1:078e96685ed3 126 calibrate_triceps = false;
willem_hoitzing 1:078e96685ed3 127 end_calibration_triceps_ticker.detach();
willem_hoitzing 1:078e96685ed3 128 }
willem_hoitzing 1:078e96685ed3 129
willem_hoitzing 1:078e96685ed3 130 Ticker end_calibration_switch_ticker;
willem_hoitzing 1:078e96685ed3 131 void end_calibration_switch()
willem_hoitzing 1:078e96685ed3 132 {
willem_hoitzing 1:078e96685ed3 133 ledb = 1;
willem_hoitzing 1:078e96685ed3 134 calibrate_switch = false;
willem_hoitzing 1:078e96685ed3 135 end_calibration_switch_ticker.detach();
willem_hoitzing 1:078e96685ed3 136 calibration_finished = true;
willem_hoitzing 0:e03285f8a410 137 }
willem_hoitzing 0:e03285f8a410 138
willem_hoitzing 1:078e96685ed3 139 volatile int n = 0;
willem_hoitzing 1:078e96685ed3 140 void start_calibration()
willem_hoitzing 1:078e96685ed3 141 {
willem_hoitzing 1:078e96685ed3 142 calibration_finished = false;
willem_hoitzing 1:078e96685ed3 143 n++;
willem_hoitzing 1:078e96685ed3 144 if (n == 1) {
willem_hoitzing 1:078e96685ed3 145 ledr = 0;
willem_hoitzing 1:078e96685ed3 146 bEMG_max = 0;
willem_hoitzing 1:078e96685ed3 147 calibrate_biceps = true;
willem_hoitzing 1:078e96685ed3 148 end_calibration_biceps_ticker.attach(&end_calibration_biceps, 10);
willem_hoitzing 1:078e96685ed3 149 }
willem_hoitzing 1:078e96685ed3 150 if (n == 2) {
willem_hoitzing 1:078e96685ed3 151 ledg = 0;
willem_hoitzing 1:078e96685ed3 152 tEMG_max = 0;
willem_hoitzing 1:078e96685ed3 153 calibrate_triceps = true;
willem_hoitzing 1:078e96685ed3 154 end_calibration_triceps_ticker.attach(&end_calibration_triceps, 10);
willem_hoitzing 1:078e96685ed3 155 }
willem_hoitzing 1:078e96685ed3 156 if (n == 3) {
willem_hoitzing 1:078e96685ed3 157 ledb = 0;
willem_hoitzing 1:078e96685ed3 158 sEMG_max = 0;
willem_hoitzing 1:078e96685ed3 159 calibrate_switch = true;
willem_hoitzing 1:078e96685ed3 160 end_calibration_switch_ticker.attach(&end_calibration_switch, 10);
willem_hoitzing 1:078e96685ed3 161 n = 0;
willem_hoitzing 1:078e96685ed3 162 }
willem_hoitzing 1:078e96685ed3 163 }
willem_hoitzing 1:078e96685ed3 164
willem_hoitzing 1:078e96685ed3 165
willem_hoitzing 0:e03285f8a410 166 Ticker PIDcontrol;
willem_hoitzing 0:e03285f8a410 167 volatile bool go_flag_controller = false;
willem_hoitzing 0:e03285f8a410 168
willem_hoitzing 0:e03285f8a410 169 void flag_controller()
willem_hoitzing 0:e03285f8a410 170 {
willem_hoitzing 0:e03285f8a410 171 go_flag_controller = true;
willem_hoitzing 0:e03285f8a410 172 }
willem_hoitzing 0:e03285f8a410 173
willem_hoitzing 0:e03285f8a410 174 volatile bool active_PID_ticker = false;
willem_hoitzing 0:e03285f8a410 175
willem_hoitzing 0:e03285f8a410 176 void begin_hoeken()
willem_hoitzing 0:e03285f8a410 177 {
willem_hoitzing 0:e03285f8a410 178 wait(1);
willem_hoitzing 0:e03285f8a410 179 q1_ref = wheel_M1.getPulses()/(1334.355/2);
willem_hoitzing 0:e03285f8a410 180 q2_ref = wheel_M2.getPulses()/(1334.355/2);
willem_hoitzing 0:e03285f8a410 181 active_PID_ticker = true;
willem_hoitzing 0:e03285f8a410 182 }
willem_hoitzing 0:e03285f8a410 183
willem_hoitzing 0:e03285f8a410 184 void initialize()
willem_hoitzing 0:e03285f8a410 185 {
willem_hoitzing 0:e03285f8a410 186 dir_M1 = 0; //ccw
willem_hoitzing 0:e03285f8a410 187 dir_M2 = 1; //cw
willem_hoitzing 2:6523e21391e5 188 while ( (q1 < 20*2*pi/360) || (q2 > -45*2*pi/360) ) {
willem_hoitzing 0:e03285f8a410 189 q1 = wheel_M1.getPulses()/(1334.355/2);
willem_hoitzing 2:6523e21391e5 190 q2 = wheel_M2.getPulses()/(1334.355/2);
willem_hoitzing 2:6523e21391e5 191 if (q1 < 20*2*pi/360) {
willem_hoitzing 2:6523e21391e5 192 pwm_M1 = 0.05;
willem_hoitzing 2:6523e21391e5 193 } else {
willem_hoitzing 2:6523e21391e5 194 pwm_M1 = 0;
willem_hoitzing 2:6523e21391e5 195 }
willem_hoitzing 2:6523e21391e5 196 if (q2 > -45*2*pi/360) {
willem_hoitzing 2:6523e21391e5 197 pwm_M2 = 0.06;
willem_hoitzing 2:6523e21391e5 198 } else {
willem_hoitzing 2:6523e21391e5 199 pwm_M2 = 0;
willem_hoitzing 2:6523e21391e5 200 }
willem_hoitzing 0:e03285f8a410 201 wait(0.005f);
willem_hoitzing 0:e03285f8a410 202 }
willem_hoitzing 0:e03285f8a410 203 pwm_M1 = 0;
willem_hoitzing 0:e03285f8a410 204 pwm_M2 = 0;
willem_hoitzing 0:e03285f8a410 205 begin_hoeken();
willem_hoitzing 0:e03285f8a410 206 }
willem_hoitzing 0:e03285f8a410 207
willem_hoitzing 0:e03285f8a410 208 void biceps()
willem_hoitzing 0:e03285f8a410 209 {
willem_hoitzing 1:078e96685ed3 210 q1_ref_prev = 0;
willem_hoitzing 1:078e96685ed3 211 q2_ref_prev = 0;
willem_hoitzing 0:e03285f8a410 212 q1IntError = 0;
willem_hoitzing 0:e03285f8a410 213 q2IntError = 0;
willem_hoitzing 0:e03285f8a410 214 q1_error_prev = 0;
willem_hoitzing 0:e03285f8a410 215 q2_error_prev = 0;
willem_hoitzing 0:e03285f8a410 216 if (translatie_richting == true) { // verticaal / up
willem_hoitzing 0:e03285f8a410 217 vx = 0;
willem_hoitzing 0:e03285f8a410 218 vy = 0.1;
willem_hoitzing 0:e03285f8a410 219 } else { // horizontaal / right
willem_hoitzing 0:e03285f8a410 220 vx = 0.1;
willem_hoitzing 0:e03285f8a410 221 vy = 0;
willem_hoitzing 0:e03285f8a410 222 }
willem_hoitzing 0:e03285f8a410 223 }
willem_hoitzing 0:e03285f8a410 224
willem_hoitzing 0:e03285f8a410 225 void triceps()
willem_hoitzing 0:e03285f8a410 226 {
willem_hoitzing 1:078e96685ed3 227 q1_ref_prev = 0;
willem_hoitzing 1:078e96685ed3 228 q2_ref_prev = 0;
willem_hoitzing 0:e03285f8a410 229 q1IntError = 0;
willem_hoitzing 0:e03285f8a410 230 q2IntError = 0;
willem_hoitzing 0:e03285f8a410 231 q1_error_prev = 0;
willem_hoitzing 0:e03285f8a410 232 q2_error_prev = 0;
willem_hoitzing 0:e03285f8a410 233 if (translatie_richting == true) { // verticaal / down
willem_hoitzing 0:e03285f8a410 234 vx = 0;
willem_hoitzing 0:e03285f8a410 235 vy = -0.1;
willem_hoitzing 0:e03285f8a410 236 } else { // horizontaal / left
willem_hoitzing 0:e03285f8a410 237 vx = -0.1;
willem_hoitzing 0:e03285f8a410 238 vy = 0;
willem_hoitzing 0:e03285f8a410 239 }
willem_hoitzing 0:e03285f8a410 240
willem_hoitzing 0:e03285f8a410 241 }
willem_hoitzing 0:e03285f8a410 242
willem_hoitzing 0:e03285f8a410 243 void switcher()
willem_hoitzing 0:e03285f8a410 244 {
willem_hoitzing 0:e03285f8a410 245 if ( (vx == 0) && (vy == 0) && (translatie_richting == true) ) {
willem_hoitzing 0:e03285f8a410 246 translatie_richting = false;
willem_hoitzing 0:e03285f8a410 247 } else if ( (vx == 0) && (vy == 0) && (translatie_richting == false) ) {
willem_hoitzing 0:e03285f8a410 248 translatie_richting = true;
willem_hoitzing 0:e03285f8a410 249 } else {
willem_hoitzing 0:e03285f8a410 250 vx = 0;
willem_hoitzing 0:e03285f8a410 251 vy = 0;
willem_hoitzing 1:078e96685ed3 252 q1_ref = q1;
willem_hoitzing 1:078e96685ed3 253 q2_ref = q2;
willem_hoitzing 1:078e96685ed3 254 q1_error = 0;
willem_hoitzing 1:078e96685ed3 255 q2_error = 0;
willem_hoitzing 0:e03285f8a410 256 q1IntError = 0;
willem_hoitzing 0:e03285f8a410 257 q2IntError = 0;
willem_hoitzing 0:e03285f8a410 258 q1_error_prev = 0;
willem_hoitzing 0:e03285f8a410 259 q2_error_prev = 0;
willem_hoitzing 1:078e96685ed3 260 q1_total_error = 0;
willem_hoitzing 1:078e96685ed3 261 q2_total_error = 0;
willem_hoitzing 0:e03285f8a410 262 }
willem_hoitzing 0:e03285f8a410 263
willem_hoitzing 0:e03285f8a410 264 if (translatie_richting == 1) {
willem_hoitzing 0:e03285f8a410 265 ledr = 1; // blauw - verticaal
willem_hoitzing 0:e03285f8a410 266 ledg = 1;
willem_hoitzing 0:e03285f8a410 267 ledb = 0;
willem_hoitzing 0:e03285f8a410 268 } else {
willem_hoitzing 0:e03285f8a410 269 ledr = 0; // rood - horizontaal
willem_hoitzing 0:e03285f8a410 270 ledg = 1;
willem_hoitzing 0:e03285f8a410 271 ledb = 1;
willem_hoitzing 0:e03285f8a410 272 }
willem_hoitzing 0:e03285f8a410 273 }
willem_hoitzing 0:e03285f8a410 274
willem_hoitzing 0:e03285f8a410 275 Ticker switch_activate_ticker;
willem_hoitzing 0:e03285f8a410 276 volatile bool switch_active = true;
willem_hoitzing 0:e03285f8a410 277 void switch_activate()
willem_hoitzing 0:e03285f8a410 278 {
willem_hoitzing 0:e03285f8a410 279 switch_active = true;
willem_hoitzing 0:e03285f8a410 280 }
willem_hoitzing 0:e03285f8a410 281
willem_hoitzing 0:e03285f8a410 282 volatile bool go_flag_emgsample = false;
willem_hoitzing 0:e03285f8a410 283 void flag_emgsample()
willem_hoitzing 0:e03285f8a410 284 {
willem_hoitzing 0:e03285f8a410 285 go_flag_emgsample = true;
willem_hoitzing 0:e03285f8a410 286 }
willem_hoitzing 0:e03285f8a410 287
willem_hoitzing 0:e03285f8a410 288 void emgsample()
willem_hoitzing 0:e03285f8a410 289 {
willem_hoitzing 1:078e96685ed3 290 double bEMG_raw = emgB.read();
willem_hoitzing 1:078e96685ed3 291 double bEMG_HPfilt = filter1b.step( bEMG_raw );
willem_hoitzing 1:078e96685ed3 292 double bEMG_rect = abs(bEMG_HPfilt);
willem_hoitzing 1:078e96685ed3 293 double bEMG_filt = filter2b.step(bEMG_rect);
willem_hoitzing 1:078e96685ed3 294
willem_hoitzing 1:078e96685ed3 295 double tEMG_raw = emgT.read();
willem_hoitzing 1:078e96685ed3 296 double tEMG_HPfilt = filter1t.step( tEMG_raw );
willem_hoitzing 1:078e96685ed3 297 double tEMG_rect = abs(tEMG_HPfilt);
willem_hoitzing 1:078e96685ed3 298 double tEMG_filt = filter2t.step(tEMG_rect);
willem_hoitzing 1:078e96685ed3 299
willem_hoitzing 1:078e96685ed3 300 double sEMG_raw = emgS.read();
willem_hoitzing 1:078e96685ed3 301 double sEMG_HPfilt = filter1s.step( sEMG_raw );
willem_hoitzing 1:078e96685ed3 302 double sEMG_rect = abs(sEMG_HPfilt);
willem_hoitzing 1:078e96685ed3 303 double sEMG_filt = filter2s.step(sEMG_rect);
willem_hoitzing 0:e03285f8a410 304
willem_hoitzing 1:078e96685ed3 305 if ((bEMG_filt > bEMG_max) && (calibrate_biceps == true) ) {
willem_hoitzing 1:078e96685ed3 306 bEMG_max = bEMG_filt;
willem_hoitzing 1:078e96685ed3 307 threshold_biceps = bEMG_max*percentage_threshold_biceps;
willem_hoitzing 1:078e96685ed3 308 }
willem_hoitzing 0:e03285f8a410 309
willem_hoitzing 1:078e96685ed3 310 if ((tEMG_filt > tEMG_max) && (calibrate_triceps == true) ) {
willem_hoitzing 1:078e96685ed3 311 tEMG_max = tEMG_filt;
willem_hoitzing 1:078e96685ed3 312 threshold_triceps = tEMG_max*percentage_threshold_triceps;
willem_hoitzing 1:078e96685ed3 313 }
willem_hoitzing 0:e03285f8a410 314
willem_hoitzing 1:078e96685ed3 315 if ((sEMG_filt > sEMG_max) && (calibrate_switch == true) ) {
willem_hoitzing 1:078e96685ed3 316 sEMG_max = sEMG_filt;
willem_hoitzing 1:078e96685ed3 317 threshold_switch = sEMG_max*percentage_threshold_switch;
willem_hoitzing 1:078e96685ed3 318 }
willem_hoitzing 1:078e96685ed3 319
willem_hoitzing 0:e03285f8a410 320 scope.set(0, bEMG_filt);
willem_hoitzing 0:e03285f8a410 321 scope.set(1, tEMG_filt);
willem_hoitzing 1:078e96685ed3 322 scope.set(2, threshold_biceps);
willem_hoitzing 1:078e96685ed3 323 scope.set(3, threshold_triceps);
willem_hoitzing 0:e03285f8a410 324 scope.send();
willem_hoitzing 0:e03285f8a410 325
willem_hoitzing 1:078e96685ed3 326 // motor aansturing, pas uitvoeren wanneer kalibratie klaar is
willem_hoitzing 1:078e96685ed3 327 if ( calibration_finished == true ) {
willem_hoitzing 1:078e96685ed3 328 if (sEMG_filt > threshold_switch) {
willem_hoitzing 1:078e96685ed3 329 if (switch_active == true) {
willem_hoitzing 1:078e96685ed3 330 switcher();
willem_hoitzing 1:078e96685ed3 331 switch_active = false;
willem_hoitzing 1:078e96685ed3 332 switch_activate_ticker.attach(&switch_activate, 0.5f);
willem_hoitzing 1:078e96685ed3 333 }
willem_hoitzing 1:078e96685ed3 334 } else if (tEMG_filt > threshold_triceps) {
willem_hoitzing 1:078e96685ed3 335 triceps();
willem_hoitzing 1:078e96685ed3 336 } else if (bEMG_filt > threshold_biceps) {
willem_hoitzing 1:078e96685ed3 337 biceps();
willem_hoitzing 0:e03285f8a410 338 }
willem_hoitzing 0:e03285f8a410 339 }
willem_hoitzing 0:e03285f8a410 340 }
willem_hoitzing 0:e03285f8a410 341
willem_hoitzing 0:e03285f8a410 342 Ticker update_ref_ticker;
willem_hoitzing 1:078e96685ed3 343 volatile double J_1;
willem_hoitzing 1:078e96685ed3 344 volatile double J_2;
willem_hoitzing 1:078e96685ed3 345 volatile double J_3;
willem_hoitzing 1:078e96685ed3 346 volatile double J_4;
willem_hoitzing 0:e03285f8a410 347 volatile bool go_flag_update_ref = false;
willem_hoitzing 0:e03285f8a410 348 void flag_update_ref()
willem_hoitzing 0:e03285f8a410 349 {
willem_hoitzing 0:e03285f8a410 350 go_flag_update_ref = true;
willem_hoitzing 0:e03285f8a410 351 }
willem_hoitzing 0:e03285f8a410 352
willem_hoitzing 0:e03285f8a410 353 void update_ref()
willem_hoitzing 0:e03285f8a410 354 {
willem_hoitzing 0:e03285f8a410 355 q1 = wheel_M1.getPulses() / (1334.355/2); // rad
willem_hoitzing 0:e03285f8a410 356 q2 = wheel_M2.getPulses() / (1334.355/2);
willem_hoitzing 0:e03285f8a410 357
willem_hoitzing 0:e03285f8a410 358 J_1 = -(l2*sin(q1 + q2))/(l2*sin(q1 + q2)*(l2*cos(q1 + q2) + l1*cos(q1)) - l2*cos(q1 + q2)*(l2*sin(q1 + q2) + l1*sin(q1)));
willem_hoitzing 0:e03285f8a410 359 J_2 = (l2*cos(q1 + q2))/(l2*sin(q1 + q2)*(l2*cos(q1 + q2) + l1*cos(q1)) - l2*cos(q1 + q2)*(l2*sin(q1 + q2) + l1*sin(q1)));
willem_hoitzing 0:e03285f8a410 360 J_3 = (l2*sin(q1 + q2) + l1*sin(q1))/(l2*sin(q1 + q2)*(l2*cos(q1 + q2) + l1*cos(q1)) - l2*cos(q1 + q2)*(l2*sin(q1 + q2) + l1*sin(q1)));
willem_hoitzing 0:e03285f8a410 361 J_4 = -(l2*cos(q1 + q2) + l1*cos(q1))/(l2*sin(q1 + q2)*(l2*cos(q1 + q2) + l1*cos(q1)) - l2*cos(q1 + q2)*(l2*sin(q1 + q2) + l1*sin(q1)));
willem_hoitzing 0:e03285f8a410 362
willem_hoitzing 0:e03285f8a410 363 q1_v = J_1 * vx + J_2 * vy;
willem_hoitzing 0:e03285f8a410 364 q2_v = J_3 * vx + J_4 * vy;
willem_hoitzing 0:e03285f8a410 365
willem_hoitzing 2:6523e21391e5 366 if ( (q1 > (135*2*pi/360)) && (q1_v > 0 ) ) { // WAARDES VINDEN 0.8726 (50 graden)
willem_hoitzing 0:e03285f8a410 367 q1_v = 0;
willem_hoitzing 0:e03285f8a410 368 q2_v = 0;
willem_hoitzing 1:078e96685ed3 369 q1_ref = q1;
willem_hoitzing 1:078e96685ed3 370 q2_ref = q2;
willem_hoitzing 1:078e96685ed3 371 q1IntError = 0;
willem_hoitzing 1:078e96685ed3 372 q2IntError = 0;
willem_hoitzing 1:078e96685ed3 373 q1_error_prev = 0;
willem_hoitzing 1:078e96685ed3 374 q2_error_prev = 0;
willem_hoitzing 2:6523e21391e5 375 } else if ( (q1 < -(135*2*pi/360)) && (q1_v < 0) ) {
willem_hoitzing 0:e03285f8a410 376 q1_v = 0;
willem_hoitzing 0:e03285f8a410 377 q2_v = 0;
willem_hoitzing 1:078e96685ed3 378 q1_ref = q1;
willem_hoitzing 1:078e96685ed3 379 q2_ref = q2;
willem_hoitzing 1:078e96685ed3 380 q1IntError = 0;
willem_hoitzing 1:078e96685ed3 381 q2IntError = 0;
willem_hoitzing 1:078e96685ed3 382 q1_error_prev = 0;
willem_hoitzing 1:078e96685ed3 383 q2_error_prev = 0;
willem_hoitzing 2:6523e21391e5 384 } else if ( (q2 < (-2.6*2*pi/360)) && (q2_v < 0) ) { // WAARDES VINDEN -2.4434 (-140 graden) --> werkelijke max -2.672452
willem_hoitzing 0:e03285f8a410 385 q1_v = 0;
willem_hoitzing 0:e03285f8a410 386 q2_v = 0;
willem_hoitzing 1:078e96685ed3 387 q1_ref = q1;
willem_hoitzing 1:078e96685ed3 388 q2_ref = q2;
willem_hoitzing 1:078e96685ed3 389 q1IntError = 0;
willem_hoitzing 1:078e96685ed3 390 q2IntError = 0;
willem_hoitzing 1:078e96685ed3 391 q1_error_prev = 0;
willem_hoitzing 1:078e96685ed3 392 q2_error_prev = 0;
willem_hoitzing 2:6523e21391e5 393 } else if ( (q2 >= 0) && (q2_v > 0) ) {
willem_hoitzing 0:e03285f8a410 394 q1_v = 0;
willem_hoitzing 0:e03285f8a410 395 q2_v = 0;
willem_hoitzing 1:078e96685ed3 396 q1_ref = q1;
willem_hoitzing 1:078e96685ed3 397 q2_ref = q2;
willem_hoitzing 1:078e96685ed3 398 q1IntError = 0;
willem_hoitzing 1:078e96685ed3 399 q2IntError = 0;
willem_hoitzing 1:078e96685ed3 400 q1_error_prev = 0;
willem_hoitzing 1:078e96685ed3 401 q2_error_prev = 0;
willem_hoitzing 0:e03285f8a410 402 }
willem_hoitzing 0:e03285f8a410 403
willem_hoitzing 1:078e96685ed3 404 q1_ref_prev = q1_ref;
willem_hoitzing 1:078e96685ed3 405 q2_ref_prev = q2_ref;
willem_hoitzing 1:078e96685ed3 406
willem_hoitzing 1:078e96685ed3 407 q1_ref = q1_ref_prev + q1_v*TS;
willem_hoitzing 1:078e96685ed3 408 q2_ref = q2_ref_prev + q2_v*TS;
willem_hoitzing 0:e03285f8a410 409 }
willem_hoitzing 0:e03285f8a410 410
willem_hoitzing 1:078e96685ed3 411 void PID(double q1,double q1_ref,double q2,double q2_ref,double TS,double &ctrlOutput_M1, double &ctrlOutput_M2)
willem_hoitzing 0:e03285f8a410 412 {
willem_hoitzing 0:e03285f8a410 413 // linear feedback control
willem_hoitzing 0:e03285f8a410 414 q1_error = q1_ref - q1; //referencePosition1 - Position1; // proportional angular error in radians
willem_hoitzing 0:e03285f8a410 415 q2_error = q2_ref - q2; //referencePosition1 - Position1; // proportional angular error in radians
willem_hoitzing 1:078e96685ed3 416 double Kp = 10;
willem_hoitzing 0:e03285f8a410 417
willem_hoitzing 0:e03285f8a410 418 q1IntError = q1IntError + q1_error*TS; // integrated error in radians
willem_hoitzing 0:e03285f8a410 419 q2IntError = q2IntError + q2_error*TS; // integrated error in radians
willem_hoitzing 1:078e96685ed3 420 double Ki = 1;
willem_hoitzing 0:e03285f8a410 421
willem_hoitzing 0:e03285f8a410 422 q1DerivativeError = (q1_error - q1_error_prev)/TS; // derivative of error in radians
willem_hoitzing 0:e03285f8a410 423 q2DerivativeError = (q2_error - q2_error_prev)/TS; // derivative of error in radians
willem_hoitzing 1:078e96685ed3 424 double Kd = 0;
willem_hoitzing 0:e03285f8a410 425
willem_hoitzing 1:078e96685ed3 426 q1_total_error = (q1_error * Kp) + (q1IntError * Ki) + (q1DerivativeError * Kd); //total controller output = motor input
willem_hoitzing 1:078e96685ed3 427 q2_total_error = (q2_error * Kp) + (q2IntError * Ki) + (q2DerivativeError * Kd); //total controller output = motor input
willem_hoitzing 0:e03285f8a410 428
willem_hoitzing 1:078e96685ed3 429 ctrlOutput_M1 = q1_total_error/MotorGain_M1;
willem_hoitzing 1:078e96685ed3 430 ctrlOutput_M2 = q2_total_error/MotorGain_M2;
willem_hoitzing 0:e03285f8a410 431
willem_hoitzing 0:e03285f8a410 432 q1_error_prev = q1_error;
willem_hoitzing 0:e03285f8a410 433 q2_error_prev = q2_error;
willem_hoitzing 0:e03285f8a410 434 }
willem_hoitzing 0:e03285f8a410 435
willem_hoitzing 0:e03285f8a410 436 void Controller()
willem_hoitzing 0:e03285f8a410 437 {
willem_hoitzing 0:e03285f8a410 438 PID(q1,q1_ref,q2,q2_ref,TS,ctrlOutput_M1,ctrlOutput_M2);
willem_hoitzing 0:e03285f8a410 439
willem_hoitzing 0:e03285f8a410 440 if (ctrlOutput_M1 < 0) {
willem_hoitzing 0:e03285f8a410 441 dir_M1 = 1;
willem_hoitzing 0:e03285f8a410 442 } else {
willem_hoitzing 0:e03285f8a410 443 dir_M1 = 0;
willem_hoitzing 0:e03285f8a410 444 }
willem_hoitzing 0:e03285f8a410 445 pwm_M1 = abs(ctrlOutput_M1);
willem_hoitzing 0:e03285f8a410 446 if (pwm_M1 <= 0) {
willem_hoitzing 0:e03285f8a410 447 pwm_M1 = 0;
willem_hoitzing 0:e03285f8a410 448 } else {
willem_hoitzing 0:e03285f8a410 449 pwm_M1 = pwm_M1 + 0.05;
willem_hoitzing 0:e03285f8a410 450 }
willem_hoitzing 0:e03285f8a410 451
willem_hoitzing 0:e03285f8a410 452 if (ctrlOutput_M2 < 0) {
willem_hoitzing 0:e03285f8a410 453 dir_M2 = 1;
willem_hoitzing 0:e03285f8a410 454 } else {
willem_hoitzing 0:e03285f8a410 455 dir_M2 = 0;
willem_hoitzing 0:e03285f8a410 456 }
willem_hoitzing 0:e03285f8a410 457 pwm_M2 = abs(ctrlOutput_M2);
willem_hoitzing 0:e03285f8a410 458 if (pwm_M2 <= 0) {
willem_hoitzing 0:e03285f8a410 459 pwm_M2 = 0;
willem_hoitzing 0:e03285f8a410 460 } else {
willem_hoitzing 0:e03285f8a410 461 pwm_M2 = pwm_M2 + 0.05;
willem_hoitzing 0:e03285f8a410 462 }
willem_hoitzing 0:e03285f8a410 463 }
willem_hoitzing 0:e03285f8a410 464
willem_hoitzing 0:e03285f8a410 465 int main()
willem_hoitzing 0:e03285f8a410 466 {
willem_hoitzing 0:e03285f8a410 467 ledr = 1;
willem_hoitzing 0:e03285f8a410 468 ledg = 1;
willem_hoitzing 1:078e96685ed3 469 ledb = 1;
willem_hoitzing 0:e03285f8a410 470 pc.baud(115200);
willem_hoitzing 0:e03285f8a410 471 wheel_M1.reset();
willem_hoitzing 0:e03285f8a410 472 wheel_M2.reset();
willem_hoitzing 0:e03285f8a410 473 filter1b.add(&bq1b).add(&bq2b).add(&bq3b);
willem_hoitzing 0:e03285f8a410 474 filter2b.add(&bq4b);
willem_hoitzing 0:e03285f8a410 475 filter1t.add(&bq1t).add(&bq2t).add(&bq3t);
willem_hoitzing 0:e03285f8a410 476 filter2t.add(&bq4t);
willem_hoitzing 0:e03285f8a410 477 filter1s.add(&bq1s).add(&bq2s).add(&bq3s);
willem_hoitzing 0:e03285f8a410 478 filter2s.add(&bq4s);
willem_hoitzing 0:e03285f8a410 479 knop_biceps.rise(&biceps);
willem_hoitzing 0:e03285f8a410 480 knop_triceps.rise(&triceps);
willem_hoitzing 0:e03285f8a410 481 knop_switch.rise(&switcher);
willem_hoitzing 1:078e96685ed3 482 knop_calibrate.rise(&start_calibration);
willem_hoitzing 1:078e96685ed3 483
willem_hoitzing 1:078e96685ed3 484 // initialize -> beginposities
willem_hoitzing 1:078e96685ed3 485 initialize();
willem_hoitzing 0:e03285f8a410 486
willem_hoitzing 0:e03285f8a410 487 // flag functions/tickers
willem_hoitzing 0:e03285f8a410 488 emgticker.attach(&emgsample, 0.002f); // 500 Hz --> moet kloppen met frequentie gebruikt voor filter coefficienten
willem_hoitzing 0:e03285f8a410 489 update_encoder_ticker.attach(&flag_update_encoder, TS);
willem_hoitzing 0:e03285f8a410 490 update_ref_ticker.attach(&flag_update_ref, TS);
willem_hoitzing 0:e03285f8a410 491
willem_hoitzing 0:e03285f8a410 492 if (active_PID_ticker == true) {
willem_hoitzing 0:e03285f8a410 493 PIDcontrol.attach(&flag_controller, TS);
willem_hoitzing 0:e03285f8a410 494 }
willem_hoitzing 0:e03285f8a410 495
willem_hoitzing 0:e03285f8a410 496 while(1) {
willem_hoitzing 0:e03285f8a410 497 // sample EMG
willem_hoitzing 0:e03285f8a410 498 if (go_flag_emgsample == true) {
willem_hoitzing 0:e03285f8a410 499 go_flag_emgsample = false;
willem_hoitzing 0:e03285f8a410 500 emgsample();
willem_hoitzing 0:e03285f8a410 501 }
willem_hoitzing 0:e03285f8a410 502 // update encoder
willem_hoitzing 0:e03285f8a410 503 if (go_flag_update_encoder == true) {
willem_hoitzing 0:e03285f8a410 504 go_flag_update_encoder = false;
willem_hoitzing 0:e03285f8a410 505 update_encoder();
willem_hoitzing 0:e03285f8a410 506 }
willem_hoitzing 0:e03285f8a410 507 // update joint positions/velocities
willem_hoitzing 0:e03285f8a410 508 if (go_flag_update_ref == true) {
willem_hoitzing 0:e03285f8a410 509 go_flag_update_ref = false;
willem_hoitzing 0:e03285f8a410 510 update_ref();
willem_hoitzing 0:e03285f8a410 511 }
willem_hoitzing 0:e03285f8a410 512 // controller M1+M2
willem_hoitzing 0:e03285f8a410 513 if (go_flag_controller == true) {
willem_hoitzing 0:e03285f8a410 514 go_flag_controller = false;
willem_hoitzing 0:e03285f8a410 515 Controller();
willem_hoitzing 0:e03285f8a410 516 }
willem_hoitzing 0:e03285f8a410 517 }
willem_hoitzing 0:e03285f8a410 518 }