Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- fionalin
- Date:
- 2022-06-28
- Revision:
- 0:4e3ad938564e
- Child:
- 1:40e5ac1119a6
File content as of revision 0:4e3ad938564e:
/** * This is an example of an FSM controlling a motor. */ #include <mbed.h> #include "stepper.h" #include "capsense.h" #include "dis_exp.h" const PinName MOTOR_ENABLE = D1; const PinName MOTOR_STEP = D2; const PinName MOTOR_DIR = D3; const PinName MOTOR_MS1 = D4; const PinName MOTOR_MS2 = D5; const PinName MOTOR_MS3 = D6; const PinName CS_MEASURE = D7; const PinName CS_SQUARE = D8; const int32_t N_MEASURE = 20; const uint32_t T_MEASURE_US = 400; //currently not in use const int32_t DELTA_STEPS = 200; const int32_t MAX_STEPS = 1000; enum State { STOPPED, UP, DOWN, DISCRETE, CONTINUOUS }; int main(void) { State curState = State::STOPPED; BufferedSerial pc(USBRX, USBTX); pc.set_baud(9600); StepperMotor motor(MOTOR_ENABLE, MOTOR_STEP, MOTOR_DIR, MOTOR_MS1, MOTOR_MS2, MOTOR_MS3); CapSense cap_sense(CS_MEASURE, CS_SQUARE); DiscreteExperiment discrete_exp(motor, cap_sense, N_MEASURE, T_MEASURE_US, DELTA_STEPS, MAX_STEPS); // ContinuousExperiment continuous_exp(motor, cap_sense); while (true) { // State behavior switch (curState) { case State::STOPPED: break; case State::UP: /* move motor up */ motor.step_positive(); break; case State::DOWN: /* move motor down */ motor.step_negative(); break; case State::DISCRETE: /* discrete state behavior */ discrete_exp.proceed(); if (discrete_exp.is_done()) { discrete_exp.report(); } break; case State::CONTINUOUS: /*continuous state behavior */ // continuous_exp.proceed(); // if (continuous_exp.is_done()) { // continuous_exp.report(); // } break; default: printf("[ERROR, %d] Not a state!\n", __LINE__); break; } // Switch to next state char input; ssize_t len = pc.read(&input, 1); if (len > 0) { switch (input) { case '1': curState = State::UP; break; case '2': curState = State::DOWN; break; case '3': curState = State::DISCRETE; discrete_exp.reset(); discrete_exp.start(); break; case '4': curState = State::CONTINUOUS; // continuous_exp.reset(); // continuous_exp.start(); break; case '0': if (curState == DISCRETE) { discrete_exp.report(); discrete_exp.reset(); } else if (curState == CONTINUOUS) { // continuous_exp.stop(); // continuous_exp.report(); } curState = State::STOPPED; break; default: printf("[ERROR, %d] Not a command!\n", __LINE__); break; } } } return 0; }