Threaded version of ES200 1121 Team Dolphin Club sub code.

Dependencies:   Motor Servo

Files at this revision

API Documentation at this revision

Comitter:
evangeli
Date:
Mon Oct 22 15:24:51 2018 +0000
Parent:
1:b9ffc0aeb3ce
Commit message:
DE added torpedo, planes, and lights callback

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r b9ffc0aeb3ce -r c7591cd567c8 main.cpp
--- a/main.cpp	Mon Oct 22 13:44:14 2018 +0000
+++ b/main.cpp	Mon Oct 22 15:24:51 2018 +0000
@@ -28,17 +28,11 @@
 Servo planes_servo(p22); //planes servo
 
 // other indicators
-DigitalOut heartbeat_led(LED1);
+DigitalOut heartbeat(LED1);
 DigitalOut prop_led(LED2);
 DigitalOut torpedo_led(LED3);
 DigitalOut planes_led(LED4); 
 
-// do i need these???
-int screw;
-int fire;
-int fw;
-int running;
-
 // threads
 Thread prop_thread;
 Thread torpedo_thread;
@@ -56,92 +50,144 @@
 
 
 
-int main(void){  //main open
-  pc.printf("ES200 1121 Project 2 Team 2 Dolphin Club\r\n");
-  pc.printf("Submarines go deeper.\r\n");
-
-
+int main(void)
+{
+    // setup
+    pc.printf("ES200 1121 Project 2 Team 2 Dolphin Club\r\n");
+    pc.printf("Submarines go deeper.\r\n");
+    pc.printf("main() thread running\r\n");
 
-   while(1) { //while open
-//        printf("reading switch positions\r\n");
-//      screw = prop.read();  //reads switch for prop
-//       fire = shoot.read();  //reads switch for torpedo
-//       fw = planes.read();  //reads switch for planes
-//       running = lights.read();  //reads switch for lights
-       
-   
-    
-   
-   
-    if (fw == 1) { //switch 3 for planes
-      printf("planes\r\n");
-      tilt = 0.2;
-      wait(0.2);
-      tilt = 0.7;
-      wait(0.2); 
+    // start the other processes running too
+    prop_thread.start(callback(prop_callback));
+    torpedo_thread.start(callback(torpedo_callback));
+    planes_thread.start(callback(planes_callback));
+    lights_thread.start(callback(lights_callback));
+
+    pc.printf("main() entering heartbeat indication loop\r\n");
+    while(1) {
+        // blink heartbeat to show program is alive and running
+        heartbeat = !heartbeat;
+        ThisThread::sleep_for(500);
     }
-    else {
-        printf("no planes\r\n");
-        tilt = 0.0; 
-        }
-    /*while(1) {
-    tilt = 0.2; //initial position
-    wait(0.7);  //wait
-    tilt = 0.7;  //tilted position
-    wait(0.7);  //wait
-} //while close
-} //else if close
-    else if (fw==0) {
-        tilt = 0.2;
-        }
-   */
-   
-   
- /* if (running== 1) { //switch 4 for lights to turn on
-   green = 1;
-   red = 1;
-   }
-   */
-  } //while close
-  } //main close
+} // main()
 
 
 
 
 
+/** Callback for running main propulsion thread
+*/ 
 void prop_callback(void)
 {
+    // setup
     pc.printf("prop_thread standing by ready to answer bells on main engine\r\n");
 
+    // propulsion loop
     while(1) {
-        if (prop_sw.read()) {
-            pc.printf("prop_thread all ahead flank\r\n");   
-            prop_motor.speed(1.0);
-        } else {
-            pc.printf("prop_thread all stop\r\n"); 
-            prop_motor.speed(0.0);
-        }
+        pc.printf("prop_thread all stop\r\n"); 
+        prop_led.write(0); 
+        prop_motor.speed(0.0);
+        while (!prop_sw.read()) {
+            ThisThread::sleep_for(200); 
+        } // all stop
+        
+        pc.printf("prop_thread all ahead flank\r\n");   
+        prop_led.write(1); 
+        prop_motor.speed(1.0);
+        while (prop_sw.read()) {
+            ThisThread::sleep_for(200);     
+        }  // all ahead flankn
     } // while(1)
 } // prop_callback()
+   
+   
         
 
+/** Callback for running torpedo thread
+*/ 
+void torpedo_callback(void)
+{
+    // setup 
+    pc.printf("torpedo_thread running\r\n");
+    
+    // torpedo loop
+    while(1) {
+        pc.printf("torpedo_thread idle\r\n");
+        torpedo_led.write(0); 
+        torpedo_servo.write(0.01);
+        while (!torpedo_sw.read()){
+            ThisThread::sleep_for(200); 
+            } // torpedo awaiting shoot
+            
+        torpedo_led.write(1);
+        pc.printf("torpedo_thread shoot tube 1, bearing 000\r\n");
+        ThisThread::sleep_for(1500);
+        torpedo_servo.write(0.5); 
+        ThisThread::sleep_for(1000); 
+        pc.printf("torpedo_thread reload\r\n"); 
+        torpedo_servo.write(0.0);
+        while (torpedo_sw.read()){
+            ThisThread::sleep_for(200); 
+            } // torpedo awaiting return to idle
+        pc.printf("torpedo_thread returning to idle state\r\n"); 
+        } // while(1)
+} // torpedo_callback()
+
 
-void torpedo_callback(void)
+
+
+/** Callback for running fairwater planes
+*/ 
+void planes_callback(void)
 {
-    //if (fire == 1) { //switch 2 for torpeo firing
-     //printf("torpedo\r\n");
-     //}
-/*       while(1) {
-    torpedo = 0.01; //start position inside body of sub
-    wait(1.5);  //wait
-    torpedo = 0.5;  //fire 
-    position outside of sub
-    wait (1.0);  //wait
-       }
-       }
-       else if (fire==0) {
-           torpedo = 0.01;
-           }*/
-    
-    }
-    
\ No newline at end of file
+    // setup
+    pc.printf("planes_thread running\r\n");
+
+    // planes loop
+    while(1) {
+        pc.printf("planes_thread idle\r\n");
+        planes_led.write(0);
+        planes_servo.write(0.2);
+        while (!planes_sw.read()) {
+            ThisThread::sleep_for(200);
+        } // planes idle wait
+
+        planes_led.write(1);
+        pc.printf("plane_thread cycle the fairwater planes\r\n");
+        while (planes_sw.read()) {
+            planes_servo.write(0.2);
+            ThisThread::sleep_for(1000);
+            planes_servo.write(0.7);
+            ThisThread::sleep_for(1000);
+        } // planes cycling state
+        pc.printf("planes_thread secure cycling\r\n");
+    } // while(1)
+} // planes_callback()
+
+
+
+
+/** Callback for navigational running lights
+*/
+void lights_callback(void)
+{
+    // setup
+    pc.printf("lights_thread running\r\n");
+
+    // lights loop
+    while(1) {
+        pc.printf("ligths_thread running lights de-energized\r\n");
+        green.write(0);
+        red.write(0);
+        while (!lights_sw.read()) {
+            ThisThread::sleep_for(200);
+        } // lights idle wait
+
+        pc.printf("lights_thread running lights energized\r\n");
+        green.write(1);
+        red.write(1);
+        while (lights_sw.read()) {
+            ThisThread::sleep_for(200);
+        } // lights wait to turn off
+    } // while(1)
+} // lights_callback()
\ No newline at end of file