State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Committer:
hermanindeput
Date:
Thu Nov 01 08:50:20 2018 +0000
Revision:
35:38a5af0afee8
Parent:
21:d541303a2ea6
Child:
36:691ea4660f29
EMG_calibration toevoegen;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MAHCSnijders 0:7d25c2ade6c5 1 #include "mbed.h"
MAHCSnijders 0:7d25c2ade6c5 2
brass_phoenix 20:af1a6cd7469b 3 #include "constants.h"
brass_phoenix 1:cfa5abca6d43 4 #include "Button.h"
brass_phoenix 3:4b19b6cf6cc7 5 #include "Screen.h"
brass_phoenix 8:9090ab7c19a8 6 #include "motor.h"
brass_phoenix 16:9c5ef6fe6780 7 #include "motor_calibration.h"
hermanindeput 35:38a5af0afee8 8 #include "EMG_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 3:4b19b6cf6cc7 124 void do_state_calib_bicep1()
brass_phoenix 3:4b19b6cf6cc7 125 {
brass_phoenix 2:141cfcafe72b 126 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 127 last_state = current_state;
brass_phoenix 2:141cfcafe72b 128 // State just changed to this one.
brass_phoenix 6:bfc6e68774f5 129 screen.clear_display();
brass_phoenix 5:2632dfc8454c 130 screen.display_state_name("EMG 1 calibration");
brass_phoenix 2:141cfcafe72b 131 }
brass_phoenix 9:27d00b64076e 132
brass_phoenix 9:27d00b64076e 133 if (ud_button.has_just_been_pressed()) {
brass_phoenix 9:27d00b64076e 134 current_state = calib_bicep2;
brass_phoenix 9:27d00b64076e 135 }
brass_phoenix 2:141cfcafe72b 136 }
brass_phoenix 2:141cfcafe72b 137
brass_phoenix 3:4b19b6cf6cc7 138 void do_state_calib_bicep2()
brass_phoenix 3:4b19b6cf6cc7 139 {
brass_phoenix 2:141cfcafe72b 140 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 141 last_state = current_state;
brass_phoenix 2:141cfcafe72b 142 // State just changed to this one.
brass_phoenix 6:bfc6e68774f5 143 screen.clear_display();
brass_phoenix 5:2632dfc8454c 144 screen.display_state_name("EMG 2 calibration");
brass_phoenix 2:141cfcafe72b 145 }
brass_phoenix 9:27d00b64076e 146
brass_phoenix 9:27d00b64076e 147 if (ud_button.has_just_been_pressed()) {
brass_phoenix 9:27d00b64076e 148 current_state = homing;
brass_phoenix 9:27d00b64076e 149 }
brass_phoenix 2:141cfcafe72b 150 }
brass_phoenix 2:141cfcafe72b 151
brass_phoenix 3:4b19b6cf6cc7 152 void do_state_homing()
brass_phoenix 3:4b19b6cf6cc7 153 {
brass_phoenix 20:af1a6cd7469b 154 double main_home = PI * 0.5;
brass_phoenix 20:af1a6cd7469b 155 double sec_home = 0;
brass_phoenix 20:af1a6cd7469b 156
brass_phoenix 2:141cfcafe72b 157 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 158 last_state = current_state;
brass_phoenix 2:141cfcafe72b 159 // State just changed to this one.
brass_phoenix 6:bfc6e68774f5 160 screen.clear_display();
brass_phoenix 5:2632dfc8454c 161 screen.display_state_name("Homing");
brass_phoenix 20:af1a6cd7469b 162
brass_phoenix 20:af1a6cd7469b 163 main_motor.set_target_angle(main_home);
brass_phoenix 20:af1a6cd7469b 164 sec_motor.set_target_angle(sec_home);
brass_phoenix 2:141cfcafe72b 165 }
brass_phoenix 9:27d00b64076e 166
brass_phoenix 9:27d00b64076e 167 if (ud_button.has_just_been_pressed()) {
brass_phoenix 19:53b9729fbab5 168 current_state = calib_bicep1;
brass_phoenix 9:27d00b64076e 169 }
brass_phoenix 2:141cfcafe72b 170 }
brass_phoenix 2:141cfcafe72b 171
brass_phoenix 3:4b19b6cf6cc7 172 void do_state_operation()
brass_phoenix 3:4b19b6cf6cc7 173 {
brass_phoenix 21:d541303a2ea6 174 static bool debug_forward_kinematics;
brass_phoenix 21:d541303a2ea6 175
brass_phoenix 2:141cfcafe72b 176 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 177 last_state = current_state;
brass_phoenix 2:141cfcafe72b 178 // State just changed to this one.
brass_phoenix 6:bfc6e68774f5 179 screen.clear_display();
brass_phoenix 5:2632dfc8454c 180 screen.display_state_name("Normal operation");
brass_phoenix 15:f65b4566193e 181
brass_phoenix 15:f65b4566193e 182 control_goes_up = true;
brass_phoenix 15:f65b4566193e 183 control_goes_right = true;
brass_phoenix 15:f65b4566193e 184
brass_phoenix 15:f65b4566193e 185 screen.display_up_down_arrow(control_goes_up);
brass_phoenix 15:f65b4566193e 186 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 21:d541303a2ea6 187
brass_phoenix 21:d541303a2ea6 188 debug_forward_kinematics = true;
brass_phoenix 2:141cfcafe72b 189 }
brass_phoenix 9:27d00b64076e 190
brass_phoenix 21:d541303a2ea6 191 if (debug_forward_kinematics) {
brass_phoenix 21:d541303a2ea6 192 // Using potmeters for debugging purposes;
brass_phoenix 21:d541303a2ea6 193 double main_angle = ((potmeter1.read() * 2) - 1) * PI;
brass_phoenix 21:d541303a2ea6 194 double sec_angle = ((potmeter2.read() * 2) - 1) * PI;
brass_phoenix 21:d541303a2ea6 195
brass_phoenix 21:d541303a2ea6 196 double e_x = 0.0;
brass_phoenix 21:d541303a2ea6 197 double e_y = 0.0;
brass_phoenix 21:d541303a2ea6 198
brass_phoenix 21:d541303a2ea6 199 forward_kinematics(main_angle, sec_angle, e_x, e_y);
brass_phoenix 21:d541303a2ea6 200
brass_phoenix 21:d541303a2ea6 201 screen.get_screen_handle()->setTextCursor(0, 0);
brass_phoenix 21:d541303a2ea6 202 screen.get_screen_handle()->printf("M_a: %.6f \n", main_angle);
brass_phoenix 21:d541303a2ea6 203 screen.get_screen_handle()->printf("S_a: %.6f \n", sec_angle);
brass_phoenix 21:d541303a2ea6 204 screen.get_screen_handle()->printf("X: %.6f \n", e_x);
brass_phoenix 21:d541303a2ea6 205 screen.get_screen_handle()->printf("Y: %.6f ", e_y);
brass_phoenix 21:d541303a2ea6 206 screen.display();
brass_phoenix 20:af1a6cd7469b 207
brass_phoenix 21:d541303a2ea6 208 } else {
brass_phoenix 21:d541303a2ea6 209 // Using potmeters for debugging purposes;
brass_phoenix 21:d541303a2ea6 210 double e_x = potmeter1.read();
brass_phoenix 21:d541303a2ea6 211 double e_y = potmeter2.read();
brass_phoenix 21:d541303a2ea6 212
brass_phoenix 21:d541303a2ea6 213 double main_angle = 0.0;
brass_phoenix 21:d541303a2ea6 214 double sec_angle = 0.0;
brass_phoenix 21:d541303a2ea6 215
brass_phoenix 21:d541303a2ea6 216 inverse_kinematics(e_x, e_y, main_angle, sec_angle);
brass_phoenix 21:d541303a2ea6 217
brass_phoenix 21:d541303a2ea6 218 screen.get_screen_handle()->setTextCursor(0, 0);
brass_phoenix 21:d541303a2ea6 219 screen.get_screen_handle()->printf("E_x: %.6f \n", e_x);
brass_phoenix 21:d541303a2ea6 220 screen.get_screen_handle()->printf("E_y: %.6f \n", e_y);
brass_phoenix 21:d541303a2ea6 221 screen.get_screen_handle()->printf("M_a: %.6f \n", main_angle);
brass_phoenix 21:d541303a2ea6 222 screen.get_screen_handle()->printf("S_a: %.6f ", sec_angle);
brass_phoenix 21:d541303a2ea6 223 screen.display();
brass_phoenix 21:d541303a2ea6 224 }
brass_phoenix 20:af1a6cd7469b 225
brass_phoenix 21:d541303a2ea6 226 if (lr_button.has_just_been_pressed()) {
brass_phoenix 21:d541303a2ea6 227 debug_forward_kinematics = !debug_forward_kinematics;
brass_phoenix 21:d541303a2ea6 228 }
brass_phoenix 20:af1a6cd7469b 229
brass_phoenix 19:53b9729fbab5 230 /*
brass_phoenix 14:b97e7a41ec23 231 double main_target = ((potmeter1.read() * 2) - 1) * PI;
brass_phoenix 13:88967c004446 232 main_motor.set_target_angle(main_target);
brass_phoenix 14:b97e7a41ec23 233 double sec_target = ((potmeter2.read() * 2) - 1) * PI;
brass_phoenix 13:88967c004446 234 sec_motor.set_target_angle(sec_target);
brass_phoenix 13:88967c004446 235
brass_phoenix 9:27d00b64076e 236 if (ud_button.has_just_been_pressed()) {
brass_phoenix 12:0c10396d0615 237 control_goes_up = !control_goes_up;
brass_phoenix 15:f65b4566193e 238 screen.display_up_down_arrow(control_goes_up);
brass_phoenix 12:0c10396d0615 239 }
brass_phoenix 12:0c10396d0615 240 if (lr_button.has_just_been_pressed()) {
brass_phoenix 12:0c10396d0615 241 control_goes_right = !control_goes_right;
brass_phoenix 15:f65b4566193e 242 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 9:27d00b64076e 243 }
brass_phoenix 19:53b9729fbab5 244 */
brass_phoenix 2:141cfcafe72b 245 }
brass_phoenix 2:141cfcafe72b 246
brass_phoenix 3:4b19b6cf6cc7 247 void do_state_failure()
brass_phoenix 3:4b19b6cf6cc7 248 {
brass_phoenix 2:141cfcafe72b 249 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 250 last_state = current_state;
brass_phoenix 2:141cfcafe72b 251 // State just changed.
brass_phoenix 2:141cfcafe72b 252 // Update the display.
brass_phoenix 2:141cfcafe72b 253 led_red = 0;
brass_phoenix 2:141cfcafe72b 254 led_green = 1;
brass_phoenix 6:bfc6e68774f5 255 screen.clear_display();
brass_phoenix 11:d980e0e581db 256 screen.display_state_name("STOP");
brass_phoenix 2:141cfcafe72b 257 }
brass_phoenix 3:4b19b6cf6cc7 258
brass_phoenix 2:141cfcafe72b 259 // Stop the motors!
brass_phoenix 12:0c10396d0615 260 main_motor.stop();
brass_phoenix 12:0c10396d0615 261 sec_motor.stop();
brass_phoenix 2:141cfcafe72b 262 }
brass_phoenix 2:141cfcafe72b 263
brass_phoenix 2:141cfcafe72b 264
brass_phoenix 1:cfa5abca6d43 265 void main_loop()
brass_phoenix 1:cfa5abca6d43 266 {
brass_phoenix 1:cfa5abca6d43 267 ud_button.update();
brass_phoenix 1:cfa5abca6d43 268 lr_button.update();
brass_phoenix 1:cfa5abca6d43 269 p_button.update();
brass_phoenix 3:4b19b6cf6cc7 270
brass_phoenix 1:cfa5abca6d43 271 switch (current_state) {
brass_phoenix 1:cfa5abca6d43 272 case waiting:
brass_phoenix 2:141cfcafe72b 273 do_state_waiting();
brass_phoenix 1:cfa5abca6d43 274 break;
brass_phoenix 1:cfa5abca6d43 275 case calib_motor:
brass_phoenix 2:141cfcafe72b 276 do_state_calib_motor();
brass_phoenix 1:cfa5abca6d43 277 break;
brass_phoenix 1:cfa5abca6d43 278 case calib_bicep1:
brass_phoenix 2:141cfcafe72b 279 do_state_calib_bicep1();
brass_phoenix 1:cfa5abca6d43 280 break;
brass_phoenix 1:cfa5abca6d43 281 case calib_bicep2:
brass_phoenix 2:141cfcafe72b 282 do_state_calib_bicep2();
brass_phoenix 1:cfa5abca6d43 283 break;
brass_phoenix 1:cfa5abca6d43 284 case homing:
brass_phoenix 2:141cfcafe72b 285 do_state_homing();
brass_phoenix 1:cfa5abca6d43 286 break;
brass_phoenix 1:cfa5abca6d43 287 case operation:
brass_phoenix 2:141cfcafe72b 288 do_state_operation();
brass_phoenix 1:cfa5abca6d43 289 break;
brass_phoenix 1:cfa5abca6d43 290 case failure:
brass_phoenix 2:141cfcafe72b 291 do_state_failure();
brass_phoenix 1:cfa5abca6d43 292 break;
brass_phoenix 1:cfa5abca6d43 293 }
brass_phoenix 3:4b19b6cf6cc7 294
brass_phoenix 2:141cfcafe72b 295 // Check if the panic button was pressed.
brass_phoenix 2:141cfcafe72b 296 // Doesn't matter in which state we are, we need to go to failure.
brass_phoenix 2:141cfcafe72b 297 if (p_button.is_pressed()) {
brass_phoenix 2:141cfcafe72b 298 current_state = failure;
brass_phoenix 3:4b19b6cf6cc7 299 }
brass_phoenix 1:cfa5abca6d43 300 }
MAHCSnijders 0:7d25c2ade6c5 301
brass_phoenix 7:e7f808875bc4 302 void poll_buttons() {
brass_phoenix 7:e7f808875bc4 303 // We need to poll the pins periodically.
brass_phoenix 7:e7f808875bc4 304 // Normally one would use rise and fall interrupts, so this wouldn't be
brass_phoenix 7:e7f808875bc4 305 // needed. But the buttons we use generate so much chatter that
brass_phoenix 7:e7f808875bc4 306 // sometimes a rising or a falling edge doesn't get registered.
brass_phoenix 7:e7f808875bc4 307 // With all the confusion that accompanies it.
brass_phoenix 7:e7f808875bc4 308 ud_button.poll_pin();
brass_phoenix 7:e7f808875bc4 309 lr_button.poll_pin();
brass_phoenix 7:e7f808875bc4 310 p_button.poll_pin();
brass_phoenix 7:e7f808875bc4 311 }
brass_phoenix 7:e7f808875bc4 312
brass_phoenix 1:cfa5abca6d43 313 int main()
brass_phoenix 1:cfa5abca6d43 314 {
brass_phoenix 1:cfa5abca6d43 315 led_red = 1;
brass_phoenix 9:27d00b64076e 316 led_green = 1;
brass_phoenix 9:27d00b64076e 317 led_blue = 1;
brass_phoenix 8:9090ab7c19a8 318
brass_phoenix 10:b165ccd11afd 319 screen.clear_display();
brass_phoenix 10:b165ccd11afd 320
brass_phoenix 12:0c10396d0615 321 main_motor.set_pid_k_values(Kp, Ki, Kd);
brass_phoenix 12:0c10396d0615 322 sec_motor.set_pid_k_values(Kp, Ki, Kd);
brass_phoenix 12:0c10396d0615 323
brass_phoenix 12:0c10396d0615 324 // One of the motors is reversed in the electronics.
brass_phoenix 12:0c10396d0615 325 // This is fixed in the motor controll board, so we have to account
brass_phoenix 12:0c10396d0615 326 // for it in software.
brass_phoenix 19:53b9729fbab5 327 main_motor.set_extra_reduction_ratio(-main_gear_ratio);
brass_phoenix 19:53b9729fbab5 328 sec_motor.set_extra_reduction_ratio(sec_gear_ratio);
brass_phoenix 19:53b9729fbab5 329
brass_phoenix 19:53b9729fbab5 330 // Set the maximum pwm fraction for both motors.
brass_phoenix 19:53b9729fbab5 331 main_motor.set_max_pwm_fraction(0.5);
brass_phoenix 19:53b9729fbab5 332 sec_motor.set_max_pwm_fraction(0.5);
brass_phoenix 12:0c10396d0615 333
brass_phoenix 8:9090ab7c19a8 334 // Start the motor controller at the desired frequency.
brass_phoenix 12:0c10396d0615 335 main_motor.start(pid_period);
brass_phoenix 12:0c10396d0615 336 sec_motor.start(pid_period);
brass_phoenix 3:4b19b6cf6cc7 337
brass_phoenix 2:141cfcafe72b 338 // Start in the waiting state.
brass_phoenix 1:cfa5abca6d43 339 current_state = waiting;
brass_phoenix 4:5a44ab7e94b3 340 // Pretend we come from the operation state.
brass_phoenix 4:5a44ab7e94b3 341 // So that the waiting state knows it just got started.
brass_phoenix 4:5a44ab7e94b3 342 last_state = operation;
brass_phoenix 7:e7f808875bc4 343
brass_phoenix 7:e7f808875bc4 344 // Start the button polling ticker.
brass_phoenix 7:e7f808875bc4 345 button_ticker.attach(&poll_buttons, button_poll_interval);
brass_phoenix 3:4b19b6cf6cc7 346
brass_phoenix 1:cfa5abca6d43 347 while (true) {
brass_phoenix 1:cfa5abca6d43 348 main_loop();
brass_phoenix 9:27d00b64076e 349
brass_phoenix 9:27d00b64076e 350 // Button debugging.
brass_phoenix 9:27d00b64076e 351 if (ud_button.has_just_been_pressed() || lr_button.has_just_been_pressed() || p_button.has_just_been_pressed()) {
brass_phoenix 9:27d00b64076e 352 led_blue = 0;
brass_phoenix 9:27d00b64076e 353 } else {
brass_phoenix 9:27d00b64076e 354 led_blue = 1;
brass_phoenix 9:27d00b64076e 355 }
brass_phoenix 9:27d00b64076e 356
brass_phoenix 1:cfa5abca6d43 357 wait(main_loop_wait_time);
brass_phoenix 1:cfa5abca6d43 358 }
brass_phoenix 1:cfa5abca6d43 359 }