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.
Dependencies: mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
main.cpp
- Committer:
- joostbonekamp
- Date:
- 2019-10-03
- Revision:
- 6:354a6509405f
- Parent:
- 5:aa8b5d5e632f
- Child:
- 7:78bc59c7753c
File content as of revision 6:354a6509405f:
#include "mbed.h"
#include "MODSERIAL.h"
#include "FastPWM.h"
#include "QEI.h"
MODSERIAL pc(USBTX, USBRX); //verbinden met pc
DigitalOut motor2_direction(D4); //verbinden met motor 2 op board (altijd d4)
FastPWM motor2_pwm(D5); //verbinden met motor 2 pwm (altijd d5)
Ticker loop_ticker; //used in main()
InterruptIn button(D10); //knop op birorobotics shield
QEI encoder (D11, D12, NC, 8400, QEI::X4_ENCODING); //encoder gebruiken
//variables
enum States {idle, cw, ccw, end, failure};
States current_state;
class motor_state {
public:
float pwm1; //pwm of 1st motor
float pwm2; //pwm of 2nd motor
int dir1; //direction of 1st motor
int dir2; //direction of 2nd motor
};
motor_state motor;
bool state_changed = false;
volatile bool button_pressed = false;
void measure_signals() {return;}
void do_nothing() {
if (button_pressed) { //this moves the program from the idle to cw state
current_state = cw;
state_changed = true; //to show next state it can initialize
pc.printf("Changed state from idle to cw\r\n");
button_pressed = false; //reset button
}
}
void rotate_cw() {
if (state_changed) {
pc.printf("State changed to CW\r\n");
state_changed = false; //reset this so it wont print next loop
}
motor.dir2 = 1; //todo: check if this is actually clockwise
motor.pwm2 = 0.5f;
if (!state_changed && button_pressed) { //state wasnt just changed, button has been pressed -> change state
current_state = ccw;
state_changed = true;
button_pressed = false; //reset this
}
}
void rotate_ccw() {
//similar to rotate_cw()
if (state_changed) {
pc.printf("State changed to CCW\r\n");
state_changed = false;
}
motor.dir2 = 0;
motor.pwm2 = 0.5f;
if (!state_changed && button_pressed) { //state niet veranderd, button gepressd -> state verandert
current_state = cw;
state_changed = true;
button_pressed = false;
}
}
void motor_controller() {return;}
void output() {return;}
void button_interrupt () {
button_pressed = true;
}
void state_machine() {
//run current state
switch (current_state) {
case idle:
do_nothing();
break;
case cw:
rotate_cw();
break;
case ccw:
rotate_ccw();
break;
case end:
break;
case failure:
break;
}
}
void main_loop() {
measure_signals();
state_machine();
motor_controller();
output();
}
int main() {
pc.baud(115200);
pc.printf("Executing main()\r\n");
current_state = idle;
motor.pwm1 = 0;
motor.pwm2 = 0;
motor.dir1 = 0;
motor.dir2 = 0;
button.fall(&button_interrupt);
loop_ticker.attach(&main_loop, 0.001f); //main loop at 1kHz
while (true) {}
}