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:
- PatrickZieverink
- Date:
- 2019-10-04
- Revision:
- 7:78bc59c7753c
- Parent:
- 6:354a6509405f
- Child:
- 8:6f6a4dc12036
File content as of revision 7:78bc59c7753c:
#include "mbed.h"
#include "MODSERIAL.h"
#include "FastPWM.h"
#include "QEI.h"
Serial 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
DigitalOut led1(D9); // led op D9 aanzetten
QEI encoder (D11, D12, NC, 8400, QEI::X4_ENCODING); //encoder gebruiken
//variables
enum States {idle, cw, ccw, end, failure};
States current_state; //wat gebeurd hier?
class motor_state { // je maakt hier motor_state?
public: //wat gebeurd hier?
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 == true) { //this moves the program from the idle to cw state
current_state = cw;
led1 = 0;
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
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;
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: // hoezo de :?
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;
//motor.dir2 = 1; //todo: check if this is actually clockwise
//motor.pwm2 = 0.5f;
button.fall(&button_interrupt);
loop_ticker.attach(&main_loop, 0.1f); //main loop at 1kHz
pc.printf("Iickerloop finished \n \r");
while (true) {}
}