Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Servoaansturing mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
main.cpp@31:967b455bc328, 2019-10-30 (annotated)
- Committer:
- Renate
- Date:
- Wed Oct 30 10:59:10 2019 +0000
- Revision:
- 31:967b455bc328
- Parent:
- 30:0a328a9a4788
- Child:
- 32:d651c23bbb77
Script tot aan stukje omrekenen absolute theta naar motorsnelheid
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobertoO | 0:67c50348f842 | 1 | #include "mbed.h" |
Rosalie | 3:6ee0b20c23b0 | 2 | #include "HIDScope.h" |
Rosalie | 3:6ee0b20c23b0 | 3 | #include "QEI.h" |
RobertoO | 1:b862262a9d14 | 4 | #include "MODSERIAL.h" |
Rosalie | 3:6ee0b20c23b0 | 5 | #include "BiQuad.h" |
Rosalie | 3:6ee0b20c23b0 | 6 | #include "FastPWM.h" |
Renate | 21:456acc79726c | 7 | #define M_PI 3.14159265358979323846 /* pi */ |
WiesjeRoskamp | 2:aee655d11b6d | 8 | #include <math.h> |
Rosalie | 5:9f1260408ef2 | 9 | #include "Servo.h" |
Renate | 21:456acc79726c | 10 | #include <cmath> |
Renate | 29:8e0a7c33e4e7 | 11 | #include <complex> |
RobertoO | 0:67c50348f842 | 12 | |
WiesjeRoskamp | 2:aee655d11b6d | 13 | Serial pc(USBTX, USBRX); |
Rosalie | 3:6ee0b20c23b0 | 14 | |
Renate | 23:4572750a5c59 | 15 | // TICKERS |
Renate | 23:4572750a5c59 | 16 | Ticker loop_ticker; |
Renate | 15:ad065ab92d11 | 17 | |
Renate | 23:4572750a5c59 | 18 | // BENODIGD VOOR PROCESS STATE MACHINE |
Renate | 23:4572750a5c59 | 19 | enum states {Motors_off, Calib_motor, Calib_EMG, Homing, Operation_mode}; |
Renate | 28:7c7508bdb21f | 20 | states currentState = Motors_off; |
Renate | 23:4572750a5c59 | 21 | bool stateChanged = true; // Make sure the initialization of first state is executed |
Renate | 23:4572750a5c59 | 22 | |
Renate | 23:4572750a5c59 | 23 | // INPUTS |
Renate | 14:54343b9fd708 | 24 | DigitalIn Power_button_pressed(D1); // Geen InterruptIn gebruiken! |
Renate | 9:4de589636f50 | 25 | DigitalIn Emergency_button_pressed(D2); |
Renate | 22:8585d41a670b | 26 | DigitalIn Motor_calib_button_pressed(SW2); |
WiesjeRoskamp | 2:aee655d11b6d | 27 | |
Renate | 15:ad065ab92d11 | 28 | AnalogIn EMG_biceps_right_raw (A0); |
Renate | 15:ad065ab92d11 | 29 | AnalogIn EMG_biceps_left_raw (A1); |
Renate | 19:1fd39a2afc30 | 30 | AnalogIn EMG_calf_raw (A2); |
Renate | 15:ad065ab92d11 | 31 | |
Renate | 23:4572750a5c59 | 32 | QEI Encoder1(D12, D13, NC, 8400, QEI::X4_ENCODING); //Checken of die D12, D9 etc wel kloppen, 8400= gear ratio x 64 |
Renate | 28:7c7508bdb21f | 33 | QEI Encoder2(D9, D10, NC, 8400, QEI::X4_ENCODING); |
Renate | 21:456acc79726c | 34 | |
Renate | 23:4572750a5c59 | 35 | // OUTPUTS |
Renate | 28:7c7508bdb21f | 36 | PwmOut motor1(D6); // Misschien moeten we hiervoor DigitalOut gebruiken, moet |
Renate | 23:4572750a5c59 | 37 | PwmOut motor2(D5); // samen kunnen gaan met de servo motor |
Renate | 21:456acc79726c | 38 | |
Renate | 23:4572750a5c59 | 39 | DigitalOut motor1_dir(D7); |
Renate | 23:4572750a5c59 | 40 | DigitalOut motor2_dir(D4); |
Renate | 23:4572750a5c59 | 41 | |
Renate | 23:4572750a5c59 | 42 | // VARIABELEN VOOR ENCODER, MOTORHOEK ETC. |
Renate | 21:456acc79726c | 43 | int counts1; |
Renate | 21:456acc79726c | 44 | int counts2; |
Renate | 21:456acc79726c | 45 | const int CPR = 64; // Counts per revolution |
Renate | 21:456acc79726c | 46 | const int full_degrees = 360; |
Renate | 21:456acc79726c | 47 | const int half_degrees = 180; |
Renate | 21:456acc79726c | 48 | double theta_h_1_deg; |
Renate | 21:456acc79726c | 49 | double theta_h_2_deg; |
Renate | 21:456acc79726c | 50 | double theta_h_1_rad; |
Renate | 21:456acc79726c | 51 | double theta_h_2_rad; |
Renate | 21:456acc79726c | 52 | |
Renate | 23:4572750a5c59 | 53 | // DEFINITIES VOOR FILTERS |
Renate | 20:a6a5bdd7d118 | 54 | |
Renate | 21:456acc79726c | 55 | // BICEPS-RECHTS |
Renate | 28:7c7508bdb21f | 56 | // Definities voor eerste BiQuadChain (High-pass en Notch) |
Renate | 28:7c7508bdb21f | 57 | BiQuadChain bqcbr; |
Renate | 28:7c7508bdb21f | 58 | BiQuad bqbr1(0.8006, -1.6012, 0.8006, -1.5610, 0.6414); // High-pass |
Renate | 21:456acc79726c | 59 | BiQuad bqbr2(1, -1.6180, 1, -1.6019, 0.9801); // Notch |
Renate | 21:456acc79726c | 60 | // Na het nemen van de absolute waarde moet de tweede BiQuadChain worden toegepast. |
Renate | 28:7c7508bdb21f | 61 | // Definieer (twee Low-pass -> vierde orde verkrijgen): |
Renate | 21:456acc79726c | 62 | BiQuadChain bqcbr2; |
Renate | 21:456acc79726c | 63 | BiQuad bqbr3(1.5515e-4, 3.1030e-4, 1.5515e-4, -1.9645, 0.9651); // Low-pass |
Renate | 21:456acc79726c | 64 | BiQuad bqbr4(1.5515e-4, 3.1030e-4, 1.5515e-4, -1.9645, 0.9651); // Low-pass |
Renate | 20:a6a5bdd7d118 | 65 | |
Renate | 21:456acc79726c | 66 | // BICEPS-LINKS |
Renate | 28:7c7508bdb21f | 67 | // Definities voor eerste BiQuadChain (High-pass en Notch) |
Renate | 28:7c7508bdb21f | 68 | BiQuadChain bqcbl; |
Renate | 28:7c7508bdb21f | 69 | BiQuad bqbl1(0.8006, -1.6012, 0.8006, -1.5610, 0.6414); // High-pass |
Renate | 21:456acc79726c | 70 | BiQuad bqbl2(1, -1.6180, 1, -1.6019, 0.9801); // Notch |
Renate | 20:a6a5bdd7d118 | 71 | // Na het nemen van de absolute waarde moet de tweede BiQuadChain worden toegepast. |
Renate | 28:7c7508bdb21f | 72 | // Definieer (twee Low-pass -> vierde orde verkrijgen): |
Renate | 21:456acc79726c | 73 | BiQuadChain bqcbl2; |
Renate | 21:456acc79726c | 74 | BiQuad bqbl3(1.5515e-4, 3.1030e-4, 1.5515e-4, -1.9645, 0.9651); // Low-pass |
Renate | 21:456acc79726c | 75 | BiQuad bqbl4(1.5515e-4, 3.1030e-4, 1.5515e-4, -1.9645, 0.9651); // Low-pass |
Renate | 21:456acc79726c | 76 | |
Renate | 21:456acc79726c | 77 | // KUIT |
Renate | 28:7c7508bdb21f | 78 | // Definities voor eerste BiQuadChain (High-pass en Notch) |
Renate | 28:7c7508bdb21f | 79 | BiQuadChain bqck; |
Renate | 28:7c7508bdb21f | 80 | BiQuad bqk1(0.8006, -1.6012, 0.8006, -1.5610, 0.6414); // High-pass |
Renate | 21:456acc79726c | 81 | BiQuad bqk2(1, -1.6180, 1, -1.6019, 0.9801); // Notch |
Renate | 21:456acc79726c | 82 | // Na het nemen van de absolute waarde moet de tweede BiQuadChain worden toegepast. |
Renate | 28:7c7508bdb21f | 83 | // Definieer (twee Low-pass -> vierde orde verkrijgen): |
Renate | 21:456acc79726c | 84 | BiQuadChain bqck2; |
Renate | 21:456acc79726c | 85 | BiQuad bqk3(1.5515e-4, 3.1030e-4, 1.5515e-4, -1.9645, 0.9651); // Low-pass |
Renate | 21:456acc79726c | 86 | BiQuad bqk4(1.5515e-4, 3.1030e-4, 1.5515e-4, -1.9645, 0.9651); // Low-pass |
Renate | 20:a6a5bdd7d118 | 87 | |
Renate | 28:7c7508bdb21f | 88 | // VARIABELEN VOOR EMG + FILTEREN |
Renate | 23:4572750a5c59 | 89 | double filtered_EMG_biceps_right; |
Renate | 23:4572750a5c59 | 90 | double filtered_EMG_biceps_left; |
Renate | 23:4572750a5c59 | 91 | double filtered_EMG_calf; |
Renate | 23:4572750a5c59 | 92 | |
Renate | 23:4572750a5c59 | 93 | double filtered_EMG_biceps_right_1; |
Renate | 23:4572750a5c59 | 94 | double filtered_EMG_biceps_left_1; |
Renate | 23:4572750a5c59 | 95 | double filtered_EMG_calf_1; |
Renate | 23:4572750a5c59 | 96 | |
Renate | 23:4572750a5c59 | 97 | double filtered_EMG_biceps_right_abs; |
Renate | 23:4572750a5c59 | 98 | double filtered_EMG_biceps_left_abs; |
Renate | 23:4572750a5c59 | 99 | double filtered_EMG_calf_abs; |
Renate | 23:4572750a5c59 | 100 | |
Renate | 28:7c7508bdb21f | 101 | double filtered_EMG_biceps_right_total; |
Renate | 23:4572750a5c59 | 102 | double filtered_EMG_biceps_left_total; |
Renate | 23:4572750a5c59 | 103 | double filtered_EMG_calf_total; |
Renate | 19:1fd39a2afc30 | 104 | |
Renate | 23:4572750a5c59 | 105 | // Variabelen voor HIDScope |
Renate | 23:4572750a5c59 | 106 | HIDScope scope(3); |
Renate | 23:4572750a5c59 | 107 | |
Renate | 23:4572750a5c59 | 108 | // VARIABELEN VOOR (INITIATIE VAN) EMG KALIBRATIE LOOP |
Renate | 28:7c7508bdb21f | 109 | bool calib = false; |
Renate | 23:4572750a5c59 | 110 | static int i_calib = 0; |
Renate | 21:456acc79726c | 111 | |
Renate | 23:4572750a5c59 | 112 | double mean_EMG_biceps_right; |
Renate | 23:4572750a5c59 | 113 | double mean_EMG_biceps_left; |
Renate | 23:4572750a5c59 | 114 | double mean_EMG_calf; |
Renate | 23:4572750a5c59 | 115 | |
Renate | 29:8e0a7c33e4e7 | 116 | // VARIABELEN VOOR OPERATION MODE (EMG) |
Renate | 23:4572750a5c59 | 117 | double normalized_EMG_biceps_right; |
Renate | 23:4572750a5c59 | 118 | double normalized_EMG_biceps_left; |
Renate | 23:4572750a5c59 | 119 | double normalized_EMG_calf; |
Renate | 23:4572750a5c59 | 120 | |
Renate | 29:8e0a7c33e4e7 | 121 | // VARIABELEN VOOR OPERATION MODE (RKI) |
Renate | 29:8e0a7c33e4e7 | 122 | double vx; // Gelijk aan variabele die snelheid aangeeft in EMG (in x-richting) |
Renate | 29:8e0a7c33e4e7 | 123 | double vy; // Gelijk aan variabele die snelheid aangeeft in EMG (in y-richting) |
Renate | 29:8e0a7c33e4e7 | 124 | |
Renate | 29:8e0a7c33e4e7 | 125 | const double delta_t = 0.002; |
Renate | 29:8e0a7c33e4e7 | 126 | |
Renate | 30:0a328a9a4788 | 127 | double Joint_1_position_x = 0.0; |
Renate | 30:0a328a9a4788 | 128 | double Joint_2_position_x = 0.0; |
Renate | 30:0a328a9a4788 | 129 | |
Renate | 30:0a328a9a4788 | 130 | double Joint_1_position_y = 0.0; |
Renate | 30:0a328a9a4788 | 131 | double Joint_2_position_y = 0.0; |
Renate | 29:8e0a7c33e4e7 | 132 | |
Renate | 30:0a328a9a4788 | 133 | double Joint_1_position_x_prev = 0.0; |
Renate | 30:0a328a9a4788 | 134 | double Joint_2_position_x_prev = 0.0; |
Renate | 30:0a328a9a4788 | 135 | |
Renate | 30:0a328a9a4788 | 136 | double Joint_1_position_y_prev; |
Renate | 30:0a328a9a4788 | 137 | double Joint_2_position_y_prev; |
Renate | 30:0a328a9a4788 | 138 | |
Renate | 30:0a328a9a4788 | 139 | double Joint_velocity_x[2][1] = {{0.0}, {0.0}}; |
Renate | 30:0a328a9a4788 | 140 | double Joint_velocity_y[2][1] = {{0.0}, {0.0}}; |
Renate | 29:8e0a7c33e4e7 | 141 | |
Renate | 29:8e0a7c33e4e7 | 142 | double q1_dot_x; |
Renate | 29:8e0a7c33e4e7 | 143 | double q2_dot_x; |
Renate | 29:8e0a7c33e4e7 | 144 | |
Renate | 29:8e0a7c33e4e7 | 145 | double q1_dot_y; |
Renate | 29:8e0a7c33e4e7 | 146 | double q2_dot_y; |
Renate | 29:8e0a7c33e4e7 | 147 | |
Renate | 29:8e0a7c33e4e7 | 148 | double q1; |
Renate | 29:8e0a7c33e4e7 | 149 | double q2; |
Renate | 29:8e0a7c33e4e7 | 150 | |
Renate | 30:0a328a9a4788 | 151 | double Motor_1_position_x = 0.0; |
Renate | 30:0a328a9a4788 | 152 | double Motor_2_position_x = 0.0; |
Renate | 30:0a328a9a4788 | 153 | |
Renate | 30:0a328a9a4788 | 154 | double Motor_1_position_y = 0.0; |
Renate | 30:0a328a9a4788 | 155 | double Motor_2_position_y = 0.0; |
Renate | 30:0a328a9a4788 | 156 | |
Renate | 30:0a328a9a4788 | 157 | // VARIABELEN VOOR OPERATION MODE (PI-CONTROLLER) |
Renate | 29:8e0a7c33e4e7 | 158 | |
Renate | 30:0a328a9a4788 | 159 | const double Kp = 17.5; |
Renate | 30:0a328a9a4788 | 160 | const double Ki = 1.02; |
Renate | 30:0a328a9a4788 | 161 | |
Renate | 31:967b455bc328 | 162 | // Voor x-richting |
Renate | 31:967b455bc328 | 163 | double theta_k_1_x = 0.0; |
Renate | 31:967b455bc328 | 164 | double theta_k_2_x = 0.0; |
Renate | 31:967b455bc328 | 165 | |
Renate | 31:967b455bc328 | 166 | double error_integral_1_x = 0.0; |
Renate | 31:967b455bc328 | 167 | double error_integral_2_x = 0.0; |
Renate | 29:8e0a7c33e4e7 | 168 | |
Renate | 31:967b455bc328 | 169 | double u_i_1_x; |
Renate | 31:967b455bc328 | 170 | double u_i_2_x; |
Renate | 31:967b455bc328 | 171 | |
Renate | 31:967b455bc328 | 172 | double theta_t_1_x; |
Renate | 31:967b455bc328 | 173 | double theta_t_2_x; |
Renate | 31:967b455bc328 | 174 | |
Renate | 31:967b455bc328 | 175 | double error_q1_x; |
Renate | 31:967b455bc328 | 176 | double error_q2_x; |
Renate | 30:0a328a9a4788 | 177 | |
Renate | 31:967b455bc328 | 178 | // Voor y-richting |
Renate | 31:967b455bc328 | 179 | double theta_k_1_y = 0.0; |
Renate | 31:967b455bc328 | 180 | double theta_k_2_y = 0.0; |
Renate | 31:967b455bc328 | 181 | |
Renate | 31:967b455bc328 | 182 | double error_integral_1_y = 0.0; |
Renate | 31:967b455bc328 | 183 | double error_integral_2_y = 0.0; |
Renate | 30:0a328a9a4788 | 184 | |
Renate | 31:967b455bc328 | 185 | double u_i_1_y; |
Renate | 31:967b455bc328 | 186 | double u_i_2_y; |
Renate | 31:967b455bc328 | 187 | |
Renate | 31:967b455bc328 | 188 | double theta_t_1_y; |
Renate | 31:967b455bc328 | 189 | double theta_t_2_y; |
Renate | 31:967b455bc328 | 190 | |
Renate | 31:967b455bc328 | 191 | double error_q1_y; |
Renate | 31:967b455bc328 | 192 | double error_q2_y; |
Renate | 31:967b455bc328 | 193 | |
Renate | 31:967b455bc328 | 194 | double abs_theta_t_1_y; |
Renate | 31:967b455bc328 | 195 | double abs_theta_t_2_y; |
Renate | 29:8e0a7c33e4e7 | 196 | |
Renate | 23:4572750a5c59 | 197 | // VOIDS |
Renate | 23:4572750a5c59 | 198 | |
Renate | 23:4572750a5c59 | 199 | // Noodfunctie waarbij alles uitgaat (evt. nog een rood LEDje laten branden). |
Renate | 28:7c7508bdb21f | 200 | // Enige optie is resetten, dan wordt het script opnieuw opgestart. |
Renate | 8:c7d3b67346db | 201 | void emergency() |
Renate | 28:7c7508bdb21f | 202 | { |
Renate | 28:7c7508bdb21f | 203 | loop_ticker.detach(); |
Renate | 28:7c7508bdb21f | 204 | motor1.write(0); |
Renate | 28:7c7508bdb21f | 205 | motor2.write(0); |
Renate | 28:7c7508bdb21f | 206 | pc.printf("Ik ga exploderen!!!\r\n"); |
Renate | 28:7c7508bdb21f | 207 | } |
Renate | 11:4bc0304978e2 | 208 | |
Renate | 28:7c7508bdb21f | 209 | // Motoren uitzetten |
Renate | 8:c7d3b67346db | 210 | void motors_off() |
Renate | 28:7c7508bdb21f | 211 | { |
Renate | 28:7c7508bdb21f | 212 | motor1.write(0); |
Renate | 28:7c7508bdb21f | 213 | motor2.write(0); |
Renate | 28:7c7508bdb21f | 214 | pc.printf("Motoren uit functie\r\n"); |
Renate | 28:7c7508bdb21f | 215 | } |
Renate | 28:7c7508bdb21f | 216 | |
Renate | 14:54343b9fd708 | 217 | // Motoren aanzetten |
Renate | 15:ad065ab92d11 | 218 | void motors_on() |
Renate | 28:7c7508bdb21f | 219 | { |
Renate | 31:967b455bc328 | 220 | motor1.write(0.7); |
Renate | 31:967b455bc328 | 221 | motor1_dir.write(0); |
Renate | 31:967b455bc328 | 222 | motor2.write(0.3); |
Renate | 28:7c7508bdb21f | 223 | motor2_dir.write(1); |
Renate | 31:967b455bc328 | 224 | // Door motor 1 een richting van 1 te geven en motor 2 een van 0, draaien |
Renate | 31:967b455bc328 | 225 | // beide motoren rechtsom |
Renate | 28:7c7508bdb21f | 226 | pc.printf("Motoren aan functie\r\n"); |
Renate | 28:7c7508bdb21f | 227 | } |
Rosalie | 3:6ee0b20c23b0 | 228 | |
Renate | 29:8e0a7c33e4e7 | 229 | void Inverse_Kinematics() |
Renate | 29:8e0a7c33e4e7 | 230 | { |
Renate | 30:0a328a9a4788 | 231 | // Defining joint velocities based on calculations of matlab |
Renate | 29:8e0a7c33e4e7 | 232 | |
Renate | 29:8e0a7c33e4e7 | 233 | Joint_velocity_x[0][0] = (vx*(exp(q2*sqrt(-1.0))*(-1.798599718146044E18+6.546367607647411E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*(-1.798599718146044E18-6.546367607647411E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*(1.168095770314175E18+1.321169505615574E18*sqrt(-1.0))+exp(q1*sqrt(-1.0))*exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*3.828059683264922E18+1.168095770314175E18-1.321169505615574E18*sqrt(-1.0))*(2.0E1/1.7E1))/(cos(q1+3.141592653589793/9.0)*(6.605847528077872E17+5.840478851570874E17*sqrt(-1.0))+sin(q1+3.141592653589793/9.0)*(-5.840478851570874E17+6.605847528077872E17*sqrt(-1.0))+exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(-3.273183803823705E17-8.992998590730221E17*sqrt(-1.0))+exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(8.992998590730221E17-3.273183803823705E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(-3.273183803823705E17+8.992998590730221E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(6.605847528077872E17-5.840478851570874E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(8.992998590730221E17+3.273183803823705E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(-5.840478851570874E17-6.605847528077872E17*sqrt(-1.0))); |
Renate | 29:8e0a7c33e4e7 | 234 | Joint_velocity_x[1][0] = (vx*(exp(q2*sqrt(-1.0))*(1.798599718146044E18-6.546367607647411E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*(1.798599718146044E18+6.546367607647411E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*(-1.168095770314175E18-1.321169505615574E18*sqrt(-1.0))+-1.168095770314175E18+1.321169505615574E18*sqrt(-1.0))*(2.0E1/1.7E1))/(cos(q1+3.141592653589793/9.0)*(6.605847528077872E17+5.840478851570874E17*sqrt(-1.0))+sin(q1+3.141592653589793/9.0)*(-5.840478851570874E17+6.605847528077872E17*sqrt(-1.0))+exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(-3.273183803823705E17-8.992998590730221E17*sqrt(-1.0))+exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(8.992998590730221E17-3.273183803823705E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(-3.273183803823705E17+8.992998590730221E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(6.605847528077872E17-5.840478851570874E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(8.992998590730221E17+3.273183803823705E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(-5.840478851570874E17-6.605847528077872E17*sqrt(-1.0))); |
Renate | 29:8e0a7c33e4e7 | 235 | |
Renate | 29:8e0a7c33e4e7 | 236 | Joint_velocity_y[0][0] = (vy*(exp(q2*sqrt(-1.0))*(-3.273183803823705E17-8.992998590730221E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*(-3.273183803823705E17+8.992998590730221E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*(6.605847528077872E17-5.840478851570874E17*sqrt(-1.0))+exp(q1*sqrt(-1.0))*exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*1.914029841632461E18+6.605847528077872E17+5.840478851570874E17*sqrt(-1.0))*(4.0E1/1.7E1))/(cos(q1+3.141592653589793/9.0)*(6.605847528077872E17+5.840478851570874E17*sqrt(-1.0))+sin(q1+3.141592653589793/9.0)*(-5.840478851570874E17+6.605847528077872E17*sqrt(-1.0))+exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(-3.273183803823705E17-8.992998590730221E17*sqrt(-1.0))+exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(8.992998590730221E17-3.273183803823705E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(-3.273183803823705E17+8.992998590730221E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(6.605847528077872E17-5.840478851570874E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(8.992998590730221E17+3.273183803823705E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(-5.840478851570874E17-6.605847528077872E17*sqrt(-1.0))); |
Renate | 29:8e0a7c33e4e7 | 237 | Joint_velocity_y[1][0] = (vy*(exp(q2*sqrt(-1.0))*(3.273183803823705E17+8.992998590730221E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*(3.273183803823705E17-8.992998590730221E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*(-6.605847528077872E17+5.840478851570874E17*sqrt(-1.0))+-6.605847528077872E17-5.840478851570874E17*sqrt(-1.0))*(4.0E1/1.7E1))/(cos(q1+3.141592653589793/9.0)*(6.605847528077872E17+5.840478851570874E17*sqrt(-1.0))+sin(q1+3.141592653589793/9.0)*(-5.840478851570874E17+6.605847528077872E17*sqrt(-1.0))+exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(-3.273183803823705E17-8.992998590730221E17*sqrt(-1.0))+exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(8.992998590730221E17-3.273183803823705E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(-3.273183803823705E17+8.992998590730221E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*cos(q1+3.141592653589793/9.0)*(6.605847528077872E17-5.840478851570874E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(8.992998590730221E17+3.273183803823705E17*sqrt(-1.0))+exp(q1*2.0*sqrt(-1.0))*exp(q2*2.0*sqrt(-1.0))*sin(q1+3.141592653589793/9.0)*(-5.840478851570874E17-6.605847528077872E17*sqrt(-1.0))); |
Renate | 29:8e0a7c33e4e7 | 238 | |
Renate | 30:0a328a9a4788 | 239 | q1_dot_x = Joint_velocity_x[0][0]; |
Renate | 30:0a328a9a4788 | 240 | q2_dot_x = Joint_velocity_x[1][0]; |
Renate | 29:8e0a7c33e4e7 | 241 | |
Renate | 30:0a328a9a4788 | 242 | q1_dot_y = Joint_velocity_y[0][0]; |
Renate | 30:0a328a9a4788 | 243 | q2_dot_y = Joint_velocity_y[1][0]; |
Renate | 29:8e0a7c33e4e7 | 244 | |
Renate | 29:8e0a7c33e4e7 | 245 | // Integratie |
Renate | 30:0a328a9a4788 | 246 | Joint_1_position_x = Joint_1_position_x_prev + Joint_velocity_x[0][0]*delta_t; |
Renate | 30:0a328a9a4788 | 247 | Joint_2_position_x = Joint_2_position_x_prev + Joint_velocity_x[1][0]*delta_t; |
Renate | 30:0a328a9a4788 | 248 | |
Renate | 30:0a328a9a4788 | 249 | Joint_1_position_y = Joint_1_position_y_prev + Joint_velocity_y[0][0]*delta_t; |
Renate | 30:0a328a9a4788 | 250 | Joint_2_position_y = Joint_2_position_y_prev + Joint_velocity_y[1][0]*delta_t; |
Renate | 29:8e0a7c33e4e7 | 251 | |
Renate | 30:0a328a9a4788 | 252 | Joint_1_position_x_prev = Joint_1_position_x; |
Renate | 30:0a328a9a4788 | 253 | Joint_2_position_x_prev = Joint_2_position_x; |
Renate | 30:0a328a9a4788 | 254 | |
Renate | 30:0a328a9a4788 | 255 | Joint_1_position_y_prev = Joint_1_position_y; |
Renate | 30:0a328a9a4788 | 256 | Joint_2_position_y_prev = Joint_2_position_y; |
Renate | 30:0a328a9a4788 | 257 | |
Renate | 30:0a328a9a4788 | 258 | Motor_1_position_x = Joint_1_position_x; |
Renate | 30:0a328a9a4788 | 259 | Motor_2_position_x = Joint_1_position_x + Joint_2_position_x; |
Renate | 29:8e0a7c33e4e7 | 260 | |
Renate | 30:0a328a9a4788 | 261 | Motor_1_position_y = Joint_1_position_y; |
Renate | 30:0a328a9a4788 | 262 | Motor_2_position_y = Joint_1_position_y + Joint_2_position_y; |
Renate | 30:0a328a9a4788 | 263 | } |
Renate | 29:8e0a7c33e4e7 | 264 | |
Renate | 31:967b455bc328 | 265 | // PI-CONTROLLER X-RICHTING |
Renate | 31:967b455bc328 | 266 | void PI_controller_x() |
Renate | 31:967b455bc328 | 267 | { |
Renate | 31:967b455bc328 | 268 | // Proportional part: |
Renate | 31:967b455bc328 | 269 | theta_k_1_x= Kp * error_q1_x; |
Renate | 31:967b455bc328 | 270 | theta_k_2_x= Kp * error_q2_x; |
Renate | 31:967b455bc328 | 271 | |
Renate | 31:967b455bc328 | 272 | // Integral part |
Renate | 31:967b455bc328 | 273 | error_integral_1_x = error_integral_1_x + error_q1_x *delta_t; |
Renate | 31:967b455bc328 | 274 | error_integral_2_x = error_integral_2_x + error_q2_x *delta_t; |
Renate | 31:967b455bc328 | 275 | u_i_1_x= Ki * error_integral_1_x; |
Renate | 31:967b455bc328 | 276 | u_i_2_x= Ki * error_integral_2_x; |
Renate | 31:967b455bc328 | 277 | |
Renate | 31:967b455bc328 | 278 | // sum all parts and return it |
Renate | 31:967b455bc328 | 279 | theta_t_1_x = theta_k_1_x + u_i_1_x; |
Renate | 31:967b455bc328 | 280 | theta_t_2_x = theta_k_2_x + u_i_2_x; |
Renate | 31:967b455bc328 | 281 | } |
Renate | 31:967b455bc328 | 282 | |
Renate | 31:967b455bc328 | 283 | // PI-CONTROLLER Y-RICHTING |
Renate | 30:0a328a9a4788 | 284 | void PI_controller_y() |
Renate | 30:0a328a9a4788 | 285 | { |
Renate | 30:0a328a9a4788 | 286 | // Proportional part: |
Renate | 31:967b455bc328 | 287 | theta_k_1_y= Kp * error_q1_y; |
Renate | 31:967b455bc328 | 288 | theta_k_2_y= Kp * error_q2_y; |
Renate | 30:0a328a9a4788 | 289 | |
Renate | 30:0a328a9a4788 | 290 | // Integral part |
Renate | 31:967b455bc328 | 291 | error_integral_1_y = error_integral_1_y + error_q1_y *delta_t; |
Renate | 31:967b455bc328 | 292 | error_integral_2_y = error_integral_2_y + error_q2_y *delta_t; |
Renate | 31:967b455bc328 | 293 | u_i_1_y= Ki * error_integral_1_y; |
Renate | 31:967b455bc328 | 294 | u_i_2_y= Ki * error_integral_2_y; |
Renate | 30:0a328a9a4788 | 295 | |
Renate | 30:0a328a9a4788 | 296 | // sum all parts and return it |
Renate | 31:967b455bc328 | 297 | theta_t_1_y = theta_k_1_y + u_i_1_y; |
Renate | 31:967b455bc328 | 298 | theta_t_2_y = theta_k_2_y + u_i_2_y; |
Renate | 29:8e0a7c33e4e7 | 299 | } |
Renate | 29:8e0a7c33e4e7 | 300 | |
Renate | 31:967b455bc328 | 301 | void Define_motor_dir() |
Renate | 31:967b455bc328 | 302 | { |
Renate | 31:967b455bc328 | 303 | if (theta_t_1_y >= 0 && theta_t_2_y >= 0) { |
Renate | 31:967b455bc328 | 304 | motor1_dir.write(0); |
Renate | 31:967b455bc328 | 305 | motor2_dir.write(1); |
Renate | 31:967b455bc328 | 306 | } |
Renate | 31:967b455bc328 | 307 | if (theta_t_1_y < 0 && theta_t_2_y >= 0) { |
Renate | 31:967b455bc328 | 308 | motor1_dir.write(1); |
Renate | 31:967b455bc328 | 309 | motor1_dir.write(1); |
Renate | 31:967b455bc328 | 310 | } |
Renate | 31:967b455bc328 | 311 | if (theta_t_1_y >= 0 && theta_t_2_y < 0) { |
Renate | 31:967b455bc328 | 312 | motor1_dir.write(0); |
Renate | 31:967b455bc328 | 313 | motor2_dir.write(0); |
Renate | 31:967b455bc328 | 314 | } else { |
Renate | 31:967b455bc328 | 315 | motor1_dir.write(1); |
Renate | 31:967b455bc328 | 316 | motor2_dir.write(0); |
Renate | 31:967b455bc328 | 317 | } |
Renate | 31:967b455bc328 | 318 | } |
Renate | 6:64146e16e10c | 319 | // Finite state machine programming (calibration servo motor?) |
Renate | 28:7c7508bdb21f | 320 | void ProcessStateMachine(void) |
Renate | 28:7c7508bdb21f | 321 | { |
Renate | 23:4572750a5c59 | 322 | // Berekenen van de motorhoeken (in radialen) |
Renate | 28:7c7508bdb21f | 323 | counts1 = Encoder1.getPulses(); |
Renate | 28:7c7508bdb21f | 324 | counts2 = Encoder2.getPulses(); |
Renate | 23:4572750a5c59 | 325 | theta_h_1_deg=(counts1/(double)CPR)*(double)full_degrees; |
Renate | 23:4572750a5c59 | 326 | theta_h_2_deg=(counts2/(double)CPR)*(double)full_degrees; |
Renate | 23:4572750a5c59 | 327 | theta_h_1_rad=(theta_h_1_deg/half_degrees)*M_PI; |
Renate | 23:4572750a5c59 | 328 | theta_h_2_rad=(theta_h_2_deg/half_degrees)*M_PI; |
Renate | 28:7c7508bdb21f | 329 | |
Renate | 29:8e0a7c33e4e7 | 330 | // Calculating joint angles based on motor angles (current encoder values) |
Renate | 29:8e0a7c33e4e7 | 331 | q1 = theta_h_1_rad; // Relative angle joint 1 (rad) |
Renate | 29:8e0a7c33e4e7 | 332 | q2 = theta_h_2_rad - theta_h_1_rad; // Relative angle joint 2 (rad) |
Renate | 29:8e0a7c33e4e7 | 333 | |
Renate | 23:4572750a5c59 | 334 | // Eerste deel van de filters (High-pass + Notch) over het ruwe EMG signaal |
Renate | 28:7c7508bdb21f | 335 | // doen. Het ruwe signaal wordt gelezen binnen een ticker en wordt daardoor 'gesampled' |
Renate | 23:4572750a5c59 | 336 | filtered_EMG_biceps_right_1=bqbr1.step(EMG_biceps_right_raw.read()); |
Renate | 23:4572750a5c59 | 337 | filtered_EMG_biceps_left_1=bqcbl.step(EMG_biceps_left_raw.read()); |
Renate | 23:4572750a5c59 | 338 | filtered_EMG_calf_1=bqck.step(EMG_calf_raw.read()); |
Renate | 28:7c7508bdb21f | 339 | |
Renate | 23:4572750a5c59 | 340 | // Vervolgens wordt de absolute waarde hiervan genomen |
Renate | 23:4572750a5c59 | 341 | filtered_EMG_biceps_right_abs=abs(filtered_EMG_biceps_right_1); |
Renate | 23:4572750a5c59 | 342 | filtered_EMG_biceps_left_abs=abs(filtered_EMG_biceps_left_1); |
Renate | 23:4572750a5c59 | 343 | filtered_EMG_calf_abs=abs(filtered_EMG_calf_1); |
Renate | 28:7c7508bdb21f | 344 | |
Renate | 23:4572750a5c59 | 345 | // Tenslotte wordt het tweede deel van de filters (twee low-pass, voor 4e orde filter) |
Renate | 23:4572750a5c59 | 346 | // over het signaal gedaan |
Renate | 23:4572750a5c59 | 347 | filtered_EMG_biceps_right=bqcbr2.step(filtered_EMG_biceps_right_abs); |
Renate | 23:4572750a5c59 | 348 | filtered_EMG_biceps_left=bqcbl2.step(filtered_EMG_biceps_left_abs); |
Renate | 23:4572750a5c59 | 349 | filtered_EMG_calf=bqck2.step(filtered_EMG_calf_abs); |
Renate | 28:7c7508bdb21f | 350 | |
Renate | 28:7c7508bdb21f | 351 | // De gefilterde EMG-signalen kunnen tevens visueel worden weergegeven in de HIDScope |
Renate | 28:7c7508bdb21f | 352 | scope.set(0, normalized_EMG_biceps_right); |
Renate | 28:7c7508bdb21f | 353 | scope.set(1, normalized_EMG_biceps_left); |
Renate | 28:7c7508bdb21f | 354 | scope.set(2, normalized_EMG_calf); |
Renate | 23:4572750a5c59 | 355 | scope.send(); |
Renate | 28:7c7508bdb21f | 356 | |
Renate | 28:7c7508bdb21f | 357 | // Tijdens de kalibratie moet vervolgens een maximale spierspanning worden bepaald, die |
Renate | 28:7c7508bdb21f | 358 | // later kan worden gebruikt voor een normalisatie. De spieren worden hiertoe gedurende |
Renate | 23:4572750a5c59 | 359 | // 5 seconden maximaal aangespannen. De EMG waarden worden bij elkaar opgeteld, |
Renate | 28:7c7508bdb21f | 360 | // waarna het gemiddelde wordt bepaald. |
Renate | 28:7c7508bdb21f | 361 | if (calib) { |
Renate | 28:7c7508bdb21f | 362 | if (i_calib == 0) { |
Renate | 28:7c7508bdb21f | 363 | filtered_EMG_biceps_right_total=0; |
Renate | 28:7c7508bdb21f | 364 | filtered_EMG_biceps_left_total=0; |
Renate | 28:7c7508bdb21f | 365 | filtered_EMG_calf_total=0; |
Renate | 28:7c7508bdb21f | 366 | } |
Renate | 28:7c7508bdb21f | 367 | if (i_calib <= 2500) { |
Renate | 28:7c7508bdb21f | 368 | filtered_EMG_biceps_right_total+=filtered_EMG_biceps_right; |
Renate | 28:7c7508bdb21f | 369 | filtered_EMG_biceps_left_total+=filtered_EMG_biceps_left; |
Renate | 28:7c7508bdb21f | 370 | filtered_EMG_calf_total+=filtered_EMG_calf; |
Renate | 28:7c7508bdb21f | 371 | i_calib++; |
Renate | 28:7c7508bdb21f | 372 | } |
Renate | 28:7c7508bdb21f | 373 | if (i_calib > 2500) { |
Renate | 28:7c7508bdb21f | 374 | mean_EMG_biceps_right=filtered_EMG_biceps_right_total/2500.0; |
Renate | 28:7c7508bdb21f | 375 | mean_EMG_biceps_left=filtered_EMG_biceps_left_total/2500.0; |
Renate | 28:7c7508bdb21f | 376 | mean_EMG_calf=filtered_EMG_calf_total/2500.0; |
Renate | 28:7c7508bdb21f | 377 | pc.printf("Ontspan spieren\r\n"); |
Renate | 28:7c7508bdb21f | 378 | pc.printf("Rechterbiceps_max = %f, Linkerbiceps_max = %f, Kuit_max = %f\r\n", mean_EMG_biceps_right, mean_EMG_biceps_left, mean_EMG_calf); |
Renate | 28:7c7508bdb21f | 379 | calib = false; |
Renate | 28:7c7508bdb21f | 380 | } |
Renate | 28:7c7508bdb21f | 381 | } |
Renate | 28:7c7508bdb21f | 382 | |
Renate | 23:4572750a5c59 | 383 | // Genormaliseerde EMG's berekenen |
Renate | 23:4572750a5c59 | 384 | normalized_EMG_biceps_right=filtered_EMG_biceps_right/mean_EMG_biceps_right; |
Renate | 23:4572750a5c59 | 385 | normalized_EMG_biceps_left=filtered_EMG_biceps_left/mean_EMG_biceps_left; |
Renate | 23:4572750a5c59 | 386 | normalized_EMG_calf=filtered_EMG_calf/mean_EMG_calf; |
Renate | 28:7c7508bdb21f | 387 | |
Renate | 28:7c7508bdb21f | 388 | // Finite state machine |
Renate | 28:7c7508bdb21f | 389 | switch (currentState) { |
Renate | 6:64146e16e10c | 390 | case Motors_off: |
Renate | 28:7c7508bdb21f | 391 | |
Renate | 28:7c7508bdb21f | 392 | if (stateChanged) { |
Renate | 8:c7d3b67346db | 393 | motors_off(); // functie waarbij motoren uitgaan |
Renate | 11:4bc0304978e2 | 394 | stateChanged = false; |
Renate | 9:4de589636f50 | 395 | pc.printf("Motors off state\r\n"); |
Renate | 28:7c7508bdb21f | 396 | } |
Renate | 29:8e0a7c33e4e7 | 397 | if (Emergency_button_pressed.read() == false) { // Normaal waarde 1 bij indrukken, nu nul -> false |
Renate | 29:8e0a7c33e4e7 | 398 | emergency(); |
Renate | 29:8e0a7c33e4e7 | 399 | } |
Renate | 28:7c7508bdb21f | 400 | if (Power_button_pressed.read() == false) { // Normaal waarde 1 bij indrukken, nu nul -> false |
Renate | 9:4de589636f50 | 401 | currentState = Calib_motor; |
Renate | 11:4bc0304978e2 | 402 | stateChanged = true; |
Renate | 11:4bc0304978e2 | 403 | pc.printf("Moving to Calib_motor state\r\n"); |
Renate | 6:64146e16e10c | 404 | } |
Renate | 6:64146e16e10c | 405 | break; |
Renate | 28:7c7508bdb21f | 406 | |
Renate | 9:4de589636f50 | 407 | case Calib_motor: |
Renate | 28:7c7508bdb21f | 408 | |
Renate | 29:8e0a7c33e4e7 | 409 | if (stateChanged) { |
Renate | 29:8e0a7c33e4e7 | 410 | pc.printf("Zet motoren handmatig in home positie\r\n"); |
Renate | 29:8e0a7c33e4e7 | 411 | stateChanged = false; |
Renate | 29:8e0a7c33e4e7 | 412 | } |
Renate | 29:8e0a7c33e4e7 | 413 | |
Renate | 29:8e0a7c33e4e7 | 414 | if (Emergency_button_pressed.read() == false) { |
Renate | 29:8e0a7c33e4e7 | 415 | emergency(); |
Renate | 29:8e0a7c33e4e7 | 416 | } |
Renate | 29:8e0a7c33e4e7 | 417 | if (Motor_calib_button_pressed.read() == false) { |
Renate | 21:456acc79726c | 418 | theta_h_1_rad = 0; |
Renate | 21:456acc79726c | 419 | theta_h_2_rad = 0; |
Renate | 21:456acc79726c | 420 | pc.printf("Huidige hoek in radialen motor 1:%f en motor 2: %f (moet 0 zijn) \r\n", theta_h_1_rad, theta_h_2_rad); |
Renate | 11:4bc0304978e2 | 421 | currentState = Calib_EMG; |
Renate | 11:4bc0304978e2 | 422 | stateChanged = true; |
Renate | 9:4de589636f50 | 423 | pc.printf("Moving to Calib_EMG state\r\n"); |
Renate | 28:7c7508bdb21f | 424 | } |
Renate | 11:4bc0304978e2 | 425 | break; |
Renate | 28:7c7508bdb21f | 426 | |
Renate | 28:7c7508bdb21f | 427 | case Calib_EMG: |
Renate | 28:7c7508bdb21f | 428 | |
Renate | 28:7c7508bdb21f | 429 | if (stateChanged) { |
Renate | 28:7c7508bdb21f | 430 | i_calib = 0; |
Renate | 28:7c7508bdb21f | 431 | calib = true; |
Renate | 28:7c7508bdb21f | 432 | pc.printf("Span spieren aan\r\n"); |
Renate | 28:7c7508bdb21f | 433 | stateChanged = false; |
Renate | 28:7c7508bdb21f | 434 | } |
Renate | 28:7c7508bdb21f | 435 | |
Renate | 29:8e0a7c33e4e7 | 436 | if (Emergency_button_pressed.read() == false) { |
Renate | 29:8e0a7c33e4e7 | 437 | emergency(); |
Renate | 29:8e0a7c33e4e7 | 438 | } |
Renate | 29:8e0a7c33e4e7 | 439 | |
Renate | 28:7c7508bdb21f | 440 | if (i_calib > 2500) { |
Renate | 28:7c7508bdb21f | 441 | calib = false; |
Renate | 28:7c7508bdb21f | 442 | currentState = Homing; |
Renate | 28:7c7508bdb21f | 443 | stateChanged = true; |
Renate | 28:7c7508bdb21f | 444 | pc.printf("Moving to Homing state\r\n"); |
Renate | 28:7c7508bdb21f | 445 | } |
Renate | 28:7c7508bdb21f | 446 | break; |
Renate | 28:7c7508bdb21f | 447 | |
Renate | 28:7c7508bdb21f | 448 | case Homing: // NOG NAAR KIJKEN |
Renate | 28:7c7508bdb21f | 449 | |
Renate | 28:7c7508bdb21f | 450 | if (stateChanged) { |
Renate | 28:7c7508bdb21f | 451 | // Ervoor zorgen dat de motoren zo bewegen dat de robotarm |
Renate | 11:4bc0304978e2 | 452 | // (inclusief de end-effector) in de juiste home positie wordt gezet |
Renate | 21:456acc79726c | 453 | motors_on(); |
Renate | 28:7c7508bdb21f | 454 | stateChanged = false; |
Renate | 11:4bc0304978e2 | 455 | } |
Renate | 28:7c7508bdb21f | 456 | if (Emergency_button_pressed.read() == false) { |
Renate | 10:83f3cec8dd1c | 457 | emergency(); |
Renate | 28:7c7508bdb21f | 458 | } |
Renate | 29:8e0a7c33e4e7 | 459 | |
Renate | 29:8e0a7c33e4e7 | 460 | // Hier moet iets van encoder counts waarde maal -1 worden gedaan om |
Renate | 29:8e0a7c33e4e7 | 461 | // naar positie 0 te bewegen |
Renate | 29:8e0a7c33e4e7 | 462 | |
Renate | 29:8e0a7c33e4e7 | 463 | if (Power_button_pressed.read() == false) |
Renate | 29:8e0a7c33e4e7 | 464 | //if (theta_h_1_rad == 0.0 && theta_h_2_rad == 0.0) |
Renate | 29:8e0a7c33e4e7 | 465 | // Veranderen wanneer encoders werken en motor in elkaar zit |
Renate | 28:7c7508bdb21f | 466 | { |
Renate | 28:7c7508bdb21f | 467 | currentState = Operation_mode; |
Renate | 28:7c7508bdb21f | 468 | stateChanged = true; |
Renate | 29:8e0a7c33e4e7 | 469 | pc.printf("Moving to operation mode \r\n"); |
Renate | 28:7c7508bdb21f | 470 | } |
Renate | 29:8e0a7c33e4e7 | 471 | |
Renate | 28:7c7508bdb21f | 472 | break; |
Renate | 28:7c7508bdb21f | 473 | |
Renate | 28:7c7508bdb21f | 474 | case Operation_mode: // Overgaan tot emergency wanneer referentie niet |
Renate | 28:7c7508bdb21f | 475 | // overeenkomt met werkelijkheid |
Renate | 28:7c7508bdb21f | 476 | |
Renate | 23:4572750a5c59 | 477 | // pc.printf("normalized_EMG_biceps_right= %f, mean_EMG_biceps_right = %f, filtered_EMG_biceps_right = %f\r\n", normalized_EMG_biceps_right, mean_EMG_biceps_right, filtered_EMG_biceps_right); |
Renate | 29:8e0a7c33e4e7 | 478 | if (stateChanged) { |
Renate | 29:8e0a7c33e4e7 | 479 | motors_off(); |
Renate | 30:0a328a9a4788 | 480 | Joint_1_position_x = 0; |
Renate | 30:0a328a9a4788 | 481 | Joint_2_position_x = 0; |
Renate | 30:0a328a9a4788 | 482 | Joint_1_position_y = 0; |
Renate | 30:0a328a9a4788 | 483 | Joint_2_position_y = 0; |
Renate | 30:0a328a9a4788 | 484 | Joint_1_position_x_prev = Joint_1_position_x; |
Renate | 30:0a328a9a4788 | 485 | Joint_2_position_x_prev = Joint_2_position_x; |
Renate | 30:0a328a9a4788 | 486 | Joint_1_position_y_prev = Joint_1_position_y; |
Renate | 30:0a328a9a4788 | 487 | Joint_2_position_y_prev = Joint_2_position_y; |
Renate | 29:8e0a7c33e4e7 | 488 | Joint_velocity_x[0][0] = 0; |
Renate | 29:8e0a7c33e4e7 | 489 | Joint_velocity_x[1][0] = 0; |
Renate | 29:8e0a7c33e4e7 | 490 | Joint_velocity_y[0][0] = 0; |
Renate | 29:8e0a7c33e4e7 | 491 | Joint_velocity_y[1][0] = 0; |
Renate | 30:0a328a9a4788 | 492 | Motor_1_position_x = 0; |
Renate | 30:0a328a9a4788 | 493 | Motor_2_position_x = 0; |
Renate | 30:0a328a9a4788 | 494 | Motor_1_position_y = 0; |
Renate | 30:0a328a9a4788 | 495 | Motor_2_position_y = 0; |
Renate | 31:967b455bc328 | 496 | theta_k_1_x = 0.0; |
Renate | 31:967b455bc328 | 497 | theta_k_2_x = 0.0; |
Renate | 31:967b455bc328 | 498 | error_integral_1_x = 0.0; |
Renate | 31:967b455bc328 | 499 | error_integral_2_x = 0.0; |
Renate | 31:967b455bc328 | 500 | theta_k_1_y = 0.0; |
Renate | 31:967b455bc328 | 501 | theta_k_2_y = 0.0; |
Renate | 31:967b455bc328 | 502 | error_integral_1_y = 0.0; |
Renate | 31:967b455bc328 | 503 | error_integral_2_y = 0.0; |
Renate | 29:8e0a7c33e4e7 | 504 | stateChanged = false; |
Renate | 29:8e0a7c33e4e7 | 505 | } |
Renate | 29:8e0a7c33e4e7 | 506 | // Hier moet een functie worden aangeroepen die ervoor zorgt dat |
Renate | 29:8e0a7c33e4e7 | 507 | // aan de hand van EMG-signalen de motoren kunnen worden aangestuurd, |
Renate | 29:8e0a7c33e4e7 | 508 | // zodat de robotarm kan bewegen |
Renate | 28:7c7508bdb21f | 509 | |
Renate | 29:8e0a7c33e4e7 | 510 | // if (Power_button_pressed.read() == false) { // Normaal waarde 1 bij indrukken, nu nul -> false |
Renate | 29:8e0a7c33e4e7 | 511 | // motors_off(); |
Renate | 29:8e0a7c33e4e7 | 512 | // currentState = Motors_off; |
Renate | 29:8e0a7c33e4e7 | 513 | // stateChanged = true; |
Renate | 29:8e0a7c33e4e7 | 514 | // pc.printf("Terug naar de state Motors_off\r\n"); |
Renate | 29:8e0a7c33e4e7 | 515 | // } |
Renate | 29:8e0a7c33e4e7 | 516 | if (Emergency_button_pressed.read() == false) { |
Renate | 29:8e0a7c33e4e7 | 517 | emergency(); |
Renate | 29:8e0a7c33e4e7 | 518 | } |
Renate | 29:8e0a7c33e4e7 | 519 | if (Motor_calib_button_pressed.read() == false) { // Is nu voor de homing |
Renate | 29:8e0a7c33e4e7 | 520 | motors_off(); |
Renate | 29:8e0a7c33e4e7 | 521 | currentState = Homing; |
Renate | 29:8e0a7c33e4e7 | 522 | stateChanged = true; |
Renate | 29:8e0a7c33e4e7 | 523 | pc.printf("Terug naar de state Homing\r\n"); |
Renate | 29:8e0a7c33e4e7 | 524 | } |
Renate | 29:8e0a7c33e4e7 | 525 | if (normalized_EMG_biceps_right >= 0.3) { |
Renate | 31:967b455bc328 | 526 | |
Renate | 31:967b455bc328 | 527 | if (normalized_EMG_calf < 0.3) |
Renate | 31:967b455bc328 | 528 | { |
Renate | 31:967b455bc328 | 529 | vx = 0.0; |
Renate | 31:967b455bc328 | 530 | vy = 0.05; |
Renate | 31:967b455bc328 | 531 | } |
Renate | 31:967b455bc328 | 532 | if (normalized_EMG_calf >= 0.3) |
Renate | 31:967b455bc328 | 533 | { |
Renate | 31:967b455bc328 | 534 | vx = 0.0; |
Renate | 31:967b455bc328 | 535 | vy = -0.05; |
Renate | 31:967b455bc328 | 536 | } |
Renate | 30:0a328a9a4788 | 537 | Inverse_Kinematics(); |
Renate | 30:0a328a9a4788 | 538 | error_q1_y = Motor_1_position_y - theta_h_1_rad; |
Renate | 31:967b455bc328 | 539 | error_q2_y = Motor_2_position_y - theta_h_2_rad; |
Renate | 30:0a328a9a4788 | 540 | PI_controller_y(); |
Renate | 31:967b455bc328 | 541 | Define_motor_dir(); |
Renate | 31:967b455bc328 | 542 | |
Renate | 31:967b455bc328 | 543 | abs_theta_t_1_y = abs(theta_t_1_y); |
Renate | 31:967b455bc328 | 544 | abs_theta_t_2_y = abs(theta_t_2_y); |
Renate | 30:0a328a9a4788 | 545 | |
Renate | 29:8e0a7c33e4e7 | 546 | //if (normalized_EMG_calf >= 0.3) |
Renate | 29:8e0a7c33e4e7 | 547 | //{ |
Renate | 29:8e0a7c33e4e7 | 548 | //motor1.write(0.1); |
Renate | 29:8e0a7c33e4e7 | 549 | //motor1_dir = !motor1_dir; |
Renate | 29:8e0a7c33e4e7 | 550 | //} |
Renate | 28:7c7508bdb21f | 551 | |
Renate | 29:8e0a7c33e4e7 | 552 | } else if (normalized_EMG_biceps_left >= 0.3) { |
Renate | 31:967b455bc328 | 553 | vx = 0.05; |
Renate | 31:967b455bc328 | 554 | vy = 0.0; |
Renate | 31:967b455bc328 | 555 | Inverse_Kinematics(); |
Renate | 31:967b455bc328 | 556 | error_q1_x = Motor_1_position_x - theta_h_1_rad; |
Renate | 31:967b455bc328 | 557 | error_q2_x = Motor_2_position_x - theta_h_2_rad; |
Renate | 31:967b455bc328 | 558 | PI_controller_x(); |
Renate | 31:967b455bc328 | 559 | |
Renate | 31:967b455bc328 | 560 | |
Renate | 31:967b455bc328 | 561 | |
Renate | 31:967b455bc328 | 562 | |
Renate | 31:967b455bc328 | 563 | |
Renate | 31:967b455bc328 | 564 | |
Renate | 31:967b455bc328 | 565 | motor2.write(0.7); |
Renate | 31:967b455bc328 | 566 | motor1.write(0.7); |
Renate | 29:8e0a7c33e4e7 | 567 | motor1_dir.write(1); |
Renate | 31:967b455bc328 | 568 | motor2_dir.write(0); |
Renate | 28:7c7508bdb21f | 569 | //if (normalized_EMG_calf >= 0.3) |
Renate | 28:7c7508bdb21f | 570 | //{ |
Renate | 28:7c7508bdb21f | 571 | // motor1_dir = !motor1_dir; |
Renate | 28:7c7508bdb21f | 572 | // pc.printf("Richting zou om moeten draaien"); |
Renate | 28:7c7508bdb21f | 573 | // motor2_dir = !motor2_dir; |
Renate | 28:7c7508bdb21f | 574 | //} |
Renate | 29:8e0a7c33e4e7 | 575 | } else { |
Renate | 29:8e0a7c33e4e7 | 576 | motor1.write(0); |
Renate | 29:8e0a7c33e4e7 | 577 | motor2.write(0); |
Renate | 28:7c7508bdb21f | 578 | } |
Renate | 28:7c7508bdb21f | 579 | |
Renate | 21:456acc79726c | 580 | break; |
Renate | 28:7c7508bdb21f | 581 | |
Renate | 7:1d57463393c6 | 582 | default: |
Renate | 7:1d57463393c6 | 583 | // Zelfde functie als die eerder is toegepast om motoren uit te schakelen -> safety! |
Renate | 14:54343b9fd708 | 584 | motors_off(); |
Renate | 9:4de589636f50 | 585 | pc.printf("Unknown or uninplemented state reached!\r\n"); |
Renate | 28:7c7508bdb21f | 586 | |
WiesjeRoskamp | 2:aee655d11b6d | 587 | } |
Renate | 11:4bc0304978e2 | 588 | } |
WiesjeRoskamp | 2:aee655d11b6d | 589 | |
Renate | 8:c7d3b67346db | 590 | int main(void) |
Renate | 28:7c7508bdb21f | 591 | { |
Renate | 28:7c7508bdb21f | 592 | pc.printf("Opstarten\r\n"); |
Renate | 23:4572750a5c59 | 593 | |
Renate | 28:7c7508bdb21f | 594 | // Chain voor rechter biceps |
Renate | 28:7c7508bdb21f | 595 | bqcbr.add(&bqbr1).add(&bqbr2); |
Renate | 28:7c7508bdb21f | 596 | bqcbr2.add(&bqbr3).add(&bqbr4); |
Renate | 28:7c7508bdb21f | 597 | // Chain voor linker biceps |
Renate | 28:7c7508bdb21f | 598 | bqcbl.add(&bqbl1).add(&bqbl2); |
Renate | 28:7c7508bdb21f | 599 | bqcbl2.add(&bqbl3).add(&bqbl4); |
Renate | 28:7c7508bdb21f | 600 | // Chain voor kuit |
Renate | 28:7c7508bdb21f | 601 | bqck.add(&bqk1).add(&bqk2); |
Renate | 28:7c7508bdb21f | 602 | bqck2.add(&bqk3).add(&bqk4); |
Renate | 28:7c7508bdb21f | 603 | |
Renate | 28:7c7508bdb21f | 604 | loop_ticker.attach(&ProcessStateMachine, 0.002f); |
Renate | 28:7c7508bdb21f | 605 | |
Renate | 28:7c7508bdb21f | 606 | while(true) { |
Renate | 28:7c7508bdb21f | 607 | // wait(0.2); |
Renate | 28:7c7508bdb21f | 608 | /* do nothing */ |
Renate | 28:7c7508bdb21f | 609 | } |
Renate | 28:7c7508bdb21f | 610 | } |