State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Committer:
brass_phoenix
Date:
Thu Nov 01 08:40:10 2018 +0000
Revision:
24:e1092f95c82b
Parent:
23:fb681b074a92
Child:
25:cc81f2120eda
+ Homing state is back to angles.; + Down arrows on screen should be correct now.

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 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 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 23:fb681b074a92 154 const double home_x = 0.6524; // Meters.
brass_phoenix 23:fb681b074a92 155 const double home_y = 0.3409;
brass_phoenix 23:fb681b074a92 156
brass_phoenix 24:e1092f95c82b 157 double main_home = PI *2;
brass_phoenix 24:e1092f95c82b 158 double sec_home = 0;
brass_phoenix 20:af1a6cd7469b 159
brass_phoenix 2:141cfcafe72b 160 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 161 last_state = current_state;
brass_phoenix 2:141cfcafe72b 162 // State just changed to this one.
brass_phoenix 6:bfc6e68774f5 163 screen.clear_display();
brass_phoenix 5:2632dfc8454c 164 screen.display_state_name("Homing");
brass_phoenix 20:af1a6cd7469b 165
brass_phoenix 24:e1092f95c82b 166 //inverse_kinematics(home_x, home_y, main_home, sec_home);
brass_phoenix 23:fb681b074a92 167
brass_phoenix 20:af1a6cd7469b 168 main_motor.set_target_angle(main_home);
brass_phoenix 20:af1a6cd7469b 169 sec_motor.set_target_angle(sec_home);
brass_phoenix 2:141cfcafe72b 170 }
brass_phoenix 9:27d00b64076e 171
brass_phoenix 9:27d00b64076e 172 if (ud_button.has_just_been_pressed()) {
brass_phoenix 19:53b9729fbab5 173 current_state = calib_bicep1;
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 9:27d00b64076e 241 if (ud_button.has_just_been_pressed()) {
brass_phoenix 12:0c10396d0615 242 control_goes_up = !control_goes_up;
brass_phoenix 15:f65b4566193e 243 screen.display_up_down_arrow(control_goes_up);
brass_phoenix 12:0c10396d0615 244 }
brass_phoenix 12:0c10396d0615 245 if (lr_button.has_just_been_pressed()) {
brass_phoenix 12:0c10396d0615 246 control_goes_right = !control_goes_right;
brass_phoenix 15:f65b4566193e 247 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 9:27d00b64076e 248 }
brass_phoenix 19:53b9729fbab5 249 */
brass_phoenix 2:141cfcafe72b 250 }
brass_phoenix 2:141cfcafe72b 251
brass_phoenix 3:4b19b6cf6cc7 252 void do_state_failure()
brass_phoenix 3:4b19b6cf6cc7 253 {
brass_phoenix 2:141cfcafe72b 254 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 255 last_state = current_state;
brass_phoenix 2:141cfcafe72b 256 // State just changed.
brass_phoenix 2:141cfcafe72b 257 // Update the display.
brass_phoenix 2:141cfcafe72b 258 led_red = 0;
brass_phoenix 2:141cfcafe72b 259 led_green = 1;
brass_phoenix 6:bfc6e68774f5 260 screen.clear_display();
brass_phoenix 11:d980e0e581db 261 screen.display_state_name("STOP");
brass_phoenix 2:141cfcafe72b 262 }
brass_phoenix 3:4b19b6cf6cc7 263
brass_phoenix 2:141cfcafe72b 264 // Stop the motors!
brass_phoenix 12:0c10396d0615 265 main_motor.stop();
brass_phoenix 12:0c10396d0615 266 sec_motor.stop();
brass_phoenix 2:141cfcafe72b 267 }
brass_phoenix 2:141cfcafe72b 268
brass_phoenix 2:141cfcafe72b 269
brass_phoenix 1:cfa5abca6d43 270 void main_loop()
brass_phoenix 1:cfa5abca6d43 271 {
brass_phoenix 1:cfa5abca6d43 272 ud_button.update();
brass_phoenix 1:cfa5abca6d43 273 lr_button.update();
brass_phoenix 1:cfa5abca6d43 274 p_button.update();
brass_phoenix 3:4b19b6cf6cc7 275
brass_phoenix 1:cfa5abca6d43 276 switch (current_state) {
brass_phoenix 1:cfa5abca6d43 277 case waiting:
brass_phoenix 2:141cfcafe72b 278 do_state_waiting();
brass_phoenix 1:cfa5abca6d43 279 break;
brass_phoenix 1:cfa5abca6d43 280 case calib_motor:
brass_phoenix 2:141cfcafe72b 281 do_state_calib_motor();
brass_phoenix 1:cfa5abca6d43 282 break;
brass_phoenix 1:cfa5abca6d43 283 case calib_bicep1:
brass_phoenix 2:141cfcafe72b 284 do_state_calib_bicep1();
brass_phoenix 1:cfa5abca6d43 285 break;
brass_phoenix 1:cfa5abca6d43 286 case calib_bicep2:
brass_phoenix 2:141cfcafe72b 287 do_state_calib_bicep2();
brass_phoenix 1:cfa5abca6d43 288 break;
brass_phoenix 1:cfa5abca6d43 289 case homing:
brass_phoenix 2:141cfcafe72b 290 do_state_homing();
brass_phoenix 1:cfa5abca6d43 291 break;
brass_phoenix 1:cfa5abca6d43 292 case operation:
brass_phoenix 2:141cfcafe72b 293 do_state_operation();
brass_phoenix 1:cfa5abca6d43 294 break;
brass_phoenix 1:cfa5abca6d43 295 case failure:
brass_phoenix 2:141cfcafe72b 296 do_state_failure();
brass_phoenix 1:cfa5abca6d43 297 break;
brass_phoenix 1:cfa5abca6d43 298 }
brass_phoenix 3:4b19b6cf6cc7 299
brass_phoenix 2:141cfcafe72b 300 // Check if the panic button was pressed.
brass_phoenix 2:141cfcafe72b 301 // Doesn't matter in which state we are, we need to go to failure.
brass_phoenix 2:141cfcafe72b 302 if (p_button.is_pressed()) {
brass_phoenix 2:141cfcafe72b 303 current_state = failure;
brass_phoenix 3:4b19b6cf6cc7 304 }
brass_phoenix 1:cfa5abca6d43 305 }
MAHCSnijders 0:7d25c2ade6c5 306
brass_phoenix 7:e7f808875bc4 307 void poll_buttons() {
brass_phoenix 7:e7f808875bc4 308 // We need to poll the pins periodically.
brass_phoenix 7:e7f808875bc4 309 // Normally one would use rise and fall interrupts, so this wouldn't be
brass_phoenix 7:e7f808875bc4 310 // needed. But the buttons we use generate so much chatter that
brass_phoenix 7:e7f808875bc4 311 // sometimes a rising or a falling edge doesn't get registered.
brass_phoenix 7:e7f808875bc4 312 // With all the confusion that accompanies it.
brass_phoenix 7:e7f808875bc4 313 ud_button.poll_pin();
brass_phoenix 7:e7f808875bc4 314 lr_button.poll_pin();
brass_phoenix 7:e7f808875bc4 315 p_button.poll_pin();
brass_phoenix 7:e7f808875bc4 316 }
brass_phoenix 7:e7f808875bc4 317
brass_phoenix 1:cfa5abca6d43 318 int main()
brass_phoenix 1:cfa5abca6d43 319 {
brass_phoenix 1:cfa5abca6d43 320 led_red = 1;
brass_phoenix 9:27d00b64076e 321 led_green = 1;
brass_phoenix 9:27d00b64076e 322 led_blue = 1;
brass_phoenix 8:9090ab7c19a8 323
brass_phoenix 10:b165ccd11afd 324 screen.clear_display();
brass_phoenix 10:b165ccd11afd 325
brass_phoenix 12:0c10396d0615 326 main_motor.set_pid_k_values(Kp, Ki, Kd);
brass_phoenix 12:0c10396d0615 327 sec_motor.set_pid_k_values(Kp, Ki, Kd);
brass_phoenix 12:0c10396d0615 328
brass_phoenix 12:0c10396d0615 329 // One of the motors is reversed in the electronics.
brass_phoenix 12:0c10396d0615 330 // This is fixed in the motor controll board, so we have to account
brass_phoenix 12:0c10396d0615 331 // for it in software.
brass_phoenix 19:53b9729fbab5 332 main_motor.set_extra_reduction_ratio(-main_gear_ratio);
brass_phoenix 19:53b9729fbab5 333 sec_motor.set_extra_reduction_ratio(sec_gear_ratio);
brass_phoenix 19:53b9729fbab5 334
brass_phoenix 19:53b9729fbab5 335 // Set the maximum pwm fraction for both motors.
brass_phoenix 19:53b9729fbab5 336 main_motor.set_max_pwm_fraction(0.5);
brass_phoenix 19:53b9729fbab5 337 sec_motor.set_max_pwm_fraction(0.5);
brass_phoenix 12:0c10396d0615 338
brass_phoenix 8:9090ab7c19a8 339 // Start the motor controller at the desired frequency.
brass_phoenix 12:0c10396d0615 340 main_motor.start(pid_period);
brass_phoenix 12:0c10396d0615 341 sec_motor.start(pid_period);
brass_phoenix 3:4b19b6cf6cc7 342
brass_phoenix 2:141cfcafe72b 343 // Start in the waiting state.
brass_phoenix 1:cfa5abca6d43 344 current_state = waiting;
brass_phoenix 4:5a44ab7e94b3 345 // Pretend we come from the operation state.
brass_phoenix 4:5a44ab7e94b3 346 // So that the waiting state knows it just got started.
brass_phoenix 4:5a44ab7e94b3 347 last_state = operation;
brass_phoenix 7:e7f808875bc4 348
brass_phoenix 7:e7f808875bc4 349 // Start the button polling ticker.
brass_phoenix 7:e7f808875bc4 350 button_ticker.attach(&poll_buttons, button_poll_interval);
brass_phoenix 3:4b19b6cf6cc7 351
brass_phoenix 1:cfa5abca6d43 352 while (true) {
brass_phoenix 1:cfa5abca6d43 353 main_loop();
brass_phoenix 9:27d00b64076e 354
brass_phoenix 9:27d00b64076e 355 // Button debugging.
brass_phoenix 9:27d00b64076e 356 if (ud_button.has_just_been_pressed() || lr_button.has_just_been_pressed() || p_button.has_just_been_pressed()) {
brass_phoenix 9:27d00b64076e 357 led_blue = 0;
brass_phoenix 9:27d00b64076e 358 } else {
brass_phoenix 9:27d00b64076e 359 led_blue = 1;
brass_phoenix 9:27d00b64076e 360 }
brass_phoenix 9:27d00b64076e 361
brass_phoenix 1:cfa5abca6d43 362 wait(main_loop_wait_time);
brass_phoenix 1:cfa5abca6d43 363 }
brass_phoenix 1:cfa5abca6d43 364 }