code for basic movement of robot

Dependencies:   MODSERIAL QEI mbed

main.cpp

Committer:
Technical_Muffin
Date:
2015-10-15
Revision:
10:2dd707673b3b
Parent:
9:80dad8d81c33
Child:
11:be7660614c5c

File content as of revision 10:2dd707673b3b:

#include "mbed.h"
#include "QEI.h"
#include "MODSERIAL.h"
//#include <"math.h">

QEI motor1(D13,D12,NC, 624);//encoder for motor 1
QEI motor2(D11,D10,NC, 624);//encoder for motor 2
MODSERIAL pc(USBTX,USBRX);
DigitalOut direction1(D7);//direction input for motor 1
DigitalOut direction2(D4);//direction input for motor 2
PwmOut speed1(D6);//speed input for motor 1
PwmOut speed2(D5);//speed input for motor 2
DigitalIn button1(PTC6);//test button for starting motor 1
DigitalIn button2(PTA4);//test button for starting motor 2
DigitalOut led1(LED_RED);
DigitalOut led2(LED_BLUE);
DigitalOut led3(LED_GREEN);

int main()
{
   float cycle = 0.7f;//define the speed of the motor
   bool motor1_on = 1;//set the on variable of motor 1
   int motor1_dir=0;//set the direction of motor 1
   bool motor2_on =1;//set the on variable of motor 2
   int motor2_dir = 0;//set the direction of motor 1
   
while(1){
       led3.write(0);
       led1.write(1);
       led2.write(1);
       speed1.write(0);//set motor 1 speed to 0 
       speed2.write(0);//set motor 2 speed to 0
       pc.baud(115200);
       
        int diffa1 = button1.read();//read out the button 1 signal and calculate if it is being pressed or released
        wait(0.2);//from this we can determine if the rotation direction needs to be reversed.
        int diffb1 = button1.read();
        int button_toggle1 = diffa1-diffb1;
        if(button_toggle1 == 1 && motor1_dir == 1){
                motor1_dir = 0;
                }
        else if(button_toggle1 == 1 && motor1_dir == 0){
                motor1_dir = 1;
                }       
   while(button1.read() != motor1_on){// turn on motor 1 when the button is being pressed
       led3.write(1);
       led1.write(0);
       speed1.write(cycle);//write the speed to the motor  
      // pc.printf("%f",speed1.read());//klopt nog niet, maar voorlopig zorgt het uitlezen ervoor dat het werkt.
       direction1.write(motor1_dir);//turn motor CCW or CW
       wait_us(3000);
   //motor CW = 0
   //motor CCW = 1
       }
       
       int diffa2 = button2.read();//read out the button 2 signal and calculate if it is being pressed or released
       wait(0.2);//from this we can determine if the rotation direction needs to be reversed.
       int diffb2 = button2.read();
       int button_toggle2 = diffa2-diffb2;
       if(button_toggle2 == 1 && motor2_dir == 1){
                motor2_dir = 0;
                }
        else if(button_toggle2 == 1 && motor2_dir == 0){
                motor2_dir = 1;
                }
    while(button2.read() != motor2_on){// turn on motor 2 when the button is being pressed
       led3.write(1);
       led2.write(0);
       speed2.write(cycle);//write the speed to the motor  
       pc.printf("%f",speed2.read());//klopt nog niet, maar voorlopig zorgt het uitlezen ervoor dat het werkt.
       direction2.write(motor2_dir);//turn motor CCW or CW
   //motor CW = 0
   //motor CCW = 1
       }                   
}
}