State machine
Dependencies: mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter
main.cpp@26:a8f4a117cc0d, 2018-11-01 (annotated)
- Committer:
- brass_phoenix
- Date:
- Thu Nov 01 10:12:35 2018 +0000
- Revision:
- 26:a8f4a117cc0d
- Parent:
- 25:cc81f2120eda
- Child:
- 28:25917b26022c
+ Inverse and forward kinematics works.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MAHCSnijders | 0:7d25c2ade6c5 | 1 | #include "mbed.h" |
MAHCSnijders | 0:7d25c2ade6c5 | 2 | |
brass_phoenix | 20:af1a6cd7469b | 3 | #include "constants.h" |
brass_phoenix | 20:af1a6cd7469b | 4 | |
brass_phoenix | 1:cfa5abca6d43 | 5 | #include "Button.h" |
brass_phoenix | 3:4b19b6cf6cc7 | 6 | #include "Screen.h" |
brass_phoenix | 8:9090ab7c19a8 | 7 | #include "motor.h" |
brass_phoenix | 16:9c5ef6fe6780 | 8 | #include "motor_calibration.h" |
brass_phoenix | 20:af1a6cd7469b | 9 | #include "forward_kinematics.h" |
brass_phoenix | 21:d541303a2ea6 | 10 | #include "inverse_kinematics.h" |
MAHCSnijders | 0:7d25c2ade6c5 | 11 | |
brass_phoenix | 1:cfa5abca6d43 | 12 | |
brass_phoenix | 1:cfa5abca6d43 | 13 | enum States {waiting, calib_motor, calib_bicep1, calib_bicep2, homing, operation, failure}; // The possible states of the state machine |
MAHCSnijders | 0:7d25c2ade6c5 | 14 | |
MAHCSnijders | 0:7d25c2ade6c5 | 15 | // Global variables |
brass_phoenix | 1:cfa5abca6d43 | 16 | |
brass_phoenix | 8:9090ab7c19a8 | 17 | |
brass_phoenix | 12:0c10396d0615 | 18 | Motor main_motor(D6, D7, D13, D12); |
brass_phoenix | 12:0c10396d0615 | 19 | Motor sec_motor(D5, D4, D10, D11); |
brass_phoenix | 12:0c10396d0615 | 20 | |
brass_phoenix | 1:cfa5abca6d43 | 21 | |
brass_phoenix | 13:88967c004446 | 22 | AnalogIn potmeter1(A5); // Analoge input van potmeter 1 -> Motor 1 |
brass_phoenix | 13:88967c004446 | 23 | AnalogIn potmeter2(A4); // Analoge input van potmeter 2 -> Motor 2 |
brass_phoenix | 13:88967c004446 | 24 | |
brass_phoenix | 13:88967c004446 | 25 | |
brass_phoenix | 1:cfa5abca6d43 | 26 | States current_state; // Defining the state we are currently in |
brass_phoenix | 2:141cfcafe72b | 27 | States last_state; // To detect state changes. |
MAHCSnijders | 0:7d25c2ade6c5 | 28 | Ticker loop_ticker; // Ticker for the loop function |
brass_phoenix | 1:cfa5abca6d43 | 29 | |
brass_phoenix | 1:cfa5abca6d43 | 30 | // Order of buttons: up_down, left_right, panic |
brass_phoenix | 1:cfa5abca6d43 | 31 | // D2, D3, D8 |
brass_phoenix | 7:e7f808875bc4 | 32 | Button ud_button(D2); |
brass_phoenix | 2:141cfcafe72b | 33 | Button lr_button(D3); |
brass_phoenix | 7:e7f808875bc4 | 34 | Button p_button(D8); |
brass_phoenix | 7:e7f808875bc4 | 35 | |
brass_phoenix | 7:e7f808875bc4 | 36 | Ticker button_ticker; |
MAHCSnijders | 0:7d25c2ade6c5 | 37 | |
brass_phoenix | 1:cfa5abca6d43 | 38 | DigitalOut led_red(LED_RED); |
brass_phoenix | 1:cfa5abca6d43 | 39 | DigitalOut led_green(LED_GREEN); |
brass_phoenix | 9:27d00b64076e | 40 | DigitalOut led_blue(LED_BLUE); |
brass_phoenix | 1:cfa5abca6d43 | 41 | |
brass_phoenix | 3:4b19b6cf6cc7 | 42 | // The last arguent is the reset pin. |
brass_phoenix | 3:4b19b6cf6cc7 | 43 | // The screen doesn't use it, but the library requires it |
brass_phoenix | 3:4b19b6cf6cc7 | 44 | // So pick a pin we don't use. |
brass_phoenix | 3:4b19b6cf6cc7 | 45 | Screen screen(D14, D15, D9); |
brass_phoenix | 2:141cfcafe72b | 46 | |
brass_phoenix | 12:0c10396d0615 | 47 | // Which direction the emg will control the arm. |
brass_phoenix | 12:0c10396d0615 | 48 | // Up or down. |
brass_phoenix | 12:0c10396d0615 | 49 | // Left or right. |
brass_phoenix | 12:0c10396d0615 | 50 | bool control_goes_up = false; |
brass_phoenix | 12:0c10396d0615 | 51 | bool control_goes_right = false; |
brass_phoenix | 12:0c10396d0615 | 52 | |
brass_phoenix | 3:4b19b6cf6cc7 | 53 | |
brass_phoenix | 3:4b19b6cf6cc7 | 54 | void do_state_waiting() |
brass_phoenix | 3:4b19b6cf6cc7 | 55 | { |
brass_phoenix | 4:5a44ab7e94b3 | 56 | if(last_state != current_state) { |
brass_phoenix | 4:5a44ab7e94b3 | 57 | last_state = current_state; |
brass_phoenix | 4:5a44ab7e94b3 | 58 | // State just changed to this one. |
brass_phoenix | 4:5a44ab7e94b3 | 59 | |
brass_phoenix | 4:5a44ab7e94b3 | 60 | led_green = 1; |
brass_phoenix | 6:bfc6e68774f5 | 61 | screen.clear_display(); |
brass_phoenix | 4:5a44ab7e94b3 | 62 | screen.display_state_name("Waiting"); |
brass_phoenix | 6:bfc6e68774f5 | 63 | screen.get_screen_handle()->printf(" Press to start "); |
brass_phoenix | 6:bfc6e68774f5 | 64 | screen.get_screen_handle()->printf(" | "); |
brass_phoenix | 6:bfc6e68774f5 | 65 | screen.get_screen_handle()->printf(" V "); |
brass_phoenix | 6:bfc6e68774f5 | 66 | screen.display(); |
brass_phoenix | 4:5a44ab7e94b3 | 67 | } |
brass_phoenix | 4:5a44ab7e94b3 | 68 | |
brass_phoenix | 9:27d00b64076e | 69 | if (ud_button.has_just_been_pressed()) { |
brass_phoenix | 2:141cfcafe72b | 70 | current_state = calib_motor; |
brass_phoenix | 2:141cfcafe72b | 71 | } |
brass_phoenix | 20:af1a6cd7469b | 72 | |
brass_phoenix | 20:af1a6cd7469b | 73 | // TODO: |
brass_phoenix | 20:af1a6cd7469b | 74 | // THIS OPTION IS ONLY HERE FOR DEBUGGING PURPOSES. |
brass_phoenix | 20:af1a6cd7469b | 75 | // REMOVE WHEN THE DEMO STATE IS IMPLEMENTED. |
brass_phoenix | 20:af1a6cd7469b | 76 | if (lr_button.has_just_been_pressed()) { |
brass_phoenix | 20:af1a6cd7469b | 77 | current_state = operation; |
brass_phoenix | 20:af1a6cd7469b | 78 | } |
brass_phoenix | 2:141cfcafe72b | 79 | } |
brass_phoenix | 2:141cfcafe72b | 80 | |
brass_phoenix | 3:4b19b6cf6cc7 | 81 | void do_state_calib_motor() |
brass_phoenix | 3:4b19b6cf6cc7 | 82 | { |
brass_phoenix | 16:9c5ef6fe6780 | 83 | static double main_last_angle; |
brass_phoenix | 16:9c5ef6fe6780 | 84 | static double sec_last_angle; |
brass_phoenix | 16:9c5ef6fe6780 | 85 | static int main_iterations_not_moving; |
brass_phoenix | 16:9c5ef6fe6780 | 86 | static int sec_iterations_not_moving; |
brass_phoenix | 16:9c5ef6fe6780 | 87 | static bool main_is_calibrated; |
brass_phoenix | 16:9c5ef6fe6780 | 88 | static bool sec_is_calibrated; |
brass_phoenix | 16:9c5ef6fe6780 | 89 | |
brass_phoenix | 2:141cfcafe72b | 90 | if(last_state != current_state) { |
brass_phoenix | 2:141cfcafe72b | 91 | last_state = current_state; |
brass_phoenix | 2:141cfcafe72b | 92 | // State just changed to this one. |
brass_phoenix | 3:4b19b6cf6cc7 | 93 | |
brass_phoenix | 2:141cfcafe72b | 94 | led_green = 0; |
brass_phoenix | 6:bfc6e68774f5 | 95 | screen.clear_display(); |
brass_phoenix | 5:2632dfc8454c | 96 | screen.display_state_name("Motor calibration"); |
brass_phoenix | 16:9c5ef6fe6780 | 97 | |
brass_phoenix | 16:9c5ef6fe6780 | 98 | main_last_angle = -10; |
brass_phoenix | 16:9c5ef6fe6780 | 99 | sec_last_angle = -10; |
brass_phoenix | 16:9c5ef6fe6780 | 100 | main_iterations_not_moving = 0; |
brass_phoenix | 16:9c5ef6fe6780 | 101 | sec_iterations_not_moving = 0; |
brass_phoenix | 16:9c5ef6fe6780 | 102 | main_is_calibrated = false; |
brass_phoenix | 16:9c5ef6fe6780 | 103 | sec_is_calibrated = false; |
brass_phoenix | 2:141cfcafe72b | 104 | } |
brass_phoenix | 9:27d00b64076e | 105 | |
brass_phoenix | 16:9c5ef6fe6780 | 106 | if (!main_is_calibrated) { |
brass_phoenix | 16:9c5ef6fe6780 | 107 | main_is_calibrated = calibrate_motor(main_motor, main_last_angle, main_iterations_not_moving); |
brass_phoenix | 16:9c5ef6fe6780 | 108 | if (main_is_calibrated) { |
brass_phoenix | 16:9c5ef6fe6780 | 109 | main_motor.define_current_angle_as_x_radians(0.785398); // 45 degrees. |
brass_phoenix | 16:9c5ef6fe6780 | 110 | } |
brass_phoenix | 16:9c5ef6fe6780 | 111 | } |
brass_phoenix | 16:9c5ef6fe6780 | 112 | if (!sec_is_calibrated) { |
brass_phoenix | 16:9c5ef6fe6780 | 113 | sec_is_calibrated = calibrate_motor(sec_motor, sec_last_angle, sec_iterations_not_moving); |
brass_phoenix | 16:9c5ef6fe6780 | 114 | if (sec_is_calibrated) { |
brass_phoenix | 16:9c5ef6fe6780 | 115 | sec_motor.define_current_angle_as_x_radians(-0.733038); // -42 degrees. |
brass_phoenix | 16:9c5ef6fe6780 | 116 | } |
brass_phoenix | 16:9c5ef6fe6780 | 117 | } |
brass_phoenix | 16:9c5ef6fe6780 | 118 | |
brass_phoenix | 16:9c5ef6fe6780 | 119 | if (main_is_calibrated && sec_is_calibrated) { |
brass_phoenix | 19:53b9729fbab5 | 120 | current_state = homing; |
brass_phoenix | 9:27d00b64076e | 121 | } |
brass_phoenix | 2:141cfcafe72b | 122 | } |
brass_phoenix | 2:141cfcafe72b | 123 | |
brass_phoenix | 25:cc81f2120eda | 124 | void do_state_homing() |
brass_phoenix | 25:cc81f2120eda | 125 | { |
brass_phoenix | 25:cc81f2120eda | 126 | const double home_x = 0.6524; // Meters. |
brass_phoenix | 25:cc81f2120eda | 127 | const double home_y = 0.3409; |
brass_phoenix | 25:cc81f2120eda | 128 | |
brass_phoenix | 26:a8f4a117cc0d | 129 | double main_home; |
brass_phoenix | 26:a8f4a117cc0d | 130 | double sec_home; |
brass_phoenix | 25:cc81f2120eda | 131 | |
brass_phoenix | 25:cc81f2120eda | 132 | if(last_state != current_state) { |
brass_phoenix | 25:cc81f2120eda | 133 | last_state = current_state; |
brass_phoenix | 25:cc81f2120eda | 134 | // State just changed to this one. |
brass_phoenix | 25:cc81f2120eda | 135 | screen.clear_display(); |
brass_phoenix | 25:cc81f2120eda | 136 | screen.display_state_name("Homing"); |
brass_phoenix | 25:cc81f2120eda | 137 | |
brass_phoenix | 26:a8f4a117cc0d | 138 | inverse_kinematics(home_x, home_y, main_home, sec_home); |
brass_phoenix | 25:cc81f2120eda | 139 | |
brass_phoenix | 25:cc81f2120eda | 140 | main_motor.set_target_angle(main_home); |
brass_phoenix | 25:cc81f2120eda | 141 | sec_motor.set_target_angle(sec_home); |
brass_phoenix | 25:cc81f2120eda | 142 | } |
brass_phoenix | 25:cc81f2120eda | 143 | |
brass_phoenix | 25:cc81f2120eda | 144 | if (ud_button.has_just_been_pressed()) { |
brass_phoenix | 25:cc81f2120eda | 145 | current_state = calib_bicep1; |
brass_phoenix | 25:cc81f2120eda | 146 | } |
brass_phoenix | 25:cc81f2120eda | 147 | } |
brass_phoenix | 25:cc81f2120eda | 148 | |
brass_phoenix | 3:4b19b6cf6cc7 | 149 | void do_state_calib_bicep1() |
brass_phoenix | 3:4b19b6cf6cc7 | 150 | { |
brass_phoenix | 2:141cfcafe72b | 151 | if(last_state != current_state) { |
brass_phoenix | 2:141cfcafe72b | 152 | last_state = current_state; |
brass_phoenix | 2:141cfcafe72b | 153 | // State just changed to this one. |
brass_phoenix | 6:bfc6e68774f5 | 154 | screen.clear_display(); |
brass_phoenix | 5:2632dfc8454c | 155 | screen.display_state_name("EMG 1 calibration"); |
brass_phoenix | 2:141cfcafe72b | 156 | } |
brass_phoenix | 9:27d00b64076e | 157 | |
brass_phoenix | 9:27d00b64076e | 158 | if (ud_button.has_just_been_pressed()) { |
brass_phoenix | 9:27d00b64076e | 159 | current_state = calib_bicep2; |
brass_phoenix | 9:27d00b64076e | 160 | } |
brass_phoenix | 2:141cfcafe72b | 161 | } |
brass_phoenix | 2:141cfcafe72b | 162 | |
brass_phoenix | 3:4b19b6cf6cc7 | 163 | void do_state_calib_bicep2() |
brass_phoenix | 3:4b19b6cf6cc7 | 164 | { |
brass_phoenix | 2:141cfcafe72b | 165 | if(last_state != current_state) { |
brass_phoenix | 2:141cfcafe72b | 166 | last_state = current_state; |
brass_phoenix | 2:141cfcafe72b | 167 | // State just changed to this one. |
brass_phoenix | 6:bfc6e68774f5 | 168 | screen.clear_display(); |
brass_phoenix | 5:2632dfc8454c | 169 | screen.display_state_name("EMG 2 calibration"); |
brass_phoenix | 2:141cfcafe72b | 170 | } |
brass_phoenix | 9:27d00b64076e | 171 | |
brass_phoenix | 9:27d00b64076e | 172 | if (ud_button.has_just_been_pressed()) { |
brass_phoenix | 9:27d00b64076e | 173 | current_state = homing; |
brass_phoenix | 9:27d00b64076e | 174 | } |
brass_phoenix | 2:141cfcafe72b | 175 | } |
brass_phoenix | 2:141cfcafe72b | 176 | |
brass_phoenix | 3:4b19b6cf6cc7 | 177 | void do_state_operation() |
brass_phoenix | 3:4b19b6cf6cc7 | 178 | { |
brass_phoenix | 21:d541303a2ea6 | 179 | static bool debug_forward_kinematics; |
brass_phoenix | 21:d541303a2ea6 | 180 | |
brass_phoenix | 2:141cfcafe72b | 181 | if(last_state != current_state) { |
brass_phoenix | 2:141cfcafe72b | 182 | last_state = current_state; |
brass_phoenix | 2:141cfcafe72b | 183 | // State just changed to this one. |
brass_phoenix | 6:bfc6e68774f5 | 184 | screen.clear_display(); |
brass_phoenix | 5:2632dfc8454c | 185 | screen.display_state_name("Normal operation"); |
brass_phoenix | 15:f65b4566193e | 186 | |
brass_phoenix | 15:f65b4566193e | 187 | control_goes_up = true; |
brass_phoenix | 15:f65b4566193e | 188 | control_goes_right = true; |
brass_phoenix | 15:f65b4566193e | 189 | |
brass_phoenix | 15:f65b4566193e | 190 | screen.display_up_down_arrow(control_goes_up); |
brass_phoenix | 15:f65b4566193e | 191 | screen.display_left_right_arrow(control_goes_right); |
brass_phoenix | 21:d541303a2ea6 | 192 | |
brass_phoenix | 21:d541303a2ea6 | 193 | debug_forward_kinematics = true; |
brass_phoenix | 2:141cfcafe72b | 194 | } |
brass_phoenix | 9:27d00b64076e | 195 | |
brass_phoenix | 21:d541303a2ea6 | 196 | if (debug_forward_kinematics) { |
brass_phoenix | 21:d541303a2ea6 | 197 | // Using potmeters for debugging purposes; |
brass_phoenix | 21:d541303a2ea6 | 198 | double main_angle = ((potmeter1.read() * 2) - 1) * PI; |
brass_phoenix | 21:d541303a2ea6 | 199 | double sec_angle = ((potmeter2.read() * 2) - 1) * PI; |
brass_phoenix | 21:d541303a2ea6 | 200 | |
brass_phoenix | 21:d541303a2ea6 | 201 | double e_x = 0.0; |
brass_phoenix | 21:d541303a2ea6 | 202 | double e_y = 0.0; |
brass_phoenix | 21:d541303a2ea6 | 203 | |
brass_phoenix | 21:d541303a2ea6 | 204 | forward_kinematics(main_angle, sec_angle, e_x, e_y); |
brass_phoenix | 21:d541303a2ea6 | 205 | |
brass_phoenix | 21:d541303a2ea6 | 206 | screen.get_screen_handle()->setTextCursor(0, 0); |
brass_phoenix | 21:d541303a2ea6 | 207 | screen.get_screen_handle()->printf("M_a: %.6f \n", main_angle); |
brass_phoenix | 21:d541303a2ea6 | 208 | screen.get_screen_handle()->printf("S_a: %.6f \n", sec_angle); |
brass_phoenix | 21:d541303a2ea6 | 209 | screen.get_screen_handle()->printf("X: %.6f \n", e_x); |
brass_phoenix | 21:d541303a2ea6 | 210 | screen.get_screen_handle()->printf("Y: %.6f ", e_y); |
brass_phoenix | 21:d541303a2ea6 | 211 | screen.display(); |
brass_phoenix | 20:af1a6cd7469b | 212 | |
brass_phoenix | 21:d541303a2ea6 | 213 | } else { |
brass_phoenix | 21:d541303a2ea6 | 214 | // Using potmeters for debugging purposes; |
brass_phoenix | 21:d541303a2ea6 | 215 | double e_x = potmeter1.read(); |
brass_phoenix | 21:d541303a2ea6 | 216 | double e_y = potmeter2.read(); |
brass_phoenix | 21:d541303a2ea6 | 217 | |
brass_phoenix | 21:d541303a2ea6 | 218 | double main_angle = 0.0; |
brass_phoenix | 21:d541303a2ea6 | 219 | double sec_angle = 0.0; |
brass_phoenix | 21:d541303a2ea6 | 220 | |
brass_phoenix | 21:d541303a2ea6 | 221 | inverse_kinematics(e_x, e_y, main_angle, sec_angle); |
brass_phoenix | 21:d541303a2ea6 | 222 | |
brass_phoenix | 21:d541303a2ea6 | 223 | screen.get_screen_handle()->setTextCursor(0, 0); |
brass_phoenix | 21:d541303a2ea6 | 224 | screen.get_screen_handle()->printf("E_x: %.6f \n", e_x); |
brass_phoenix | 21:d541303a2ea6 | 225 | screen.get_screen_handle()->printf("E_y: %.6f \n", e_y); |
brass_phoenix | 21:d541303a2ea6 | 226 | screen.get_screen_handle()->printf("M_a: %.6f \n", main_angle); |
brass_phoenix | 21:d541303a2ea6 | 227 | screen.get_screen_handle()->printf("S_a: %.6f ", sec_angle); |
brass_phoenix | 21:d541303a2ea6 | 228 | screen.display(); |
brass_phoenix | 21:d541303a2ea6 | 229 | } |
brass_phoenix | 20:af1a6cd7469b | 230 | |
brass_phoenix | 21:d541303a2ea6 | 231 | if (lr_button.has_just_been_pressed()) { |
brass_phoenix | 21:d541303a2ea6 | 232 | debug_forward_kinematics = !debug_forward_kinematics; |
brass_phoenix | 21:d541303a2ea6 | 233 | } |
brass_phoenix | 20:af1a6cd7469b | 234 | |
brass_phoenix | 19:53b9729fbab5 | 235 | /* |
brass_phoenix | 14:b97e7a41ec23 | 236 | double main_target = ((potmeter1.read() * 2) - 1) * PI; |
brass_phoenix | 13:88967c004446 | 237 | main_motor.set_target_angle(main_target); |
brass_phoenix | 14:b97e7a41ec23 | 238 | double sec_target = ((potmeter2.read() * 2) - 1) * PI; |
brass_phoenix | 13:88967c004446 | 239 | sec_motor.set_target_angle(sec_target); |
brass_phoenix | 13:88967c004446 | 240 | |
brass_phoenix | 25:cc81f2120eda | 241 | if (lr_button.has_just_been_pressed()) { |
brass_phoenix | 25:cc81f2120eda | 242 | control_goes_right = !control_goes_right; |
brass_phoenix | 25:cc81f2120eda | 243 | screen.display_left_right_arrow(control_goes_right); |
brass_phoenix | 25:cc81f2120eda | 244 | } |
brass_phoenix | 25:cc81f2120eda | 245 | */ |
brass_phoenix | 25:cc81f2120eda | 246 | |
brass_phoenix | 9:27d00b64076e | 247 | if (ud_button.has_just_been_pressed()) { |
brass_phoenix | 12:0c10396d0615 | 248 | control_goes_up = !control_goes_up; |
brass_phoenix | 25:cc81f2120eda | 249 | control_goes_right = !control_goes_right; |
brass_phoenix | 15:f65b4566193e | 250 | screen.display_up_down_arrow(control_goes_up); |
brass_phoenix | 15:f65b4566193e | 251 | screen.display_left_right_arrow(control_goes_right); |
brass_phoenix | 9:27d00b64076e | 252 | } |
brass_phoenix | 2:141cfcafe72b | 253 | } |
brass_phoenix | 2:141cfcafe72b | 254 | |
brass_phoenix | 3:4b19b6cf6cc7 | 255 | void do_state_failure() |
brass_phoenix | 3:4b19b6cf6cc7 | 256 | { |
brass_phoenix | 2:141cfcafe72b | 257 | if(last_state != current_state) { |
brass_phoenix | 2:141cfcafe72b | 258 | last_state = current_state; |
brass_phoenix | 2:141cfcafe72b | 259 | // State just changed. |
brass_phoenix | 2:141cfcafe72b | 260 | // Update the display. |
brass_phoenix | 2:141cfcafe72b | 261 | led_red = 0; |
brass_phoenix | 2:141cfcafe72b | 262 | led_green = 1; |
brass_phoenix | 6:bfc6e68774f5 | 263 | screen.clear_display(); |
brass_phoenix | 11:d980e0e581db | 264 | screen.display_state_name("STOP"); |
brass_phoenix | 2:141cfcafe72b | 265 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 266 | |
brass_phoenix | 2:141cfcafe72b | 267 | // Stop the motors! |
brass_phoenix | 12:0c10396d0615 | 268 | main_motor.stop(); |
brass_phoenix | 12:0c10396d0615 | 269 | sec_motor.stop(); |
brass_phoenix | 2:141cfcafe72b | 270 | } |
brass_phoenix | 2:141cfcafe72b | 271 | |
brass_phoenix | 2:141cfcafe72b | 272 | |
brass_phoenix | 1:cfa5abca6d43 | 273 | void main_loop() |
brass_phoenix | 1:cfa5abca6d43 | 274 | { |
brass_phoenix | 1:cfa5abca6d43 | 275 | ud_button.update(); |
brass_phoenix | 1:cfa5abca6d43 | 276 | lr_button.update(); |
brass_phoenix | 1:cfa5abca6d43 | 277 | p_button.update(); |
brass_phoenix | 3:4b19b6cf6cc7 | 278 | |
brass_phoenix | 1:cfa5abca6d43 | 279 | switch (current_state) { |
brass_phoenix | 1:cfa5abca6d43 | 280 | case waiting: |
brass_phoenix | 2:141cfcafe72b | 281 | do_state_waiting(); |
brass_phoenix | 1:cfa5abca6d43 | 282 | break; |
brass_phoenix | 1:cfa5abca6d43 | 283 | case calib_motor: |
brass_phoenix | 2:141cfcafe72b | 284 | do_state_calib_motor(); |
brass_phoenix | 1:cfa5abca6d43 | 285 | break; |
brass_phoenix | 1:cfa5abca6d43 | 286 | case calib_bicep1: |
brass_phoenix | 2:141cfcafe72b | 287 | do_state_calib_bicep1(); |
brass_phoenix | 1:cfa5abca6d43 | 288 | break; |
brass_phoenix | 1:cfa5abca6d43 | 289 | case calib_bicep2: |
brass_phoenix | 2:141cfcafe72b | 290 | do_state_calib_bicep2(); |
brass_phoenix | 1:cfa5abca6d43 | 291 | break; |
brass_phoenix | 1:cfa5abca6d43 | 292 | case homing: |
brass_phoenix | 2:141cfcafe72b | 293 | do_state_homing(); |
brass_phoenix | 1:cfa5abca6d43 | 294 | break; |
brass_phoenix | 1:cfa5abca6d43 | 295 | case operation: |
brass_phoenix | 2:141cfcafe72b | 296 | do_state_operation(); |
brass_phoenix | 1:cfa5abca6d43 | 297 | break; |
brass_phoenix | 1:cfa5abca6d43 | 298 | case failure: |
brass_phoenix | 2:141cfcafe72b | 299 | do_state_failure(); |
brass_phoenix | 1:cfa5abca6d43 | 300 | break; |
brass_phoenix | 1:cfa5abca6d43 | 301 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 302 | |
brass_phoenix | 2:141cfcafe72b | 303 | // Check if the panic button was pressed. |
brass_phoenix | 2:141cfcafe72b | 304 | // Doesn't matter in which state we are, we need to go to failure. |
brass_phoenix | 2:141cfcafe72b | 305 | if (p_button.is_pressed()) { |
brass_phoenix | 2:141cfcafe72b | 306 | current_state = failure; |
brass_phoenix | 3:4b19b6cf6cc7 | 307 | } |
brass_phoenix | 1:cfa5abca6d43 | 308 | } |
MAHCSnijders | 0:7d25c2ade6c5 | 309 | |
brass_phoenix | 7:e7f808875bc4 | 310 | void poll_buttons() { |
brass_phoenix | 7:e7f808875bc4 | 311 | // We need to poll the pins periodically. |
brass_phoenix | 7:e7f808875bc4 | 312 | // Normally one would use rise and fall interrupts, so this wouldn't be |
brass_phoenix | 7:e7f808875bc4 | 313 | // needed. But the buttons we use generate so much chatter that |
brass_phoenix | 7:e7f808875bc4 | 314 | // sometimes a rising or a falling edge doesn't get registered. |
brass_phoenix | 7:e7f808875bc4 | 315 | // With all the confusion that accompanies it. |
brass_phoenix | 7:e7f808875bc4 | 316 | ud_button.poll_pin(); |
brass_phoenix | 7:e7f808875bc4 | 317 | lr_button.poll_pin(); |
brass_phoenix | 7:e7f808875bc4 | 318 | p_button.poll_pin(); |
brass_phoenix | 7:e7f808875bc4 | 319 | } |
brass_phoenix | 7:e7f808875bc4 | 320 | |
brass_phoenix | 1:cfa5abca6d43 | 321 | int main() |
brass_phoenix | 1:cfa5abca6d43 | 322 | { |
brass_phoenix | 1:cfa5abca6d43 | 323 | led_red = 1; |
brass_phoenix | 9:27d00b64076e | 324 | led_green = 1; |
brass_phoenix | 9:27d00b64076e | 325 | led_blue = 1; |
brass_phoenix | 8:9090ab7c19a8 | 326 | |
brass_phoenix | 10:b165ccd11afd | 327 | screen.clear_display(); |
brass_phoenix | 10:b165ccd11afd | 328 | |
brass_phoenix | 12:0c10396d0615 | 329 | main_motor.set_pid_k_values(Kp, Ki, Kd); |
brass_phoenix | 12:0c10396d0615 | 330 | sec_motor.set_pid_k_values(Kp, Ki, Kd); |
brass_phoenix | 12:0c10396d0615 | 331 | |
brass_phoenix | 12:0c10396d0615 | 332 | // One of the motors is reversed in the electronics. |
brass_phoenix | 12:0c10396d0615 | 333 | // This is fixed in the motor controll board, so we have to account |
brass_phoenix | 12:0c10396d0615 | 334 | // for it in software. |
brass_phoenix | 19:53b9729fbab5 | 335 | main_motor.set_extra_reduction_ratio(-main_gear_ratio); |
brass_phoenix | 19:53b9729fbab5 | 336 | sec_motor.set_extra_reduction_ratio(sec_gear_ratio); |
brass_phoenix | 19:53b9729fbab5 | 337 | |
brass_phoenix | 19:53b9729fbab5 | 338 | // Set the maximum pwm fraction for both motors. |
brass_phoenix | 19:53b9729fbab5 | 339 | main_motor.set_max_pwm_fraction(0.5); |
brass_phoenix | 19:53b9729fbab5 | 340 | sec_motor.set_max_pwm_fraction(0.5); |
brass_phoenix | 12:0c10396d0615 | 341 | |
brass_phoenix | 8:9090ab7c19a8 | 342 | // Start the motor controller at the desired frequency. |
brass_phoenix | 12:0c10396d0615 | 343 | main_motor.start(pid_period); |
brass_phoenix | 12:0c10396d0615 | 344 | sec_motor.start(pid_period); |
brass_phoenix | 3:4b19b6cf6cc7 | 345 | |
brass_phoenix | 2:141cfcafe72b | 346 | // Start in the waiting state. |
brass_phoenix | 1:cfa5abca6d43 | 347 | current_state = waiting; |
brass_phoenix | 4:5a44ab7e94b3 | 348 | // Pretend we come from the operation state. |
brass_phoenix | 4:5a44ab7e94b3 | 349 | // So that the waiting state knows it just got started. |
brass_phoenix | 4:5a44ab7e94b3 | 350 | last_state = operation; |
brass_phoenix | 7:e7f808875bc4 | 351 | |
brass_phoenix | 7:e7f808875bc4 | 352 | // Start the button polling ticker. |
brass_phoenix | 7:e7f808875bc4 | 353 | button_ticker.attach(&poll_buttons, button_poll_interval); |
brass_phoenix | 3:4b19b6cf6cc7 | 354 | |
brass_phoenix | 1:cfa5abca6d43 | 355 | while (true) { |
brass_phoenix | 1:cfa5abca6d43 | 356 | main_loop(); |
brass_phoenix | 9:27d00b64076e | 357 | |
brass_phoenix | 9:27d00b64076e | 358 | // Button debugging. |
brass_phoenix | 9:27d00b64076e | 359 | if (ud_button.has_just_been_pressed() || lr_button.has_just_been_pressed() || p_button.has_just_been_pressed()) { |
brass_phoenix | 9:27d00b64076e | 360 | led_blue = 0; |
brass_phoenix | 9:27d00b64076e | 361 | } else { |
brass_phoenix | 9:27d00b64076e | 362 | led_blue = 1; |
brass_phoenix | 9:27d00b64076e | 363 | } |
brass_phoenix | 9:27d00b64076e | 364 | |
brass_phoenix | 1:cfa5abca6d43 | 365 | wait(main_loop_wait_time); |
brass_phoenix | 1:cfa5abca6d43 | 366 | } |
brass_phoenix | 1:cfa5abca6d43 | 367 | } |