UNFINISHED

Dependencies:   HCSR04 SRF05 mbed

Files at this revision

API Documentation at this revision

Comitter:
Charlie_He
Date:
Fri Jun 08 22:22:10 2018 +0000
Parent:
0:4ca3e247b86a
Commit message:
TDPS; ; ;

Changed in this revision

Distance.h Show annotated file Show diff for this revision Revisions of this file
colorsensor.cpp Show annotated file Show diff for this revision Revisions of this file
colorsensor.h Show annotated file Show diff for this revision Revisions of this file
gyro.cpp Show annotated file Show diff for this revision Revisions of this file
gyro.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
motor.cpp Show annotated file Show diff for this revision Revisions of this file
motor.h Show annotated file Show diff for this revision Revisions of this file
servogo.h Show annotated file Show diff for this revision Revisions of this file
--- a/Distance.h	Fri Jun 08 12:29:16 2018 +0000
+++ b/Distance.h	Fri Jun 08 22:22:10 2018 +0000
@@ -5,42 +5,69 @@
 SRF05 Usound1(p26,p15);
 HCSR04 Usound2(p25,p16);
 Ticker t1;
+
+extern Motor M1;
+extern Motor M2;
+
 int n=0;
+int turns=0;
+void enable1();
+void enable2();
+void turnR()
+{
+    M1=1;M2=-1; //右转90
+    wait(0.75);   
+    }
+void turnL()
+{
+    M1=-1;M2=1; //左转90
+    wait(0.75);
+    }
 void distancedetectL()
 {
     float u= Usound1.read();
     if(7<=u<=20)  {
         n+=1;
-        if (n>=3)
-        {
-            //turnL();
+        if (n>=3) {
+            turnL();
             change(90);
-            //driveit();
+            driveit();
             n=0;
-            }
+            enable2();
         }
     }
+}
 void distancedetectR()
 {
     float u= Usound2.read_cm();
-    if(7<=u<=20)  {
+    if(30<=u)  {
         n+=1;
-        if (n>=3)
-        {
-            //turnL();
-            change(90);
-            //driveit();
-            n=0;
+        if (n>=3) {
+            turns+=1;
+            turnL();
+            if(turns>=3) {
+                t1.detach();
+                M1=0.5;M2=0.5;
+                wait(3);
+                M1=0;M2=0;
+            } else {
+                turnR();
+                change(-90);
+                driveit();
+                n=0;
+                enable1();
             }
         }
     }
+}
 void enable1()
 {
-   t1.detach();
-   t1.attach(&distancedetectR,0.4);
-    }  
+   M1=0;M2=0;
+   /* t1.detach();
+    t1.attach(&distancedetectR,0.4);*/
+}
 void enable2()
 {
-   t1.detach();
-   t1.attach(&distancedetectL,0.4);
-    }    
\ No newline at end of file
+    t1.detach();
+    t1.attach(&distancedetectL,0.4);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/colorsensor.cpp	Fri Jun 08 22:22:10 2018 +0000
@@ -0,0 +1,69 @@
+#include "mbed.h"
+#include "gyro.h"
+#include "Distance.h"
+
+Timer ccc;
+
+void blue()
+{
+    M1=-1;M2=1;//左边45度
+    wait(0.4);
+    M1=0;M2=0;
+    change(135);
+    driveit();
+    ccc.start();
+    while(1)
+    {
+        if(ccc.read()>15)
+        {
+            M1=1;M2=-1;//右边135度
+            wait(1.25);
+            M1=0;M2=0;
+            change(-135);
+            driveit();
+            enable1();
+            break;
+            }
+        }
+    }
+void red()
+{   
+     M1=1;M2=-1;//右边45度
+    wait(0.4);
+    M1=0;M2=0;
+    change(45);
+    driveit();
+    ccc.start();
+    while(1)
+    {
+        if(ccc.read()>15)
+        {
+            M1=1;M2=-1;//右边45度
+            wait(0.4);
+            M1=0;M2=0;
+            change(-45);
+            driveit();
+            enable1();
+            break;
+            }
+        }
+    }
+void green()
+{
+    change(90);
+    driveit();
+    ccc.start();
+    while(1)
+    {
+        if(ccc.read()>20)
+        {
+            M1=1;M2=-1; //右转90
+            wait(0.75);
+            change(-90);
+            driveit();
+            enable1();
+            break;
+            }
+        }
+    }
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/colorsensor.h	Fri Jun 08 22:22:10 2018 +0000
@@ -0,0 +1,17 @@
+#include "mbed.h"
+#include "motor.h"
+
+extern Serial Rasp;
+extern Motor M1;
+extern Motor M2;
+void blue();
+void green();
+void red();
+void colorsensor()
+{
+    char temp;
+    temp=Rasp.getc();
+    if (temp=='0'){red();}
+    else if (temp=='1') {green();}
+    else if (temp=='2') {blue();}
+}
\ No newline at end of file
--- a/gyro.cpp	Fri Jun 08 12:29:16 2018 +0000
+++ b/gyro.cpp	Fri Jun 08 22:22:10 2018 +0000
@@ -1,14 +1,12 @@
 #include "gyro.h"
-//#include "motor.h"
+
 #define IMU_TX_PINMAP p28
 #define IMU_RX_PINMAP p27
 Serial device(IMU_TX_PINMAP, IMU_RX_PINMAP);  // tx, rx
 Ticker ddrive;
-Timer gotime;
+
 int started = false;
 Serial pc(USBTX,USBRX);
-Motor M1(p19,p20,p21);
-Motor M2(p17,p18,p22);
 unsigned char Re_buf[11],counter=0;
 unsigned char sign=0;
 int nnn=0;
@@ -81,27 +79,12 @@
 {
     Standang += up;
     }
-void turnL()
+
+void driveit()
 {
-    M1=-0.5;M2=0.5;
-    wait(1);
-    }
-void turnR()
-{
-    M1=0.5;M2=-0.5;
-    wait(1);
+    ddrive.attach(&drive,0.5);
     }
-
-void driveit(float t)
-{
-        ddrive.attach(&drive,0.5);
-        if (gotime.read()>t)
-        {
-            ddrive.detach();
-            gotime.reset();
-            }
-    }
-    
+ 
 void init_gyro(){
    device.baud(115200);
    device.attach(&deviceEvent, device.RxIrq);
--- a/gyro.h	Fri Jun 08 12:29:16 2018 +0000
+++ b/gyro.h	Fri Jun 08 22:22:10 2018 +0000
@@ -3,17 +3,15 @@
 #include "mbed.h"
 #include "motor.h"
 
+extern Motor M1;
+extern Motor M2;
+
 extern int started;
 extern float yaw;
-extern Serial pc;
-extern Timer gotime;
-//extern Motor M1;
-//extern Motor M2;
 
 void init_gyro();
-void driveit(float t);
+void driveit();
 void change(int up);
-void turnL();
-void turnR();
+
 
 #endif
\ No newline at end of file
--- a/main.cpp	Fri Jun 08 12:29:16 2018 +0000
+++ b/main.cpp	Fri Jun 08 22:22:10 2018 +0000
@@ -1,38 +1,34 @@
 #include "mbed.h"
 #include "gyro.h"
-#include "Distance.h"
+#include "motor.h"
+#include "colorsensor.h"
+
+Serial Rasp(USBTX,USBRX);
+
 Ticker tim;
-void data(int i)
-{
-    change(i);
-    //driveit();
-    }
+Motor M1(p19,p20,p21);
+Motor M2(p17,p18,p22);
 void ddd()
 {
    
     if(started == true)
     {
         tim.detach();
-        gotime.start();
-        driveit(7.5);
-        turnL();
-        //M1=0.3;M2=0.3;
-        //wait(6.75);
-        //直走识别色块
-        
         
+        M1=0.3;M2=0.3;  //直走识别色块
+        wait(6.75);
+        M1=-1;M2=1; //90度
+        wait(0.75);
+        M1=0;M2=0;
         
-        //得到返回值 转角(定好三个函数) 
-        // change(测量)+ driveit()
-        // 到达的判断 + change + driveit() 超声波
-
-        //driveit();   
-        
+        Rasp.printf("8");
+        Rasp.attach(&colorsensor, Rasp.RxIrq);  
         }
     }
 
 int main()
 {
+    Rasp.baud(9600);
     init_gyro();
     tim.attach(&ddd,0.1);
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motor.cpp	Fri Jun 08 22:22:10 2018 +0000
@@ -0,0 +1,17 @@
+#include "mbed.h"
+#include "motor.h"
+
+void Motor::setSpeed(float speed)
+{
+    if(speed > 0) {
+        motorA = direction?1:0;
+        motorB = direction?0:1;
+    } else {
+        motorA = direction?0:1;
+        motorB = direction?1:0;
+    }
+
+    motorSpeed = speed;
+    
+    motorS = abs(speed);
+}
--- a/motor.h	Fri Jun 08 12:29:16 2018 +0000
+++ b/motor.h	Fri Jun 08 22:22:10 2018 +0000
@@ -1,6 +1,5 @@
 #ifndef MOTOR_H
 #define MOTOR_H
-#include "mbed.h"
 class Motor
 {
 public:
@@ -30,19 +29,6 @@
 
 };
 
-void Motor::setSpeed(float speed)
-{
-    if(speed > 0) {
-        motorA = direction?1:0;
-        motorB = direction?0:1;
-    } else {
-        motorA = direction?0:1;
-        motorB = direction?1:0;
-    }
 
-    motorSpeed = speed;
-    
-    motorS = abs(speed);
-}
 
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servogo.h	Fri Jun 08 22:22:10 2018 +0000
@@ -0,0 +1,17 @@
+#include "mbed.h"
+#include "Servo.h"
+
+PwmOut myservo(p23);
+
+int main() {
+    myservo.period_ms(20);
+         for(int i=0; i<=10; i++) {
+             myservo.pulsewidth_ms((2.5+i)/5);
+             wait(0.01);
+        }
+        for(int i=10; i>=0; i--) {
+             myservo.pulsewidth_ms((2.5+i)/5);
+             wait(0.01);
+         }
+         
+}