State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Committer:
brass_phoenix
Date:
Thu Nov 01 15:46:25 2018 +0000
Revision:
40:7e8d0632757c
Parent:
39:f119ca6fc821
Child:
41:8640b5782bc7
+ EMG threshold debug works.

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 40:7e8d0632757c 226 /*
brass_phoenix 21:d541303a2ea6 227 if (debug_forward_kinematics) {
brass_phoenix 21:d541303a2ea6 228 // Using potmeters for debugging purposes;
brass_phoenix 21:d541303a2ea6 229 double main_angle = ((potmeter1.read() * 2) - 1) * PI;
brass_phoenix 21:d541303a2ea6 230 double sec_angle = ((potmeter2.read() * 2) - 1) * PI;
brass_phoenix 21:d541303a2ea6 231
brass_phoenix 21:d541303a2ea6 232 double e_x = 0.0;
brass_phoenix 21:d541303a2ea6 233 double e_y = 0.0;
brass_phoenix 21:d541303a2ea6 234
brass_phoenix 21:d541303a2ea6 235 forward_kinematics(main_angle, sec_angle, e_x, e_y);
brass_phoenix 21:d541303a2ea6 236
brass_phoenix 21:d541303a2ea6 237 screen.get_screen_handle()->setTextCursor(0, 0);
brass_phoenix 21:d541303a2ea6 238 screen.get_screen_handle()->printf("M_a: %.6f \n", main_angle);
brass_phoenix 21:d541303a2ea6 239 screen.get_screen_handle()->printf("S_a: %.6f \n", sec_angle);
brass_phoenix 21:d541303a2ea6 240 screen.get_screen_handle()->printf("X: %.6f \n", e_x);
brass_phoenix 21:d541303a2ea6 241 screen.get_screen_handle()->printf("Y: %.6f ", e_y);
brass_phoenix 21:d541303a2ea6 242 screen.display();
brass_phoenix 20:af1a6cd7469b 243
brass_phoenix 21:d541303a2ea6 244 } else {
brass_phoenix 21:d541303a2ea6 245 // Using potmeters for debugging purposes;
brass_phoenix 21:d541303a2ea6 246 double e_x = potmeter1.read();
brass_phoenix 21:d541303a2ea6 247 double e_y = potmeter2.read();
brass_phoenix 21:d541303a2ea6 248
brass_phoenix 21:d541303a2ea6 249 double main_angle = 0.0;
brass_phoenix 21:d541303a2ea6 250 double sec_angle = 0.0;
brass_phoenix 21:d541303a2ea6 251
brass_phoenix 21:d541303a2ea6 252 inverse_kinematics(e_x, e_y, main_angle, sec_angle);
brass_phoenix 21:d541303a2ea6 253
brass_phoenix 21:d541303a2ea6 254 screen.get_screen_handle()->setTextCursor(0, 0);
brass_phoenix 21:d541303a2ea6 255 screen.get_screen_handle()->printf("E_x: %.6f \n", e_x);
brass_phoenix 21:d541303a2ea6 256 screen.get_screen_handle()->printf("E_y: %.6f \n", e_y);
brass_phoenix 21:d541303a2ea6 257 screen.get_screen_handle()->printf("M_a: %.6f \n", main_angle);
brass_phoenix 21:d541303a2ea6 258 screen.get_screen_handle()->printf("S_a: %.6f ", sec_angle);
brass_phoenix 21:d541303a2ea6 259 screen.display();
brass_phoenix 21:d541303a2ea6 260 }
brass_phoenix 20:af1a6cd7469b 261
brass_phoenix 21:d541303a2ea6 262 if (lr_button.has_just_been_pressed()) {
brass_phoenix 21:d541303a2ea6 263 debug_forward_kinematics = !debug_forward_kinematics;
brass_phoenix 40:7e8d0632757c 264 }*/
brass_phoenix 40:7e8d0632757c 265
brass_phoenix 40:7e8d0632757c 266 // Debug emg calibration.
brass_phoenix 40:7e8d0632757c 267 led_blue = emg_1.get_is_envelope_over_threshold();
brass_phoenix 40:7e8d0632757c 268 led_green = emg_2.get_is_envelope_over_threshold();
brass_phoenix 20:af1a6cd7469b 269
brass_phoenix 19:53b9729fbab5 270 /*
brass_phoenix 14:b97e7a41ec23 271 double main_target = ((potmeter1.read() * 2) - 1) * PI;
brass_phoenix 13:88967c004446 272 main_motor.set_target_angle(main_target);
brass_phoenix 14:b97e7a41ec23 273 double sec_target = ((potmeter2.read() * 2) - 1) * PI;
brass_phoenix 13:88967c004446 274 sec_motor.set_target_angle(sec_target);
brass_phoenix 13:88967c004446 275
brass_phoenix 25:cc81f2120eda 276 if (lr_button.has_just_been_pressed()) {
brass_phoenix 25:cc81f2120eda 277 control_goes_right = !control_goes_right;
brass_phoenix 25:cc81f2120eda 278 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 25:cc81f2120eda 279 }
brass_phoenix 25:cc81f2120eda 280 */
brass_phoenix 25:cc81f2120eda 281
brass_phoenix 9:27d00b64076e 282 if (ud_button.has_just_been_pressed()) {
brass_phoenix 12:0c10396d0615 283 control_goes_up = !control_goes_up;
brass_phoenix 25:cc81f2120eda 284 control_goes_right = !control_goes_right;
brass_phoenix 15:f65b4566193e 285 screen.display_up_down_arrow(control_goes_up);
brass_phoenix 15:f65b4566193e 286 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 9:27d00b64076e 287 }
brass_phoenix 2:141cfcafe72b 288 }
brass_phoenix 2:141cfcafe72b 289
brass_phoenix 32:b63b5837bcb1 290 void do_state_demo() {
brass_phoenix 34:ae62ebf4d494 291 static DebugControlDirection control_direction;
brass_phoenix 34:ae62ebf4d494 292 static const double max_speed = 0.01;
brass_phoenix 34:ae62ebf4d494 293 static double speed_x;
brass_phoenix 34:ae62ebf4d494 294 static double speed_y;
brass_phoenix 34:ae62ebf4d494 295
brass_phoenix 32:b63b5837bcb1 296 if(last_state != current_state) {
brass_phoenix 32:b63b5837bcb1 297 last_state = current_state;
brass_phoenix 32:b63b5837bcb1 298 // State just changed.
brass_phoenix 32:b63b5837bcb1 299 // Update the display.
brass_phoenix 32:b63b5837bcb1 300 led_red = 1;
brass_phoenix 32:b63b5837bcb1 301 led_green = 0;
brass_phoenix 32:b63b5837bcb1 302 led_blue = 1;
brass_phoenix 32:b63b5837bcb1 303 screen.clear_display();
brass_phoenix 32:b63b5837bcb1 304 screen.display_state_name("Demo mode!");
brass_phoenix 32:b63b5837bcb1 305
brass_phoenix 34:ae62ebf4d494 306 control_direction = debug_up;
brass_phoenix 34:ae62ebf4d494 307
brass_phoenix 34:ae62ebf4d494 308 speed_x = 0;
brass_phoenix 34:ae62ebf4d494 309 speed_y = 0;
brass_phoenix 34:ae62ebf4d494 310
brass_phoenix 32:b63b5837bcb1 311 screen.display_up_down_arrow(control_goes_up);
brass_phoenix 32:b63b5837bcb1 312 screen.display_left_right_arrow(control_goes_right);
brass_phoenix 32:b63b5837bcb1 313 }
brass_phoenix 32:b63b5837bcb1 314
brass_phoenix 32:b63b5837bcb1 315 if (lr_button.has_just_been_pressed()) {
brass_phoenix 34:ae62ebf4d494 316 switch (control_direction) {
brass_phoenix 34:ae62ebf4d494 317 case debug_up:
brass_phoenix 34:ae62ebf4d494 318 control_direction = debug_right;
brass_phoenix 34:ae62ebf4d494 319 speed_x = max_speed;
brass_phoenix 34:ae62ebf4d494 320 speed_y = 0;
brass_phoenix 34:ae62ebf4d494 321 break;
brass_phoenix 34:ae62ebf4d494 322 case debug_right:
brass_phoenix 34:ae62ebf4d494 323 control_direction = debug_down;
brass_phoenix 34:ae62ebf4d494 324 speed_x = 0;
brass_phoenix 34:ae62ebf4d494 325 speed_y = -max_speed;
brass_phoenix 34:ae62ebf4d494 326 break;
brass_phoenix 34:ae62ebf4d494 327 case debug_down:
brass_phoenix 34:ae62ebf4d494 328 control_direction = debug_left;
brass_phoenix 34:ae62ebf4d494 329 speed_x = -max_speed;
brass_phoenix 34:ae62ebf4d494 330 speed_y = 0;
brass_phoenix 34:ae62ebf4d494 331 break;
brass_phoenix 34:ae62ebf4d494 332 case debug_left:
brass_phoenix 34:ae62ebf4d494 333 control_direction = debug_up;
brass_phoenix 34:ae62ebf4d494 334 speed_x = 0;
brass_phoenix 34:ae62ebf4d494 335 speed_y = max_speed;
brass_phoenix 34:ae62ebf4d494 336 break;
brass_phoenix 34:ae62ebf4d494 337 }
brass_phoenix 32:b63b5837bcb1 338 }
brass_phoenix 32:b63b5837bcb1 339
brass_phoenix 34:ae62ebf4d494 340 if (ud_button.is_pressed()) {
brass_phoenix 32:b63b5837bcb1 341
brass_phoenix 32:b63b5837bcb1 342 led_blue = 0;
brass_phoenix 34:ae62ebf4d494 343
brass_phoenix 32:b63b5837bcb1 344
brass_phoenix 32:b63b5837bcb1 345 double main_cur_angle = main_motor.get_current_angle();
brass_phoenix 32:b63b5837bcb1 346 double sec_cur_angle = sec_motor.get_current_angle();
brass_phoenix 32:b63b5837bcb1 347
brass_phoenix 32:b63b5837bcb1 348 double main_target, sec_target;
brass_phoenix 32:b63b5837bcb1 349
brass_phoenix 32:b63b5837bcb1 350 end_effector_control(speed_x, speed_y, main_cur_angle, sec_cur_angle, main_target, sec_target);
brass_phoenix 32:b63b5837bcb1 351
brass_phoenix 32:b63b5837bcb1 352 main_motor.set_target_angle(main_target);
brass_phoenix 32:b63b5837bcb1 353 sec_motor.set_target_angle(sec_target);
brass_phoenix 32:b63b5837bcb1 354
brass_phoenix 32:b63b5837bcb1 355 screen.get_screen_handle()->setTextCursor(0, 0);
brass_phoenix 32:b63b5837bcb1 356 screen.get_screen_handle()->printf("M_a: %.6f \n", main_cur_angle);
brass_phoenix 32:b63b5837bcb1 357 screen.get_screen_handle()->printf("S_a: %.6f \n", sec_cur_angle);
brass_phoenix 32:b63b5837bcb1 358 screen.get_screen_handle()->printf("Vx: %.6f \n", main_target);
brass_phoenix 32:b63b5837bcb1 359 screen.get_screen_handle()->printf("Vy: %.6f ", sec_target);
brass_phoenix 32:b63b5837bcb1 360 screen.display();
brass_phoenix 32:b63b5837bcb1 361 }
brass_phoenix 32:b63b5837bcb1 362 }
brass_phoenix 32:b63b5837bcb1 363
brass_phoenix 3:4b19b6cf6cc7 364 void do_state_failure()
brass_phoenix 3:4b19b6cf6cc7 365 {
brass_phoenix 2:141cfcafe72b 366 if(last_state != current_state) {
brass_phoenix 2:141cfcafe72b 367 last_state = current_state;
brass_phoenix 2:141cfcafe72b 368 // State just changed.
brass_phoenix 2:141cfcafe72b 369 // Update the display.
brass_phoenix 2:141cfcafe72b 370 led_red = 0;
brass_phoenix 2:141cfcafe72b 371 led_green = 1;
brass_phoenix 30:a45bbfa6bd22 372 led_blue = 1;
brass_phoenix 6:bfc6e68774f5 373 screen.clear_display();
brass_phoenix 11:d980e0e581db 374 screen.display_state_name("STOP");
brass_phoenix 2:141cfcafe72b 375 }
brass_phoenix 3:4b19b6cf6cc7 376
brass_phoenix 2:141cfcafe72b 377 // Stop the motors!
brass_phoenix 12:0c10396d0615 378 main_motor.stop();
brass_phoenix 12:0c10396d0615 379 sec_motor.stop();
brass_phoenix 2:141cfcafe72b 380 }
brass_phoenix 2:141cfcafe72b 381
brass_phoenix 2:141cfcafe72b 382
brass_phoenix 1:cfa5abca6d43 383 void main_loop()
brass_phoenix 1:cfa5abca6d43 384 {
brass_phoenix 1:cfa5abca6d43 385 ud_button.update();
brass_phoenix 1:cfa5abca6d43 386 lr_button.update();
brass_phoenix 1:cfa5abca6d43 387 p_button.update();
brass_phoenix 3:4b19b6cf6cc7 388
brass_phoenix 1:cfa5abca6d43 389 switch (current_state) {
brass_phoenix 1:cfa5abca6d43 390 case waiting:
brass_phoenix 2:141cfcafe72b 391 do_state_waiting();
brass_phoenix 1:cfa5abca6d43 392 break;
brass_phoenix 1:cfa5abca6d43 393 case calib_motor:
brass_phoenix 2:141cfcafe72b 394 do_state_calib_motor();
brass_phoenix 1:cfa5abca6d43 395 break;
brass_phoenix 1:cfa5abca6d43 396 case calib_bicep1:
brass_phoenix 2:141cfcafe72b 397 do_state_calib_bicep1();
brass_phoenix 1:cfa5abca6d43 398 break;
brass_phoenix 1:cfa5abca6d43 399 case calib_bicep2:
brass_phoenix 2:141cfcafe72b 400 do_state_calib_bicep2();
brass_phoenix 1:cfa5abca6d43 401 break;
brass_phoenix 1:cfa5abca6d43 402 case homing:
brass_phoenix 2:141cfcafe72b 403 do_state_homing();
brass_phoenix 1:cfa5abca6d43 404 break;
brass_phoenix 1:cfa5abca6d43 405 case operation:
brass_phoenix 2:141cfcafe72b 406 do_state_operation();
brass_phoenix 1:cfa5abca6d43 407 break;
brass_phoenix 32:b63b5837bcb1 408 case demo:
brass_phoenix 32:b63b5837bcb1 409 do_state_demo();
brass_phoenix 32:b63b5837bcb1 410 break;
brass_phoenix 1:cfa5abca6d43 411 case failure:
brass_phoenix 2:141cfcafe72b 412 do_state_failure();
brass_phoenix 1:cfa5abca6d43 413 break;
brass_phoenix 1:cfa5abca6d43 414 }
brass_phoenix 3:4b19b6cf6cc7 415
brass_phoenix 2:141cfcafe72b 416 // Check if the panic button was pressed.
brass_phoenix 2:141cfcafe72b 417 // Doesn't matter in which state we are, we need to go to failure.
brass_phoenix 2:141cfcafe72b 418 if (p_button.is_pressed()) {
brass_phoenix 2:141cfcafe72b 419 current_state = failure;
brass_phoenix 3:4b19b6cf6cc7 420 }
brass_phoenix 1:cfa5abca6d43 421 }
MAHCSnijders 0:7d25c2ade6c5 422
brass_phoenix 7:e7f808875bc4 423 void poll_buttons() {
brass_phoenix 7:e7f808875bc4 424 // We need to poll the pins periodically.
brass_phoenix 7:e7f808875bc4 425 // Normally one would use rise and fall interrupts, so this wouldn't be
brass_phoenix 7:e7f808875bc4 426 // needed. But the buttons we use generate so much chatter that
brass_phoenix 7:e7f808875bc4 427 // sometimes a rising or a falling edge doesn't get registered.
brass_phoenix 7:e7f808875bc4 428 // With all the confusion that accompanies it.
brass_phoenix 7:e7f808875bc4 429 ud_button.poll_pin();
brass_phoenix 7:e7f808875bc4 430 lr_button.poll_pin();
brass_phoenix 7:e7f808875bc4 431 p_button.poll_pin();
brass_phoenix 7:e7f808875bc4 432 }
brass_phoenix 7:e7f808875bc4 433
brass_phoenix 1:cfa5abca6d43 434 int main()
brass_phoenix 1:cfa5abca6d43 435 {
brass_phoenix 1:cfa5abca6d43 436 led_red = 1;
brass_phoenix 9:27d00b64076e 437 led_green = 1;
brass_phoenix 9:27d00b64076e 438 led_blue = 1;
brass_phoenix 8:9090ab7c19a8 439
brass_phoenix 10:b165ccd11afd 440 screen.clear_display();
brass_phoenix 10:b165ccd11afd 441
brass_phoenix 12:0c10396d0615 442 main_motor.set_pid_k_values(Kp, Ki, Kd);
brass_phoenix 12:0c10396d0615 443 sec_motor.set_pid_k_values(Kp, Ki, Kd);
brass_phoenix 12:0c10396d0615 444
brass_phoenix 12:0c10396d0615 445 // One of the motors is reversed in the electronics.
brass_phoenix 12:0c10396d0615 446 // This is fixed in the motor controll board, so we have to account
brass_phoenix 12:0c10396d0615 447 // for it in software.
brass_phoenix 19:53b9729fbab5 448 main_motor.set_extra_reduction_ratio(-main_gear_ratio);
brass_phoenix 19:53b9729fbab5 449 sec_motor.set_extra_reduction_ratio(sec_gear_ratio);
brass_phoenix 19:53b9729fbab5 450
brass_phoenix 29:77fee8a01529 451 // Set the maximum speed for both motors.
brass_phoenix 29:77fee8a01529 452 main_motor.set_max_speed(0.5);
brass_phoenix 29:77fee8a01529 453 sec_motor.set_max_speed(0.5);
brass_phoenix 12:0c10396d0615 454
brass_phoenix 8:9090ab7c19a8 455 // Start the motor controller at the desired frequency.
brass_phoenix 12:0c10396d0615 456 main_motor.start(pid_period);
brass_phoenix 12:0c10396d0615 457 sec_motor.start(pid_period);
brass_phoenix 3:4b19b6cf6cc7 458
brass_phoenix 2:141cfcafe72b 459 // Start in the waiting state.
brass_phoenix 1:cfa5abca6d43 460 current_state = waiting;
brass_phoenix 4:5a44ab7e94b3 461 // Pretend we come from the operation state.
brass_phoenix 4:5a44ab7e94b3 462 // So that the waiting state knows it just got started.
brass_phoenix 4:5a44ab7e94b3 463 last_state = operation;
brass_phoenix 7:e7f808875bc4 464
brass_phoenix 39:f119ca6fc821 465 emg_1.start(emg_period);
brass_phoenix 39:f119ca6fc821 466 emg_2.start(emg_period);
brass_phoenix 39:f119ca6fc821 467
brass_phoenix 7:e7f808875bc4 468 // Start the button polling ticker.
brass_phoenix 7:e7f808875bc4 469 button_ticker.attach(&poll_buttons, button_poll_interval);
brass_phoenix 3:4b19b6cf6cc7 470
brass_phoenix 1:cfa5abca6d43 471 while (true) {
brass_phoenix 1:cfa5abca6d43 472 main_loop();
brass_phoenix 9:27d00b64076e 473
brass_phoenix 1:cfa5abca6d43 474 wait(main_loop_wait_time);
brass_phoenix 1:cfa5abca6d43 475 }
brass_phoenix 1:cfa5abca6d43 476 }