Example of using threads to run different actions happening at the same time.
Fork of Fa2018-es200-1121-3321-thread-example by
Diff: main.cpp
- Revision:
- 6:ed7539be9fa3
- Parent:
- 5:0c7c692db414
- Child:
- 7:b7257f01ced7
diff -r 0c7c692db414 -r ed7539be9fa3 main.cpp --- a/main.cpp Mon Oct 15 11:34:44 2018 +0000 +++ b/main.cpp Fri Oct 19 12:27:25 2018 +0000 @@ -11,15 +11,19 @@ #include "Servo.h" // Declare some stuff -DigitalIn sw1(p19); // switch for activating motor -DigitalIn sw2(p18); // switch for activating servo +DigitalIn sw1(p18); // switch for activating motor +DigitalIn sw2(p16); // switch for activating servo +DigitalIn sw3(p20); // DigitalOut led1(LED1); // status LED for sw1 DigitalOut led2(LED2); // status LED for sw2 +DigitalOut led3(LED3); // status LED for sw3 DigitalOut heartbeat(LED4); // heartbeat LED Motor m(p26,p30,p29); Servo s1(p21); +Servo s2(p22); Thread m_thread; // NEW IN mbed OS 5, threads are used to (sorta) do multiple things at once Thread s1_thread; +Thread s2_thread; /** Callback for executing a simple motor action. When sw1 is high, * the motor turns forward, otherwise the motor turns off. @@ -31,17 +35,21 @@ */ void s1_callback(void); +void s2_callback(void); + // main() runs in its own thread in the OS int main() { // startup things - printf("ES200 Project 2 thread example\n"); + printf("ES200 Project 2 Rockem Sockem\n"); s1.calibrate(0.0009,90.0); // calibrate servo s1 once at startup + s2.calibrate(0.0009,90.0); //calibrate servo s2 once at startup m.speed(0.0); // set motor speed to zero at startup // start tasks printf("main thread running\n"); m_thread.start(callback(m_callback)); // starts the motor thread going s1_thread.start(callback(s1_callback)); // starts the servo thread going + s2_thread.start(callback(s2_callback)); //starts the servo2 thread going // main loop while(1){ @@ -60,14 +68,16 @@ void m_callback(void){ printf("m_thread running\n"); while(1) { - if (sw1.read()){ - led1.write(1); // light a light for debugging purposes - m.speed(0.7); // spin motor ahead - } - else { - led1.write(0); - m.speed(0.0); // stop the shaft - } + + led1.write(1); // light a light for entertaining purposes + m.speed(0.7); // spin motor ahead to move players together + wait (1.2); + m.speed(0.0); //stop players when they are close. + wait (1+rand()%5); //let them fight for a random amount of time + led1.write(0); + m.speed(-0.7); // move the players back + wait (1.2); + //ThisThread::sleep_for(200); // this thread executes 5x a second Thread::wait(200); } // while(1) @@ -79,14 +89,30 @@ printf("s1_thread running\n"); while(1){ if (sw2.read()){ - led2.write(1); // light a light for debugging purposes - s1.write(s1.read()+0.1); // move servo a step to right + led2.write(1); // light a light for entertaining purposes + s1.write(s1.read()+0.5); // move servo a step to right } else{ led2.write(0); - s1.write(s1.read()-0.1); // move servo a step to left + s1.write(0); // move servo a step to left } //ThisThread::sleep_for(200); // this thread executes 5x a second Thread::wait(200); } // while(1) } // s1_callback() + +void s2_callback(void){ + printf("s2_thread running\n"); + while(1){ + if (sw3.read()){ + led3.write(1); // light a light for debugging purposes + s2.write(s2.read()+0.5); // move servo a step to right + } + else{ + led3.write(0); + s2.write(0); // move servo a step to left + } + //ThisThread::sleep_for(200); // this thread executes 5x a second + Thread::wait(200); + } // while(1) +} // s2_callback()