Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Fa2018-es200-1121-3321-thread-example by
main.cpp
00001 /* 00002 ES200 Project 2 thread use 00003 Rock em Sock em 2018 00004 Note that this uses mbed OS 5 vice OS 2. 00005 */ 00006 00007 #include "mbed.h" 00008 #include "rtos.h" 00009 #include "stdio.h" 00010 #include "Motor.h" 00011 #include "Servo.h" 00012 00013 // Declare some stuff 00014 DigitalIn sw1(p17); // switch for activating led5 00015 DigitalIn sw2(p16); // switch for activating servo 00016 DigitalIn sw3(p20); // switch for activating servo2 00017 DigitalIn sw4(p19); // switch for led4 00018 DigitalOut led1(p5); // status LED for motor 00019 DigitalOut led2(p6); // status LED for sw2 00020 DigitalOut led3(p7); // status LED for sw3 00021 DigitalOut led4(p8); // status LED for sw4 00022 DigitalOut led5(p11); //status LED for sw1 00023 DigitalOut heartbeat(LED4); // heartbeat LED adds visual effects 00024 Motor m(p26,p30,p29); 00025 Servo s1(p21); 00026 Servo s2(p22); 00027 Thread m_thread; // NEW IN mbed OS 5, threads are used to (sorta) do multiple things at once 00028 Thread s1_thread; //creates thread for servo 00029 Thread s2_thread; // creates thread for servo2 00030 00031 /** Callback for executing a simple motor action. When sw1 is high, 00032 * the motor turns forward, otherwise the motor turns off. 00033 */ 00034 void m_callback(void); // This function will be run within the corresponding thread 00035 00036 /** Simple callback for servo motion. When sw2 is high, the servo steps right, 00037 * otherwise it steps left. If it hits the ends it stays there. 00038 */ 00039 void s1_callback(void); 00040 00041 void s2_callback(void); 00042 00043 // main() runs in its own thread in the OS 00044 int main() { 00045 // startup things 00046 printf("ES200 Project 2 Rockem Sockem\n"); 00047 s1.calibrate(0.0009,90.0); // calibrate servo s1 once at startup 00048 s2.calibrate(0.0009,90.0); //calibrate servo s2 once at startup 00049 m.speed(0.0); // set motor speed to zero at startup 00050 00051 // start tasks 00052 printf("In the left corner: MIDN Slappy weighing 0.02 lbs.\n In the right corner: MIDN Hitme weighing 0.021 lbs.\n"); 00053 m_thread.start(callback(m_callback)); // starts the motor thread going 00054 s1_thread.start(callback(s1_callback)); // starts the servo thread going 00055 s2_thread.start(callback(s2_callback)); //starts the servo2 thread going 00056 printf("\"ding ding ding\" Fight!\n"); 00057 00058 // main loop 00059 while(1){ 00060 heartbeat = !heartbeat; // blink heartbeat once a second 00061 //ThisThread::sleep_for(1000); 00062 Thread::wait(500+rand()%1000); 00063 00064 // motor or servo stuff not needed here because they are 00065 // running in their own threads. 00066 } // main loop 00067 } // main() 00068 00069 00070 00071 00072 void m_callback(void){ 00073 printf("THE fighters are moving back and forth like dancers!!\n"); 00074 while(1) { 00075 00076 led1.write(1); // light a light for entertaining purposes 00077 m.speed(-0.45); // spin motor ahead to move players together 00078 wait (0.65); //time of motor movement 00079 m.speed(0.0); //stop players when they are close. 00080 wait (3+rand()%5); //let them fight for a random amount of time 00081 led1.write(0); 00082 m.speed(0.45); // move the players back 00083 wait (1.5); //time of opposite motor movement 00084 00085 if (sw4.read()) { 00086 led4.write(1); 00087 } 00088 if (sw1.read()) { 00089 led5.write(1); 00090 } 00091 00092 //ThisThread::sleep_for(200); // this thread executes 100x a second 00093 Thread::wait(50); //possibly change to small or erase 00094 } // while(1) 00095 } // m_callback() 00096 00097 00098 00099 void s1_callback(void){ 00100 printf("Slappy threw a punch!\n"); 00101 while(1){ 00102 if (sw2.read()){ 00103 led2.write(1); // light a light for entertaining purposes 00104 s1.write(s1.read()+0.5); // move servo a step to right 00105 } 00106 else{ 00107 led2.write(0); 00108 s1.write(0.0); // move servo a step to left 00109 } 00110 //ThisThread::sleep_for(200); // this thread executes 100x a second 00111 Thread::wait(50); //Not used so that switches move players immediately - - - may have to use so that switches can move independently 00112 } // while(1) 00113 } // s1_callback() 00114 00115 void s2_callback(void){ 00116 printf("Hitme is fighting back!\n"); 00117 while(1){ 00118 if (sw3.read()){ 00119 led3.write(1); // light a light for entertaining purposes 00120 s2.write(s2.read()+0.5); // move servo a step to right 00121 } 00122 else{ 00123 led3.write(0); 00124 s2.write(0.0); // move servo a step to left 00125 } 00126 //ThisThread::sleep_for(200); // this thread executes 100x a second 00127 Thread::wait(50); //possibly change 00128 } // while(1) 00129 } // s2_callback()
Generated on Mon Jul 18 2022 14:33:21 by
1.7.2
