repurposed void update_encoder copy

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of EMG_controlled_Inv_Kin_PID_Control by Marlowe Noll

Committer:
JeffreyBrimm
Date:
Tue Nov 01 09:53:08 2016 +0000
Revision:
3:9f0c4e8e21db
Parent:
2:6523e21391e5
removed obsolete code in void update_encoder. Now only prints variables.

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