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