State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Committer:
brass_phoenix
Date:
Thu Nov 01 15:41:34 2018 +0000
Revision:
39:f119ca6fc821
Parent:
34:ae62ebf4d494
Child:
40:7e8d0632757c
+ EMG possibly calibratable.

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 39:f119ca6fc821 8 #include "EMGFilter.h"
brass_phoenix 16:9c5ef6fe6780 9 #include "motor_calibration.h"
brass_phoenix 20:af1a6cd7469b 10 #include "forward_kinematics.h"
brass_phoenix 21:d541303a2ea6 11 #include "inverse_kinematics.h"
brass_phoenix 32:b63b5837bcb1 12 #include "end_effector_control.h"
brass_phoenix 39:f119ca6fc821 13 #include "EMG_calibration.h"
MAHCSnijders 0:7d25c2ade6c5 14
brass_phoenix 1:cfa5abca6d43 15
brass_phoenix 32:b63b5837bcb1 16 enum States {waiting, calib_motor, calib_bicep1, calib_bicep2, homing, operation, demo, failure}; // The possible states of the state machine
MAHCSnijders 0:7d25c2ade6c5 17
brass_phoenix 34:ae62ebf4d494 18
brass_phoenix 34:ae62ebf4d494 19
brass_phoenix 34:ae62ebf4d494 20 // Controll directions for the demo controller.
brass_phoenix 34:ae62ebf4d494 21 enum DebugControlDirection {debug_up, debug_down, debug_left, debug_right};
brass_phoenix 1:cfa5abca6d43 22
brass_phoenix 8:9090ab7c19a8 23
brass_phoenix 12:0c10396d0615 24 Motor main_motor(D6, D7, D13, D12);
brass_phoenix 12:0c10396d0615 25 Motor sec_motor(D5, D4, D10, D11);
brass_phoenix 12:0c10396d0615 26
brass_phoenix 1:cfa5abca6d43 27
brass_phoenix 13:88967c004446 28 AnalogIn potmeter1(A5); // Analoge input van potmeter 1 -> Motor 1
brass_phoenix 13:88967c004446 29 AnalogIn potmeter2(A4); // Analoge input van potmeter 2 -> Motor 2
brass_phoenix 13:88967c004446 30
brass_phoenix 39:f119ca6fc821 31 EMGFilter emg_1(A0);
brass_phoenix 39:f119ca6fc821 32 EMGFilter emg_2(A1);
brass_phoenix 39:f119ca6fc821 33
brass_phoenix 13:88967c004446 34
brass_phoenix 1:cfa5abca6d43 35 States current_state; // Defining the state we are currently in
brass_phoenix 2:141cfcafe72b 36 States last_state; // To detect state changes.
MAHCSnijders 0:7d25c2ade6c5 37 Ticker loop_ticker; // Ticker for the loop function
brass_phoenix 1:cfa5abca6d43 38
brass_phoenix 1:cfa5abca6d43 39 // Order of buttons: up_down, left_right, panic
brass_phoenix 1:cfa5abca6d43 40 // D2, D3, D8
brass_phoenix 7:e7f808875bc4 41 Button ud_button(D2);
brass_phoenix 2:141cfcafe72b 42 Button lr_button(D3);
brass_phoenix 7:e7f808875bc4 43 Button p_button(D8);
brass_phoenix 7:e7f808875bc4 44
brass_phoenix 7:e7f808875bc4 45 Ticker button_ticker;
MAHCSnijders 0:7d25c2ade6c5 46
brass_phoenix 1:cfa5abca6d43 47 DigitalOut led_red(LED_RED);
brass_phoenix 1:cfa5abca6d43 48 DigitalOut led_green(LED_GREEN);
brass_phoenix 9:27d00b64076e 49 DigitalOut led_blue(LED_BLUE);
brass_phoenix 1:cfa5abca6d43 50
brass_phoenix 3:4b19b6cf6cc7 51 // The last arguent is the reset pin.
brass_phoenix 3:4b19b6cf6cc7 52 // The screen doesn't use it, but the library requires it
brass_phoenix 3:4b19b6cf6cc7 53 // So pick a pin we don't use.
brass_phoenix 3:4b19b6cf6cc7 54 Screen screen(D14, D15, D9);
brass_phoenix 2:141cfcafe72b 55
brass_phoenix 12:0c10396d0615 56 // Which direction the emg will control the arm.
brass_phoenix 12:0c10396d0615 57 // Up or down.
brass_phoenix 12:0c10396d0615 58 // Left or right.
brass_phoenix 12:0c10396d0615 59 bool control_goes_up = false;
brass_phoenix 12:0c10396d0615 60 bool control_goes_right = false;
brass_phoenix 12:0c10396d0615 61
brass_phoenix 3:4b19b6cf6cc7 62
brass_phoenix 3:4b19b6cf6cc7 63 void do_state_waiting()
brass_phoenix 3:4b19b6cf6cc7 64 {
brass_phoenix 4:5a44ab7e94b3 65 if(last_state != current_state) {
brass_phoenix 4:5a44ab7e94b3 66 last_state = current_state;
brass_phoenix 4:5a44ab7e94b3 67 // State just changed to this one.
brass_phoenix 4:5a44ab7e94b3 68
brass_phoenix 4:5a44ab7e94b3 69 led_green = 1;
brass_phoenix 6:bfc6e68774f5 70 screen.clear_display();
brass_phoenix 4:5a44ab7e94b3 71 screen.display_state_name("Waiting");
brass_phoenix 6:bfc6e68774f5 72 screen.get_screen_handle()->printf(" Press to start ");
brass_phoenix 6:bfc6e68774f5 73 screen.get_screen_handle()->printf(" | ");
brass_phoenix 6:bfc6e68774f5 74 screen.get_screen_handle()->printf(" V ");
brass_phoenix 6:bfc6e68774f5 75 screen.display();
brass_phoenix 4:5a44ab7e94b3 76 }
brass_phoenix 4:5a44ab7e94b3 77
brass_phoenix 9:27d00b64076e 78 if (ud_button.has_just_been_pressed()) {
brass_phoenix 2:141cfcafe72b 79 current_state = calib_motor;
brass_phoenix 2:141cfcafe72b 80 }
brass_phoenix 2:141cfcafe72b 81 }
brass_phoenix 2:141cfcafe72b 82
brass_phoenix 3:4b19b6cf6cc7 83 void do_state_calib_motor()
brass_phoenix 3:4b19b6cf6cc7 84 {
brass_phoenix 16:9c5ef6fe6780 85 static double main_last_angle;
brass_phoenix 16:9c5ef6fe6780 86 static double sec_last_angle;
brass_phoenix 16:9c5ef6fe6780 87 static int main_iterations_not_moving;
brass_phoenix 16:9c5ef6fe6780 88 static int sec_iterations_not_moving;
brass_phoenix 16:9c5ef6fe6780 89 static bool main_is_calibrated;
brass_phoenix 16:9c5ef6fe6780 90 static bool sec_is_calibrated;
brass_phoenix 16:9c5ef6fe6780 91
brass_phoenix 2:141cfcafe72b 92 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 93 last_state = current_state;
brass_phoenix 2:141cfcafe72b 94 // State just changed to this one.
brass_phoenix 3:4b19b6cf6cc7 95
brass_phoenix 28:25917b26022c 96 led_green = 1;
brass_phoenix 28:25917b26022c 97 led_blue = 1;
brass_phoenix 6:bfc6e68774f5 98 screen.clear_display();
brass_phoenix 5:2632dfc8454c 99 screen.display_state_name("Motor calibration");
brass_phoenix 16:9c5ef6fe6780 100
brass_phoenix 16:9c5ef6fe6780 101 main_last_angle = -10;
brass_phoenix 16:9c5ef6fe6780 102 sec_last_angle = -10;
brass_phoenix 16:9c5ef6fe6780 103 main_iterations_not_moving = 0;
brass_phoenix 16:9c5ef6fe6780 104 sec_iterations_not_moving = 0;
brass_phoenix 16:9c5ef6fe6780 105 main_is_calibrated = false;
brass_phoenix 16:9c5ef6fe6780 106 sec_is_calibrated = false;
brass_phoenix 2:141cfcafe72b 107 }
brass_phoenix 9:27d00b64076e 108
brass_phoenix 16:9c5ef6fe6780 109 if (!main_is_calibrated) {
brass_phoenix 33:543debddb3a9 110 led_green = 1;
brass_phoenix 16:9c5ef6fe6780 111 main_is_calibrated = calibrate_motor(main_motor, main_last_angle, main_iterations_not_moving);
brass_phoenix 16:9c5ef6fe6780 112 if (main_is_calibrated) {
brass_phoenix 33:543debddb3a9 113 main_motor.define_current_angle_as_x_radians(main_motor_calibration_angle);
brass_phoenix 33:543debddb3a9 114 //main_motor.set_target_angle(main_motor_calibration_angle - 0.2); // Give the arm some breathing space.
brass_phoenix 28:25917b26022c 115 led_green = 0;
brass_phoenix 16:9c5ef6fe6780 116 }
brass_phoenix 16:9c5ef6fe6780 117 }
brass_phoenix 16:9c5ef6fe6780 118 if (!sec_is_calibrated) {
brass_phoenix 33:543debddb3a9 119 led_blue = 1;
brass_phoenix 16:9c5ef6fe6780 120 sec_is_calibrated = calibrate_motor(sec_motor, sec_last_angle, sec_iterations_not_moving);
brass_phoenix 16:9c5ef6fe6780 121 if (sec_is_calibrated) {
brass_phoenix 33:543debddb3a9 122 sec_motor.define_current_angle_as_x_radians(sec_motor_calibration_angle); // -42 degrees.
brass_phoenix 33:543debddb3a9 123 //main_motor.set_target_angle(sec_motor_calibration_angle + 0.2); // Give the arm some breathing space.
brass_phoenix 28:25917b26022c 124 led_blue = 0;
brass_phoenix 16:9c5ef6fe6780 125 }
brass_phoenix 16:9c5ef6fe6780 126 }
brass_phoenix 16:9c5ef6fe6780 127
brass_phoenix 28:25917b26022c 128 screen.get_screen_handle()->setTextCursor(0, 8);
brass_phoenix 28:25917b26022c 129 screen.get_screen_handle()->printf("M: %i \n", main_iterations_not_moving);
brass_phoenix 28:25917b26022c 130 screen.get_screen_handle()->printf("S: %i \n", sec_iterations_not_moving);
brass_phoenix 28:25917b26022c 131 screen.display();
brass_phoenix 28:25917b26022c 132
brass_phoenix 16:9c5ef6fe6780 133 if (main_is_calibrated && sec_is_calibrated) {
brass_phoenix 19:53b9729fbab5 134 current_state = homing;
brass_phoenix 9:27d00b64076e 135 }
brass_phoenix 2:141cfcafe72b 136 }
brass_phoenix 2:141cfcafe72b 137
brass_phoenix 25:cc81f2120eda 138 void do_state_homing()
brass_phoenix 25:cc81f2120eda 139 {
brass_phoenix 25:cc81f2120eda 140 const double home_x = 0.6524; // Meters.
brass_phoenix 25:cc81f2120eda 141 const double home_y = 0.3409;
brass_phoenix 25:cc81f2120eda 142
brass_phoenix 26:a8f4a117cc0d 143 double main_home;
brass_phoenix 26:a8f4a117cc0d 144 double sec_home;
brass_phoenix 25:cc81f2120eda 145
brass_phoenix 25:cc81f2120eda 146 if(last_state != current_state) {
brass_phoenix 25:cc81f2120eda 147 last_state = current_state;
brass_phoenix 25:cc81f2120eda 148 // State just changed to this one.
brass_phoenix 25:cc81f2120eda 149 screen.clear_display();
brass_phoenix 25:cc81f2120eda 150 screen.display_state_name("Homing");
brass_phoenix 25:cc81f2120eda 151
brass_phoenix 26:a8f4a117cc0d 152 inverse_kinematics(home_x, home_y, main_home, sec_home);
brass_phoenix 25:cc81f2120eda 153
brass_phoenix 25:cc81f2120eda 154 main_motor.set_target_angle(main_home);
brass_phoenix 25:cc81f2120eda 155 sec_motor.set_target_angle(sec_home);
brass_phoenix 33:543debddb3a9 156
brass_phoenix 33:543debddb3a9 157 screen.get_screen_handle()->setTextCursor(0, 8);
brass_phoenix 33:543debddb3a9 158 screen.get_screen_handle()->printf("Ma: %.6f \n", main_home);
brass_phoenix 33:543debddb3a9 159 screen.get_screen_handle()->printf("Sa: %.6f \n", sec_home);
brass_phoenix 33:543debddb3a9 160 screen.display();
brass_phoenix 25:cc81f2120eda 161 }
brass_phoenix 25:cc81f2120eda 162
brass_phoenix 25:cc81f2120eda 163 if (ud_button.has_just_been_pressed()) {
brass_phoenix 25:cc81f2120eda 164 current_state = calib_bicep1;
brass_phoenix 25:cc81f2120eda 165 }
brass_phoenix 32:b63b5837bcb1 166 if (lr_button.has_just_been_pressed()) {
brass_phoenix 32:b63b5837bcb1 167 current_state = demo;
brass_phoenix 32:b63b5837bcb1 168 }
brass_phoenix 25:cc81f2120eda 169 }
brass_phoenix 25:cc81f2120eda 170
brass_phoenix 3:4b19b6cf6cc7 171 void do_state_calib_bicep1()
brass_phoenix 3:4b19b6cf6cc7 172 {
brass_phoenix 39:f119ca6fc821 173 static EMG_calibration calibration = EMG_calibration(&screen, &emg_1);
brass_phoenix 39:f119ca6fc821 174
brass_phoenix 2:141cfcafe72b 175 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 176 last_state = current_state;
brass_phoenix 2:141cfcafe72b 177 // State just changed to this one.
brass_phoenix 6:bfc6e68774f5 178 screen.clear_display();
brass_phoenix 5:2632dfc8454c 179 screen.display_state_name("EMG 1 calibration");
brass_phoenix 39:f119ca6fc821 180
brass_phoenix 39:f119ca6fc821 181 calibration.start();
brass_phoenix 2:141cfcafe72b 182 }
brass_phoenix 9:27d00b64076e 183
brass_phoenix 39:f119ca6fc821 184 if (ud_button.has_just_been_pressed() && calibration.is_calibrated()) {
brass_phoenix 9:27d00b64076e 185 current_state = calib_bicep2;
brass_phoenix 9:27d00b64076e 186 }
brass_phoenix 2:141cfcafe72b 187 }
brass_phoenix 2:141cfcafe72b 188
brass_phoenix 3:4b19b6cf6cc7 189 void do_state_calib_bicep2()
brass_phoenix 3:4b19b6cf6cc7 190 {
brass_phoenix 39:f119ca6fc821 191 static EMG_calibration calibration = EMG_calibration(&screen, &emg_2);
brass_phoenix 39:f119ca6fc821 192
brass_phoenix 2:141cfcafe72b 193 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 194 last_state = current_state;
brass_phoenix 2:141cfcafe72b 195 // State just changed to this one.
brass_phoenix 6:bfc6e68774f5 196 screen.clear_display();
brass_phoenix 5:2632dfc8454c 197 screen.display_state_name("EMG 2 calibration");
brass_phoenix 39:f119ca6fc821 198
brass_phoenix 39:f119ca6fc821 199 calibration.start();
brass_phoenix 2:141cfcafe72b 200 }
brass_phoenix 9:27d00b64076e 201
brass_phoenix 39:f119ca6fc821 202 if (ud_button.has_just_been_pressed() && calibration.is_calibrated()) {
brass_phoenix 39:f119ca6fc821 203 current_state = operation;
brass_phoenix 9:27d00b64076e 204 }
brass_phoenix 2:141cfcafe72b 205 }
brass_phoenix 2:141cfcafe72b 206
brass_phoenix 3:4b19b6cf6cc7 207 void do_state_operation()
brass_phoenix 3:4b19b6cf6cc7 208 {
brass_phoenix 21:d541303a2ea6 209 static bool debug_forward_kinematics;
brass_phoenix 21:d541303a2ea6 210
brass_phoenix 2:141cfcafe72b 211 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 212 last_state = current_state;
brass_phoenix 2:141cfcafe72b 213 // State just changed to this one.
brass_phoenix 6:bfc6e68774f5 214 screen.clear_display();
brass_phoenix 5:2632dfc8454c 215 screen.display_state_name("Normal operation");
brass_phoenix 15:f65b4566193e 216
brass_phoenix 15:f65b4566193e 217 control_goes_up = true;
brass_phoenix 15:f65b4566193e 218 control_goes_right = true;
brass_phoenix 15:f65b4566193e 219
brass_phoenix 15:f65b4566193e 220 screen.display_up_down_arrow(control_goes_up);
brass_phoenix 15:f65b4566193e 221 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 21:d541303a2ea6 222
brass_phoenix 21:d541303a2ea6 223 debug_forward_kinematics = true;
brass_phoenix 2:141cfcafe72b 224 }
brass_phoenix 9:27d00b64076e 225
brass_phoenix 21:d541303a2ea6 226 if (debug_forward_kinematics) {
brass_phoenix 21:d541303a2ea6 227 // Using potmeters for debugging purposes;
brass_phoenix 21:d541303a2ea6 228 double main_angle = ((potmeter1.read() * 2) - 1) * PI;
brass_phoenix 21:d541303a2ea6 229 double sec_angle = ((potmeter2.read() * 2) - 1) * PI;
brass_phoenix 21:d541303a2ea6 230
brass_phoenix 21:d541303a2ea6 231 double e_x = 0.0;
brass_phoenix 21:d541303a2ea6 232 double e_y = 0.0;
brass_phoenix 21:d541303a2ea6 233
brass_phoenix 21:d541303a2ea6 234 forward_kinematics(main_angle, sec_angle, e_x, e_y);
brass_phoenix 21:d541303a2ea6 235
brass_phoenix 21:d541303a2ea6 236 screen.get_screen_handle()->setTextCursor(0, 0);
brass_phoenix 21:d541303a2ea6 237 screen.get_screen_handle()->printf("M_a: %.6f \n", main_angle);
brass_phoenix 21:d541303a2ea6 238 screen.get_screen_handle()->printf("S_a: %.6f \n", sec_angle);
brass_phoenix 21:d541303a2ea6 239 screen.get_screen_handle()->printf("X: %.6f \n", e_x);
brass_phoenix 21:d541303a2ea6 240 screen.get_screen_handle()->printf("Y: %.6f ", e_y);
brass_phoenix 21:d541303a2ea6 241 screen.display();
brass_phoenix 20:af1a6cd7469b 242
brass_phoenix 21:d541303a2ea6 243 } else {
brass_phoenix 21:d541303a2ea6 244 // Using potmeters for debugging purposes;
brass_phoenix 21:d541303a2ea6 245 double e_x = potmeter1.read();
brass_phoenix 21:d541303a2ea6 246 double e_y = potmeter2.read();
brass_phoenix 21:d541303a2ea6 247
brass_phoenix 21:d541303a2ea6 248 double main_angle = 0.0;
brass_phoenix 21:d541303a2ea6 249 double sec_angle = 0.0;
brass_phoenix 21:d541303a2ea6 250
brass_phoenix 21:d541303a2ea6 251 inverse_kinematics(e_x, e_y, main_angle, sec_angle);
brass_phoenix 21:d541303a2ea6 252
brass_phoenix 21:d541303a2ea6 253 screen.get_screen_handle()->setTextCursor(0, 0);
brass_phoenix 21:d541303a2ea6 254 screen.get_screen_handle()->printf("E_x: %.6f \n", e_x);
brass_phoenix 21:d541303a2ea6 255 screen.get_screen_handle()->printf("E_y: %.6f \n", e_y);
brass_phoenix 21:d541303a2ea6 256 screen.get_screen_handle()->printf("M_a: %.6f \n", main_angle);
brass_phoenix 21:d541303a2ea6 257 screen.get_screen_handle()->printf("S_a: %.6f ", sec_angle);
brass_phoenix 21:d541303a2ea6 258 screen.display();
brass_phoenix 21:d541303a2ea6 259 }
brass_phoenix 20:af1a6cd7469b 260
brass_phoenix 21:d541303a2ea6 261 if (lr_button.has_just_been_pressed()) {
brass_phoenix 21:d541303a2ea6 262 debug_forward_kinematics = !debug_forward_kinematics;
brass_phoenix 21:d541303a2ea6 263 }
brass_phoenix 20:af1a6cd7469b 264
brass_phoenix 19:53b9729fbab5 265 /*
brass_phoenix 14:b97e7a41ec23 266 double main_target = ((potmeter1.read() * 2) - 1) * PI;
brass_phoenix 13:88967c004446 267 main_motor.set_target_angle(main_target);
brass_phoenix 14:b97e7a41ec23 268 double sec_target = ((potmeter2.read() * 2) - 1) * PI;
brass_phoenix 13:88967c004446 269 sec_motor.set_target_angle(sec_target);
brass_phoenix 13:88967c004446 270
brass_phoenix 25:cc81f2120eda 271 if (lr_button.has_just_been_pressed()) {
brass_phoenix 25:cc81f2120eda 272 control_goes_right = !control_goes_right;
brass_phoenix 25:cc81f2120eda 273 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 25:cc81f2120eda 274 }
brass_phoenix 25:cc81f2120eda 275 */
brass_phoenix 25:cc81f2120eda 276
brass_phoenix 9:27d00b64076e 277 if (ud_button.has_just_been_pressed()) {
brass_phoenix 12:0c10396d0615 278 control_goes_up = !control_goes_up;
brass_phoenix 25:cc81f2120eda 279 control_goes_right = !control_goes_right;
brass_phoenix 15:f65b4566193e 280 screen.display_up_down_arrow(control_goes_up);
brass_phoenix 15:f65b4566193e 281 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 9:27d00b64076e 282 }
brass_phoenix 2:141cfcafe72b 283 }
brass_phoenix 2:141cfcafe72b 284
brass_phoenix 32:b63b5837bcb1 285 void do_state_demo() {
brass_phoenix 34:ae62ebf4d494 286 static DebugControlDirection control_direction;
brass_phoenix 34:ae62ebf4d494 287 static const double max_speed = 0.01;
brass_phoenix 34:ae62ebf4d494 288 static double speed_x;
brass_phoenix 34:ae62ebf4d494 289 static double speed_y;
brass_phoenix 34:ae62ebf4d494 290
brass_phoenix 32:b63b5837bcb1 291 if(last_state != current_state) {
brass_phoenix 32:b63b5837bcb1 292 last_state = current_state;
brass_phoenix 32:b63b5837bcb1 293 // State just changed.
brass_phoenix 32:b63b5837bcb1 294 // Update the display.
brass_phoenix 32:b63b5837bcb1 295 led_red = 1;
brass_phoenix 32:b63b5837bcb1 296 led_green = 0;
brass_phoenix 32:b63b5837bcb1 297 led_blue = 1;
brass_phoenix 32:b63b5837bcb1 298 screen.clear_display();
brass_phoenix 32:b63b5837bcb1 299 screen.display_state_name("Demo mode!");
brass_phoenix 32:b63b5837bcb1 300
brass_phoenix 34:ae62ebf4d494 301 control_direction = debug_up;
brass_phoenix 34:ae62ebf4d494 302
brass_phoenix 34:ae62ebf4d494 303 speed_x = 0;
brass_phoenix 34:ae62ebf4d494 304 speed_y = 0;
brass_phoenix 34:ae62ebf4d494 305
brass_phoenix 32:b63b5837bcb1 306 screen.display_up_down_arrow(control_goes_up);
brass_phoenix 32:b63b5837bcb1 307 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 32:b63b5837bcb1 308 }
brass_phoenix 32:b63b5837bcb1 309
brass_phoenix 32:b63b5837bcb1 310 if (lr_button.has_just_been_pressed()) {
brass_phoenix 34:ae62ebf4d494 311 switch (control_direction) {
brass_phoenix 34:ae62ebf4d494 312 case debug_up:
brass_phoenix 34:ae62ebf4d494 313 control_direction = debug_right;
brass_phoenix 34:ae62ebf4d494 314 speed_x = max_speed;
brass_phoenix 34:ae62ebf4d494 315 speed_y = 0;
brass_phoenix 34:ae62ebf4d494 316 break;
brass_phoenix 34:ae62ebf4d494 317 case debug_right:
brass_phoenix 34:ae62ebf4d494 318 control_direction = debug_down;
brass_phoenix 34:ae62ebf4d494 319 speed_x = 0;
brass_phoenix 34:ae62ebf4d494 320 speed_y = -max_speed;
brass_phoenix 34:ae62ebf4d494 321 break;
brass_phoenix 34:ae62ebf4d494 322 case debug_down:
brass_phoenix 34:ae62ebf4d494 323 control_direction = debug_left;
brass_phoenix 34:ae62ebf4d494 324 speed_x = -max_speed;
brass_phoenix 34:ae62ebf4d494 325 speed_y = 0;
brass_phoenix 34:ae62ebf4d494 326 break;
brass_phoenix 34:ae62ebf4d494 327 case debug_left:
brass_phoenix 34:ae62ebf4d494 328 control_direction = debug_up;
brass_phoenix 34:ae62ebf4d494 329 speed_x = 0;
brass_phoenix 34:ae62ebf4d494 330 speed_y = max_speed;
brass_phoenix 34:ae62ebf4d494 331 break;
brass_phoenix 34:ae62ebf4d494 332 }
brass_phoenix 32:b63b5837bcb1 333 }
brass_phoenix 32:b63b5837bcb1 334
brass_phoenix 34:ae62ebf4d494 335 if (ud_button.is_pressed()) {
brass_phoenix 32:b63b5837bcb1 336
brass_phoenix 32:b63b5837bcb1 337 led_blue = 0;
brass_phoenix 34:ae62ebf4d494 338
brass_phoenix 32:b63b5837bcb1 339
brass_phoenix 32:b63b5837bcb1 340 double main_cur_angle = main_motor.get_current_angle();
brass_phoenix 32:b63b5837bcb1 341 double sec_cur_angle = sec_motor.get_current_angle();
brass_phoenix 32:b63b5837bcb1 342
brass_phoenix 32:b63b5837bcb1 343 double main_target, sec_target;
brass_phoenix 32:b63b5837bcb1 344
brass_phoenix 32:b63b5837bcb1 345 end_effector_control(speed_x, speed_y, main_cur_angle, sec_cur_angle, main_target, sec_target);
brass_phoenix 32:b63b5837bcb1 346
brass_phoenix 32:b63b5837bcb1 347 main_motor.set_target_angle(main_target);
brass_phoenix 32:b63b5837bcb1 348 sec_motor.set_target_angle(sec_target);
brass_phoenix 32:b63b5837bcb1 349
brass_phoenix 32:b63b5837bcb1 350 screen.get_screen_handle()->setTextCursor(0, 0);
brass_phoenix 32:b63b5837bcb1 351 screen.get_screen_handle()->printf("M_a: %.6f \n", main_cur_angle);
brass_phoenix 32:b63b5837bcb1 352 screen.get_screen_handle()->printf("S_a: %.6f \n", sec_cur_angle);
brass_phoenix 32:b63b5837bcb1 353 screen.get_screen_handle()->printf("Vx: %.6f \n", main_target);
brass_phoenix 32:b63b5837bcb1 354 screen.get_screen_handle()->printf("Vy: %.6f ", sec_target);
brass_phoenix 32:b63b5837bcb1 355 screen.display();
brass_phoenix 32:b63b5837bcb1 356 }
brass_phoenix 32:b63b5837bcb1 357 }
brass_phoenix 32:b63b5837bcb1 358
brass_phoenix 3:4b19b6cf6cc7 359 void do_state_failure()
brass_phoenix 3:4b19b6cf6cc7 360 {
brass_phoenix 2:141cfcafe72b 361 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 362 last_state = current_state;
brass_phoenix 2:141cfcafe72b 363 // State just changed.
brass_phoenix 2:141cfcafe72b 364 // Update the display.
brass_phoenix 2:141cfcafe72b 365 led_red = 0;
brass_phoenix 2:141cfcafe72b 366 led_green = 1;
brass_phoenix 30:a45bbfa6bd22 367 led_blue = 1;
brass_phoenix 6:bfc6e68774f5 368 screen.clear_display();
brass_phoenix 11:d980e0e581db 369 screen.display_state_name("STOP");
brass_phoenix 2:141cfcafe72b 370 }
brass_phoenix 3:4b19b6cf6cc7 371
brass_phoenix 2:141cfcafe72b 372 // Stop the motors!
brass_phoenix 12:0c10396d0615 373 main_motor.stop();
brass_phoenix 12:0c10396d0615 374 sec_motor.stop();
brass_phoenix 2:141cfcafe72b 375 }
brass_phoenix 2:141cfcafe72b 376
brass_phoenix 2:141cfcafe72b 377
brass_phoenix 1:cfa5abca6d43 378 void main_loop()
brass_phoenix 1:cfa5abca6d43 379 {
brass_phoenix 1:cfa5abca6d43 380 ud_button.update();
brass_phoenix 1:cfa5abca6d43 381 lr_button.update();
brass_phoenix 1:cfa5abca6d43 382 p_button.update();
brass_phoenix 3:4b19b6cf6cc7 383
brass_phoenix 1:cfa5abca6d43 384 switch (current_state) {
brass_phoenix 1:cfa5abca6d43 385 case waiting:
brass_phoenix 2:141cfcafe72b 386 do_state_waiting();
brass_phoenix 1:cfa5abca6d43 387 break;
brass_phoenix 1:cfa5abca6d43 388 case calib_motor:
brass_phoenix 2:141cfcafe72b 389 do_state_calib_motor();
brass_phoenix 1:cfa5abca6d43 390 break;
brass_phoenix 1:cfa5abca6d43 391 case calib_bicep1:
brass_phoenix 2:141cfcafe72b 392 do_state_calib_bicep1();
brass_phoenix 1:cfa5abca6d43 393 break;
brass_phoenix 1:cfa5abca6d43 394 case calib_bicep2:
brass_phoenix 2:141cfcafe72b 395 do_state_calib_bicep2();
brass_phoenix 1:cfa5abca6d43 396 break;
brass_phoenix 1:cfa5abca6d43 397 case homing:
brass_phoenix 2:141cfcafe72b 398 do_state_homing();
brass_phoenix 1:cfa5abca6d43 399 break;
brass_phoenix 1:cfa5abca6d43 400 case operation:
brass_phoenix 2:141cfcafe72b 401 do_state_operation();
brass_phoenix 1:cfa5abca6d43 402 break;
brass_phoenix 32:b63b5837bcb1 403 case demo:
brass_phoenix 32:b63b5837bcb1 404 do_state_demo();
brass_phoenix 32:b63b5837bcb1 405 break;
brass_phoenix 1:cfa5abca6d43 406 case failure:
brass_phoenix 2:141cfcafe72b 407 do_state_failure();
brass_phoenix 1:cfa5abca6d43 408 break;
brass_phoenix 1:cfa5abca6d43 409 }
brass_phoenix 3:4b19b6cf6cc7 410
brass_phoenix 2:141cfcafe72b 411 // Check if the panic button was pressed.
brass_phoenix 2:141cfcafe72b 412 // Doesn't matter in which state we are, we need to go to failure.
brass_phoenix 2:141cfcafe72b 413 if (p_button.is_pressed()) {
brass_phoenix 2:141cfcafe72b 414 current_state = failure;
brass_phoenix 3:4b19b6cf6cc7 415 }
brass_phoenix 1:cfa5abca6d43 416 }
MAHCSnijders 0:7d25c2ade6c5 417
brass_phoenix 7:e7f808875bc4 418 void poll_buttons() {
brass_phoenix 7:e7f808875bc4 419 // We need to poll the pins periodically.
brass_phoenix 7:e7f808875bc4 420 // Normally one would use rise and fall interrupts, so this wouldn't be
brass_phoenix 7:e7f808875bc4 421 // needed. But the buttons we use generate so much chatter that
brass_phoenix 7:e7f808875bc4 422 // sometimes a rising or a falling edge doesn't get registered.
brass_phoenix 7:e7f808875bc4 423 // With all the confusion that accompanies it.
brass_phoenix 7:e7f808875bc4 424 ud_button.poll_pin();
brass_phoenix 7:e7f808875bc4 425 lr_button.poll_pin();
brass_phoenix 7:e7f808875bc4 426 p_button.poll_pin();
brass_phoenix 7:e7f808875bc4 427 }
brass_phoenix 7:e7f808875bc4 428
brass_phoenix 1:cfa5abca6d43 429 int main()
brass_phoenix 1:cfa5abca6d43 430 {
brass_phoenix 1:cfa5abca6d43 431 led_red = 1;
brass_phoenix 9:27d00b64076e 432 led_green = 1;
brass_phoenix 9:27d00b64076e 433 led_blue = 1;
brass_phoenix 8:9090ab7c19a8 434
brass_phoenix 10:b165ccd11afd 435 screen.clear_display();
brass_phoenix 10:b165ccd11afd 436
brass_phoenix 12:0c10396d0615 437 main_motor.set_pid_k_values(Kp, Ki, Kd);
brass_phoenix 12:0c10396d0615 438 sec_motor.set_pid_k_values(Kp, Ki, Kd);
brass_phoenix 12:0c10396d0615 439
brass_phoenix 12:0c10396d0615 440 // One of the motors is reversed in the electronics.
brass_phoenix 12:0c10396d0615 441 // This is fixed in the motor controll board, so we have to account
brass_phoenix 12:0c10396d0615 442 // for it in software.
brass_phoenix 19:53b9729fbab5 443 main_motor.set_extra_reduction_ratio(-main_gear_ratio);
brass_phoenix 19:53b9729fbab5 444 sec_motor.set_extra_reduction_ratio(sec_gear_ratio);
brass_phoenix 19:53b9729fbab5 445
brass_phoenix 29:77fee8a01529 446 // Set the maximum speed for both motors.
brass_phoenix 29:77fee8a01529 447 main_motor.set_max_speed(0.5);
brass_phoenix 29:77fee8a01529 448 sec_motor.set_max_speed(0.5);
brass_phoenix 12:0c10396d0615 449
brass_phoenix 8:9090ab7c19a8 450 // Start the motor controller at the desired frequency.
brass_phoenix 12:0c10396d0615 451 main_motor.start(pid_period);
brass_phoenix 12:0c10396d0615 452 sec_motor.start(pid_period);
brass_phoenix 3:4b19b6cf6cc7 453
brass_phoenix 2:141cfcafe72b 454 // Start in the waiting state.
brass_phoenix 1:cfa5abca6d43 455 current_state = waiting;
brass_phoenix 4:5a44ab7e94b3 456 // Pretend we come from the operation state.
brass_phoenix 4:5a44ab7e94b3 457 // So that the waiting state knows it just got started.
brass_phoenix 4:5a44ab7e94b3 458 last_state = operation;
brass_phoenix 7:e7f808875bc4 459
brass_phoenix 39:f119ca6fc821 460 emg_1.start(emg_period);
brass_phoenix 39:f119ca6fc821 461 emg_2.start(emg_period);
brass_phoenix 39:f119ca6fc821 462
brass_phoenix 7:e7f808875bc4 463 // Start the button polling ticker.
brass_phoenix 7:e7f808875bc4 464 button_ticker.attach(&poll_buttons, button_poll_interval);
brass_phoenix 3:4b19b6cf6cc7 465
brass_phoenix 1:cfa5abca6d43 466 while (true) {
brass_phoenix 1:cfa5abca6d43 467 main_loop();
brass_phoenix 9:27d00b64076e 468
brass_phoenix 1:cfa5abca6d43 469 wait(main_loop_wait_time);
brass_phoenix 1:cfa5abca6d43 470 }
brass_phoenix 1:cfa5abca6d43 471 }