Code

Dependencies:   mbed QEI MPU6050 TextLCD

main.cpp

Committer:
belsarekaiwalya
Date:
2017-03-22
Revision:
3:86145fd89b45
Parent:
2:156f4732fbf1
Child:
4:b084af72f9a6

File content as of revision 3:86145fd89b45:

#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(PA_0,PA_1,PA_4,PB_0,PC_1,PC_0); // rs, e, d4-d7
Serial serial(USBTX, USBRX);
Serial bt(PC_6,PC_7);//tx,rx
DigitalOut dirRa(PB_3);
DigitalOut dirRb(PB_5);
DigitalOut dirLa(PB_4);
DigitalOut dirLb(PB_10);
PwmOut pwmL(PA_8);//left
PwmOut pwmR(PA_9);//right 

void InitSerial();
void front(float xpwmL,float xpwmR);
void back(float xpwm,float xpwmR);
void left(float xpwm, float xpwmR);
void right(float xpwm,float xpwmR);
void clockWise(float xpwm,float xpwmR);
void antiClock(float xpwm,float xpwmR);
void Brake();

int ch;

void InitSerial()
{
    bt.baud(9600);
    serial.baud(9600);
}
void SetPwmf_kHz(float freq)
{
    
    pwmL.period_ms(freq); 
    pwmR.period_ms(freq); 
    
}   
int main()
{
    InitSerial();
    SetPwmf_kHz(1);
    Brake();
    while(1) 
    {
        if(bt.readable()) 
        {
         ch =  bt.getc();
         lcd.cls();  
         switch(ch)
            {
              case 'w'://Front
                   front(0.5,0.5);
                   serial.printf("Forward\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Forward\n");
                   break;
                  
              case 'a'://Left
                   left(0,0.5);
                   serial.printf("Left\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Left\n");
                   break; 
                  
              case 's'://Back
                   back(0.5,0.5);
                   serial.printf("Back\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Back\n");
                   break; 
                  
              case 'd'://Right
                   right(0.5,0);
                   serial.printf("Right\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Right\n");
                   break;
                  
              case 'O'://Stop
                   Brake();
                   serial.printf("STOP\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("STOP\n");
                   break;
                  
              case 'A'://Anticlock
                   antiClock(0.5,0.5);
                   serial.printf("AntiClock\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("AntiClock\n");
                   break; 
                  
              case 'C'://Clock
                   clockWise(0.5,0.5);
                   serial.printf("Clock\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Clock\n");
                   break;
              
              default:
                   Brake();
                   serial.printf("Brake");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Brake");
            }    
        }
    }
}
void front(float xpwmL,float xpwmR)
{  
    dirLa = 1;
    dirLb = 0;
    pwmL= xpwmL;
    
    dirRa = 1;
    dirRb = 0;
    pwmR= xpwmR; 
}
void back(float xpwmL,float xpwmR)
{
    dirLa = 0;
    dirLb = 1;
    pwmL= xpwmL;
    
    dirRa = 0;
    dirRb = 1;
    pwmR= xpwmR;
}
void left(float xpwmL,float xpwmR)
{
    dirLa = 0;
    dirLb = 0;
    pwmL= xpwmL;
    
    dirRa = 1;
    dirRb = 0;
    pwmR= xpwmR; 
}
void right(float xpwmL,float xpwmR)
{
    dirLa = 1;
    dirLb = 0;
    pwmL = xpwmL;
    
    dirRa = 0;
    dirRb = 0;
    pwmR = xpwmR; 
}
void Brake()
{
    dirRa = 0;
    dirRb = 0;
    dirLa = 0;
    dirLb = 0;
    pwmL=0.0;
    pwmR=0.0; 
}
void antiClock(float xpwmL,float xpwmR)
{
    dirRa = 1;            
    dirRb = 0;   
    dirLa = 0;
    dirLb = 1;
    pwmL = xpwmL;
    pwmR = xpwmR;  
}
void clockWise(float xpwmL,float xpwmR)
{
    dirRa = 0;
    dirRb = 1;
    dirLa = 1;
    dirLb = 0;
    pwmL = xpwmL;
    pwmR = xpwmR;
}