State machine
Dependencies: mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter
Diff: main.cpp
- Revision:
- 1:cfa5abca6d43
- Parent:
- 0:7d25c2ade6c5
- Child:
- 2:141cfcafe72b
--- a/main.cpp Mon Oct 29 13:44:06 2018 +0000 +++ b/main.cpp Tue Oct 30 11:01:00 2018 +0000 @@ -1,26 +1,71 @@ #include "mbed.h" +#include "Button.h" -enum states {wait, calib_motor, calib_bicep1, calib_bicep2, homing, operation, failure}; // The possible states of the state machine + +enum States {waiting, calib_motor, calib_bicep1, calib_bicep2, homing, operation, failure}; // The possible states of the state machine // Global variables const double PI = 3.14159265359; -const double PULSES_PER_ROTATION = 6533; -states current_state; // Defining the state we are currently in +// Main loop wait time per cycle. This does not influence the motor PID or EMG reading frequencies. +const double main_loop_wait_time = 0.01; + + +States current_state; // Defining the state we are currently in Ticker loop_ticker; // Ticker for the loop function -float emg1_unfiltered, emg1_filtered; // Floats for EMG bicep 1 -float emg2_unfiltered, emg2_filtered; // Floats for EMG bicep 2 -float u1, u2; // Floats for motor duty cycle -float des_motor1_angle, des_motor2_angle; // Floats for position end-effector + +// Order of buttons: up_down, left_right, panic +// D2, D3, D8 +Button ud_button(PTA4); +Button lr_button(PTC6); +Button p_button(D8); -void measure_everything() { - motor1_angle = motor1_counts * 2.0f * PI/PULSES_PER_ROTATION; // Gives angle of motor1 - motor2_angle = motor2_counts * 2.0f * PI/PULSES_PER_ROTATION; // Gives angle of motor2 +DigitalOut led_red(LED_RED); +DigitalOut led_green(LED_GREEN); + +void main_loop() +{ + ud_button.update(); + lr_button.update(); + p_button.update(); - + led_red = !ud_button.has_just_been_pressed(); + + led_green = lr_button.is_pressed(); + + switch (current_state) { + case waiting: + //do_state_waiting(): + break; + case calib_motor: + //do_state_calib_motor(): + break; + case calib_bicep1: + //do_state_calib_bicep1(): + break; + case calib_bicep2: + //do_state_calib_bicep2(): + break; + case homing: + //do_state_homing(): + break; + case operation: + //do_state_operation(): + break; + case failure: + //do_state_failure(): + break; + } + +} -void - - - +int main() +{ + led_red = 1; + current_state = waiting; + while (true) { + main_loop(); + wait(main_loop_wait_time); + } +} \ No newline at end of file