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