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

Files at this revision

API Documentation at this revision

Comitter:
evangeli
Date:
Sun Oct 14 21:54:14 2018 +0000
Child:
1:184af0de4da7
Commit message:
Initial commit. This example shows how to use threads to run two different things at once.

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
Motor.lib Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Sun Oct 14 21:54:14 2018 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.lib	Sun Oct 14 21:54:14 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/simon/code/Motor/#2fb29b1f7d1c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Sun Oct 14 21:54:14 2018 +0000
@@ -0,0 +1,57 @@
+# Getting started with Blinky on mbed OS
+
+This guide reviews the steps required to get Blinky working on an mbed OS platform.
+
+Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
+
+## Import the example application
+
+From the command-line, import the example:
+
+```
+mbed import mbed-os-example-blinky
+cd mbed-os-example-blinky
+```
+
+### Now compile
+
+Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
+
+```
+mbed compile -m K64F -t ARM
+```
+
+Your PC may take a few minutes to compile your code. At the end, you see the following result:
+
+```
+[snip]
++----------------------------+-------+-------+------+
+| Module                     | .text | .data | .bss |
++----------------------------+-------+-------+------+
+| Misc                       | 13939 |    24 | 1372 |
+| core/hal                   | 16993 |    96 |  296 |
+| core/rtos                  |  7384 |    92 | 4204 |
+| features/FEATURE_IPV4      |    80 |     0 |  176 |
+| frameworks/greentea-client |  1830 |    60 |   44 |
+| frameworks/utest           |  2392 |   512 |  292 |
+| Subtotals                  | 42618 |   784 | 6384 |
++----------------------------+-------+-------+------+
+Allocated Heap: unknown
+Allocated Stack: unknown
+Total Static RAM memory (data + bss): 7168 bytes
+Total RAM memory (data + bss + heap + stack): 7168 bytes
+Total Flash memory (text + data + misc): 43402 bytes
+Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin
+```
+
+### Program your board
+
+1. Connect your mbed device to the computer over USB.
+1. Copy the binary file to the mbed device.
+1. Press the reset button to start the program.
+
+The LED on your platform turns on and off.
+
+## Troubleshooting
+
+If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Sun Oct 14 21:54:14 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/simon/code/Servo/#36b69a7ced07
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Oct 14 21:54:14 2018 +0000
@@ -0,0 +1,91 @@
+/*
+ES200 Project 2 thread example
+D Evangelista, 2018
+*/
+
+#include "mbed.h"
+#include "rtos.h"
+#include "stdio.h"
+#include "Motor.h"
+#include "Servo.h"
+
+// Declare some stuff
+DigitalIn sw1(p19); // switch for activating motor 
+DigitalIn sw2(p18); // switch for activating servo 
+DigitalOut led1(LED1); // status LED for sw1
+DigitalOut led2(LED2); // status LED for sw2
+DigitalOut heartbeat(LED4); // heartbeat LED
+Motor m(p26,p30,p29); 
+Servo s1(p21);
+Thread m_thread; // NEW IN mbed OS 5, threads are used to (sorta) do multiple things at once
+Thread s1_thread;
+
+void m_callback(void); // This function will be run within the corresponding thread
+void s1_callback(void);
+
+// main() runs in its own thread in the OS
+int main() {
+    // startup things
+    printf("ES200 Project 2 thread example\n");
+    s1.calibrate(0.0009,90.0); // calibrate servo s1 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
+    
+    // main loop
+    while(1){
+        heartbeat = !heartbeat; // blink heartbeat once a second
+        ThisThread::sleep_for(1000);
+        
+        // I don't have to do my motor or servo stuff here because they are
+        // running in their own threads. 
+        } // main loop
+} // main()
+
+
+
+
+
+void m_callback(void){
+    /**
+    Callback for executing a simple motor action. When sw1 is high,
+    the motor turns forward, otherwise the motor turns off. 
+    */
+    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
+                }
+        ThisThread::sleep_for(200); // this thread executes 5x a second
+    } // while(1)
+} // m_callback()
+    
+    
+    
+    
+void s1_callback(void){
+    /** 
+    Simple callback for servo motion. When sw2 is high, the servo steps right,
+    otherwise it steps left. If it hits the ends it stays there.  
+    */
+    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
+            }
+            else{
+                led2.write(0); 
+                s1.write(s1.read()-0.1); // move servo a step to left
+                }
+        ThisThread::sleep_for(200); // this thread executes 5x a second
+        } // while(1)
+} // s1_callback()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Sun Oct 14 21:54:14 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#c53d51fe9220728bf8ed27afe7afc1ecc3f6f5d7