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