Updated to latest version. Added in new version of the Siren

Dependencies:   mbed-src

Fork of VolundrIntroCase2015 by Volundr

Revision:
3:ce51f3857195
Parent:
2:7ca8b2154664
Child:
4:17d1343916ec
--- a/main.cpp	Sun Aug 09 17:28:16 2015 +0000
+++ b/main.cpp	Sun Aug 16 11:44:48 2015 +0000
@@ -21,18 +21,36 @@
 DigitalOut en_a(p21);
 DigitalOut en_b(p22);
 
-DigitalOut LED(LED1);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+AnalogIn collision(p15);
+
+DigitalOut sirene(p19);
+DigitalOut lights(p20);
 
 Serial bluetooth(p9, p10);
-Serial pc(USBTX, USBRX);
+//Serial pc(USBTX, USBRX);
 
+static int control_state = 0; // When 0, allow bluetooth control, When 1 disable bluetooth control
 
-void SysTick_handler()
+void reset_LED()
 {
-    in1.write(0.5);
-    in2.write(0.5);
-    in3.write(0.5);
-    in4.write(0.5);
+    led1.write(0);
+    led2.write(0);
+    led3.write(0);
+    led4.write(0);
+}
+
+void TimeOut_handler()
+{
+    in1.write(0.0);
+    in2.write(0.0);
+    in3.write(0.0);
+    in4.write(0.0);
+    reset_LED();
 }
 
 /* Initialize timers for the H-bridge*/
@@ -43,6 +61,11 @@
     in2.period_us(100);
     in3.period_us(100);
     in4.period_us(100);
+
+    in1.write(0.0);
+    in2.write(0.0);
+    in3.write(0.0);
+    in4.write(0.0);
     /* Set enable pin high. NEVER set this pins low during driving.
     There are no protection diodes and with EN low, H-bridge is in Hi-Z mode.
     This can generate a high voltage that breaks the H-bridge.*/
@@ -52,104 +75,121 @@
     return 1;
 }
 
-/*Handler for Bluetooth messages*/
 void rxCallback()
 {
-    char a;
-    a = bluetooth.getc();
-    if(a == 'g') {
-        LED = 1;
+    static int sirene_state = 0;
+    static int lights_state = 0;
+
+    if (control_state == 1)
+    { 
+        return;
     }
-}
-
-void rxPCCallback()
-{
-    int i;
+    
+    float time_out_sec = 0.1;
     char c;
-    c = pc.getc();
+    c = bluetooth.getc();
     switch (c) {
             // Forward direction
-        case 'w':
+        case 'W':
+            reset_LED();
+            led1.write(1);
             in1.write(1.0);
             in2.write(0.0);
-            in3.write(1.0);
-            in4.write(0.0);
-            systicker.attach(&SysTick_handler, 0.6);
+            in3.write(0.0);
+            in4.write(1.0);
+            systicker.attach(&TimeOut_handler, time_out_sec);
             break;
             // Backward direction
-        case 's':
+        case 'S':
+            reset_LED();
+            led2.write(1);
             in1.write(0.0);
             in2.write(1.0);
-            in3.write(0.0);
-            in4.write(1.0);
+            in3.write(1.0);
+            in4.write(0.0);
+            systicker.attach(&TimeOut_handler, time_out_sec);
             break;
             // Left direction
-        case 'a':
+        case 'A':
+            reset_LED();
+            led3.write(1);
             in1.write(1.0);
             in2.write(0.0);
             in3.write(0.0);
             in4.write(0.0);
+            systicker.attach(&TimeOut_handler, time_out_sec);
             break;
-        case 'd':
+        case 'D':
+            reset_LED();
+            led4.write(1);
             in1.write(0.0);
-            in2.write(1.0);
+            in2.write(0.0);
             in3.write(0.0);
-            in4.write(0.0);
+            in4.write(1.0);
+            systicker.attach(&TimeOut_handler, time_out_sec);
             break;
-        default:
-            in1.write(0.5);
-            in2.write(0.5);
-            in3.write(0.5);
-            in4.write(0.5);
+        case 'g':
+            if (sirene_state == 0) {
+                sirene.write(0);
+                sirene_state = 1;
+            } else {
+                sirene.write(1);
+                sirene_state = 0;
+            }
+            break;
+        case 'k':
+            if (lights_state == 0) {
+                lights.write(1);
+                lights_state = 1;
+            } else {
+                lights.write(0);
+                lights_state = 0;
+            }
+            break;
     }
-
-
 }
 
 /* Initialize the bluetooth module*/
 int init_bluetooth()
 {
     bluetooth.baud(9600);
-    //wait(0.2);
-    //bluetooth.printf("AT+NAMETHOR1\r\n");
     bluetooth.attach(&rxCallback, Serial::RxIrq);
-
-    return 1;
-}
-
-/* Initialize ADC ports*/
-int init_ADC()
-{
-    return 1;
-}
-
-
-/* Initialize GPIO's*/
-int init_GPIO()
-{
     return 1;
 }
 
 int main()
 {
-    pc.baud(9600);
-    //wait(0.2);
-    //bluetooth.printf("AT+NAMETHOR1\r\n");
-    pc.attach(&rxPCCallback, Serial::RxIrq);
+    float value=0.0;
+
+    sirene.write(1); // Is switched with PNP --> High is off
+    lights.write(0); // Is directly connected to IO-pin --> Low is off
 
     init_drive();
-//    init_bluetooth();
-
-    
-
-    while(1) {
+    init_bluetooth();
 
-    /*        if(pc.readable()) {
-                bluetooth.putc(pc.getc());
-            }
-            if(bluetooth.readable()) {
-                pc.putc(bluetooth.getc());
-            }*/
-}
+    /** Collision Low == 1.2 V
+        Collision High == 1.7V
+        Trigger on 1.5V -> 0.45
+    */
+    while(1) {
+        value = collision.read();
+        if (value >= 0.45) {
+            control_state = 1; // Disable bluetooth control
+            // Drive backwards for a second
+            led2.write(1);
+            led3.write(1);
+            led4.write(1);
+            led1.write(1);
+            in1.write(0.0);
+            in2.write(1.0);
+            in3.write(1.0);
+            in4.write(0.0);
+            wait (1);
+            reset_LED(); 
+            control_state = 0; // Allow bluetooth control
+        } else {
+            // do nothing
+        }
 
+    }
 }
\ No newline at end of file