Threaded version of ES200 1121 Team Dolphin Club sub code.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 ES200 1121 Project 2 Dolphin Club 00003 Run silent run deep. 00004 MIDN 3/c Enger, Ewing, Hicks, Jordan 00005 */ 00006 00007 #include "mbed.h" // DE transitioned this to mbed OS 5 to do multitasking and threads 00008 #include "rtos.h" 00009 00010 #include "stdio.h" 00011 #include "Motor.h" 00012 #include "Servo.h" 00013 00014 // Global hardware objects 00015 Serial pc(USBTX,USBRX); // for talking back to computer 00016 00017 // inputs 00018 DigitalIn prop_sw(p16); //switch to turn on the prop 00019 DigitalIn torpedo_sw(p17); //switch to fire the torpedo 00020 DigitalIn planes_sw(p18); //switch to tilt the fairwater planes 00021 DigitalIn lights_sw(p19); //switch to turn on the running lights 00022 00023 // outputs 00024 DigitalOut green(p6); // external green LED 00025 DigitalOut red(p7); // external red LED 00026 Motor prop_motor(p26,p30,p29); //motor variable 00027 Servo torpedo_servo(p21); //torpedo servo 00028 Servo planes_servo(p22); //planes servo 00029 00030 // other indicators 00031 DigitalOut heartbeat(LED1); 00032 DigitalOut prop_led(LED2); 00033 DigitalOut torpedo_led(LED3); 00034 DigitalOut planes_led(LED4); 00035 00036 // threads 00037 Thread prop_thread; 00038 Thread torpedo_thread; 00039 Thread planes_thread; 00040 Thread lights_thread; 00041 00042 // callback function prototypes 00043 void prop_callback(void); 00044 void torpedo_callback(void); 00045 void planes_callback(void); 00046 void lights_callback(void); 00047 00048 00049 00050 00051 00052 00053 int main(void) 00054 { 00055 // setup 00056 pc.printf("ES200 1121 Project 2 Team 2 Dolphin Club\r\n"); 00057 pc.printf("Submarines go deeper.\r\n"); 00058 pc.printf("main() thread running\r\n"); 00059 00060 // start the other processes running too 00061 prop_thread.start(callback(prop_callback)); 00062 torpedo_thread.start(callback(torpedo_callback)); 00063 planes_thread.start(callback(planes_callback)); 00064 lights_thread.start(callback(lights_callback)); 00065 00066 pc.printf("main() entering heartbeat indication loop\r\n"); 00067 while(1) { 00068 // blink heartbeat to show program is alive and running 00069 heartbeat = !heartbeat; 00070 ThisThread::sleep_for(500); 00071 } 00072 } // main() 00073 00074 00075 00076 00077 00078 /** Callback for running main propulsion thread 00079 */ 00080 void prop_callback(void) 00081 { 00082 // setup 00083 pc.printf("prop_thread standing by ready to answer bells on main engine\r\n"); 00084 00085 // propulsion loop 00086 while(1) { 00087 pc.printf("prop_thread all stop\r\n"); 00088 prop_led.write(0); 00089 prop_motor.speed(0.0); 00090 while (!prop_sw.read()) { 00091 ThisThread::sleep_for(200); 00092 } // all stop 00093 00094 pc.printf("prop_thread all ahead flank\r\n"); 00095 prop_led.write(1); 00096 prop_motor.speed(1.0); 00097 while (prop_sw.read()) { 00098 ThisThread::sleep_for(200); 00099 } // all ahead flankn 00100 } // while(1) 00101 } // prop_callback() 00102 00103 00104 00105 00106 /** Callback for running torpedo thread 00107 */ 00108 void torpedo_callback(void) 00109 { 00110 // setup 00111 pc.printf("torpedo_thread running\r\n"); 00112 00113 // torpedo loop 00114 while(1) { 00115 pc.printf("torpedo_thread idle\r\n"); 00116 torpedo_led.write(0); 00117 torpedo_servo.write(0.01); 00118 while (!torpedo_sw.read()){ 00119 ThisThread::sleep_for(200); 00120 } // torpedo awaiting shoot 00121 00122 torpedo_led.write(1); 00123 pc.printf("torpedo_thread shoot tube 1, bearing 000\r\n"); 00124 ThisThread::sleep_for(1500); 00125 torpedo_servo.write(0.5); 00126 ThisThread::sleep_for(1000); 00127 pc.printf("torpedo_thread reload\r\n"); 00128 torpedo_servo.write(0.0); 00129 while (torpedo_sw.read()){ 00130 ThisThread::sleep_for(200); 00131 } // torpedo awaiting return to idle 00132 pc.printf("torpedo_thread returning to idle state\r\n"); 00133 } // while(1) 00134 } // torpedo_callback() 00135 00136 00137 00138 00139 /** Callback for running fairwater planes 00140 */ 00141 void planes_callback(void) 00142 { 00143 // setup 00144 pc.printf("planes_thread running\r\n"); 00145 00146 // planes loop 00147 while(1) { 00148 pc.printf("planes_thread idle\r\n"); 00149 planes_led.write(0); 00150 planes_servo.write(0.2); 00151 while (!planes_sw.read()) { 00152 ThisThread::sleep_for(200); 00153 } // planes idle wait 00154 00155 planes_led.write(1); 00156 pc.printf("plane_thread cycle the fairwater planes\r\n"); 00157 while (planes_sw.read()) { 00158 planes_servo.write(0.2); 00159 ThisThread::sleep_for(1000); 00160 planes_servo.write(0.7); 00161 ThisThread::sleep_for(1000); 00162 } // planes cycling state 00163 pc.printf("planes_thread secure cycling\r\n"); 00164 } // while(1) 00165 } // planes_callback() 00166 00167 00168 00169 00170 /** Callback for navigational running lights 00171 */ 00172 void lights_callback(void) 00173 { 00174 // setup 00175 pc.printf("lights_thread running\r\n"); 00176 00177 // lights loop 00178 while(1) { 00179 pc.printf("ligths_thread running lights de-energized\r\n"); 00180 green.write(0); 00181 red.write(0); 00182 while (!lights_sw.read()) { 00183 ThisThread::sleep_for(200); 00184 } // lights idle wait 00185 00186 pc.printf("lights_thread running lights energized\r\n"); 00187 green.write(1); 00188 red.write(1); 00189 while (lights_sw.read()) { 00190 ThisThread::sleep_for(200); 00191 } // lights wait to turn off 00192 } // while(1) 00193 } // lights_callback()
Generated on Thu Jul 21 2022 00:06:58 by
1.7.2