De hele robot in 1 keer bam
Dependencies: mbed QEI Servo HIDScope biquadFilter MODSERIAL FastPWM
main.cpp
- Committer:
- Jellehierck
- Date:
- 2019-10-30
- Revision:
- 37:806c7c8381a7
- Parent:
- 36:ec2bb2a02856
- Child:
- 38:8b597ab8344f
File content as of revision 37:806c7c8381a7:
/* ------------------------------ ADD LIBRARIES ------------------------------ */ #include "mbed.h" //Base library #include "MODSERIAL.h"// in order for connection with the pc /* ------------------------------ DEFINE MBED CONNECTIONS ------------------------------ */ // PC serial connection MODSERIAL pc(USBTX, USBRX); // Buttons InterruptIn button1(D11); InterruptIn button2(D10); InterruptIn switch2(SW2); InterruptIn switch3(SW3); /* ------------------------------ GLOBAL VARIABLES ------------------------------ */ // State machine variables enum GLOBAL_States { global_failure, global_wait, global_cal_emg, global_cal_motor, global_operation, global_demo }; // Define global states GLOBAL_States global_curr_state = global_wait; // Initialize global state to waiting state bool global_state_changed = true; // Enable entry functions bool failure_mode = false; bool cal_emg_done = false; bool cal_motor_done = false; // Button press interrupts (to prevent bounce) bool button1_pressed = false; bool button2_pressed = false; bool switch2_pressed = false; // Global program variables double Fs = 500.0; double Ts = 1/Fs; double Tcal_test = 5.0; /* ------------------------------ HELPER FUNCTIONS ------------------------------ */ void doStuff() {} // Empty placeholder function, needs to be deleted at end of project /* ------------------------------ BUTTON FUNCTIONS ------------------------------ */ // Handle button press void button1Press() { button1_pressed = true; } // Handle button press void button2Press() { button2_pressed = true; } void switch2Press() { switch2_pressed = true; } void switch3Press() { global_curr_state = global_failure; global_state_changed = true; } /* ------------------------------ TICKER, TIMER & TIMEOUT FUNCTIONS ------------------------------ */ // Initialize tickers and timeouts Ticker tickGlobal; // Set global ticker Timer timerStateMachineTest; // Set testing timer /* ------------------------------ GLOBAL STATE FUNCTIONS ------------------------------ */ /* ALL STATES HAVE THE FOLLOWING FORM: void do_state_function() { // Entry function if ( global_state_changed == true ) { global_state_changed = false; // More functions } // Do stuff until end condition is met doStuff(); // State transition guard if ( endCondition == true ) { global_curr_state = next_state; global_state_changed = true; // More functions } } */ // FAILURE MODE void do_global_failure() { // Entry function if ( global_state_changed == true ) { global_state_changed = false; failure_mode = true; // Set failure mode } // Do stuff until end condition is met // State transition guard if ( false ) { // Never move to other state global_curr_state = global_wait; global_state_changed = true; } } // DEMO MODE void do_global_demo() { // Entry function if ( global_state_changed == true ) { global_state_changed = false; // More functions } // Do stuff until end condition is met doStuff(); // State transition guard if ( switch2_pressed == true ) { switch2_pressed = false; global_curr_state = global_wait; global_state_changed = true; } } // WAIT MODE void do_global_wait() { // Entry function if ( global_state_changed == true ) { global_state_changed = false; } // Do nothing until end condition is met // State transition guard if ( switch2_pressed == true ) { // DEMO MODE switch2_pressed = false; global_curr_state = global_demo; global_state_changed = true; } else if ( button1_pressed == true ) { // EMG CALIBRATION button1_pressed = false; global_curr_state = global_cal_emg; global_state_changed = true; } else if ( button2_pressed == true ) { // MOTOR CALIBRATION button2_pressed = false; global_curr_state = global_cal_motor; global_state_changed = true; } } // EMG CALIBRATION MODE void do_global_cal_emg() { // Entry function if ( global_state_changed == true ) { global_state_changed = false; } // Do stuff until end condition is met doStuff(); // State transition guard if ( cal_motor_done == true ) { // OPERATION MODE cal_emg_done = true; global_curr_state = global_operation; global_state_changed = true; } else if ( button1_pressed == true ) { // WAIT MODE button1_pressed = false; cal_emg_done = true; global_curr_state = global_wait; global_state_changed = true; } } // MOTOR CALIBRATION MODE void do_global_cal_motor() { // Entry function if ( global_state_changed == true ) { global_state_changed = false; } // Do stuff until end condition is met doStuff(); // State transition guard if ( cal_emg_done == true ) { // OPERATION MODE cal_motor_done = true; global_curr_state = global_operation; global_state_changed = true; } else if ( button2_pressed == true ) { // WAIT MODE button2_pressed = false; cal_motor_done = true; global_curr_state = global_wait; global_state_changed = true; } } // OPERATION MODE void do_global_operation() { // Entry function if ( global_state_changed == true ) { global_state_changed = false; } // Do stuff until end condition is met doStuff(); // State transition guard if ( false ) { // Always stay in operation mode (can be changed) global_curr_state = global_wait; global_state_changed = true; } } /* ------------------------------ GLOBAL STATE MACHINE ------------------------------ */ void global_state_machine() { switch(global_curr_state) { case global_failure: do_global_failure(); break; case global_wait: do_global_wait(); break; case global_cal_emg: do_global_cal_emg(); break; case global_cal_motor: do_global_cal_motor(); break; case global_operation: do_global_operation(); break; case global_demo: do_global_demo(); break; } } /* ------------------------------ GLOBAL PROGRAM LOOP ------------------------------ */ void tickGlobalFunc() { // sampleSignalsAndInputs(); global_state_machine(); // controller(); // outputToMotors(); } /* ------------------------------ MAIN FUNCTION ------------------------------ */ void main() { pc.baud(115200); // MODSERIAL rate pc.printf("Starting\r\n"); global_curr_state = global_wait; // Start off in EMG Wait state tickGlobal.attach( &tickGlobalFunc, Ts ); // Start global ticker button1.fall( &button1Press ); button2.fall( &button2Press ); switch2.fall( &switch2Press ); switch3.fall( &switch3Press ); while(true) { pc.printf("Global state: %i \r\n", global_curr_state); wait(0.5f); } }