Code

Dependencies:   mbed QEI MPU6050 TextLCD

main.cpp

Committer:
belsarekaiwalya
Date:
2017-03-22
Revision:
4:b084af72f9a6
Parent:
3:86145fd89b45
Child:
5:84e63108a1b6

File content as of revision 4:b084af72f9a6:

#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 runMotor(char mot, bool dir, float pwm);
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
                   runMotor('L', 1, 0.5);
                   runMotor('R', 1, 0.5);
                   serial.printf("Forward\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Forward\n");
                   break;
                  
              case 'a'://Left
                   runMotor('L', 1, 0);
                   runMotor('R', 1, 0.5);
                   serial.printf("Left\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Left\n");
                   break; 
                  
              case 's'://Back
                   runMotor('L', 0, 0.5);
                   runMotor('R', 0, 0.5);
                   serial.printf("Back\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Back\n");
                   break; 
                  
              case 'd'://Right
                   runMotor('L', 1, 0.5);
                   runMotor('R', 1, 0);
                   serial.printf("Right\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Right\n");
                   break;
                  
              case 'A'://Anticlock
                   runMotor('L', 0, 0.5);
                   runMotor('R', 1, 0.5);
                   serial.printf("AntiClock\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("AntiClock\n");
                   break; 
                  
              case 'C'://Clock
                   runMotor('L', 1, 0.5);
                   runMotor('R', 0, 0.5);
                   serial.printf("Clock\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Clock\n");
                   break;
          
              case 'O'://Stop
                   Brake();
                   serial.printf("STOP\n");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("STOP\n");
                   break;
        
              default:
                   Brake();
                   serial.printf("Brake");
                   lcd.cls();
                   lcd.locate(1,0);
                   lcd.printf("Brake");
            }    
        }
    }
}
void runMotor(char mot, bool dir, float pwm)
{
    switch(mot)
    {
     case 'L':
      if(dir)
        {
         dirLa = 1;
         dirLb = 0;
        }
      else
        {
         dirLa = 0;
         dirLb = 1;
        }
      pwmL= pwm;
      break;
     
     case 'R':
       if(dir)
        {
         dirRa = 1;
         dirRb = 0;
        }
      else
        {
         dirRa = 0;
         dirRb = 1;
        }
      pwmR= pwm;
      break;
      
      default: 
      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()
{
    dirLa = 0;
    dirLb = 0;
    pwmL=0.0;
    
    dirRa = 0;
    dirRb = 0;
    pwmR=0.0; 
}
void antiClock(float xpwmL,float xpwmR)
{
    dirLa = 0;
    dirLb = 1;
    pwmL = xpwmL;
    
    dirRa = 1;            
    dirRb = 0;   
    pwmR = xpwmR;  
}
void clockWise(float xpwmL,float xpwmR)
{
    dirLa = 1;
    dirLb = 0;
    pwmL = xpwmL;
    
    dirRa = 0;
    dirRb = 1;
    pwmR = xpwmR;
}