Code

Dependencies:   mbed QEI MPU6050 TextLCD

Revision:
1:59c416ccba42
Parent:
0:c9c8c027609e
Child:
2:156f4732fbf1
--- a/main.cpp	Sat Mar 11 09:43:33 2017 +0000
+++ b/main.cpp	Tue Mar 14 15:31:21 2017 +0000
@@ -1,11 +1,30 @@
-/*#include "mbed.h"
-DigitalOut myled(LED1);
+#include "mbed.h"
+/*DigitalOut myled(LED1);
+DigitalOut m2dir1(PB_4);
+DigitalOut m2dir2(PB_10);
+DigitalOut m1dir1(PB_3);
+DigitalOut m1dir2(PB_5);
+PwmOut pwm1(PA_8);
+PwmOut pwm2(PA_9);
 
 int main() {
+    
+        pwm1.period_ms(1);
+        pwm1 = 1;
+    
+        pwm2.period_ms(1);
+        pwm2 = 1;
+    
     while(1) 
     {
-        lcd.locate(1,0);
-        lcd.printf("Hello\n");
+        m2dir1 = 1;
+        m2dir2 = 0;
+        m1dir1 = 1;
+        m1dir2 = 0;
+  
+  
+        //lcd.locate(1,0);
+        //lcd.printf("Hello\n");
         //myled = 1; // LED is ON
         //wait(0.2); // 200 ms
         //myled = 0; // LED is OFF
@@ -17,12 +36,12 @@
 #include "mbed.h"
 #include "TextLCD.h"
 TextLCD lcd(PA_0,PA_1,PA_4,PB_0,PC_1,PC_0); // rs, e, d4-d7
-DigitalOut m1dir1(PB_3);
-DigitalOut m1dir2(PB_5);
-DigitalOut m2dir1(PB_4);
-DigitalOut m2dir2(PB_10);
-PwmOut pwm1(PA_8);
-PwmOut pwm2(PA_9); 
+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 
 
 Serial bt(PC_6,PC_7);//tx,rx
 PwmOut mypwm(LED1);
@@ -34,6 +53,7 @@
 {
     bt.baud(9600);
     serial.baud(9600);
+    void brake();
     while(1) 
     {
         if(bt.readable()) 
@@ -42,23 +62,50 @@
          lcd.cls();  
          switch(ch)
             {
-              case 'w':
+              case 'w'://Front
+              void front();
+              dirRa = 1;
+              dirRb = 0;
+              dirLa = 1;
+              dirLb = 0;
+              pwmL.period_ms(1); 
+              pwmL=0.5;
+              pwmR.period_ms(1); 
+              pwmR=0.5; 
+    
               lcd.cls();
               serial.printf("Forward\n");
               lcd.locate(1,0);
               lcd.printf("Forward\n");
-              void front();
               break;
               
-              case 'a':
-              lcd.cls();
-              serial.printf("Left\n");
-              lcd.locate(1,0);
-              lcd.printf("Left\n");
-              void left();
+              case 'a'://Left
+               dirRa = 1;
+               dirRb = 0;
+               dirLa = 0;
+               dirLb = 0;
+               pwmL.period_ms(1); 
+               pwmL=0;
+               pwmR.period_ms(1); 
+               pwmR=0.5; 
+   
+               lcd.cls();
+               serial.printf("Left\n");
+               lcd.locate(1,0);
+               lcd.printf("Left\n");
+               //void left();
               break; 
               
-              case 's':
+              case 's'://Back
+              dirRa = 0;
+              dirRb = 1;
+              dirLa = 0;
+              dirLb = 1;
+              pwmL.period_ms(1); 
+              pwmL=0.5;
+              pwmR.period_ms(1); 
+              pwmR=0.5;
+    
               lcd.cls();
               serial.printf("Back\n");
               lcd.locate(1,0);
@@ -66,7 +113,16 @@
               void back();
               break; 
               
-              case 'd':
+              case 'd'://Right
+               dirRa = 0;
+               dirRb = 0;
+               dirLa = 1;
+               dirLb = 0;
+               pwmL.period_ms(1); 
+               pwmL=0.5f;
+               pwmR.period_ms(1); 
+               pwmR=0; 
+   
               lcd.cls();
               serial.printf("Right\n");
               lcd.locate(1,0);
@@ -74,6 +130,59 @@
               void right();
               break;
               
+              case 'O'://Stop
+              dirRa = 1;
+              dirRb = 0;
+              dirLa = 1;
+              dirLb = 0;
+              pwmL.period_ms(1); 
+              pwmL=0;
+              pwmR.period_ms(1); 
+              pwmR=0; 
+    
+              lcd.cls();
+              serial.printf("STOP\n");
+              lcd.locate(1,0);
+              lcd.printf("STOP\n");
+              void Brake();
+              break;
+              
+              case 'A'://Anticlock
+               dirRa = 1;
+               dirRb = 0;
+               dirLa = 0;
+               dirLb = 1;
+               pwmL.period_ms(1); 
+               pwmL=0.5;
+               pwmR.period_ms(1); 
+               pwmR=0.5; 
+   
+               lcd.cls();
+               serial.printf("AntiClock\n");
+               lcd.locate(1,0);
+               lcd.printf("AntiClock\n");
+               break; 
+              
+              case 'C'://Clock
+               dirRa = 0;
+               dirRb = 1;
+               dirLa = 1;
+               dirLb = 0;
+               pwmL.period_ms(1); 
+               pwmL=0.5f;
+               pwmR.period_ms(1); 
+               pwmR=0.5f;
+   
+              lcd.cls();
+              serial.printf("Clock\n");
+              lcd.locate(1,0);
+              lcd.printf("Clock\n");
+              break;
+              
+              
+              
+              
+              
               default:
               serial.printf("Brake");
               lcd.cls();
@@ -90,61 +199,62 @@
 }
 void front()
 {
-    m1dir1 = 1;
-    m1dir2 = 0;
-    m2dir1 = 1;
-    m2dir2 = 0;
-    pwm1.period(0.001f); 
-    pwm1=0.5;
-    pwm2.period(0.001f); 
-    pwm2=0.5; 
+    
+    dirRa = 1;
+    dirRb = 0;
+    dirLa = 1;
+    dirLb = 0;
+    pwmL.period_ms(1); 
+    pwmL=0.5;
+    pwmR.period_ms(1); 
+    pwmR=0.5; 
     
 }
 void brake()
 {
-    m1dir1 = 1;
-    m1dir2 = 0;
-    m2dir1 = 1;
-    m2dir2 = 0;
-    pwm1.period(0.001f); 
-    pwm1=0.0f;
-    pwm2.period(0.001f); 
-    pwm2=0.0f; 
+    dirRa = 0;
+    dirRb = 0;
+    dirLa = 0;
+    dirLb = 0;
+    pwmL.period_ms(1); 
+    pwmL=0.0;
+    pwmR.period_ms(1); 
+    pwmR=0.0; 
     
 }
 void back()
 {
-    m1dir1 = 0;
-    m1dir2 = 1;
-    m2dir1 = 0;
-    m2dir2 = 1;
-    pwm1.period(0.001f); 
-    pwm1=0.5;
-    pwm2.period(0.001f); 
-    pwm2=0.5;
+    dirRa = 0;
+    dirRb = 1;
+    dirLa = 0;
+    dirLb = 1;
+    pwmL.period_ms(1); 
+    pwmL=0.5;
+    pwmR.period_ms(1); 
+    pwmR=0.5;
     
 }
 void left()
 {
-    m1dir1 = 0;
-    m1dir2 = 1;
-    m2dir1 = 0;
-    m2dir2 = 0;
-    pwm1.period(0.001f); 
-    pwm1=0.5f;
-    pwm2.period(0.001f); 
-    pwm2=0.0f; 
+    dirRa = 0;
+    dirRb = 1;
+    dirLa = 0;
+    dirLb = 0;
+    pwmL.period_ms(1); 
+    pwmL=0.5;
+    pwmR.period_ms(1); 
+    pwmR=0.0; 
     
 }
 void right()
 {
-    m1dir1 = 0;
-    m1dir2 = 0;
-    m2dir1 = 0;
-    m2dir2 = 1;
-    pwm1.period(0.001f); 
-    pwm1=0.0f;
-    pwm2.period(0.001f); 
-    pwm2=0.5; 
+    dirRa = 0;
+    dirRb = 0;
+    dirLa = 0;
+    dirLb = 1;
+    pwmL.period_ms(1); 
+    pwmL=0.0f;
+    pwmR.period_ms(1); 
+    pwmR=0.5f; 
     
 }