De hele robot in 1 keer bam

Dependencies:   mbed QEI Servo HIDScope biquadFilter MODSERIAL FastPWM

Committer:
BasB
Date:
Tue Nov 05 10:24:09 2019 +0000
Revision:
69:bfefdfb04c29
Parent:
68:2879967ebb25
Parent:
67:edc3c644d316
Child:
70:c4a019e3830d
Gecontroleerde versie nadat het bijna misging met publishen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jellehierck 37:806c7c8381a7 1 /*
Jellehierck 37:806c7c8381a7 2 ------------------------------ ADD LIBRARIES ------------------------------
Jellehierck 37:806c7c8381a7 3 */
Jellehierck 38:8b597ab8344f 4 #include "mbed.h" // Base library
Jellehierck 38:8b597ab8344f 5 #include "HIDScope.h" // Scope connection to PC
Jellehierck 38:8b597ab8344f 6 #include "MODSERIAL.h" // Serial connection to PC
Jellehierck 38:8b597ab8344f 7 #include "BiQuad.h" // Biquad filter management
Jellehierck 38:8b597ab8344f 8 #include <vector> // Array management
Jellehierck 42:2937ad8f1032 9 #include "FastPWM.h" // PWM control
Jellehierck 42:2937ad8f1032 10 #include "QEI.h" // Encoder reading
Jellehierck 42:2937ad8f1032 11 #include <Servo.h> // Servo control
IsaRobin 0:6972d0e91af1 12
Jellehierck 15:421d3d9c563b 13 /*
Jellehierck 37:806c7c8381a7 14 ------------------------------ DEFINE MBED CONNECTIONS ------------------------------
Jellehierck 15:421d3d9c563b 15 */
IsaRobin 0:6972d0e91af1 16
Jellehierck 38:8b597ab8344f 17 // PC connections
Jellehierck 40:c6dffb676350 18 HIDScope scope( 6 );
Jellehierck 15:421d3d9c563b 19 MODSERIAL pc(USBTX, USBRX);
IsaRobin 0:6972d0e91af1 20
Jellehierck 8:ea3de43c9e8b 21 // Buttons
Jellehierck 8:ea3de43c9e8b 22 InterruptIn button1(D11);
Jellehierck 8:ea3de43c9e8b 23 InterruptIn button2(D10);
Jellehierck 37:806c7c8381a7 24 InterruptIn switch2(SW2);
Jellehierck 37:806c7c8381a7 25 InterruptIn switch3(SW3);
BasB 47:63b5ccd969e9 26 DigitalIn extButton1(D2); //Servo button
Jellehierck 4:09a01d2db8f7 27
Jellehierck 38:8b597ab8344f 28 // LEDs
Jellehierck 38:8b597ab8344f 29 DigitalOut led_g(LED_GREEN);
Jellehierck 38:8b597ab8344f 30 DigitalOut led_r(LED_RED);
Jellehierck 38:8b597ab8344f 31 DigitalOut led_b(LED_BLUE);
Jellehierck 38:8b597ab8344f 32
Jellehierck 38:8b597ab8344f 33 // Analog EMG inputs
Jellehierck 64:a0ca27f75801 34 AnalogIn emg1_in (A0); // Right biceps -> x axis
Jellehierck 64:a0ca27f75801 35 AnalogIn emg2_in (A1); // Left biceps -> y axis
Jellehierck 64:a0ca27f75801 36 AnalogIn emg3_in (A2); // Right tibia -> x direction
Jellehierck 64:a0ca27f75801 37 AnalogIn emg4_in (A3); // Left tibia -> y direction
Jellehierck 45:d09040915cfe 38
Jellehierck 53:35282a3e0cad 39 // Analog Potmeter inputs
Jellehierck 53:35282a3e0cad 40 AnalogIn potmeter1 (A4);
Jellehierck 53:35282a3e0cad 41 AnalogIn potmeter2 (A5);
Jellehierck 40:c6dffb676350 42
BasB 68:2879967ebb25 43 // Motor Encoder inputs
Jellehierck 40:c6dffb676350 44 DigitalIn encA1(D9);
Jellehierck 40:c6dffb676350 45 DigitalIn encB1(D8);
Jellehierck 40:c6dffb676350 46 DigitalIn encA2(D13);
Jellehierck 40:c6dffb676350 47 DigitalIn encB2(D12);
Jellehierck 40:c6dffb676350 48
Jellehierck 40:c6dffb676350 49 // Motor outputs
Jellehierck 42:2937ad8f1032 50 DigitalOut motor1Direction(D7);
Jellehierck 42:2937ad8f1032 51 FastPWM motor1Power(D6);
Jellehierck 40:c6dffb676350 52 DigitalOut motor2Direction(D4);
Jellehierck 40:c6dffb676350 53 FastPWM motor2Power(D5);
Jellehierck 42:2937ad8f1032 54
Jellehierck 42:2937ad8f1032 55 // Servo
BasB 47:63b5ccd969e9 56 Servo myservo(D3);
Jellehierck 38:8b597ab8344f 57
Jellehierck 15:421d3d9c563b 58 /*
Jellehierck 38:8b597ab8344f 59 ------------------------------ INITIALIZE TICKERS, TIMERS & TIMEOUTS ------------------------------
Jellehierck 38:8b597ab8344f 60 */
Jellehierck 38:8b597ab8344f 61 Ticker tickGlobal; // Set global ticker
Jellehierck 51:7fe2659a1fcb 62 Ticker tickLED; // LED ticker
Jellehierck 57:d0d65376140f 63 Timer timerStateChange; // Set timer for various state changes
Jellehierck 38:8b597ab8344f 64
Jellehierck 38:8b597ab8344f 65 /*
Jellehierck 38:8b597ab8344f 66 ------------------------------ INITIALIZE GLOBAL VARIABLES ------------------------------
Jellehierck 15:421d3d9c563b 67 */
Jellehierck 15:421d3d9c563b 68
Jellehierck 37:806c7c8381a7 69 // State machine variables
Jellehierck 38:8b597ab8344f 70 enum GLOBAL_States { global_failure, global_wait, global_emg_cal, global_motor_cal, global_operation, global_demo }; // Define global states
Jellehierck 37:806c7c8381a7 71 GLOBAL_States global_curr_state = global_wait; // Initialize global state to waiting state
Jellehierck 37:806c7c8381a7 72 bool global_state_changed = true; // Enable entry functions
Jellehierck 42:2937ad8f1032 73 bool failure_mode = false; // Global failure mode flag (not used yet)
Jellehierck 42:2937ad8f1032 74 bool emg_cal_done = false; // Global EMG calibration flag
Jellehierck 42:2937ad8f1032 75 bool motor_cal_done = false; // Global motor calibration flag
Jellehierck 38:8b597ab8344f 76
Jellehierck 38:8b597ab8344f 77 // EMG Substate variables
Jellehierck 38:8b597ab8344f 78 enum EMG_States { emg_wait, emg_cal_MVC, emg_cal_rest, emg_operation }; // Define EMG substates
Jellehierck 38:8b597ab8344f 79 EMG_States emg_curr_state = emg_wait; // Initialize EMG substate variable
Jellehierck 42:2937ad8f1032 80 bool emg_state_changed = true; // Enable entry functions
Jellehierck 42:2937ad8f1032 81 bool emg_sampleNow = false; // Flag to enable EMG sampling and filtering in sampleSignals()
Jellehierck 42:2937ad8f1032 82 bool emg_calibrateNow = false; // Flag to enable EMG calibration in sampleSignals()
Jellehierck 42:2937ad8f1032 83 bool emg_MVC_cal_done = false; // Internal MVC calibration flag
Jellehierck 42:2937ad8f1032 84 bool emg_rest_cal_done = false; // Internal rest calibration flag
Jellehierck 8:ea3de43c9e8b 85
Jellehierck 40:c6dffb676350 86 // Motor Substate variables
Jellehierck 51:7fe2659a1fcb 87 enum Motor_States { motor_encoder_set, motor_finish }; // Define motor substates
Jellehierck 51:7fe2659a1fcb 88 Motor_States motor_curr_state = motor_encoder_set; // Initialize motor substate variable
Jellehierck 42:2937ad8f1032 89 bool motor_state_changed = true; // Enable entry functions
Jellehierck 42:2937ad8f1032 90 bool motor_encoder_cal_done = false; // Internal encoder calibration flag
Jellehierck 40:c6dffb676350 91
Jellehierck 42:2937ad8f1032 92 // Operation Substate variables
Jellehierck 42:2937ad8f1032 93 enum Operation_States { operation_wait, operation_movement, operation_finish }; // Define operation substates
Jellehierck 42:2937ad8f1032 94 Operation_States operation_curr_state = operation_wait; // Initialize operation substate variable
Jellehierck 42:2937ad8f1032 95 bool operation_state_changed = true; // Enable entry functions
Jellehierck 42:2937ad8f1032 96 bool operation_showcard = false; // Internal flag to toggle servo position
Jellehierck 51:7fe2659a1fcb 97 bool exit_operation = false;
Jellehierck 40:c6dffb676350 98
Jellehierck 43:1bd5417ded64 99 // Demo Substate variables
Jellehierck 60:762b5c925e13 100 enum Demo_States { demo_wait, demo_motor_cal, demo_positioning, demo_XY }; // Define demo substates
Jellehierck 43:1bd5417ded64 101 Demo_States demo_curr_state; // Initialize demo substate variable
Jellehierck 43:1bd5417ded64 102 bool demo_state_changed = true; // Enable entry functions
Jellehierck 57:d0d65376140f 103 bool exit_demo = false;
Jellehierck 43:1bd5417ded64 104
Jellehierck 37:806c7c8381a7 105 // Button press interrupts (to prevent bounce)
Jellehierck 37:806c7c8381a7 106 bool button1_pressed = false;
Jellehierck 37:806c7c8381a7 107 bool button2_pressed = false;
Jellehierck 37:806c7c8381a7 108 bool switch2_pressed = false;
Jellehierck 53:35282a3e0cad 109 bool servo_button1;
Jellehierck 7:7a088536f1c9 110
Jellehierck 51:7fe2659a1fcb 111 // LED states
Jellehierck 51:7fe2659a1fcb 112 enum LED_Colors { off, red, green, blue, purple, yellow, cyan, white }; // Define possible LED colors
Jellehierck 51:7fe2659a1fcb 113 LED_Colors curr_led_color; // Initialize LED color variable
Jellehierck 51:7fe2659a1fcb 114 bool led_color_changed = true; // Enable LED entry functions
Jellehierck 51:7fe2659a1fcb 115
Jellehierck 38:8b597ab8344f 116 // Global constants
BasB 68:2879967ebb25 117 const double Fs = 500.0; //Sampling frequency
BasB 68:2879967ebb25 118 const double Ts = 1/Fs;
Jellehierck 35:e82834e62e44 119
Jellehierck 35:e82834e62e44 120 /*
Jellehierck 51:7fe2659a1fcb 121 ------------------------------ LED COLOR FUNCTIONS ------------------------------
Jellehierck 51:7fe2659a1fcb 122 */
Jellehierck 51:7fe2659a1fcb 123 // Only called once when color is changed
Jellehierck 51:7fe2659a1fcb 124 void set_led_color(char color)
Jellehierck 51:7fe2659a1fcb 125 {
Jellehierck 51:7fe2659a1fcb 126 switch(color) {
Jellehierck 51:7fe2659a1fcb 127 case 'o':
Jellehierck 51:7fe2659a1fcb 128 curr_led_color = off;
Jellehierck 51:7fe2659a1fcb 129 break;
Jellehierck 51:7fe2659a1fcb 130 case 'r':
Jellehierck 51:7fe2659a1fcb 131 curr_led_color = red;
Jellehierck 51:7fe2659a1fcb 132 break;
Jellehierck 51:7fe2659a1fcb 133 case 'b':
Jellehierck 51:7fe2659a1fcb 134 curr_led_color = blue;
Jellehierck 51:7fe2659a1fcb 135 break;
Jellehierck 51:7fe2659a1fcb 136 case 'g':
Jellehierck 51:7fe2659a1fcb 137 curr_led_color = green;
Jellehierck 51:7fe2659a1fcb 138 break;
Jellehierck 51:7fe2659a1fcb 139 case 'y':
Jellehierck 51:7fe2659a1fcb 140 curr_led_color = yellow;
Jellehierck 51:7fe2659a1fcb 141 break;
Jellehierck 51:7fe2659a1fcb 142 case 'p':
Jellehierck 51:7fe2659a1fcb 143 curr_led_color = purple;
Jellehierck 51:7fe2659a1fcb 144 break;
Jellehierck 51:7fe2659a1fcb 145 case 'c':
Jellehierck 51:7fe2659a1fcb 146 curr_led_color = cyan;
Jellehierck 51:7fe2659a1fcb 147 break;
Jellehierck 51:7fe2659a1fcb 148 case 'w':
Jellehierck 51:7fe2659a1fcb 149 curr_led_color = white;
Jellehierck 51:7fe2659a1fcb 150 break;
Jellehierck 58:8d8a98ec2904 151
Jellehierck 51:7fe2659a1fcb 152 }
Jellehierck 51:7fe2659a1fcb 153 led_color_changed = true;
Jellehierck 51:7fe2659a1fcb 154 }
Jellehierck 51:7fe2659a1fcb 155
Jellehierck 51:7fe2659a1fcb 156 // Run constantly
Jellehierck 51:7fe2659a1fcb 157 void disp_led_color()
Jellehierck 51:7fe2659a1fcb 158 {
Jellehierck 51:7fe2659a1fcb 159 if (led_color_changed == true) {
Jellehierck 51:7fe2659a1fcb 160 led_color_changed = false;
Jellehierck 51:7fe2659a1fcb 161 led_g = 1;
Jellehierck 51:7fe2659a1fcb 162 led_b = 1;
Jellehierck 51:7fe2659a1fcb 163 led_r = 1;
Jellehierck 51:7fe2659a1fcb 164 }
Jellehierck 51:7fe2659a1fcb 165 switch(curr_led_color) {
Jellehierck 51:7fe2659a1fcb 166 case off:
Jellehierck 51:7fe2659a1fcb 167 break;
Jellehierck 51:7fe2659a1fcb 168 case red:
Jellehierck 51:7fe2659a1fcb 169 led_r = !led_r;
Jellehierck 51:7fe2659a1fcb 170 break;
Jellehierck 51:7fe2659a1fcb 171 case blue:
Jellehierck 51:7fe2659a1fcb 172 led_b = !led_b;
Jellehierck 51:7fe2659a1fcb 173 break;
Jellehierck 51:7fe2659a1fcb 174 case green:
Jellehierck 51:7fe2659a1fcb 175 led_g = !led_g;
Jellehierck 51:7fe2659a1fcb 176 break;
Jellehierck 51:7fe2659a1fcb 177 case yellow:
Jellehierck 51:7fe2659a1fcb 178 led_r = !led_r;
Jellehierck 51:7fe2659a1fcb 179 led_g = !led_g;
Jellehierck 51:7fe2659a1fcb 180 break;
Jellehierck 51:7fe2659a1fcb 181 case purple:
Jellehierck 51:7fe2659a1fcb 182 led_r = !led_r;
Jellehierck 51:7fe2659a1fcb 183 led_b = !led_b;
Jellehierck 51:7fe2659a1fcb 184 break;
Jellehierck 51:7fe2659a1fcb 185 case cyan:
Jellehierck 51:7fe2659a1fcb 186 led_b = !led_b;
Jellehierck 51:7fe2659a1fcb 187 led_g = !led_g;
Jellehierck 51:7fe2659a1fcb 188 break;
Jellehierck 51:7fe2659a1fcb 189 case white:
Jellehierck 51:7fe2659a1fcb 190 led_r = !led_r;
Jellehierck 51:7fe2659a1fcb 191 led_g = !led_g;
Jellehierck 51:7fe2659a1fcb 192 led_b = !led_b;
Jellehierck 51:7fe2659a1fcb 193 break;
Jellehierck 51:7fe2659a1fcb 194 }
Jellehierck 51:7fe2659a1fcb 195 }
Jellehierck 51:7fe2659a1fcb 196
Jellehierck 51:7fe2659a1fcb 197 /*
Jellehierck 37:806c7c8381a7 198 ------------------------------ HELPER FUNCTIONS ------------------------------
Jellehierck 37:806c7c8381a7 199 */
Jellehierck 38:8b597ab8344f 200
Jellehierck 38:8b597ab8344f 201 // Return max value of vector
Jellehierck 38:8b597ab8344f 202 double getMax(const vector<double> &vect)
Jellehierck 38:8b597ab8344f 203 {
Jellehierck 38:8b597ab8344f 204 double curr_max = 0.0;
Jellehierck 38:8b597ab8344f 205 int vect_n = vect.size();
Jellehierck 38:8b597ab8344f 206 for (int i = 0; i < vect_n; i++) {
Jellehierck 38:8b597ab8344f 207 if (vect[i] > curr_max) {
Jellehierck 38:8b597ab8344f 208 curr_max = vect[i];
Jellehierck 38:8b597ab8344f 209 };
Jellehierck 38:8b597ab8344f 210 }
Jellehierck 38:8b597ab8344f 211 return curr_max;
Jellehierck 38:8b597ab8344f 212 }
Jellehierck 37:806c7c8381a7 213
Jellehierck 38:8b597ab8344f 214 // Return mean of vector
Jellehierck 38:8b597ab8344f 215 double getMean(const vector<double> &vect)
Jellehierck 38:8b597ab8344f 216 {
Jellehierck 38:8b597ab8344f 217 double sum = 0.0;
Jellehierck 38:8b597ab8344f 218 int vect_n = vect.size();
Jellehierck 38:8b597ab8344f 219 for ( int i = 0; i < vect_n; i++ ) {
Jellehierck 38:8b597ab8344f 220 sum += vect[i];
Jellehierck 38:8b597ab8344f 221 }
Jellehierck 38:8b597ab8344f 222 return sum/vect_n;
Jellehierck 38:8b597ab8344f 223 }
Jellehierck 37:806c7c8381a7 224
Jellehierck 38:8b597ab8344f 225 // Return standard deviation of vector
Jellehierck 38:8b597ab8344f 226 double getStdev(const vector<double> &vect, const double vect_mean)
Jellehierck 38:8b597ab8344f 227 {
Jellehierck 38:8b597ab8344f 228 double sum2 = 0.0;
Jellehierck 38:8b597ab8344f 229 int vect_n = vect.size();
Jellehierck 38:8b597ab8344f 230 for ( int i = 0; i < vect_n; i++ ) {
Jellehierck 38:8b597ab8344f 231 sum2 += pow( vect[i] - vect_mean, 2 );
Jellehierck 38:8b597ab8344f 232 }
Jellehierck 38:8b597ab8344f 233 double output = sqrt( sum2 / vect_n );
Jellehierck 38:8b597ab8344f 234 return output;
Jellehierck 38:8b597ab8344f 235 }
Jellehierck 38:8b597ab8344f 236
Jellehierck 38:8b597ab8344f 237 // Rescale double values to certain range
Jellehierck 38:8b597ab8344f 238 double rescale(double input, double out_min, double out_max, double in_min, double in_max)
Jellehierck 38:8b597ab8344f 239 {
Jellehierck 38:8b597ab8344f 240 double output = out_min + ((input-in_min)/(in_max-in_min))*(out_max-out_min); // Based on MATLAB rescale function
Jellehierck 38:8b597ab8344f 241 return output;
Jellehierck 38:8b597ab8344f 242 }
Jellehierck 37:806c7c8381a7 243
Jellehierck 37:806c7c8381a7 244 /*
Jellehierck 37:806c7c8381a7 245 ------------------------------ BUTTON FUNCTIONS ------------------------------
Jellehierck 35:e82834e62e44 246 */
Jellehierck 35:e82834e62e44 247
Jellehierck 25:a1be4cf2ab0b 248 // Handle button press
Jellehierck 25:a1be4cf2ab0b 249 void button1Press()
Jellehierck 25:a1be4cf2ab0b 250 {
Jellehierck 25:a1be4cf2ab0b 251 button1_pressed = true;
Jellehierck 25:a1be4cf2ab0b 252 }
Jellehierck 25:a1be4cf2ab0b 253
Jellehierck 25:a1be4cf2ab0b 254 // Handle button press
Jellehierck 25:a1be4cf2ab0b 255 void button2Press()
Jellehierck 25:a1be4cf2ab0b 256 {
Jellehierck 25:a1be4cf2ab0b 257 button2_pressed = true;
Jellehierck 25:a1be4cf2ab0b 258 }
Jellehierck 25:a1be4cf2ab0b 259
Jellehierck 37:806c7c8381a7 260 void switch2Press()
Jellehierck 6:5437cc97e1e6 261 {
Jellehierck 37:806c7c8381a7 262 switch2_pressed = true;
Jellehierck 35:e82834e62e44 263 }
Jellehierck 6:5437cc97e1e6 264
Jellehierck 37:806c7c8381a7 265 void switch3Press()
Jellehierck 35:e82834e62e44 266 {
Jellehierck 37:806c7c8381a7 267 global_curr_state = global_failure;
Jellehierck 37:806c7c8381a7 268 global_state_changed = true;
Jellehierck 6:5437cc97e1e6 269 }
Jellehierck 6:5437cc97e1e6 270
Jellehierck 15:421d3d9c563b 271 /*
Jellehierck 38:8b597ab8344f 272 ------------------------------ EMG GLOBAL VARIABLES & CONSTANTS ------------------------------
Jellehierck 38:8b597ab8344f 273 */
Jellehierck 38:8b597ab8344f 274
Jellehierck 38:8b597ab8344f 275 // Set global constant values for EMG reading & analysis
Jellehierck 41:8e8141f355af 276 const double Tcal = 7.5f; // Calibration duration (s)
Jellehierck 38:8b597ab8344f 277
Jellehierck 38:8b597ab8344f 278 // Initialize variables for EMG reading & analysis
Jellehierck 38:8b597ab8344f 279 double emg1;
Jellehierck 38:8b597ab8344f 280 double emg1_env;
Jellehierck 38:8b597ab8344f 281 double emg1_MVC;
Jellehierck 38:8b597ab8344f 282 double emg1_rest;
Jellehierck 38:8b597ab8344f 283 double emg1_factor;//delete
Jellehierck 38:8b597ab8344f 284 double emg1_th;
Jellehierck 38:8b597ab8344f 285 double emg1_out;
Jellehierck 38:8b597ab8344f 286 double emg1_norm; //delete
Jellehierck 38:8b597ab8344f 287 vector<double> emg1_cal;
Jellehierck 38:8b597ab8344f 288 int emg1_cal_size; //delete
Jellehierck 41:8e8141f355af 289 double emg1_dir = 1.0;
Jellehierck 38:8b597ab8344f 290
Jellehierck 38:8b597ab8344f 291 double emg2;
Jellehierck 38:8b597ab8344f 292 double emg2_env;
Jellehierck 38:8b597ab8344f 293 double emg2_MVC;
Jellehierck 38:8b597ab8344f 294 double emg2_rest;
Jellehierck 38:8b597ab8344f 295 double emg2_factor;//delete
Jellehierck 38:8b597ab8344f 296 double emg2_th;
Jellehierck 38:8b597ab8344f 297 double emg2_out;
Jellehierck 38:8b597ab8344f 298 double emg2_norm;//delete
Jellehierck 38:8b597ab8344f 299 vector<double> emg2_cal;
Jellehierck 38:8b597ab8344f 300 int emg2_cal_size;//delete
Jellehierck 41:8e8141f355af 301 double emg2_dir = 1.0;
Jellehierck 38:8b597ab8344f 302
Jellehierck 38:8b597ab8344f 303 double emg3;
Jellehierck 38:8b597ab8344f 304 double emg3_env;
Jellehierck 38:8b597ab8344f 305 double emg3_MVC;
Jellehierck 38:8b597ab8344f 306 double emg3_rest;
Jellehierck 38:8b597ab8344f 307 double emg3_factor;//delete
Jellehierck 38:8b597ab8344f 308 double emg3_th;
Jellehierck 38:8b597ab8344f 309 double emg3_out;
Jellehierck 38:8b597ab8344f 310 double emg3_norm;//delete
Jellehierck 38:8b597ab8344f 311 vector<double> emg3_cal;
Jellehierck 45:d09040915cfe 312
Jellehierck 45:d09040915cfe 313 double emg4;
Jellehierck 45:d09040915cfe 314 double emg4_env;
Jellehierck 45:d09040915cfe 315 double emg4_MVC;
Jellehierck 45:d09040915cfe 316 double emg4_rest;
Jellehierck 45:d09040915cfe 317 double emg4_factor;//delete
Jellehierck 45:d09040915cfe 318 double emg4_th;
Jellehierck 45:d09040915cfe 319 double emg4_out;
Jellehierck 45:d09040915cfe 320 double emg4_norm;//delete
Jellehierck 45:d09040915cfe 321 vector<double> emg4_cal;
Jellehierck 38:8b597ab8344f 322
Jellehierck 38:8b597ab8344f 323 /*
Jellehierck 38:8b597ab8344f 324 ------------------------------ EMG FILTERS ------------------------------
Jellehierck 38:8b597ab8344f 325 */
Jellehierck 38:8b597ab8344f 326
Jellehierck 38:8b597ab8344f 327 // Notch biquad filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB:
Jellehierck 38:8b597ab8344f 328 BiQuad bq1_notch( 0.995636295063941, -1.89829218816065, 0.995636295063941, 1, -1.89829218816065, 0.991272590127882); // b01 b11 b21 a01 a11 a21
Jellehierck 38:8b597ab8344f 329 BiQuad bq2_notch = bq1_notch;
Jellehierck 38:8b597ab8344f 330 BiQuad bq3_notch = bq1_notch;
Jellehierck 45:d09040915cfe 331 BiQuad bq4_notch = bq1_notch;
Jellehierck 38:8b597ab8344f 332 BiQuadChain bqc1_notch;
Jellehierck 38:8b597ab8344f 333 BiQuadChain bqc2_notch;
Jellehierck 38:8b597ab8344f 334 BiQuadChain bqc3_notch;
Jellehierck 45:d09040915cfe 335 BiQuadChain bqc4_notch;
Jellehierck 38:8b597ab8344f 336
Jellehierck 38:8b597ab8344f 337 // Highpass biquad filter coefficients (butter 4th order @10Hz cutoff) from MATLAB
Jellehierck 38:8b597ab8344f 338 BiQuad bq1_H1(0.922946103200875, -1.84589220640175, 0.922946103200875, 1, -1.88920703055163, 0.892769008131025); // b01 b11 b21 a01 a11 a21
Jellehierck 38:8b597ab8344f 339 BiQuad bq1_H2(1, -2, 1, 1, -1.95046575793011, 0.954143234875078); // b02 b12 b22 a02 a12 a22
Jellehierck 38:8b597ab8344f 340 BiQuad bq2_H1 = bq1_H1;
Jellehierck 38:8b597ab8344f 341 BiQuad bq2_H2 = bq1_H2;
Jellehierck 38:8b597ab8344f 342 BiQuad bq3_H1 = bq1_H1;
Jellehierck 38:8b597ab8344f 343 BiQuad bq3_H2 = bq1_H2;
Jellehierck 45:d09040915cfe 344 BiQuad bq4_H1 = bq1_H1;
Jellehierck 45:d09040915cfe 345 BiQuad bq4_H2 = bq1_H2;
Jellehierck 38:8b597ab8344f 346 BiQuadChain bqc1_high;
Jellehierck 38:8b597ab8344f 347 BiQuadChain bqc2_high;
Jellehierck 38:8b597ab8344f 348 BiQuadChain bqc3_high;
Jellehierck 45:d09040915cfe 349 BiQuadChain bqc4_high;
Jellehierck 38:8b597ab8344f 350
Jellehierck 38:8b597ab8344f 351 // Lowpass biquad filter coefficients (butter 4th order @5Hz cutoff) from MATLAB:
Jellehierck 38:8b597ab8344f 352 BiQuad bq1_L1(5.32116245737504e-08, 1.06423249147501e-07, 5.32116245737504e-08, 1, -1.94396715039462, 0.944882378004138); // b01 b11 b21 a01 a11 a21
Jellehierck 38:8b597ab8344f 353 BiQuad bq1_L2(1, 2, 1, 1, -1.97586467534468, 0.976794920438162); // b02 b12 b22 a02 a12 a22
Jellehierck 38:8b597ab8344f 354 BiQuad bq2_L1 = bq1_L1;
Jellehierck 38:8b597ab8344f 355 BiQuad bq2_L2 = bq1_L2;
Jellehierck 38:8b597ab8344f 356 BiQuad bq3_L1 = bq1_L1;
Jellehierck 38:8b597ab8344f 357 BiQuad bq3_L2 = bq1_L2;
Jellehierck 45:d09040915cfe 358 BiQuad bq4_L1 = bq1_L1;
Jellehierck 45:d09040915cfe 359 BiQuad bq4_L2 = bq1_L2;
Jellehierck 38:8b597ab8344f 360 BiQuadChain bqc1_low;
Jellehierck 38:8b597ab8344f 361 BiQuadChain bqc2_low;
Jellehierck 38:8b597ab8344f 362 BiQuadChain bqc3_low;
Jellehierck 45:d09040915cfe 363 BiQuadChain bqc4_low;
Jellehierck 38:8b597ab8344f 364
Jellehierck 38:8b597ab8344f 365 // Function to check filter stability
Jellehierck 38:8b597ab8344f 366 bool checkBQChainStable()
Jellehierck 38:8b597ab8344f 367 {
Jellehierck 38:8b597ab8344f 368 bool n_stable = bqc1_notch.stable(); // Check stability of all BQ Chains
Jellehierck 38:8b597ab8344f 369 bool hp_stable = bqc1_high.stable();
Jellehierck 38:8b597ab8344f 370 bool l_stable = bqc1_low.stable();
Jellehierck 38:8b597ab8344f 371
Jellehierck 38:8b597ab8344f 372 if (n_stable && hp_stable && l_stable) {
Jellehierck 38:8b597ab8344f 373 return true;
Jellehierck 38:8b597ab8344f 374 } else {
Jellehierck 38:8b597ab8344f 375 return false;
Jellehierck 38:8b597ab8344f 376 }
Jellehierck 38:8b597ab8344f 377 }
Jellehierck 42:2937ad8f1032 378
Jellehierck 42:2937ad8f1032 379 /*
Jellehierck 42:2937ad8f1032 380 ------------------------------ EMG GLOBAL FUNCTIONS ------------------------------
Jellehierck 42:2937ad8f1032 381 */
Jellehierck 42:2937ad8f1032 382
Jellehierck 42:2937ad8f1032 383 void EMGOperationFunc()
Jellehierck 42:2937ad8f1032 384 {
Jellehierck 42:2937ad8f1032 385 emg1_norm = emg1_env * emg1_factor; // Normalize current EMG signal with calibrated factor
Jellehierck 42:2937ad8f1032 386 emg2_norm = emg2_env * emg2_factor; // Idem
Jellehierck 42:2937ad8f1032 387 emg3_norm = emg3_env * emg3_factor; // Idem
Jellehierck 45:d09040915cfe 388 emg4_norm = emg4_env * emg4_factor; // Idem
Jellehierck 42:2937ad8f1032 389
Jellehierck 42:2937ad8f1032 390
Jellehierck 42:2937ad8f1032 391 // Set normalized EMG output signal (CAN BE MOVED TO EXTERNAL FUNCTION BECAUSE IT IS REPEATED 3 TIMES)
Jellehierck 42:2937ad8f1032 392 if ( emg1_norm < emg1_th ) { // If below threshold, emg_out = 0 (ignored)
Jellehierck 42:2937ad8f1032 393 emg1_out = 0.0;
Jellehierck 42:2937ad8f1032 394 } else if ( emg1_norm > 1.0f ) { // If above MVC (e.g. due to filtering), emg_out = 1 (max value)
Jellehierck 42:2937ad8f1032 395 emg1_out = 1.0;
Jellehierck 42:2937ad8f1032 396 } else { // If in between threshold and MVC, scale EMG signal accordingly
Jellehierck 42:2937ad8f1032 397 // Inputs may be in range [emg_th, 1]
Jellehierck 42:2937ad8f1032 398 // Outputs are scaled to range [0, 1]
Jellehierck 42:2937ad8f1032 399 emg1_out = rescale(emg1_norm, 0, 1, emg1_th, 1);
Jellehierck 42:2937ad8f1032 400 }
Jellehierck 42:2937ad8f1032 401
Jellehierck 42:2937ad8f1032 402 // Idem for emg2
Jellehierck 42:2937ad8f1032 403 if ( emg2_norm < emg2_th ) {
Jellehierck 42:2937ad8f1032 404 emg2_out = 0.0;
Jellehierck 42:2937ad8f1032 405 } else if ( emg2_norm > 1.0f ) {
Jellehierck 42:2937ad8f1032 406 emg2_out = 1.0;
Jellehierck 42:2937ad8f1032 407 } else {
Jellehierck 42:2937ad8f1032 408 emg2_out = rescale(emg2_norm, 0, 1, emg2_th, 1);
Jellehierck 42:2937ad8f1032 409 }
Jellehierck 42:2937ad8f1032 410
Jellehierck 42:2937ad8f1032 411 // Idem for emg3
Jellehierck 42:2937ad8f1032 412 if ( emg3_norm < emg3_th ) {
Jellehierck 45:d09040915cfe 413 emg1_dir = 1.0;
Jellehierck 42:2937ad8f1032 414 } else {
Jellehierck 45:d09040915cfe 415 emg1_dir = -1.0;
Jellehierck 45:d09040915cfe 416 }
Jellehierck 51:7fe2659a1fcb 417
Jellehierck 45:d09040915cfe 418 if ( emg4_norm < emg4_th ) {
Jellehierck 45:d09040915cfe 419 emg2_dir = 1.0;
Jellehierck 45:d09040915cfe 420 } else {
Jellehierck 45:d09040915cfe 421 emg2_dir = -1.0;
Jellehierck 42:2937ad8f1032 422 }
Jellehierck 42:2937ad8f1032 423 }
Jellehierck 38:8b597ab8344f 424 /*
Jellehierck 38:8b597ab8344f 425 ------------------------------ EMG SUBSTATE FUNCTIONS ------------------------------
Jellehierck 15:421d3d9c563b 426 */
Jellehierck 38:8b597ab8344f 427
Jellehierck 38:8b597ab8344f 428 // EMG Waiting state
Jellehierck 38:8b597ab8344f 429 void do_emg_wait()
Jellehierck 38:8b597ab8344f 430 {
Jellehierck 38:8b597ab8344f 431 // Entry function
Jellehierck 38:8b597ab8344f 432 if ( emg_state_changed == true ) {
Jellehierck 38:8b597ab8344f 433 emg_state_changed = false; // Disable entry functions
Jellehierck 38:8b597ab8344f 434
Jellehierck 38:8b597ab8344f 435 button1.fall( &button1Press ); // Change to state MVC calibration on button1 press
Jellehierck 38:8b597ab8344f 436 button2.fall( &button2Press ); // Change to state rest calibration on button2 press
Jellehierck 38:8b597ab8344f 437 }
Jellehierck 38:8b597ab8344f 438
Jellehierck 38:8b597ab8344f 439 // Do nothing until end condition is met
Jellehierck 38:8b597ab8344f 440
Jellehierck 38:8b597ab8344f 441 // State transition guard
Jellehierck 38:8b597ab8344f 442 if ( button1_pressed ) { // MVC calibration
Jellehierck 38:8b597ab8344f 443 button1_pressed = false; // Disable button pressed function until next button press
Jellehierck 38:8b597ab8344f 444 button1.fall( NULL ); // Disable interrupt during calibration
Jellehierck 38:8b597ab8344f 445 button2.fall( NULL ); // Disable interrupt during calibration
Jellehierck 38:8b597ab8344f 446 emg_curr_state = emg_cal_MVC; // Set next state
Jellehierck 38:8b597ab8344f 447 emg_state_changed = true; // Enable entry functions
Jellehierck 38:8b597ab8344f 448
Jellehierck 38:8b597ab8344f 449 } else if ( button2_pressed ) { // Rest calibration
Jellehierck 38:8b597ab8344f 450 button2_pressed = false; // Disable button pressed function until next button press
Jellehierck 38:8b597ab8344f 451 button1.fall( NULL ); // Disable interrupt during calibration
Jellehierck 38:8b597ab8344f 452 button2.fall( NULL ); // Disable interrupt during calibration
Jellehierck 38:8b597ab8344f 453 emg_curr_state = emg_cal_rest; // Set next state
Jellehierck 38:8b597ab8344f 454 emg_state_changed = true; // Enable entry functions
Jellehierck 38:8b597ab8344f 455
Jellehierck 38:8b597ab8344f 456 } else if ( emg_MVC_cal_done && emg_rest_cal_done ) { // Operation mode
Jellehierck 38:8b597ab8344f 457 button1.fall( NULL ); // Disable interrupt during operation
Jellehierck 38:8b597ab8344f 458 button2.fall( NULL ); // Disable interrupt during operation
Jellehierck 38:8b597ab8344f 459 emg_curr_state = emg_operation; // Set next state
Jellehierck 38:8b597ab8344f 460 emg_state_changed = true; // Enable entry functions
Jellehierck 38:8b597ab8344f 461 }
Jellehierck 38:8b597ab8344f 462 }
Jellehierck 38:8b597ab8344f 463
Jellehierck 38:8b597ab8344f 464 // EMG Calibration state
Jellehierck 38:8b597ab8344f 465 void do_emg_cal()
Jellehierck 38:8b597ab8344f 466 {
Jellehierck 38:8b597ab8344f 467 // Entry functions
Jellehierck 38:8b597ab8344f 468 if ( emg_state_changed == true ) {
Jellehierck 38:8b597ab8344f 469 emg_state_changed = false; // Disable entry functions
Jellehierck 51:7fe2659a1fcb 470 set_led_color('b'); // Turn on calibration led (BLUE)
Jellehierck 38:8b597ab8344f 471
Jellehierck 57:d0d65376140f 472 timerStateChange.reset();
Jellehierck 57:d0d65376140f 473 timerStateChange.start(); // Sets up timer to stop calibration after Tcal seconds
Jellehierck 38:8b597ab8344f 474 emg_sampleNow = true; // Enable signal sampling in sampleSignals()
Jellehierck 38:8b597ab8344f 475 emg_calibrateNow = true; // Enable calibration vector functionality in sampleSignals()
Jellehierck 38:8b597ab8344f 476
Jellehierck 38:8b597ab8344f 477 emg1_cal.reserve(Fs * Tcal); // Initialize vector lengths to prevent memory overflow
Jellehierck 38:8b597ab8344f 478 emg2_cal.reserve(Fs * Tcal); // Idem
Jellehierck 38:8b597ab8344f 479 emg3_cal.reserve(Fs * Tcal); // Idem
Jellehierck 45:d09040915cfe 480 emg4_cal.reserve(Fs * Tcal); // Idem
Jellehierck 38:8b597ab8344f 481 }
Jellehierck 38:8b597ab8344f 482
Jellehierck 38:8b597ab8344f 483 // Do stuff until end condition is met
Jellehierck 38:8b597ab8344f 484 // Set HIDScope outputs
Jellehierck 38:8b597ab8344f 485 scope.set(0, emg1 );
Jellehierck 45:d09040915cfe 486 scope.set(1, emg2 );
Jellehierck 45:d09040915cfe 487 scope.set(2, emg3 );
Jellehierck 45:d09040915cfe 488 scope.set(3, emg4 );
Jellehierck 38:8b597ab8344f 489 //scope.set(2, emg2_env );
Jellehierck 38:8b597ab8344f 490 //scope.set(3, emg3_env );
Jellehierck 38:8b597ab8344f 491 scope.send();
Jellehierck 38:8b597ab8344f 492
Jellehierck 38:8b597ab8344f 493 // State transition guard
Jellehierck 57:d0d65376140f 494 if ( timerStateChange.read() >= Tcal ) { // After interval Tcal the calibration step is finished
Jellehierck 38:8b597ab8344f 495 emg_sampleNow = false; // Disable signal sampling in sampleSignals()
Jellehierck 38:8b597ab8344f 496 emg_calibrateNow = false; // Disable calibration sampling
BasB 55:a4e63ef1587c 497 set_led_color('p'); // Change calibration LED (BLUE) to EMG wait mode led (PURPLE)
Jellehierck 38:8b597ab8344f 498
Jellehierck 38:8b597ab8344f 499 // Extract EMG scale data from calibration
Jellehierck 38:8b597ab8344f 500 switch( emg_curr_state ) {
Jellehierck 39:f9042483b921 501 case emg_cal_MVC: // In case of MVC calibration
Jellehierck 38:8b597ab8344f 502 emg1_MVC = getMax(emg1_cal); // Store max value of MVC globally
Jellehierck 38:8b597ab8344f 503 emg2_MVC = getMax(emg2_cal);
Jellehierck 38:8b597ab8344f 504 emg3_MVC = getMax(emg3_cal);
Jellehierck 45:d09040915cfe 505 emg4_MVC = getMax(emg4_cal);
Jellehierck 38:8b597ab8344f 506 emg_MVC_cal_done = true; // Set up transition to EMG operation mode
Jellehierck 38:8b597ab8344f 507 break;
Jellehierck 39:f9042483b921 508 case emg_cal_rest: // In case of rest calibration
Jellehierck 38:8b597ab8344f 509 emg1_rest = getMean(emg1_cal); // Store mean of EMG in rest globally
Jellehierck 38:8b597ab8344f 510 emg2_rest = getMean(emg2_cal);
Jellehierck 38:8b597ab8344f 511 emg3_rest = getMean(emg3_cal);
Jellehierck 45:d09040915cfe 512 emg4_rest = getMean(emg4_cal);
Jellehierck 38:8b597ab8344f 513 emg_rest_cal_done = true; // Set up transition to EMG operation mode
Jellehierck 38:8b597ab8344f 514 break;
Jellehierck 38:8b597ab8344f 515 }
Jellehierck 38:8b597ab8344f 516 vector<double>().swap(emg1_cal); // Empty vector to prevent memory overflow
Jellehierck 38:8b597ab8344f 517 vector<double>().swap(emg2_cal);
Jellehierck 38:8b597ab8344f 518 vector<double>().swap(emg3_cal);
Jellehierck 51:7fe2659a1fcb 519 vector<double>().swap(emg4_cal);
Jellehierck 38:8b597ab8344f 520
Jellehierck 38:8b597ab8344f 521 emg_curr_state = emg_wait; // Set next substate
Jellehierck 38:8b597ab8344f 522 emg_state_changed = true; // Enable substate entry function
Jellehierck 38:8b597ab8344f 523 }
Jellehierck 38:8b597ab8344f 524 }
Jellehierck 38:8b597ab8344f 525
Jellehierck 38:8b597ab8344f 526 // EMG Operation state
Jellehierck 38:8b597ab8344f 527 void do_emg_operation()
Jellehierck 38:8b597ab8344f 528 {
Jellehierck 38:8b597ab8344f 529 // Entry function
Jellehierck 38:8b597ab8344f 530 if ( emg_state_changed == true ) {
Jellehierck 38:8b597ab8344f 531 emg_state_changed = false; // Disable entry functions
Jellehierck 40:c6dffb676350 532
Jellehierck 39:f9042483b921 533 // Compute scale factors for all EMG signals
Jellehierck 38:8b597ab8344f 534 double margin_percentage = 5; // Set up % margin for rest
Jellehierck 38:8b597ab8344f 535 emg1_factor = 1 / emg1_MVC; // Factor to normalize MVC
Jellehierck 38:8b597ab8344f 536 emg1_th = emg1_rest * emg1_factor + margin_percentage/100; // Set normalized rest threshold
Jellehierck 38:8b597ab8344f 537 emg2_factor = 1 / emg2_MVC; // Factor to normalize MVC
Jellehierck 38:8b597ab8344f 538 emg2_th = emg2_rest * emg2_factor + margin_percentage/100; // Set normalized rest threshold
Jellehierck 38:8b597ab8344f 539 emg3_factor = 1 / emg3_MVC; // Factor to normalize MVC
Jellehierck 38:8b597ab8344f 540 emg3_th = emg3_rest * emg3_factor + margin_percentage/100; // Set normalized rest threshold
Jellehierck 45:d09040915cfe 541 emg4_factor = 1 / emg4_MVC; // Factor to normalize MVC
Jellehierck 45:d09040915cfe 542 emg4_th = emg4_rest * emg4_factor + margin_percentage/100; // Set normalized rest threshold
Jellehierck 38:8b597ab8344f 543 }
Jellehierck 40:c6dffb676350 544
Jellehierck 39:f9042483b921 545 // This state only runs its entry functions ONCE and then exits the EMG substate machine
Jellehierck 38:8b597ab8344f 546
Jellehierck 38:8b597ab8344f 547 // State transition guard
Jellehierck 41:8e8141f355af 548 if ( true ) { // EMG substate machine is terminated directly after running this state once
Jellehierck 38:8b597ab8344f 549 emg_curr_state = emg_wait; // Set next state
Jellehierck 38:8b597ab8344f 550 emg_state_changed = true; // Enable entry function
Jellehierck 41:8e8141f355af 551 emg_cal_done = true; // Let the global substate machine know that EMG calibration is finished
Jellehierck 42:2937ad8f1032 552
Jellehierck 41:8e8141f355af 553 // Enable buttons again
Jellehierck 41:8e8141f355af 554 button1.fall( &button1Press );
Jellehierck 41:8e8141f355af 555 button2.fall( &button2Press );
Jellehierck 38:8b597ab8344f 556 }
Jellehierck 38:8b597ab8344f 557 }
Jellehierck 38:8b597ab8344f 558
Jellehierck 54:a14acf0d1683 559 /*
Jellehierck 54:a14acf0d1683 560 ------------------------------ EMG SUBSTATE MACHINE ------------------------------
Jellehierck 54:a14acf0d1683 561 */
Jellehierck 42:2937ad8f1032 562
Jellehierck 54:a14acf0d1683 563 void emg_state_machine()
Jellehierck 54:a14acf0d1683 564 {
Jellehierck 54:a14acf0d1683 565 switch(emg_curr_state) {
Jellehierck 54:a14acf0d1683 566 case emg_wait:
Jellehierck 54:a14acf0d1683 567 do_emg_wait();
Jellehierck 54:a14acf0d1683 568 break;
Jellehierck 54:a14acf0d1683 569 case emg_cal_MVC:
Jellehierck 54:a14acf0d1683 570 do_emg_cal();
Jellehierck 54:a14acf0d1683 571 break;
Jellehierck 54:a14acf0d1683 572 case emg_cal_rest:
Jellehierck 54:a14acf0d1683 573 do_emg_cal();
Jellehierck 54:a14acf0d1683 574 break;
Jellehierck 54:a14acf0d1683 575 case emg_operation:
Jellehierck 54:a14acf0d1683 576 do_emg_operation();
Jellehierck 54:a14acf0d1683 577 break;
Jellehierck 54:a14acf0d1683 578 }
Jellehierck 54:a14acf0d1683 579 }
Jellehierck 42:2937ad8f1032 580
Jellehierck 42:2937ad8f1032 581 /*
Jellehierck 42:2937ad8f1032 582 ------------------------------ MOTOR GLOBAL VARIABLES & CONSTANTS ------------------------------
Jellehierck 42:2937ad8f1032 583 */
Jellehierck 42:2937ad8f1032 584 // Initialize encoder
Jellehierck 42:2937ad8f1032 585 int encoder_res = 64;
Jellehierck 42:2937ad8f1032 586
Jellehierck 42:2937ad8f1032 587 QEI encoder1(D9, D8, NC, encoder_res, QEI::X4_ENCODING); //Encoder of motor 1
BasB 47:63b5ccd969e9 588 QEI encoder2(D12, D13, NC, encoder_res, QEI::X4_ENCODING); //Encoder of motor 2
Jellehierck 42:2937ad8f1032 589
Jellehierck 42:2937ad8f1032 590 // Initialize variables for encoder reading
BasB 68:2879967ebb25 591 volatile int counts1; //Counts after compensation with offset
BasB 68:2879967ebb25 592 volatile int counts1af; //Counts measured by encoder
BasB 68:2879967ebb25 593 int counts1offset; //Offset due to calibration
Jellehierck 42:2937ad8f1032 594 volatile int countsPrev1 = 0;
Jellehierck 42:2937ad8f1032 595 volatile int deltaCounts1;
Jellehierck 42:2937ad8f1032 596
BasB 68:2879967ebb25 597 volatile int counts2; // Counts after compensation with offset
BasB 68:2879967ebb25 598 volatile int counts2af; //Counts measured by encoder
BasB 68:2879967ebb25 599 int counts2offset; //Offset due to calibration
Jellehierck 42:2937ad8f1032 600 volatile int countsPrev2 = 0;
Jellehierck 42:2937ad8f1032 601 volatile int deltaCounts2;
Jellehierck 42:2937ad8f1032 602
Jellehierck 42:2937ad8f1032 603 // PWM period
Jellehierck 42:2937ad8f1032 604 const float PWM_period = 1/(18*10e3); // 18000 Hz
Jellehierck 42:2937ad8f1032 605
Jellehierck 42:2937ad8f1032 606 // Important constants
BasB 48:31676da4be7a 607 const float pi = 3.141592; // pi
BasB 48:31676da4be7a 608 const float pi2 = pi * 2.0f; // 2 pi
BasB 48:31676da4be7a 609 const float deg2rad = pi / 180.0f; //Conversion factor degree to rad
BasB 48:31676da4be7a 610 const float rad2deg = 180.0f / pi; //Conversion factor rad to degree
BasB 48:31676da4be7a 611 const float gearratio1 = 110.0f / 20.0f; // Teeth of large gear : teeth of driven gear
BasB 48:31676da4be7a 612 const float gearratio2 = 55.0f / 20.0f; // Teeth of small gear : teeth of driven gear
BasB 48:31676da4be7a 613 const float encoder_factorin = pi2 / encoder_res; // Convert encoder counts to angle in rad
Jellehierck 42:2937ad8f1032 614 const float gearbox_ratio = 131.25; // Gear ratio of motor gearboxes
Jellehierck 42:2937ad8f1032 615
Jellehierck 42:2937ad8f1032 616 // Kinematics variables
Jellehierck 42:2937ad8f1032 617 const float l1 = 26.0; // Distance base-joint2 [cm]
Jellehierck 42:2937ad8f1032 618 const float l2 = 62.0; // Distance join2-endpoint [cm]
Jellehierck 42:2937ad8f1032 619
BasB 68:2879967ebb25 620 float q1 = -10.0f * deg2rad; // Angle of first joint [rad] (in calibration position)
Jellehierck 42:2937ad8f1032 621 float q1dot; // Velocity of first joint [rad/s]
Jellehierck 42:2937ad8f1032 622
BasB 68:2879967ebb25 623 float q2 = -140.0f * deg2rad; // Angle of second joint [rad] (in calibration position
BasB 68:2879967ebb25 624 float q2dot; // Velocity of second joint [rad/s]
Jellehierck 42:2937ad8f1032 625
BasB 68:2879967ebb25 626 float Vx = 0.0; // Desired linear velocity x direction [cm/s]
BasB 68:2879967ebb25 627 float Vy = 0.0; // Desired linear velocity y direction [cm/s]
Jellehierck 42:2937ad8f1032 628
Jellehierck 42:2937ad8f1032 629 float xe; // Endpoint x position [cm]
Jellehierck 42:2937ad8f1032 630 float ye; // Endpoint y position [cm]
Jellehierck 42:2937ad8f1032 631
Jellehierck 42:2937ad8f1032 632 // Motor angles in starting position
BasB 47:63b5ccd969e9 633 const float motor1_init = ( q1 + q2 ) * gearratio1; // Measured angle motor 1 in initial (starting) position
BasB 68:2879967ebb25 634 float motor1_ref = motor1_init; // Expected motor angle (starting from initial position)
Jellehierck 42:2937ad8f1032 635 float motor1_angle = motor1_init; // Actual motor angle
Jellehierck 42:2937ad8f1032 636
BasB 47:63b5ccd969e9 637 const float motor2_init = q1 * gearratio2; // Measured angle motor 2 in initial (starting) position
BasB 68:2879967ebb25 638 float motor2_ref = motor2_init; // Motor 2 reference position (starting from initial position)
BasB 68:2879967ebb25 639 float motor2_angle = motor2_init; // Actual motor angle
Jellehierck 42:2937ad8f1032 640
Jellehierck 42:2937ad8f1032 641 // Initialize variables for motor control
Jellehierck 42:2937ad8f1032 642 float motor1offset; // Offset during calibration
BasB 68:2879967ebb25 643 float omega1; //angular velocity of motor [rad/s]
Jellehierck 42:2937ad8f1032 644 bool motordir1; // Toggle of motor direction
BasB 68:2879967ebb25 645 double controlsignal1; // Result of the PID controller
BasB 68:2879967ebb25 646 float motor1_error; // Error between desired and actual angle of the motor
Jellehierck 42:2937ad8f1032 647
BasB 68:2879967ebb25 648 float motor2offset; // Offset during calibration
BasB 68:2879967ebb25 649 float omega2; //Angular velocity of motor [rad/s]
BasB 68:2879967ebb25 650 bool motordir2; // Toggle of motor direction
BasB 68:2879967ebb25 651 double controlsignal2; // Result of the PID controller
BasB 68:2879967ebb25 652 float motor2_error; // Error between desired and actual angle of the motor
Jellehierck 42:2937ad8f1032 653
Jellehierck 42:2937ad8f1032 654 // Initialize variables for PID controller
Jellehierck 42:2937ad8f1032 655 float Kp = 0.27; // Proportional gain
Jellehierck 42:2937ad8f1032 656 float Ki = 0.35; // Integral gain
BasB 68:2879967ebb25 657 float Kd = 0.1; // Derivative gain
BasB 68:2879967ebb25 658 float Ka = 1.0; //Windup gain
Jellehierck 42:2937ad8f1032 659
BasB 68:2879967ebb25 660 float u_p1; // Proportional contribution
BasB 68:2879967ebb25 661 float u_i1; // Integral contribution
BasB 68:2879967ebb25 662 float u_d1; // Derivative contribution
BasB 68:2879967ebb25 663 float ux1; // Windup error (before multiplication with Ka)
BasB 68:2879967ebb25 664 float up1; // Summed contributions
BasB 68:2879967ebb25 665 float ek1; // Windup error
Jellehierck 42:2937ad8f1032 666 float ei1 = 0.0; // Integral error (starts at 0)
Jellehierck 42:2937ad8f1032 667
BasB 68:2879967ebb25 668 float u_p2; // Proportional contribution
BasB 68:2879967ebb25 669 float u_i2; // Integral contribution
BasB 68:2879967ebb25 670 float u_d2; // Derivative contribution
BasB 68:2879967ebb25 671 float ux2; // Windup error (before multiplication with Ka)
BasB 68:2879967ebb25 672 float up2; // Summed contributions
BasB 68:2879967ebb25 673 float ek2; // Windup error
BasB 68:2879967ebb25 674 float ei2 = 0.0; // Integral error (starts at 0)
Jellehierck 42:2937ad8f1032 675
Jellehierck 42:2937ad8f1032 676 /*
Jellehierck 42:2937ad8f1032 677 ------------------------------ MOTOR FUNCTIONS ------------------------------
Jellehierck 42:2937ad8f1032 678 */
Jellehierck 42:2937ad8f1032 679 void PID_controller()
Jellehierck 42:2937ad8f1032 680 {
Jellehierck 42:2937ad8f1032 681 // Motor 1
BasB 48:31676da4be7a 682 static float error_integral1 = 0.0;
BasB 68:2879967ebb25 683 static float e_prev1 = motor1_error; // Saving the previous error, needed for derivative computation
Jellehierck 42:2937ad8f1032 684
Jellehierck 42:2937ad8f1032 685 //Proportional part
Jellehierck 42:2937ad8f1032 686 u_p1 = Kp * motor1_error;
Jellehierck 42:2937ad8f1032 687
Jellehierck 42:2937ad8f1032 688 //Integral part
BasB 68:2879967ebb25 689 if ( motor_curr_state == motor_finish || operation_curr_state == operation_movement || demo_curr_state == demo_XY || demo_curr_state == demo_positioning ) { // Only active during operational states to prevent windup
BasB 68:2879967ebb25 690 error_integral1 = error_integral1 + ei1 * Ts;
Jellehierck 58:8d8a98ec2904 691 u_i1 = Ki * error_integral1;
Jellehierck 58:8d8a98ec2904 692 }
Jellehierck 42:2937ad8f1032 693
Jellehierck 42:2937ad8f1032 694 //Derivative part
Jellehierck 42:2937ad8f1032 695 float error_derivative1 = (motor1_error - e_prev1) / Ts;
Jellehierck 42:2937ad8f1032 696 float u_d1 = Kd * error_derivative1;
Jellehierck 42:2937ad8f1032 697 e_prev1 = motor1_error;
Jellehierck 42:2937ad8f1032 698
Jellehierck 42:2937ad8f1032 699 // Sum and limit
BasB 68:2879967ebb25 700 up1 = u_p1 + u_i1 + u_d1; // Sum the contributions of P, I and D
BasB 68:2879967ebb25 701 if ( up1 > 1.0f ) { // Saturated limit of motor, contintue with value between -1 and 1
BasB 68:2879967ebb25 702 controlsignal1 = 1.0f;
BasB 48:31676da4be7a 703 } else if ( up1 < -1.0f ) {
BasB 48:31676da4be7a 704 controlsignal1 = -1.0f;
Jellehierck 42:2937ad8f1032 705 } else {
Jellehierck 42:2937ad8f1032 706 controlsignal1 = up1;
Jellehierck 42:2937ad8f1032 707 }
Jellehierck 42:2937ad8f1032 708
Jellehierck 42:2937ad8f1032 709 // To prevent windup
BasB 68:2879967ebb25 710 ux1 = up1 - controlsignal1; // Computation of the overflow
Jellehierck 42:2937ad8f1032 711 ek1 = Ka * ux1;
BasB 68:2879967ebb25 712 ei1 = motor1_error - ek1; // Integral error with windup subtracted
Jellehierck 42:2937ad8f1032 713
Jellehierck 42:2937ad8f1032 714 // Motor 2
BasB 48:31676da4be7a 715 static float error_integral2 = 0.0;
BasB 68:2879967ebb25 716 static float e_prev2 = motor2_error; // Saving the previous error, needed for derivative computation
Jellehierck 42:2937ad8f1032 717
Jellehierck 42:2937ad8f1032 718 //Proportional part:
BasB 47:63b5ccd969e9 719 u_p2 = Kp * motor2_error;
Jellehierck 42:2937ad8f1032 720
Jellehierck 42:2937ad8f1032 721 //Integral part
BasB 68:2879967ebb25 722 if ( motor_curr_state == motor_finish || operation_curr_state == operation_movement || demo_curr_state == demo_XY || demo_curr_state == demo_positioning ) { // Only active during operational states to prevent windup
Jellehierck 58:8d8a98ec2904 723 error_integral2 = error_integral2 + ei2 * Ts;
Jellehierck 58:8d8a98ec2904 724 u_i2 = Ki * error_integral2;
Jellehierck 58:8d8a98ec2904 725 }
Jellehierck 42:2937ad8f1032 726
Jellehierck 42:2937ad8f1032 727 //Derivative part
BasB 47:63b5ccd969e9 728 float error_derivative2 = (motor2_error - e_prev2) / Ts;
Jellehierck 42:2937ad8f1032 729 float u_d2 = Kd * error_derivative2;
BasB 47:63b5ccd969e9 730 e_prev2 = motor2_error;
Jellehierck 42:2937ad8f1032 731
Jellehierck 42:2937ad8f1032 732 // Sum and limit
BasB 68:2879967ebb25 733 up2 = u_p2 + u_i2 + u_d2; // Sum the contributions of P, I and D
BasB 68:2879967ebb25 734 if ( up2 > 1.0f ) { // Saturated limit of motor, contintue with value between -1 and 1
Jellehierck 42:2937ad8f1032 735 controlsignal2 = 1.0f;
BasB 48:31676da4be7a 736 } else if ( up2 < -1.0f ) {
Jellehierck 42:2937ad8f1032 737 controlsignal2 = -1.0f;
Jellehierck 42:2937ad8f1032 738 } else {
Jellehierck 42:2937ad8f1032 739 controlsignal2 = up2;
Jellehierck 42:2937ad8f1032 740 }
Jellehierck 42:2937ad8f1032 741
Jellehierck 42:2937ad8f1032 742 // To prevent windup
BasB 68:2879967ebb25 743 ux2 = up2 - controlsignal2; // Computation of the overflow
Jellehierck 42:2937ad8f1032 744 ek2 = Ka * ux2;
BasB 68:2879967ebb25 745 ei2 = motor2_error - ek2; // Integral error with windup subtracted
Jellehierck 42:2937ad8f1032 746 }
Jellehierck 42:2937ad8f1032 747
Jellehierck 42:2937ad8f1032 748 void RKI()
Jellehierck 42:2937ad8f1032 749 {
Jellehierck 42:2937ad8f1032 750 // Derived function for angular velocity of joint angles
BasB 68:2879967ebb25 751 q1dot = (l2*cos(q1+q2)*Vx+l2*sin(q1+q2)*Vy)/((-l1*sin(q1)-l2*sin(q1+q2))*l2*cos(q1+q2)+l2*sin(q1+q2)*(l1*cos(q1)+l2*cos(q1+q2))); // Computation of the desired angular velocities with given linear velocities Vx and Vy
BasB 68:2879967ebb25 752 q2dot = ((-l1*cos(q1)-l2*cos(q1+q2))*Vx+(-l1*sin(q1)-l2*sin(q1+q2))*Vy)/((-l1*sin(q1)-l2*sin(q1+q2))*l2*cos(q1+q2)+l2*sin(q1+q2)*(l1*cos(q1)+l2*cos(q1+q2))); // Computation of the desired angular velocities with given linear velocities Vx and Vy
BasB 68:2879967ebb25 753 q1 = q1 + q1dot * Ts; // Calculating desired joint angle by integrating the desired angular velocity
BasB 68:2879967ebb25 754 q2 = q2 + q2dot * Ts; // Calculating desired joint angle by integrating the desired angular velocity
Jellehierck 42:2937ad8f1032 755
BasB 68:2879967ebb25 756 xe = l1 * cos(q1) + l2 * cos(q1+q2); // Calculation of the current endpoint position
BasB 68:2879967ebb25 757 ye = l1 * sin(q1) + l2 * sin(q1+q2); // Calculation of the current endpoint position
Jellehierck 42:2937ad8f1032 758
BasB 68:2879967ebb25 759 if ( q1 < -5.0f * deg2rad) { // Setting limits to the joint angles
BasB 48:31676da4be7a 760 q1 = -5.0f * deg2rad;
BasB 68:2879967ebb25 761 } else if ( q1 > 65.0f * deg2rad ) { // Setting limits to the joint angles
Jellehierck 42:2937ad8f1032 762 q1 = 65.0f * deg2rad;
Jellehierck 42:2937ad8f1032 763 } else {
Jellehierck 42:2937ad8f1032 764 q1 = q1;
Jellehierck 42:2937ad8f1032 765 }
Jellehierck 42:2937ad8f1032 766
BasB 68:2879967ebb25 767 if ( q2 > -50.0f * deg2rad ) { // Setting limits to the joint angles
BasB 48:31676da4be7a 768 q2 = -50.0f * deg2rad;
BasB 68:2879967ebb25 769 } else if ( q2 < -138.0f * deg2rad ) { // Setting limits to the joint angles
BasB 48:31676da4be7a 770 q2 = -138.0f * deg2rad;
Jellehierck 42:2937ad8f1032 771 } else {
Jellehierck 42:2937ad8f1032 772 q2 = q2;
Jellehierck 42:2937ad8f1032 773 }
Jellehierck 42:2937ad8f1032 774
BasB 68:2879967ebb25 775 motor1_ref = 5.5f * q1 + 5.5f * q2; // Calculating the desired motor angle to attain the correct joint angles
BasB 68:2879967ebb25 776 motor2_ref = 2.75f * q1; // Calculating the desired motor angle to attain the correct joint angles
Jellehierck 42:2937ad8f1032 777 }
Jellehierck 42:2937ad8f1032 778
Jellehierck 42:2937ad8f1032 779 void motorControl()
Jellehierck 42:2937ad8f1032 780 {
BasB 68:2879967ebb25 781 counts1 = counts1af - counts1offset; // Counts measured by encoder compensated with the offset due to calibration
BasB 68:2879967ebb25 782 motor1_angle = (counts1 * encoder_factorin / gearbox_ratio) + motor1_init; // Angle of motor shaft [rad] + correction of initial value
BasB 68:2879967ebb25 783 omega1 = deltaCounts1 / Ts * encoder_factorin / gearbox_ratio; // Angular velocity of motor shaft [rad/s]
BasB 68:2879967ebb25 784 motor1_error = motor1_ref - motor1_angle; // Difference between desired and measured angle of motor
BasB 68:2879967ebb25 785 if ( controlsignal1 < 0.0f ) { // Determine the direction
Jellehierck 42:2937ad8f1032 786 motordir1 = 0;
Jellehierck 42:2937ad8f1032 787 } else {
Jellehierck 42:2937ad8f1032 788 motordir1 = 1;
Jellehierck 42:2937ad8f1032 789 }
Jellehierck 42:2937ad8f1032 790
BasB 68:2879967ebb25 791 motor1Power.write(abs(controlsignal1)); // Assigning the desired power to the motor
BasB 68:2879967ebb25 792 motor1Direction = motordir1; // Assigning the desired direction
Jellehierck 42:2937ad8f1032 793
BasB 68:2879967ebb25 794 counts2 = counts2af - counts2offset; // Counts measured by encoder compensated with the offset due to calibration
BasB 68:2879967ebb25 795 motor2_angle = (counts2 * encoder_factorin / gearbox_ratio) + motor2_init; // Angle of motor shaft [rad] + correction of initial value
BasB 68:2879967ebb25 796 omega2 = deltaCounts2 / Ts * encoder_factorin / gearbox_ratio; // Angular velocity of motor shaft [rad/s]
BasB 68:2879967ebb25 797 motor2_error = motor2_ref-motor2_angle; // Difference between desired and measured angle of motor
BasB 68:2879967ebb25 798 if ( controlsignal2 < 0.0f ) { // Determine the direction
Jellehierck 42:2937ad8f1032 799 motordir2 = 0;
Jellehierck 42:2937ad8f1032 800 } else {
Jellehierck 42:2937ad8f1032 801 motordir2 = 1;
Jellehierck 42:2937ad8f1032 802 }
Jellehierck 42:2937ad8f1032 803 if (motor_encoder_cal_done == true) {
BasB 68:2879967ebb25 804 motor2Power.write(abs(controlsignal2)); // Assinging the desired power to the motor
Jellehierck 42:2937ad8f1032 805 }
BasB 68:2879967ebb25 806 motor2Direction = motordir2; // Assigning the desired direction to the motor
Jellehierck 42:2937ad8f1032 807 }
Jellehierck 42:2937ad8f1032 808
Jellehierck 42:2937ad8f1032 809 void motorKillPower()
Jellehierck 42:2937ad8f1032 810 {
BasB 68:2879967ebb25 811 motor1Power.write(0.0f); // Setting motor power to 0 to stop motion
BasB 68:2879967ebb25 812 motor2Power.write(0.0f); // Setting motor power to 0 to stop motion
BasB 68:2879967ebb25 813 Vx=0.0f; // Setting desired linear motion to 0 to prevent RKI from adjusting the motorpower
BasB 68:2879967ebb25 814 Vy=0.0f; // Setting desired linear motion to 0 to prevent RKI from adjusting the motorpower
Jellehierck 42:2937ad8f1032 815 }
Jellehierck 42:2937ad8f1032 816
Jellehierck 42:2937ad8f1032 817 /*
Jellehierck 42:2937ad8f1032 818 ------------------------------ MOTOR SUBSTATE FUNCTIONS ------------------------------
Jellehierck 42:2937ad8f1032 819 */
Jellehierck 42:2937ad8f1032 820 void do_motor_encoder_set()
Jellehierck 42:2937ad8f1032 821 {
Jellehierck 42:2937ad8f1032 822 // Entry function
Jellehierck 42:2937ad8f1032 823 if ( motor_state_changed == true ) {
Jellehierck 42:2937ad8f1032 824 motor_state_changed = false;
Jellehierck 42:2937ad8f1032 825 // More functions
Jellehierck 42:2937ad8f1032 826 }
BasB 68:2879967ebb25 827 motor1Power.write(0.0f); // Giving the motor no power to be able to adjust the robot configuration
Jellehierck 42:2937ad8f1032 828 motor2Power.write(0.0f);
BasB 68:2879967ebb25 829 counts1offset = counts1af ; // Storing the offset of the encoder
BasB 68:2879967ebb25 830 counts2offset = counts2af; // Storing the offset of the encoder
Jellehierck 42:2937ad8f1032 831
Jellehierck 42:2937ad8f1032 832 // State transition guard
Jellehierck 42:2937ad8f1032 833 if ( button2_pressed ) {
Jellehierck 42:2937ad8f1032 834 button2_pressed = false;
Jellehierck 42:2937ad8f1032 835 motor_encoder_cal_done = true;
Jellehierck 42:2937ad8f1032 836 motor_curr_state = motor_finish;
Jellehierck 42:2937ad8f1032 837 motor_state_changed = true;
Jellehierck 42:2937ad8f1032 838 }
Jellehierck 42:2937ad8f1032 839 }
Jellehierck 42:2937ad8f1032 840
Jellehierck 42:2937ad8f1032 841 void do_motor_finish()
Jellehierck 42:2937ad8f1032 842 {
Jellehierck 42:2937ad8f1032 843 // Entry function
Jellehierck 42:2937ad8f1032 844 if ( motor_state_changed == true ) {
Jellehierck 42:2937ad8f1032 845 motor_state_changed = false;
Jellehierck 57:d0d65376140f 846 timerStateChange.reset();
Jellehierck 57:d0d65376140f 847 timerStateChange.start();
Jellehierck 42:2937ad8f1032 848 }
Jellehierck 42:2937ad8f1032 849
Jellehierck 42:2937ad8f1032 850 // Do stuff until end condition is true
Jellehierck 42:2937ad8f1032 851 PID_controller();
BasB 68:2879967ebb25 852 motorControl(); //These three functions cause the robot to move within the link limits
Jellehierck 42:2937ad8f1032 853 RKI();
Jellehierck 42:2937ad8f1032 854
Jellehierck 42:2937ad8f1032 855 // State transition guard
Jellehierck 57:d0d65376140f 856 if ( timerStateChange.read() >= 3.0f ) { // After 3 seconds move to next state
Jellehierck 42:2937ad8f1032 857 button2_pressed = false;
Jellehierck 42:2937ad8f1032 858 motor_cal_done = true;
Jellehierck 51:7fe2659a1fcb 859 motor_curr_state = motor_encoder_set;
Jellehierck 42:2937ad8f1032 860 motor_state_changed = true;
Jellehierck 42:2937ad8f1032 861 }
Jellehierck 42:2937ad8f1032 862 }
Jellehierck 42:2937ad8f1032 863
Jellehierck 42:2937ad8f1032 864 /*
Jellehierck 54:a14acf0d1683 865 ------------------------------ MOTOR SUBSTATE MACHINE ------------------------------
Jellehierck 42:2937ad8f1032 866 */
Jellehierck 54:a14acf0d1683 867 void motor_state_machine()
Jellehierck 42:2937ad8f1032 868 {
Jellehierck 54:a14acf0d1683 869 switch(motor_curr_state) {
Jellehierck 54:a14acf0d1683 870 case motor_encoder_set:
Jellehierck 54:a14acf0d1683 871 do_motor_encoder_set();
Jellehierck 54:a14acf0d1683 872 break;
Jellehierck 54:a14acf0d1683 873 case motor_finish:
Jellehierck 54:a14acf0d1683 874 do_motor_finish();
Jellehierck 54:a14acf0d1683 875 break;
Jellehierck 42:2937ad8f1032 876 }
Jellehierck 42:2937ad8f1032 877 }
Jellehierck 42:2937ad8f1032 878
Jellehierck 42:2937ad8f1032 879 /*
Jellehierck 42:2937ad8f1032 880 ------------------------------ OPERATION SUBSTATE FUNCTIONS ------------------------------
Jellehierck 42:2937ad8f1032 881 */
Jellehierck 42:2937ad8f1032 882 void do_operation_wait()
Jellehierck 42:2937ad8f1032 883 {
Jellehierck 42:2937ad8f1032 884 // Entry function
Jellehierck 42:2937ad8f1032 885 if ( operation_state_changed == true ) {
Jellehierck 42:2937ad8f1032 886 operation_state_changed = false;
Jellehierck 42:2937ad8f1032 887 emg_sampleNow = false; // Disable signal sampling in sampleSignals()
Jellehierck 42:2937ad8f1032 888 emg_calibrateNow = false; // Disable calibration functionality in sampleSignals()
Jellehierck 42:2937ad8f1032 889 }
Jellehierck 42:2937ad8f1032 890
Jellehierck 42:2937ad8f1032 891 // Do stuff until end condition is met
Jellehierck 43:1bd5417ded64 892 EMGOperationFunc();
Jellehierck 44:342af9b3c1a0 893
BasB 69:bfefdfb04c29 894 Vx = emg1_out * (10.0f + 10.0f * potmeter1.read() ) * emg1_dir;
BasB 69:bfefdfb04c29 895 Vy = emg2_out * (10.0f + 10.0f * potmeter2.read() ) * emg2_dir;
Jellehierck 44:342af9b3c1a0 896
Jellehierck 43:1bd5417ded64 897 PID_controller();
Jellehierck 43:1bd5417ded64 898 motorControl();
Jellehierck 43:1bd5417ded64 899 RKI();
Jellehierck 44:342af9b3c1a0 900
Jellehierck 42:2937ad8f1032 901 motorKillPower(); // Disables motor outputs
Jellehierck 44:342af9b3c1a0 902
Jellehierck 42:2937ad8f1032 903 // State transition guard
Jellehierck 45:d09040915cfe 904 if ( button1_pressed == true ) {
Jellehierck 45:d09040915cfe 905 button1_pressed = false;
Jellehierck 42:2937ad8f1032 906 operation_curr_state = operation_movement;
Jellehierck 42:2937ad8f1032 907 operation_state_changed = true;
Jellehierck 45:d09040915cfe 908 } else if ( button2_pressed == true ) {
Jellehierck 45:d09040915cfe 909 button2_pressed = false;
Jellehierck 64:a0ca27f75801 910 operation_curr_state = operation_wait;
Jellehierck 45:d09040915cfe 911 operation_state_changed = true;
Jellehierck 64:a0ca27f75801 912 exit_operation = true;
Jellehierck 42:2937ad8f1032 913 }
Jellehierck 42:2937ad8f1032 914 }
Jellehierck 42:2937ad8f1032 915
Jellehierck 42:2937ad8f1032 916 void do_operation_movement()
Jellehierck 42:2937ad8f1032 917 {
Jellehierck 42:2937ad8f1032 918 // Entry function
Jellehierck 42:2937ad8f1032 919 if ( operation_state_changed == true ) {
Jellehierck 42:2937ad8f1032 920 operation_state_changed = false;
Jellehierck 42:2937ad8f1032 921 emg_sampleNow = true; // Enable signal sampling in sampleSignals()
Jellehierck 42:2937ad8f1032 922 emg_calibrateNow = false; // Disable calibration functionality in sampleSignals()
Jellehierck 42:2937ad8f1032 923 }
Jellehierck 42:2937ad8f1032 924
Jellehierck 42:2937ad8f1032 925 // Do stuff until end condition is met
Jellehierck 43:1bd5417ded64 926 EMGOperationFunc();
Jellehierck 44:342af9b3c1a0 927
BasB 69:bfefdfb04c29 928 Vx = emg1_out * (10.0f + 10.0f * potmeter1.read() ) * emg1_dir;
BasB 69:bfefdfb04c29 929 Vy = emg2_out * (10.0f + 10.0f * potmeter2.read() ) * emg2_dir;
Jellehierck 44:342af9b3c1a0 930
Jellehierck 43:1bd5417ded64 931 PID_controller();
Jellehierck 43:1bd5417ded64 932 motorControl();
Jellehierck 43:1bd5417ded64 933 RKI();
Jellehierck 44:342af9b3c1a0 934
Jellehierck 42:2937ad8f1032 935 // State transition guard
Jellehierck 45:d09040915cfe 936 if ( button1_pressed == true ) {
Jellehierck 45:d09040915cfe 937 button1_pressed = false;
Jellehierck 42:2937ad8f1032 938 operation_curr_state = operation_wait;
Jellehierck 42:2937ad8f1032 939 operation_state_changed = true;
Jellehierck 45:d09040915cfe 940 } else if ( button2_pressed == true ) {
Jellehierck 45:d09040915cfe 941 button2_pressed = false;
Jellehierck 42:2937ad8f1032 942 operation_curr_state = operation_wait;
Jellehierck 42:2937ad8f1032 943 operation_state_changed = true;
Jellehierck 51:7fe2659a1fcb 944 exit_operation = true;
Jellehierck 42:2937ad8f1032 945 }
Jellehierck 42:2937ad8f1032 946 }
Jellehierck 42:2937ad8f1032 947
Jellehierck 38:8b597ab8344f 948 /*
Jellehierck 42:2937ad8f1032 949 ------------------------------ OPERATION SUBSTATE MACHINE ------------------------------
Jellehierck 42:2937ad8f1032 950 */
Jellehierck 42:2937ad8f1032 951
Jellehierck 42:2937ad8f1032 952 void operation_state_machine()
Jellehierck 42:2937ad8f1032 953 {
Jellehierck 42:2937ad8f1032 954 switch(operation_curr_state) {
Jellehierck 42:2937ad8f1032 955 case operation_wait:
Jellehierck 42:2937ad8f1032 956 do_operation_wait();
Jellehierck 42:2937ad8f1032 957 break;
Jellehierck 42:2937ad8f1032 958 case operation_movement:
Jellehierck 42:2937ad8f1032 959 do_operation_movement();
Jellehierck 42:2937ad8f1032 960 break;
Jellehierck 42:2937ad8f1032 961 }
Jellehierck 42:2937ad8f1032 962 }
Jellehierck 42:2937ad8f1032 963
Jellehierck 42:2937ad8f1032 964 /*
Jellehierck 43:1bd5417ded64 965 ------------------------------ DEMO SUBSTATE FUNCTIONS ------------------------------
Jellehierck 43:1bd5417ded64 966 */
Jellehierck 44:342af9b3c1a0 967 void do_demo_wait()
Jellehierck 44:342af9b3c1a0 968 {
Jellehierck 43:1bd5417ded64 969 // Entry function
Jellehierck 43:1bd5417ded64 970 if ( demo_state_changed == true ) {
Jellehierck 43:1bd5417ded64 971 demo_state_changed = false;
Jellehierck 43:1bd5417ded64 972 }
Jellehierck 43:1bd5417ded64 973
Jellehierck 43:1bd5417ded64 974 // Do nothing until end condition is met
Jellehierck 67:edc3c644d316 975 PID_controller();
Jellehierck 67:edc3c644d316 976 motorControl();
Jellehierck 67:edc3c644d316 977 RKI();
Jellehierck 67:edc3c644d316 978
Jellehierck 67:edc3c644d316 979 motorKillPower();
Jellehierck 44:342af9b3c1a0 980
Jellehierck 43:1bd5417ded64 981 // State transition guard
Jellehierck 44:342af9b3c1a0 982 if ( button1_pressed == true ) { // demo_XY
Jellehierck 43:1bd5417ded64 983 button1_pressed = false;
Jellehierck 60:762b5c925e13 984 demo_curr_state = demo_positioning;
Jellehierck 43:1bd5417ded64 985 demo_state_changed = true;
Jellehierck 43:1bd5417ded64 986 // More functions
Jellehierck 57:d0d65376140f 987 } else if ( switch2_pressed == true ) { // Return to global wait
Jellehierck 57:d0d65376140f 988 switch2_pressed = false;
Jellehierck 43:1bd5417ded64 989 demo_curr_state = demo_wait;
Jellehierck 43:1bd5417ded64 990 demo_state_changed = true;
Jellehierck 43:1bd5417ded64 991 motor_cal_done = false; // Disables motor calibration again (robot is probably not in reference position)
Jellehierck 57:d0d65376140f 992 exit_demo = true;
Jellehierck 43:1bd5417ded64 993 }
Jellehierck 43:1bd5417ded64 994 }
Jellehierck 43:1bd5417ded64 995
Jellehierck 44:342af9b3c1a0 996 void do_demo_motor_cal()
Jellehierck 44:342af9b3c1a0 997 {
Jellehierck 43:1bd5417ded64 998 // Entry function
Jellehierck 43:1bd5417ded64 999 if ( demo_state_changed == true ) {
Jellehierck 43:1bd5417ded64 1000 demo_state_changed = false;
Jellehierck 57:d0d65376140f 1001 set_led_color('c'); // Set LED to motor calibration (CYAN)
Jellehierck 43:1bd5417ded64 1002 }
Jellehierck 67:edc3c644d316 1003
Jellehierck 67:edc3c644d316 1004 PID_controller();
Jellehierck 67:edc3c644d316 1005 motorControl();
Jellehierck 67:edc3c644d316 1006 RKI();
Jellehierck 43:1bd5417ded64 1007
Jellehierck 43:1bd5417ded64 1008 // Do stuff until end condition is met
Jellehierck 43:1bd5417ded64 1009 motor_state_machine();
Jellehierck 43:1bd5417ded64 1010
Jellehierck 43:1bd5417ded64 1011 // State transition guard
Jellehierck 44:342af9b3c1a0 1012 if ( motor_cal_done == true ) { // demo_wait
Jellehierck 43:1bd5417ded64 1013 demo_curr_state = demo_wait;
Jellehierck 43:1bd5417ded64 1014 demo_state_changed = true;
Jellehierck 57:d0d65376140f 1015 set_led_color('y'); // Set LED to demo mode (YELLOW)
Jellehierck 43:1bd5417ded64 1016 }
Jellehierck 43:1bd5417ded64 1017 }
Jellehierck 43:1bd5417ded64 1018
Jellehierck 59:57b9d3076930 1019 void do_demo_positioning()
Jellehierck 59:57b9d3076930 1020 {
Jellehierck 59:57b9d3076930 1021 // Entry function
Jellehierck 59:57b9d3076930 1022 if ( demo_state_changed == true ) {
Jellehierck 59:57b9d3076930 1023 demo_state_changed = false;
Jellehierck 59:57b9d3076930 1024 timerStateChange.reset();
Jellehierck 59:57b9d3076930 1025 timerStateChange.start();
Jellehierck 59:57b9d3076930 1026 }
Jellehierck 60:762b5c925e13 1027
BasB 69:bfefdfb04c29 1028
BasB 68:2879967ebb25 1029 Vx = 5.0; // Move in the positive x direction
BasB 68:2879967ebb25 1030 Vy = 5.0; // Move in the positive y direction
Jellehierck 59:57b9d3076930 1031
Jellehierck 59:57b9d3076930 1032 PID_controller();
Jellehierck 59:57b9d3076930 1033 motorControl();
Jellehierck 59:57b9d3076930 1034 RKI();
Jellehierck 59:57b9d3076930 1035
Jellehierck 59:57b9d3076930 1036 scope.set(0, motor2_ref * rad2deg );
Jellehierck 59:57b9d3076930 1037 scope.set(1, motor2_angle * rad2deg );
Jellehierck 59:57b9d3076930 1038 scope.send();
Jellehierck 59:57b9d3076930 1039
Jellehierck 59:57b9d3076930 1040 // State transition guard
Jellehierck 67:edc3c644d316 1041 if ( timerStateChange.read() >= 5.0f ) { // demo_wait
Jellehierck 59:57b9d3076930 1042 button1_pressed = false;
Jellehierck 59:57b9d3076930 1043 demo_curr_state = demo_XY;
Jellehierck 59:57b9d3076930 1044 demo_state_changed = true;
Jellehierck 59:57b9d3076930 1045 }
Jellehierck 59:57b9d3076930 1046 }
Jellehierck 59:57b9d3076930 1047
Jellehierck 44:342af9b3c1a0 1048 void do_demo_XY()
Jellehierck 44:342af9b3c1a0 1049 {
Jellehierck 43:1bd5417ded64 1050 // Entry function
Jellehierck 43:1bd5417ded64 1051 if ( demo_state_changed == true ) {
Jellehierck 43:1bd5417ded64 1052 demo_state_changed = false;
Jellehierck 43:1bd5417ded64 1053 }
Jellehierck 43:1bd5417ded64 1054
Jellehierck 43:1bd5417ded64 1055 // Do stuff until end condition is met
BasB 48:31676da4be7a 1056 static float t = 0.0;
Jellehierck 59:57b9d3076930 1057
BasB 68:2879967ebb25 1058 if ( t >= 0.0f && t < 3.0f ) { // With this code the endpoint will make a square every 12 seconds
Jellehierck 61:07035c03cf29 1059 Vx = 5.0;
Jellehierck 59:57b9d3076930 1060 } else if ( t >= 3.0f && t < 6.0f ) {
Jellehierck 61:07035c03cf29 1061 Vx = 0.0;
Jellehierck 61:07035c03cf29 1062 Vy = 5.0;
Jellehierck 59:57b9d3076930 1063 } else if ( t >= 6.0f && t < 9.0f ) {
Jellehierck 61:07035c03cf29 1064 Vx = -5.0;
Jellehierck 61:07035c03cf29 1065 Vy = 0.0;
Jellehierck 59:57b9d3076930 1066 }
Jellehierck 59:57b9d3076930 1067 if ( t >= 9.0f && t < 12.0f ) {
Jellehierck 61:07035c03cf29 1068 Vx = 0.0;
Jellehierck 61:07035c03cf29 1069 Vy = -5.0;
Jellehierck 59:57b9d3076930 1070 } else if ( t >= 12.0f) {
Jellehierck 59:57b9d3076930 1071 t = 0.0;
Jellehierck 59:57b9d3076930 1072 }
Jellehierck 43:1bd5417ded64 1073 t += Ts;
Jellehierck 44:342af9b3c1a0 1074
Jellehierck 43:1bd5417ded64 1075 PID_controller();
Jellehierck 43:1bd5417ded64 1076 motorControl();
Jellehierck 43:1bd5417ded64 1077 RKI();
Jellehierck 58:8d8a98ec2904 1078
BasB 56:3f29502fcb55 1079 scope.set(0, motor2_ref * rad2deg );
BasB 56:3f29502fcb55 1080 scope.set(1, motor2_angle * rad2deg );
BasB 56:3f29502fcb55 1081 //scope.set(2, motor2_ref );
BasB 56:3f29502fcb55 1082 //scope.set(3, motor2_angle );
BasB 56:3f29502fcb55 1083 scope.send();
Jellehierck 43:1bd5417ded64 1084
Jellehierck 43:1bd5417ded64 1085 // State transition guard
Jellehierck 44:342af9b3c1a0 1086 if ( button1_pressed == true ) { // demo_wait
Jellehierck 59:57b9d3076930 1087 t = 0.0;
Jellehierck 44:342af9b3c1a0 1088 button1_pressed = false;
Jellehierck 43:1bd5417ded64 1089 demo_curr_state = demo_wait;
Jellehierck 43:1bd5417ded64 1090 demo_state_changed = true;
Jellehierck 43:1bd5417ded64 1091 }
Jellehierck 43:1bd5417ded64 1092 }
Jellehierck 43:1bd5417ded64 1093
Jellehierck 43:1bd5417ded64 1094 /*
Jellehierck 43:1bd5417ded64 1095 ------------------------------ DEMO SUBSTATE MACHINE ------------------------------
Jellehierck 43:1bd5417ded64 1096 */
Jellehierck 43:1bd5417ded64 1097
Jellehierck 43:1bd5417ded64 1098 void demo_state_machine()
Jellehierck 43:1bd5417ded64 1099 {
Jellehierck 43:1bd5417ded64 1100 switch(demo_curr_state) {
Jellehierck 43:1bd5417ded64 1101 case demo_wait:
Jellehierck 43:1bd5417ded64 1102 do_demo_wait();
Jellehierck 43:1bd5417ded64 1103 break;
Jellehierck 43:1bd5417ded64 1104 case demo_motor_cal:
Jellehierck 43:1bd5417ded64 1105 do_demo_motor_cal();
Jellehierck 43:1bd5417ded64 1106 break;
Jellehierck 60:762b5c925e13 1107 case demo_positioning:
Jellehierck 60:762b5c925e13 1108 do_demo_positioning();
Jellehierck 60:762b5c925e13 1109 break;
Jellehierck 43:1bd5417ded64 1110 case demo_XY:
Jellehierck 43:1bd5417ded64 1111 do_demo_XY();
Jellehierck 43:1bd5417ded64 1112 break;
Jellehierck 43:1bd5417ded64 1113 }
Jellehierck 43:1bd5417ded64 1114 }
Jellehierck 43:1bd5417ded64 1115
Jellehierck 43:1bd5417ded64 1116 /*
Jellehierck 37:806c7c8381a7 1117 ------------------------------ GLOBAL STATE FUNCTIONS ------------------------------
Jellehierck 15:421d3d9c563b 1118 */
Jellehierck 25:a1be4cf2ab0b 1119 /* ALL STATES HAVE THE FOLLOWING FORM:
Jellehierck 25:a1be4cf2ab0b 1120 void do_state_function() {
Jellehierck 25:a1be4cf2ab0b 1121 // Entry function
Jellehierck 37:806c7c8381a7 1122 if ( global_state_changed == true ) {
Jellehierck 37:806c7c8381a7 1123 global_state_changed = false;
Jellehierck 25:a1be4cf2ab0b 1124 // More functions
Jellehierck 25:a1be4cf2ab0b 1125 }
Jellehierck 25:a1be4cf2ab0b 1126
Jellehierck 25:a1be4cf2ab0b 1127 // Do stuff until end condition is met
Jellehierck 25:a1be4cf2ab0b 1128 doStuff();
Jellehierck 25:a1be4cf2ab0b 1129
Jellehierck 25:a1be4cf2ab0b 1130 // State transition guard
Jellehierck 25:a1be4cf2ab0b 1131 if ( endCondition == true ) {
Jellehierck 37:806c7c8381a7 1132 global_curr_state = next_state;
Jellehierck 37:806c7c8381a7 1133 global_state_changed = true;
Jellehierck 25:a1be4cf2ab0b 1134 // More functions
Jellehierck 25:a1be4cf2ab0b 1135 }
Jellehierck 25:a1be4cf2ab0b 1136 }
Jellehierck 25:a1be4cf2ab0b 1137 */
Jellehierck 25:a1be4cf2ab0b 1138
Jellehierck 37:806c7c8381a7 1139 // FAILURE MODE
Jellehierck 37:806c7c8381a7 1140 void do_global_failure()
Jellehierck 7:7a088536f1c9 1141 {
Jellehierck 37:806c7c8381a7 1142 // Entry function
Jellehierck 37:806c7c8381a7 1143 if ( global_state_changed == true ) {
Jellehierck 37:806c7c8381a7 1144 global_state_changed = false;
Jellehierck 52:fd35025574ca 1145 set_led_color('r'); // Set failure mode LED (RED)
Jellehierck 25:a1be4cf2ab0b 1146
Jellehierck 37:806c7c8381a7 1147 failure_mode = true; // Set failure mode
Jellehierck 22:9079c6c0d898 1148 }
Jellehierck 37:806c7c8381a7 1149
Jellehierck 37:806c7c8381a7 1150 // Do stuff until end condition is met
Jellehierck 42:2937ad8f1032 1151 motorKillPower();
Jellehierck 37:806c7c8381a7 1152
Jellehierck 37:806c7c8381a7 1153 // State transition guard
Jellehierck 37:806c7c8381a7 1154 if ( false ) { // Never move to other state
Jellehierck 37:806c7c8381a7 1155 global_curr_state = global_wait;
Jellehierck 37:806c7c8381a7 1156 global_state_changed = true;
Jellehierck 52:fd35025574ca 1157 set_led_color('o'); // Disable failure mode LED
Jellehierck 37:806c7c8381a7 1158 }
Jellehierck 25:a1be4cf2ab0b 1159 }
Jellehierck 25:a1be4cf2ab0b 1160
Jellehierck 37:806c7c8381a7 1161 // DEMO MODE
Jellehierck 37:806c7c8381a7 1162 void do_global_demo()
Jellehierck 25:a1be4cf2ab0b 1163 {
Jellehierck 25:a1be4cf2ab0b 1164 // Entry function
Jellehierck 37:806c7c8381a7 1165 if ( global_state_changed == true ) {
Jellehierck 37:806c7c8381a7 1166 global_state_changed = false;
Jellehierck 52:fd35025574ca 1167 set_led_color('y'); // Set demo mode LED (YELLOW)
Jellehierck 57:d0d65376140f 1168 emg_cal_done = false; // Disables EMG calibration to prevent going into operation mode after exiting demo state
Jellehierck 51:7fe2659a1fcb 1169
Jellehierck 49:80970a03083b 1170 if ( motor_cal_done == true ) {
Jellehierck 49:80970a03083b 1171 demo_curr_state = demo_wait;
Jellehierck 49:80970a03083b 1172 } else if (motor_cal_done == false ) {
Jellehierck 49:80970a03083b 1173 demo_curr_state = demo_motor_cal;
Jellehierck 49:80970a03083b 1174 }
Jellehierck 49:80970a03083b 1175 demo_state_changed = true;
Jellehierck 37:806c7c8381a7 1176 }
Jellehierck 37:806c7c8381a7 1177
Jellehierck 37:806c7c8381a7 1178 // Do stuff until end condition is met
Jellehierck 46:8a8fa8e602a1 1179 demo_state_machine();
Jellehierck 51:7fe2659a1fcb 1180
Jellehierck 37:806c7c8381a7 1181 // State transition guard
Jellehierck 57:d0d65376140f 1182 if ( exit_demo == true ) {
Jellehierck 57:d0d65376140f 1183 exit_demo = false;
Jellehierck 37:806c7c8381a7 1184 global_curr_state = global_wait;
Jellehierck 37:806c7c8381a7 1185 global_state_changed = true;
Jellehierck 52:fd35025574ca 1186 set_led_color('o'); // Disable demo mode LED
BasB 63:d17f45d88c7a 1187
BasB 63:d17f45d88c7a 1188 motor1_ref = motor1_init;
BasB 63:d17f45d88c7a 1189 motor1_angle = motor1_init;
BasB 63:d17f45d88c7a 1190 motor2_ref = motor2_init;
BasB 63:d17f45d88c7a 1191 motor2_angle = motor2_init;
Jellehierck 37:806c7c8381a7 1192 }
Jellehierck 37:806c7c8381a7 1193 }
Jellehierck 37:806c7c8381a7 1194
Jellehierck 37:806c7c8381a7 1195 // WAIT MODE
Jellehierck 37:806c7c8381a7 1196 void do_global_wait()
Jellehierck 37:806c7c8381a7 1197 {
Jellehierck 37:806c7c8381a7 1198 // Entry function
Jellehierck 37:806c7c8381a7 1199 if ( global_state_changed == true ) {
Jellehierck 37:806c7c8381a7 1200 global_state_changed = false;
Jellehierck 52:fd35025574ca 1201 set_led_color('w'); // Set wait mode LED (WHITE)
Jellehierck 25:a1be4cf2ab0b 1202 }
Jellehierck 25:a1be4cf2ab0b 1203
Jellehierck 27:f18da01093c9 1204 // Do nothing until end condition is met
Jellehierck 67:edc3c644d316 1205 motorKillPower();
Jellehierck 67:edc3c644d316 1206
Jellehierck 25:a1be4cf2ab0b 1207
Jellehierck 37:806c7c8381a7 1208 // State transition guard
Jellehierck 37:806c7c8381a7 1209 if ( switch2_pressed == true ) { // DEMO MODE
Jellehierck 37:806c7c8381a7 1210 switch2_pressed = false;
Jellehierck 37:806c7c8381a7 1211 global_curr_state = global_demo;
Jellehierck 37:806c7c8381a7 1212 global_state_changed = true;
Jellehierck 52:fd35025574ca 1213 set_led_color('o'); // Disable wait mode LED
Jellehierck 31:b5188b6d45db 1214
Jellehierck 37:806c7c8381a7 1215 } else if ( button1_pressed == true ) { // EMG CALIBRATION
Jellehierck 37:806c7c8381a7 1216 button1_pressed = false;
Jellehierck 38:8b597ab8344f 1217 global_curr_state = global_emg_cal;
Jellehierck 37:806c7c8381a7 1218 global_state_changed = true;
Jellehierck 52:fd35025574ca 1219 set_led_color('o'); // Disable wait mode LED
Jellehierck 31:b5188b6d45db 1220
Jellehierck 37:806c7c8381a7 1221 } else if ( button2_pressed == true ) { // MOTOR CALIBRATION
Jellehierck 37:806c7c8381a7 1222 button2_pressed = false;
Jellehierck 38:8b597ab8344f 1223 global_curr_state = global_motor_cal;
Jellehierck 37:806c7c8381a7 1224 global_state_changed = true;
Jellehierck 52:fd35025574ca 1225 set_led_color('o'); // Disable wait mode LED
Jellehierck 42:2937ad8f1032 1226
Jellehierck 39:f9042483b921 1227 } else if ( emg_cal_done && motor_cal_done ) { // OPERATION MODE
Jellehierck 39:f9042483b921 1228 global_curr_state = global_operation;
Jellehierck 39:f9042483b921 1229 global_state_changed = true;
Jellehierck 52:fd35025574ca 1230 set_led_color('o'); // Disable wait mode LED
Jellehierck 25:a1be4cf2ab0b 1231 }
Jellehierck 7:7a088536f1c9 1232 }
Jellehierck 7:7a088536f1c9 1233
Jellehierck 37:806c7c8381a7 1234 // EMG CALIBRATION MODE
Jellehierck 38:8b597ab8344f 1235 void do_global_emg_cal()
Jellehierck 21:e4569b47945e 1236 {
Jellehierck 37:806c7c8381a7 1237 // Entry function
Jellehierck 37:806c7c8381a7 1238 if ( global_state_changed == true ) {
Jellehierck 37:806c7c8381a7 1239 global_state_changed = false;
Jellehierck 51:7fe2659a1fcb 1240 set_led_color('p'); // Set EMG calibration LED (PURPLE)
Jellehierck 22:9079c6c0d898 1241 }
Jellehierck 7:7a088536f1c9 1242
Jellehierck 39:f9042483b921 1243 // Run EMG state machine until emg_cal_done flag is true
Jellehierck 39:f9042483b921 1244 emg_state_machine();
Jellehierck 31:b5188b6d45db 1245
Jellehierck 29:f51683a6cbbf 1246 // State transition guard
Jellehierck 39:f9042483b921 1247 if ( emg_cal_done == true ) { // WAIT MODE
Jellehierck 37:806c7c8381a7 1248 global_curr_state = global_wait;
Jellehierck 37:806c7c8381a7 1249 global_state_changed = true;
Jellehierck 51:7fe2659a1fcb 1250 set_led_color('o'); // Disable EMG calibration LED
Jellehierck 25:a1be4cf2ab0b 1251 }
Jellehierck 25:a1be4cf2ab0b 1252 }
Jellehierck 23:8a0a0b959af1 1253
Jellehierck 37:806c7c8381a7 1254 // MOTOR CALIBRATION MODE
Jellehierck 38:8b597ab8344f 1255 void do_global_motor_cal()
Jellehierck 26:7e81c7db6e7a 1256 {
Jellehierck 25:a1be4cf2ab0b 1257 // Entry function
Jellehierck 37:806c7c8381a7 1258 if ( global_state_changed == true ) {
Jellehierck 37:806c7c8381a7 1259 global_state_changed = false;
Jellehierck 51:7fe2659a1fcb 1260 set_led_color('c'); // Set motor calibration LED (CYAN)
Jellehierck 25:a1be4cf2ab0b 1261 }
Jellehierck 25:a1be4cf2ab0b 1262
Jellehierck 25:a1be4cf2ab0b 1263 // Do stuff until end condition is met
Jellehierck 40:c6dffb676350 1264 motor_state_machine();
Jellehierck 28:59e8266f4633 1265
Jellehierck 25:a1be4cf2ab0b 1266 // State transition guard
Jellehierck 41:8e8141f355af 1267 if ( motor_cal_done == true ) { // WAIT MODE
Jellehierck 37:806c7c8381a7 1268 global_curr_state = global_wait;
Jellehierck 37:806c7c8381a7 1269 global_state_changed = true;
Jellehierck 51:7fe2659a1fcb 1270 set_led_color('o'); // Disable motor calibration LED
Jellehierck 23:8a0a0b959af1 1271 }
Jellehierck 23:8a0a0b959af1 1272 }
Jellehierck 23:8a0a0b959af1 1273
Jellehierck 37:806c7c8381a7 1274 // OPERATION MODE
Jellehierck 37:806c7c8381a7 1275 void do_global_operation()
Jellehierck 37:806c7c8381a7 1276 {
Jellehierck 37:806c7c8381a7 1277 // Entry function
Jellehierck 37:806c7c8381a7 1278 if ( global_state_changed == true ) {
Jellehierck 37:806c7c8381a7 1279 global_state_changed = false;
Jellehierck 66:17be4d8e2c26 1280
Jellehierck 66:17be4d8e2c26 1281 button1.fall( &button1Press );
Jellehierck 66:17be4d8e2c26 1282 button2.fall( &button2Press );
Jellehierck 40:c6dffb676350 1283
Jellehierck 39:f9042483b921 1284 emg_sampleNow = true; // Enable signal sampling in sampleSignals()
Jellehierck 39:f9042483b921 1285 emg_calibrateNow = false; // Disable calibration functionality in sampleSignals()
Jellehierck 51:7fe2659a1fcb 1286 set_led_color('g'); // Set operation led (GREEN)
Jellehierck 37:806c7c8381a7 1287 }
Jellehierck 37:806c7c8381a7 1288
Jellehierck 37:806c7c8381a7 1289 // Do stuff until end condition is met
Jellehierck 43:1bd5417ded64 1290 operation_state_machine();
Jellehierck 42:2937ad8f1032 1291
Jellehierck 39:f9042483b921 1292 // Set HIDScope outputs
Jellehierck 39:f9042483b921 1293 scope.set(0, emg1 );
Jellehierck 41:8e8141f355af 1294 scope.set(1, Vx );
Jellehierck 41:8e8141f355af 1295 scope.set(2, emg2 );
Jellehierck 41:8e8141f355af 1296 scope.set(3, Vy );
Jellehierck 39:f9042483b921 1297 scope.send();
Jellehierck 39:f9042483b921 1298
Jellehierck 37:806c7c8381a7 1299 // State transition guard
Jellehierck 51:7fe2659a1fcb 1300 if ( exit_operation == true ) {
Jellehierck 51:7fe2659a1fcb 1301 exit_operation = false;
Jellehierck 37:806c7c8381a7 1302 global_curr_state = global_wait;
Jellehierck 37:806c7c8381a7 1303 global_state_changed = true;
Jellehierck 52:fd35025574ca 1304 set_led_color('o'); // Disable operation led
Jellehierck 66:17be4d8e2c26 1305 emg_sampleNow = false; // Enable signal sampling in sampleSignals()
Jellehierck 66:17be4d8e2c26 1306 emg_calibrateNow = false; // Disable calibration functionality in sampleSignals()
Jellehierck 66:17be4d8e2c26 1307 emg_cal_done = false;
Jellehierck 66:17be4d8e2c26 1308 motor_cal_done = false;
Jellehierck 37:806c7c8381a7 1309 }
Jellehierck 37:806c7c8381a7 1310 }
Jellehierck 23:8a0a0b959af1 1311 /*
Jellehierck 37:806c7c8381a7 1312 ------------------------------ GLOBAL STATE MACHINE ------------------------------
Jellehierck 23:8a0a0b959af1 1313 */
Jellehierck 37:806c7c8381a7 1314 void global_state_machine()
Jellehierck 23:8a0a0b959af1 1315 {
Jellehierck 37:806c7c8381a7 1316 switch(global_curr_state) {
Jellehierck 37:806c7c8381a7 1317 case global_failure:
Jellehierck 37:806c7c8381a7 1318 do_global_failure();
Jellehierck 23:8a0a0b959af1 1319 break;
Jellehierck 37:806c7c8381a7 1320 case global_wait:
Jellehierck 37:806c7c8381a7 1321 do_global_wait();
Jellehierck 37:806c7c8381a7 1322 break;
Jellehierck 38:8b597ab8344f 1323 case global_emg_cal:
Jellehierck 38:8b597ab8344f 1324 do_global_emg_cal();
Jellehierck 23:8a0a0b959af1 1325 break;
Jellehierck 38:8b597ab8344f 1326 case global_motor_cal:
Jellehierck 38:8b597ab8344f 1327 do_global_motor_cal();
Jellehierck 23:8a0a0b959af1 1328 break;
Jellehierck 37:806c7c8381a7 1329 case global_operation:
Jellehierck 37:806c7c8381a7 1330 do_global_operation();
Jellehierck 37:806c7c8381a7 1331 break;
Jellehierck 37:806c7c8381a7 1332 case global_demo:
Jellehierck 37:806c7c8381a7 1333 do_global_demo();
Jellehierck 23:8a0a0b959af1 1334 break;
Jellehierck 23:8a0a0b959af1 1335 }
Jellehierck 23:8a0a0b959af1 1336 }
Jellehierck 23:8a0a0b959af1 1337
Jellehierck 38:8b597ab8344f 1338 /*
Jellehierck 54:a14acf0d1683 1339 ------------------------------ OTHER MAIN LOOP FUNCTIONS ------------------------------
Jellehierck 38:8b597ab8344f 1340 */
Jellehierck 38:8b597ab8344f 1341 void sampleSignals()
Jellehierck 38:8b597ab8344f 1342 {
Jellehierck 38:8b597ab8344f 1343 if (emg_sampleNow == true) { // This ticker only samples if the sample flag is true, to prevent unnecessary computations
Jellehierck 38:8b597ab8344f 1344 // Read EMG inputs
Jellehierck 38:8b597ab8344f 1345 emg1 = emg1_in.read();
Jellehierck 38:8b597ab8344f 1346 emg2 = emg2_in.read();
Jellehierck 38:8b597ab8344f 1347 emg3 = emg3_in.read();
Jellehierck 45:d09040915cfe 1348 emg4 = emg4_in.read();
Jellehierck 38:8b597ab8344f 1349
Jellehierck 38:8b597ab8344f 1350 double emg1_n = bqc1_notch.step( emg1 ); // Filter notch
Jellehierck 38:8b597ab8344f 1351 double emg1_hp = bqc1_high.step( emg1_n ); // Filter highpass
Jellehierck 38:8b597ab8344f 1352 double emg1_rectify = fabs( emg1_hp ); // Rectify
Jellehierck 38:8b597ab8344f 1353 emg1_env = bqc1_low.step( emg1_rectify ); // Filter lowpass (completes envelope)
Jellehierck 38:8b597ab8344f 1354
Jellehierck 38:8b597ab8344f 1355 double emg2_n = bqc2_notch.step( emg2 ); // Filter notch
Jellehierck 38:8b597ab8344f 1356 double emg2_hp = bqc2_high.step( emg2_n ); // Filter highpass
Jellehierck 38:8b597ab8344f 1357 double emg2_rectify = fabs( emg2_hp ); // Rectify
Jellehierck 38:8b597ab8344f 1358 emg2_env = bqc2_low.step( emg2_rectify ); // Filter lowpass (completes envelope)
Jellehierck 38:8b597ab8344f 1359
Jellehierck 38:8b597ab8344f 1360 double emg3_n = bqc3_notch.step( emg3 ); // Filter notch
Jellehierck 38:8b597ab8344f 1361 double emg3_hp = bqc3_high.step( emg3_n ); // Filter highpass
Jellehierck 38:8b597ab8344f 1362 double emg3_rectify = fabs( emg3_hp ); // Rectify
Jellehierck 38:8b597ab8344f 1363 emg3_env = bqc3_low.step( emg3_rectify ); // Filter lowpass (completes envelope)
Jellehierck 51:7fe2659a1fcb 1364
Jellehierck 45:d09040915cfe 1365 double emg4_n = bqc4_notch.step( emg4 ); // Filter notch
Jellehierck 45:d09040915cfe 1366 double emg4_hp = bqc4_high.step( emg4_n ); // Filter highpass
Jellehierck 45:d09040915cfe 1367 double emg4_rectify = fabs( emg4_hp ); // Rectify
Jellehierck 45:d09040915cfe 1368 emg4_env = bqc4_low.step( emg4_rectify ); // Filter lowpass (completes envelope)
Jellehierck 38:8b597ab8344f 1369
Jellehierck 38:8b597ab8344f 1370 if (emg_calibrateNow == true) { // Only add values to EMG vectors if calibration flag is true
Jellehierck 38:8b597ab8344f 1371 emg1_cal.push_back(emg1_env); // Add values to calibration vector
Jellehierck 38:8b597ab8344f 1372 emg2_cal.push_back(emg2_env); // Add values to calibration vector
Jellehierck 38:8b597ab8344f 1373 emg3_cal.push_back(emg3_env); // Add values to calibration vector
Jellehierck 45:d09040915cfe 1374 emg4_cal.push_back(emg4_env); // Add values to calibration vector
Jellehierck 38:8b597ab8344f 1375 }
Jellehierck 38:8b597ab8344f 1376 }
Jellehierck 40:c6dffb676350 1377
Jellehierck 40:c6dffb676350 1378 counts1af = encoder1.getPulses();
Jellehierck 40:c6dffb676350 1379 deltaCounts1 = counts1af - countsPrev1;
Jellehierck 40:c6dffb676350 1380 countsPrev1 = counts1af;
Jellehierck 40:c6dffb676350 1381
Jellehierck 40:c6dffb676350 1382 counts2af = encoder2.getPulses();
Jellehierck 40:c6dffb676350 1383 deltaCounts2 = counts2af - countsPrev2;
Jellehierck 40:c6dffb676350 1384 countsPrev2 = counts2af;
Jellehierck 38:8b597ab8344f 1385 }
Jellehierck 37:806c7c8381a7 1386
Jellehierck 54:a14acf0d1683 1387 void changeservo()
Jellehierck 54:a14acf0d1683 1388 {
Jellehierck 54:a14acf0d1683 1389 servo_button1 = extButton1.read();
Jellehierck 54:a14acf0d1683 1390 if (servo_button1 == 1) {
Jellehierck 54:a14acf0d1683 1391 myservo.SetPosition(2000);
Jellehierck 54:a14acf0d1683 1392 } else {
Jellehierck 54:a14acf0d1683 1393 myservo.SetPosition(1000);
Jellehierck 54:a14acf0d1683 1394 }
Jellehierck 54:a14acf0d1683 1395 }
Jellehierck 54:a14acf0d1683 1396
Jellehierck 37:806c7c8381a7 1397 /*
Jellehierck 37:806c7c8381a7 1398 ------------------------------ GLOBAL PROGRAM LOOP ------------------------------
Jellehierck 37:806c7c8381a7 1399 */
Jellehierck 25:a1be4cf2ab0b 1400 void tickGlobalFunc()
Jellehierck 25:a1be4cf2ab0b 1401 {
Jellehierck 38:8b597ab8344f 1402 sampleSignals();
Jellehierck 37:806c7c8381a7 1403 global_state_machine();
BasB 68:2879967ebb25 1404 changeservo(); // Servo angle can be adjusted during every state
Jellehierck 25:a1be4cf2ab0b 1405 }
Jellehierck 25:a1be4cf2ab0b 1406
Jellehierck 37:806c7c8381a7 1407 /*
Jellehierck 37:806c7c8381a7 1408 ------------------------------ MAIN FUNCTION ------------------------------
Jellehierck 37:806c7c8381a7 1409 */
Jellehierck 39:f9042483b921 1410 int main()
Jellehierck 23:8a0a0b959af1 1411 {
Jellehierck 23:8a0a0b959af1 1412 pc.baud(115200); // MODSERIAL rate
Jellehierck 23:8a0a0b959af1 1413 pc.printf("Starting\r\n");
Jellehierck 23:8a0a0b959af1 1414
Jellehierck 51:7fe2659a1fcb 1415 global_curr_state = global_wait; // Start off in global wait state
Jellehierck 34:13fac02ef324 1416 tickGlobal.attach( &tickGlobalFunc, Ts ); // Start global ticker
Jellehierck 51:7fe2659a1fcb 1417 tickLED.attach ( &disp_led_color, 0.25f ); // Start LED ticker
Jellehierck 8:ea3de43c9e8b 1418
Jellehierck 51:7fe2659a1fcb 1419 // ---------- Enable Servo ----------
Jellehierck 51:7fe2659a1fcb 1420 myservo.Enable(1000, 20000);
BasB 48:31676da4be7a 1421
Jellehierck 38:8b597ab8344f 1422 // ---------- Attach filters ----------
Jellehierck 38:8b597ab8344f 1423 bqc1_notch.add( &bq1_notch );
Jellehierck 38:8b597ab8344f 1424 bqc1_high.add( &bq1_H1 ).add( &bq1_H2 );
Jellehierck 38:8b597ab8344f 1425 bqc1_low.add( &bq1_L1 ).add( &bq1_L2 );
Jellehierck 38:8b597ab8344f 1426
Jellehierck 38:8b597ab8344f 1427 bqc2_notch.add( &bq2_notch );
Jellehierck 38:8b597ab8344f 1428 bqc2_high.add( &bq2_H1 ).add( &bq2_H2 );
Jellehierck 38:8b597ab8344f 1429 bqc2_low.add( &bq2_L1 ).add( &bq2_L2 );
Jellehierck 38:8b597ab8344f 1430
Jellehierck 38:8b597ab8344f 1431 bqc3_notch.add( &bq3_notch );
Jellehierck 38:8b597ab8344f 1432 bqc3_high.add( &bq3_H1 ).add( &bq3_H2 );
Jellehierck 38:8b597ab8344f 1433 bqc3_low.add( &bq3_L1 ).add( &bq3_L2 );
Jellehierck 51:7fe2659a1fcb 1434
Jellehierck 45:d09040915cfe 1435 bqc4_notch.add( &bq4_notch );
Jellehierck 45:d09040915cfe 1436 bqc4_high.add( &bq4_H1 ).add( &bq4_H2 );
Jellehierck 45:d09040915cfe 1437 bqc4_low.add( &bq4_L1 ).add( &bq4_L2 );
Jellehierck 38:8b597ab8344f 1438
Jellehierck 38:8b597ab8344f 1439 // ---------- Attach buttons ----------
Jellehierck 37:806c7c8381a7 1440 button1.fall( &button1Press );
Jellehierck 37:806c7c8381a7 1441 button2.fall( &button2Press );
Jellehierck 37:806c7c8381a7 1442 switch2.fall( &switch2Press );
Jellehierck 37:806c7c8381a7 1443 switch3.fall( &switch3Press );
Jellehierck 51:7fe2659a1fcb 1444 extButton1.mode( PullUp ); // To make sure the servo works
Jellehierck 42:2937ad8f1032 1445
Jellehierck 40:c6dffb676350 1446 // ---------- Attach PWM ----------
Jellehierck 40:c6dffb676350 1447 motor1Power.period(PWM_period);
Jellehierck 40:c6dffb676350 1448 motor2Power.period(PWM_period);
Jellehierck 40:c6dffb676350 1449
Jellehierck 38:8b597ab8344f 1450 // ---------- Turn OFF LEDs ----------
Jellehierck 51:7fe2659a1fcb 1451 set_led_color('o');
Jellehierck 37:806c7c8381a7 1452
Jellehierck 23:8a0a0b959af1 1453 while(true) {
Jellehierck 45:d09040915cfe 1454 pc.printf("Global state: %i EMG state: %i Motor state: %i Operation state: %i Demo state: %i\r\n", global_curr_state, emg_curr_state, motor_curr_state, operation_curr_state, demo_curr_state);
Jellehierck 53:35282a3e0cad 1455 pc.printf("Potmeter 1: %f Potmeter 2: %f\r\n", potmeter1.read(), potmeter2.read());
Jellehierck 45:d09040915cfe 1456 //pc.printf("Xe: %f Ye: %f\r\n",xe,ye);
Jellehierck 30:bac3b60d6283 1457 wait(0.5f);
Jellehierck 23:8a0a0b959af1 1458 }
Jellehierck 23:8a0a0b959af1 1459 }