Robot Bachelor
/
FinalMotorControl
Engine control program with 3 engines
Fork of MotorControl by
main.cpp
- Committer:
- mjhaugsdal
- Date:
- 2016-04-20
- Revision:
- 6:ac5d277bd497
- Parent:
- 5:fdc7a779d525
- Child:
- 7:ba0caeb296bb
File content as of revision 6:ac5d277bd497:
#include "mbed.h" #include "rtos.h" Serial pc(USBTX, USBRX); // tx, rx //Analog Pins //First engine DigitalOut IN1(A0); DigitalOut IN2(A1); DigitalOut IN3(A2); DigitalOut IN4(A3); //Second engine DigitalOut IN5(D3); DigitalOut IN6(D4); DigitalOut IN7(D5); DigitalOut IN8(D6); static int fart = 1000; static char m_cmd = 'x'; void step4Right() { //engine 1 IN1=0; IN2=1; IN3=0; IN4=1; //engine 2 IN5=0; IN6=1; IN7=0; IN8=1; wait_us(fart); //legg som global variabel "fart" //engine 1 IN1=0; IN2=1; IN3=1; IN4=0; //engine 2 IN5=0; IN6=1; IN7=1; IN8=0; wait_us(fart); //engine 1 IN1=1; IN2=0; IN3=1; IN4=0; //engine 2 IN5=1; IN6=0; IN7=1; IN8=0; wait_us(fart); //engine 1 IN1=1; IN2=0; IN3=0; IN4=1; //engine 2 IN5=1; IN6=0; IN7=0; IN8=1; wait_us(fart); } void step4Left() { //engine 1 IN1=1; IN2=0; IN3=0; IN4=1; //engine 2 IN5=1; IN6=0; IN7=0; IN8=1; wait_us(fart); //engine 1 IN1=1; IN2=0; IN3=1; IN4=0; //engine 2 IN5=1; IN6=0; IN7=1; IN8=0; wait_us(fart); //engine 1 IN1=0; IN2=1; IN3=1; IN4=0; //engine 2 IN5=0; IN6=1; IN7=1; IN8=0; wait_us(fart); //engine 1 IN1=0; IN2=1; IN3=0; IN4=1; //engine 2 IN5=0; IN6=1; IN7=0; IN8=1; wait_us(fart); } void input(void const *args) { while(true) { if(pc.readable()) { m_cmd=pc.getc(); } Thread::wait(10); //pc.printf("%d", steps); } } int main() { //Thread 1 has constant feed from usb Thread t1(input); static char global_direction; printf("Started"); while (true) { //Thread::wait(5); //0 STEPS LEFT AT MAX SPEED if (m_cmd == '0') { fart = 1000; //global_direction = '0'; step4Left(); //steps = steps + step; //printf("LEFT \n"); //printf("%c \n" + global_direction); } //1 STEPS RIGHT AT MAX SPEED else if (m_cmd == '1') { fart = 1000; //global_direction = '1'; step4Right(); } else { //global_direction = 'x'; } if (m_cmd == 'a') { fart = 2000; //global_direction = '1'; step4Right(); //steps = steps + step; // printf("%i", &steps); } else if (m_cmd == 'c') { fart = 2000; //global_direction = '0'; step4Left(); //steps = steps + step; // printf("%i", &steps); } else if (m_cmd == 'k') { int steps = 242; //Steps = 242 gives a 60' rotation to the left. while (steps >= 0) { step4Left(); steps--; } m_cmd = 'x'; } else if (m_cmd == 'l') { int steps = 242; //Steps = 242 gives a 60' rotation to the right. while (steps >= 0) { step4Right(); steps--; } m_cmd = 'x'; } } } //END Main