repurposed void update_encoder copy

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of EMG_controlled_Inv_Kin_PID_Control by Marlowe Noll

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