Example of using threads to run different actions happening at the same time.

Dependencies:   Motor Servo

Fork of Fa2018-es200-1121-3321-thread-example-rockem by Fa2018-es200-1112-proj2-rockemsockem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 ES200 Project 2 thread example
00003 D Evangelista, 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(p19); // switch for activating motor 
00015 DigitalIn sw2(p18); // switch for activating servo 
00016 DigitalOut led1(LED1); // status LED for sw1
00017 DigitalOut led2(LED2); // status LED for sw2
00018 DigitalOut heartbeat(LED4); // heartbeat LED
00019 Motor m(p26,p30,p29); 
00020 Servo s1(p21);
00021 Thread m_thread; // NEW IN mbed OS 5, threads are used to (sorta) do multiple things at once
00022 Thread s1_thread;
00023 
00024 /** Callback for executing a simple motor action. When sw1 is high,
00025  *  the motor turns forward, otherwise the motor turns off.
00026  */
00027 void m_callback(void); // This function will be run within the corresponding thread
00028 
00029 /** Simple callback for servo motion. When sw2 is high, the servo steps right,
00030  *  otherwise it steps left. If it hits the ends it stays there.
00031  */
00032 void s1_callback(void);
00033 
00034 // main() runs in its own thread in the OS
00035 int main() {
00036     // startup things
00037     printf("ES200 Project 2 thread example\n");
00038     s1.calibrate(0.0009,90.0); // calibrate servo s1 once at startup
00039     m.speed(0.0); // set motor speed to zero at startup
00040     
00041     // start tasks
00042     printf("main thread running\n");
00043     m_thread.start(callback(m_callback)); // starts the motor thread going
00044     s1_thread.start(callback(s1_callback)); // starts the servo thread going
00045     
00046     // main loop
00047     while(1){
00048         heartbeat = !heartbeat; // blink heartbeat once a second
00049         //ThisThread::sleep_for(1000);
00050         Thread::wait(1000);
00051                 
00052         // I don't have to do my motor or servo stuff here because they are
00053         // running in their own threads. 
00054         } // main loop
00055 } // main()
00056 
00057 
00058 
00059 
00060 void m_callback(void){
00061     printf("m_thread running\n");
00062     while(1) {
00063         if (sw1.read()){
00064             led1.write(1); // light a light for debugging purposes
00065             m.speed(0.7); // spin motor ahead
00066             }
00067             else {
00068                 led1.write(0); 
00069                 m.speed(0.0); // stop the shaft
00070                 }
00071         //ThisThread::sleep_for(200); // this thread executes 5x a second
00072         Thread::wait(200); 
00073     } // while(1)
00074 } // m_callback()
00075     
00076     
00077     
00078 void s1_callback(void){
00079     printf("s1_thread running\n");    
00080     while(1){
00081         if (sw2.read()){
00082             led2.write(1); // light a light for debugging purposes
00083             s1.write(s1.read()+0.1); // move servo a step to right
00084             }
00085             else{
00086                 led2.write(0); 
00087                 s1.write(s1.read()-0.1); // move servo a step to left
00088                 }
00089         //ThisThread::sleep_for(200); // this thread executes 5x a second
00090         Thread::wait(200); 
00091         } // while(1)
00092 } // s1_callback()