Robotics_Lab_BLEServo

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
roger5641
Date:
Wed May 25 07:01:38 2016 +0000
Commit message:
BLE_Servo

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r d51a060965e9 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 25 07:01:38 2016 +0000
@@ -0,0 +1,107 @@
+/*LAB_SERVO*/
+#include "mbed.h"
+
+PwmOut servo(A0);
+Serial bluetooth(D10,D2);
+Serial pc(D1,D0);
+
+//LED1 = D13 = PA_5 (LED on Nucleo board)
+DigitalOut led1(A4);
+DigitalOut led2(A5);
+int counter;
+int angle = 0;
+
+Ticker timer1;
+void timer1_interrupt(void);
+
+void init_TIMER(void);
+void init_IO(void); 
+void flash(void);
+
+//Variable(s) for internal control
+float servo_duty = 0.079;//0.079 +(0.084/180)*angle, -90<angle<90
+
+
+int main (void)
+{    
+    init_IO();
+    init_TIMER();
+    bluetooth.baud(115200); //設定鮑率
+    pc.baud(57600);
+    
+    while(1)
+    {
+        if(pc.readable())
+        {
+            bluetooth.putc(pc.getc());
+        }
+        if(bluetooth.readable())
+        {
+            pc.putc(bluetooth.getc());
+        }
+    }       
+}
+
+void timer1_interrupt(void)
+{   
+    counter++;
+    if(counter == 100)
+    {    
+        led1 = 1;
+        wait(1);
+        led1 = 0;         
+    }
+    else if(counter == 200)
+    {    
+        led2 = 1;
+        counter = 0;
+        wait(1);
+        led2 = 0;        
+    }
+    
+ //////code for internal control//////
+    switch(bluetooth.getc())
+    {
+    case 'a':
+        angle = 30;
+        break;
+    case 'b':
+        angle = 60;
+        break;
+    case 'c':
+        angle = 90;
+        break;
+    case 'd':
+        angle = 0;
+        break;          
+    }
+    
+    servo_duty = 0.079 + (0.084/180)*angle;
+    servo.write(servo_duty);
+    servo = 1;
+    wait(0.1);       
+    servo = 0;   
+            
+ ////////////////////////////////////////////   
+    
+    if(servo_duty >= 0.121f)servo_duty = 0.121;
+    else if(servo_duty <= 0.037f)servo_duty = 0.037;
+    servo.write(servo_duty);
+}
+
+void init_TIMER(void)
+{
+    timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz)
+}
+       
+void init_IO(void)
+{
+    counter = 0;
+    led1 = 0;
+    led2 = 0;
+}         
+   
+void flash(void)
+{
+    led1 = !led1;
+}
\ No newline at end of file
diff -r 000000000000 -r d51a060965e9 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed May 25 07:01:38 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/c0f6e94411f5
\ No newline at end of file