一个使用Nucleo_F411RE开发履带坦克底盘的工程,代码不断完善中

Dependencies:   Encoder HCSR04 LED MOTOR RemoteIR

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Files at this revision

API Documentation at this revision

Comitter:
adaphoto
Date:
Tue Jun 19 12:25:02 2018 +0000
Parent:
69:1ff487e9be56
Commit message:
??????

Changed in this revision

HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
LED.lib Show annotated file Show diff for this revision Revisions of this file
MOTOR.lib Show annotated file Show diff for this revision Revisions of this file
RemoteIR.lib 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
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Tue Jun 19 12:25:02 2018 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#cf3e4e307d15
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LED.lib	Tue Jun 19 12:25:02 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/adaphoto/code/LED/#94318e6e89ff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MOTOR.lib	Tue Jun 19 12:25:02 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/adaphoto/code/MOTOR/#d31307e787fd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RemoteIR.lib	Tue Jun 19 12:25:02 2018 +0000
@@ -0,0 +1,1 @@
+RemoteIR#355a854280cd
--- a/main.cpp	Fri Jun 15 11:00:02 2018 +0100
+++ b/main.cpp	Tue Jun 19 12:25:02 2018 +0000
@@ -1,12 +1,154 @@
 #include "mbed.h"
 
-DigitalOut led1(LED1);
+#include "LED.h"        //LED灯头文件
+#include "ReceiverIR.h" //红外接收头文件
+#include "motodriver.h" //电机控制头文件
+#include "HCSR04.h"     //超声波模块的头文件
+
+// 声明变量
+int     bitcount, decoded;
+char    sr04_alart;
+int     encoder_countA=0;
+
+// 声明IO口
+LED        user_led(PA_5);     //LED的IO口
+Serial     pc(PA_2,PA_3);      //串口的IO口
+ReceiverIR ir_rx(PB_12);       //红外接收管的IO口,可以是普通的IO口
+Motor      motor1(PA_8,PA_9);  //电机控制的IO口,需要具备PWM功能的IO口
+Motor      motor2(PA_11,PA_10);//电机控制的IO口,需要具备PWM功能的IO口
+HCSR04     sr04(PC_0, PC_1);   //超声波测距模块的IO口,可以是普通的IO口
+
+InterruptIn encoderA(PB_14);
+DigitalIn   encoderB(PB_13);
+
+// 声明线程
+Thread thread_ir;
+Thread thread_sr04;
+
+// Timer
+Timer sr04_timer;
+
+// 声明线程实现函数
+void ir_thread();
+void sr04_thread();
+
+void encoderA_counter(void)
+{
+    if (encoderB == 1)
+    {
+        encoder_countA+=1;
+    }
+    else
+    {
+        encoder_countA-=1;
+    }
+}
+    
+void encoderA_reset(void)
+{
+    encoder_countA=0;
+    }
 
 // main() runs in its own thread in the OS
-int main() {
-    while (true) {
-        led1 = !led1;
-        wait(0.5);
+int main() 
+{
+    // 设置串口波特率为115200
+    pc.baud(115200);
+    pc.printf("\r\n*** RTOS Robot Hello World! ***\n\r");
+    
+    encoderA.fall(&encoderA_counter);          //interrupt A rise-trig
+    
+    // 启动线程
+    thread_ir.start(ir_thread);
+    thread_sr04.start(sr04_thread);
+    
+    while (true) 
+    {
+        user_led.LED_Troggle();
+        pc.printf("\r\n encoderA: %d", encoder_countA);
+        
+        if (sr04_alart == 1)
+        {
+            motor1.Stop();
+            motor2.Stop();
+        }
+        else
+        {
+            if (decoded == 0xb9)
+            {
+                motor1.Speed(0.5);
+                motor2.Speed(0.5);
+            }
+            else if (decoded == 0xbf)
+            {
+                motor1.Stop();
+                motor2.Stop();
+            }
+            else if (decoded == 0xea)
+            {
+                motor1.Speed(-0.5);
+                motor2.Speed(-0.5);
+            }
+            else if (decoded == 0xbb)
+            {
+                motor1.Speed(-0.5);
+                motor2.Speed(0.5);
+            }
+            else if (decoded == 0xbc)
+            {
+                motor1.Speed(0.5);
+                motor2.Speed(-0.5);
+            } 
+        }
+        wait(0.1);
     }
 }
 
+/*****************************************************************/
+void ir_thread()
+{
+    // 和红外接收有关的变量
+    RemoteIR::Format format;
+    uint8_t buf[32];
+    //double  DC=0.5;
+    
+    while(true)
+    {
+        if (ir_rx.getState() == ReceiverIR::Received) 
+        {
+            bitcount = ir_rx.getData(&format, buf, sizeof(buf) * 8);
+            if (bitcount>0) 
+            {
+                decoded=buf[3];
+                pc.printf("\r\nDecoded: %x ", buf[3]);
+            }
+            pc.printf(".");
+        } 
+    }
+}
+
+void sr04_thread()
+{
+    while(true)
+    {
+        sr04_timer.reset();
+        sr04_timer.start();
+        sr04.startMeasurement();
+        while(!sr04.isNewDataReady()) {
+          // wait for new data
+          // waiting time depends on the distance
+        }
+        //pc.printf("\r\nDistance: %5.1f mm", sr04.getDistance_mm());
+        sr04_timer.stop();
+        
+        if (sr04.getDistance_mm() < 400.0)
+        {
+            sr04_alart = 1;
+        }
+        else
+        {
+            sr04_alart = 0;
+        }
+        Thread::wait(200 - sr04_timer.read_ms()); // time the loop
+    }
+}
\ No newline at end of file
--- a/mbed-os.lib	Fri Jun 15 11:00:02 2018 +0100
+++ b/mbed-os.lib	Tue Jun 19 12:25:02 2018 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#866850acc15e86cd4ac11bf4404078a49f921ddd
+https://github.com/ARMmbed/mbed-os/#03196b244ee00fc748cd4dbfa83b26f3fc0f376b