Threaded version of ES200 1121 Team Dolphin Club sub code.

Dependencies:   Motor Servo

main.cpp

Committer:
evangeli
Date:
2018-10-22
Revision:
2:c7591cd567c8
Parent:
0:cf9c71a62530

File content as of revision 2:c7591cd567c8:

/*
ES200 1121 Project 2 Dolphin Club
Run silent run deep. 
MIDN 3/c Enger, Ewing, Hicks, Jordan
*/

#include "mbed.h" // DE transitioned this to mbed OS 5 to do multitasking and threads
#include "rtos.h"

#include "stdio.h"
#include "Motor.h"
#include "Servo.h"

// Global hardware objects
Serial pc(USBTX,USBRX);  // for talking back to computer

// inputs
DigitalIn prop_sw(p16); //switch to turn on the prop
DigitalIn torpedo_sw(p17); //switch to fire the torpedo
DigitalIn planes_sw(p18); //switch to tilt the fairwater planes
DigitalIn lights_sw(p19); //switch to turn on the running lights

// outputs
DigitalOut green(p6); // external green LED
DigitalOut red(p7); // external red LED
Motor prop_motor(p26,p30,p29); //motor variable
Servo torpedo_servo(p21); //torpedo servo
Servo planes_servo(p22); //planes servo

// other indicators
DigitalOut heartbeat(LED1);
DigitalOut prop_led(LED2);
DigitalOut torpedo_led(LED3);
DigitalOut planes_led(LED4); 

// threads
Thread prop_thread;
Thread torpedo_thread;
Thread planes_thread;
Thread lights_thread;

// callback function prototypes
void prop_callback(void);
void torpedo_callback(void);
void planes_callback(void);
void lights_callback(void); 






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");

    // 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);
    }
} // 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) {
        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()




/** Callback for running fairwater planes
*/ 
void planes_callback(void)
{
    // 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()