State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Committer:
brass_phoenix
Date:
Thu Nov 01 16:05:41 2018 +0000
Revision:
41:8640b5782bc7
Parent:
40:7e8d0632757c
Child:
42:cafa56da9ed2
+ EMG should controll robot, but doesnt correctly.

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