GIU\ZF

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Fork of rtos_basic by mbed official

Files at this revision

API Documentation at this revision

Comitter:
ihexx
Date:
Tue Mar 27 14:35:09 2018 +0000
Parent:
11:0309bef74ba8
Child:
13:ab52f46c98ab
Commit message:
core loop functional.; Display task tested.; Debug frameowrk conceptualised.

Changed in this revision

MCP23017.lib Show annotated file Show diff for this revision Revisions of this file
WattBob_TextLCD.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
tasks/brakeIndicator.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/carSimulator.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/core.h Show annotated file Show diff for this revision Revisions of this file
tasks/display.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/filterSpeed.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/getControls.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/getIgnition.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/sendMail.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/sideLights.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/speedIndicator.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/turnSignal.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23017.lib	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jimherd/code/MCP23017/#d57de266cf19
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WattBob_TextLCD.lib	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jimherd/code/WattBob_TextLCD/#3b26cd028e85
--- a/main.cpp	Wed Feb 15 14:04:02 2017 -0600
+++ b/main.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -1,9 +1,80 @@
-#include "mbed.h"
-#include "rtos.h"
- 
-DigitalOut led1(LED1);
+#include "core.h"
+
+DigitalOut led1(LED3);
 DigitalOut led2(LED2);
-Thread thread;
+Thread thread,thread_group_2HZ,thread_group_1HZ;
+
+//Merge tasks with same frequency
+namespace runTimeParams{
+    Mutex liveAccess;
+    float brakeForce = 0.0f;
+    float accelForce = 0.0f;
+    float avgSpeed = 0.0f;
+    float odometer = 0.0f;
+    #if DEBUG_MODE
+    string debugLog = "task,execution_time_ms,lastSleep,drift\n";
+    #endif
+    }
+
+
+namespace task_group_1{
+    Thread thread;
+    Timer executionTimer,sleepTimer;
+    bool tick = false;
+    
+    void init(){
+        executionTimer.reset();
+        sleepTimer.reset();
+        display::init();
+    }
+    void runTask(){
+        const int const_delay = int(1000.0f/display::freq);
+        int dynamic_delay = const_delay;
+        while(true){
+            sleepTimer.stop();
+            executionTimer.start();
+            
+            
+            int sleepTime = sleepTimer.read_ms();
+            const int drift = ((sleepTime - dynamic_delay) > 0)?
+                                        (sleepTime - dynamic_delay) : 0;
+            
+            
+            // Run all tasks
+            display::hotLoop();
+            
+            
+            executionTimer.stop();
+            int exec_time = executionTimer.read_ms();
+            
+            #if DEBUG_MODE
+            runTimeParams::liveAccess.lock();
+            //runTimeParams::debugLog += "GROUP_1," + to_string(executionTimer.read_ms()) + ","
+//                        + to_string(sleepTimer.read_ms()) + ", \n";
+            runTimeParams::odometer = float(sleepTime);
+            runTimeParams::avgSpeed = float(exec_time);
+            runTimeParams::liveAccess.unlock();
+            #endif
+            
+            executionTimer.reset();
+            sleepTimer.reset();
+            sleepTimer.start();
+            dynamic_delay = const_delay - (exec_time + drift);
+            Thread::wait(dynamic_delay);
+        }   
+    }
+}
+
+int init(){
+    //Run task initializers
+    task_group_1::init();
+    
+    //Start task hotloops
+    
+    task_group_1::thread.start(task_group_1::runTask);
+//    getControls::thread.start(getControls::runTask);
+    return 0;
+}
  
 void led2_thread() {
     while (true) {
@@ -13,6 +84,7 @@
 }
  
 int main() {
+    init();
     thread.start(led2_thread);
     
     while (true) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/brakeIndicator.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,20 @@
+#include "core.h"
+
+namespace brakeIndicator{
+    Thread thread;
+    const float freq = 2.0f; //hz
+    
+    //I/O
+    PwmOut led2(PORT_REDBOX_LED1);
+    
+    void runTask(){
+//        Show use of the brake on a LED on the RedBox Unit
+        led2.period_ms(50);
+        while(1){
+            runTimeParams::liveAccess.lock();
+            led2.write(runTimeParams::brakeForce);
+            runTimeParams::liveAccess.unlock();
+            wait(1/freq);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/carSimulator.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,25 @@
+#include "core.h"
+
+namespace carSimulator{
+    Thread thread;
+    const float freq = 20.0f;
+     //hz
+    
+    //Speed
+    
+    void runTask(){
+        /*
+         simplified model of the car to allow the control system to
+        operate. For example, accelerometer causes speed to increase and brake make speed
+        decrease. This task must run at 20Hz.
+        */
+//        Show use of the brake on a LED on the RedBox Unit
+//        led2.period_ms(50);
+        while(1){
+            //runTimeParams::liveAccess.lock();
+//            led2.write(runTimeParams::brakeForce);
+//            runTimeParams::liveAccess.unlock();
+            wait(1/freq);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/core.h	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,148 @@
+#pragma once
+
+#include "mbed.h"
+#include "rtos.h"
+#include "MCP23017.h"
+#include "WattBob_TextLCD.h"
+#include <string>
+#include <sstream>
+
+//Compile Flags
+#define DEBUG_MODE 1
+
+//Inputs-------------------------
+#define PORT_BRAKE p20
+#define PORT_ACCEL p19
+#define PORT_IGNITION p21
+#define PORT_TURN_SIGNAL_SWITCH_LEFT p18
+#define PORT_TURN_SIGNAL_SWITCH_RIGHT p17
+//Outputs ----------------
+#define PORT_REDBOX_LED1 p22
+#define PORT_REDBOX_LED2 p23
+
+#define PORT_SIDE_LIGHTS p5
+#define IGNITION_LED p6
+#define PORT_TURN_SIGNAL_LED_LEFT p25
+#define PORT_TURN_SIGNAL_LED_RIGHT p26
+
+//#define PORT_SIDE_LIGHTS LED1
+//#define IGNITION_LED LED4
+//#define PORT_TURN_SIGNAL_LED_LEFT LED2
+//#define PORT_TURN_SIGNAL_LED_RIGHT LED3
+
+namespace runTimeParams{
+    extern Mutex liveAccess;
+    extern float brakeForce;
+    extern float accelForce;
+    extern float avgSpeed;
+    extern float odometer;
+    #if DEBUG_MODE
+    extern string debugLog;
+    #endif
+}
+    
+namespace carSimulator{
+    //simplified model of the car to allow the control system to operate
+    extern Thread thread;
+    extern const float freq;
+    void runTask(); 
+}
+
+namespace mailData{
+    typedef struct{
+        float   speed;
+        float   accel;
+        float   brake;
+    } mail_t;
+    extern Mail<mail_t, 100> mailBox;
+}
+    
+    
+namespace getControls{
+    //Read brake and accelerator values from variable resistors
+    extern Thread thread;
+    extern const float freq;
+    void runTask(); 
+    
+}
+
+namespace getIgnition{
+    //Read engine on/off switch and show current state on a LED
+    extern Thread thread;
+    extern const float freq;
+    void runTask(); 
+}
+
+namespace filterSpeed{
+    //Filter speed with averaging filter
+    extern Thread thread;
+    extern const float freq;
+    void runTask(); 
+}
+
+namespace brakeIndicator{
+    //Read brake and accelerator values from variable resistors
+    extern Thread thread;
+    extern const float freq;
+    void runTask(); 
+}
+
+namespace speedIndicator{
+    //Read brake and accelerator values from variable resistors
+    extern Thread thread;
+    extern const float freq;
+    void runTask(); 
+}
+
+namespace sideLights{
+    //Read brake and accelerator values from variable resistors
+    extern Thread thread;
+    extern const float freq;
+    void runTask(); 
+}
+
+namespace turnSignal{
+    //Read brake and accelerator values from variable resistors
+    extern Thread thread;
+    extern const float freq;
+    void runTask(); 
+}
+
+namespace sendMail{
+    //Send speed, accelerometer and brake values to a 100 element
+        //MAIL queue
+    extern Thread thread;
+    extern const float freq;
+    void runTask();
+}
+
+namespace display{
+    //Display on MBED text display: • odometer value • average speed
+    extern MCP23017 *port;
+    extern WattBob_TextLCD *lcd;
+    extern const float freq;
+    
+    void init(); 
+    static inline void hotLoop(){
+        lcd->cls();
+        
+        runTimeParams::liveAccess.lock();
+        float odometer = runTimeParams::odometer;
+        float avgSpeed = runTimeParams::avgSpeed;
+        runTimeParams::liveAccess.unlock();
+        
+        lcd->locate(0,0);   //located col, row.
+        lcd->printf("Odo=%.2f",odometer);
+        
+        lcd->locate(1,0);   //located col, row.
+        lcd->printf("Speed=%.2f",avgSpeed);
+    }
+}
+
+template <typename T>
+static inline std::string to_string(T value)
+{
+    std::ostringstream os ;
+    os << value ;
+    return os.str() ;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/display.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,19 @@
+#include "core.h"
+
+
+
+namespace display{
+    //Display on MBED text display • odometer value • average speed
+    const float freq = 2.0f; //hz
+    
+    //I/O
+    MCP23017 *port;
+    WattBob_TextLCD *lcd;
+    
+    void init(){
+        port = new MCP23017(p9, p10, 0x40); 
+        lcd = new WattBob_TextLCD(port);
+        port->write_bit(1,BL_BIT); // LCD backlight on.
+        lcd->cls();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/filterSpeed.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,23 @@
+#include "core.h"
+namespace filterSpeed{
+    Thread thread;
+    static const float freq = 5; //hz 
+    
+    //I/O
+    float speed[3] = {0};
+    
+    void runTask(){
+        //Filter speed with averaging filter
+        //• average of last ‘n’ readings (e.g. n =3)
+        //(raw speed value will be computed by the “car simulator”
+        //process)
+        while(1){
+            wait(1/freq);
+            float avgSpeed = (speed[0] + speed[1] +speed[2])/3;
+            runTimeParams::liveAccess.lock();
+            runTimeParams::avgSpeed = avgSpeed;
+            runTimeParams::liveAccess.unlock();
+            
+        } 
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/getControls.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,49 @@
+#include "core.h"
+
+namespace getControls{
+    Thread thread;
+    static const float freq = 10.0f; //hz
+    
+    //I/O
+    AnalogIn brake(PORT_BRAKE);
+    AnalogIn accel(PORT_ACCEL);
+    void runTask(){
+        //Read brake and accelerator values from variable resistors
+        
+        while(1){
+        
+        runTimeParams::liveAccess.lock();
+        runTimeParams::brakeForce = brake.read();
+        runTimeParams::accelForce = brake.read();
+        runTimeParams::liveAccess.unlock();
+        
+        wait(1/freq);
+        }
+        //float acceleration;
+//    float slowage;
+//    bool engineStatus;
+//
+//    while (true)
+//    {
+//        //  Read Inputs
+//        acceleration = accelerator.read ();
+//        slowage = brake.read ();
+//        //acceleration = 0.6;
+//        //slowage = 0.4;
+//        engineStatus = engineStatusSwt;
+//
+//        //  Write to Engine Manager
+//        accelerationQueueEM.put (&acceleration);
+//        slowageQueueEM.put (&slowage);
+//        engineStatusQueueEM.put (&engineStatus);
+//
+//        //  Write to Engine Status Unit
+//        accelerationQueueESU.put (&acceleration);
+//        slowageQueueESU.put (&slowage);
+//        engineStatusQueueLC.put (&engineStatus);
+//
+//        wait_us (99934);
+//    }
+        
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/getIgnition.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,20 @@
+#include "core.h"
+
+namespace getIgnition{
+    Thread thread;
+    static const float freq = 2; //hz
+    
+    //I/O
+    DigitalIn ignition(PORT_IGNITION);
+    DigitalOut led1(IGNITION_LED);
+    
+    void runTask(){
+        //Read engine on/off switch and show current state on a LED
+        while(1){
+            
+            
+            led1 = ignition.read();
+            wait(1/freq);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/sendMail.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,74 @@
+#include "core.h"
+
+namespace mailData{
+    Mail<mail_t, 100> mailBox;
+}
+
+
+
+namespace loadMail{
+    
+    Thread thread;
+    static const float freq = 0.2; //hz
+    
+    //I/O
+    
+    void runTask(){
+        //Send speed, accelerometer and brake values to a 100 element
+        //MAIL queue
+        //• A MAIL queue is a structure available in the MBED RTOS
+        while(1){
+            using namespace mailData;
+            mail_t *mail = mailBox.alloc();
+            
+            runTimeParams::liveAccess.lock();
+            
+            mail->speed = runTimeParams::avgSpeed;
+            mail->accel = runTimeParams::accelForce;
+            mail->brake = runTimeParams::brakeForce;
+            
+            runTimeParams::liveAccess.unlock();
+            
+            mailBox.put(mail);
+            
+            wait(1.0f/freq);
+        }
+        
+    }
+}
+
+namespace sendMail{
+    
+    Thread thread;
+    const float freq = 0.05;
+    const float executionTime = 0;
+    Serial pc(USBTX, USBRX); // tx, rx
+    bool init = true;
+    
+    void runTask(){
+        //Dump contents of feature_7 MAIL queue to the serial
+        //connection to the PC. (Data will be passed through the MBED
+        //USB connection). See later technical note
+        
+        /* Mail */
+        if (init){
+             pc.printf("speed,acceleration,brake\n\r");
+             init = false;
+             } //?
+        using namespace mailData;
+        osEvent evt = mailBox.get();
+        if (evt.status == osEventMail){
+            mail_t * mail = (mail_t*)evt.value.p;
+            pc.printf("%.2f,%.2f,%.2f\n\r",mail->speed,mail->accel,mail->brake);
+            
+            mailBox.free(mail);
+            wait((1.0/freq)-executionTime);
+        }
+            
+    }
+}
+
+
+
+        
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/sideLights.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,18 @@
+#include "core.h"
+
+namespace sideLights{
+    Thread thread;
+    static const float freq = 1; //hz
+    
+    //I/O
+    DigitalIn lightSwitch(PORT_BRAKE);
+    DigitalOut led(PORT_SIDE_LIGHTS);
+    void runTask(){
+        //Read a single side light switch and set side lights accordingly
+        while(1){
+            wait(1/freq);
+            led = lightSwitch;
+        }
+        
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/speedIndicator.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,23 @@
+#include "core.h"
+
+namespace speedIndicator{
+    Thread thread;
+    static const float freq = 1; //hz
+    
+    //I/O
+    DigitalOut led(PORT_REDBOX_LED1);
+    
+    void runTask(){
+        //Monitor speed and if it goes over 88 mph switch on a LED on
+        //the RedBox unit
+        led = 0;
+        while(1){
+            
+            
+            runTimeParams::liveAccess.lock();
+            led = (runTimeParams::avgSpeed>88);
+            runTimeParams::liveAccess.unlock();
+            wait(1/freq);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/turnSignal.cpp	Tue Mar 27 14:35:09 2018 +0000
@@ -0,0 +1,28 @@
+#include "core.h"
+
+namespace turnSignal{
+    Thread thread;
+    static const float freq = 0.5; //hz
+    
+    //I/O
+    DigitalIn lSwitch(PORT_TURN_SIGNAL_SWITCH_LEFT);
+    DigitalIn rSwitch(PORT_TURN_SIGNAL_SWITCH_RIGHT);
+    
+    PwmOut lLed(PORT_TURN_SIGNAL_LED_LEFT);
+    PwmOut rLed(PORT_TURN_SIGNAL_LED_RIGHT);
+    
+    void runTask(){
+        //Read the two turn indicator switches and flash appropriate
+        //indicator LEDs at a rate of 1Hz. If both switches are switched on
+        //then flash both indicator LEDs at a rate of 2Hz (hazard mode).
+
+        lLed.period(1.0f);
+        rLed.period(1.0f);
+        while(1){
+            wait(1.0f/freq);
+            lSwitch.read() ? lLed.write(0.5f) : lLed.write(0.0f);
+            rSwitch.read() ? rLed.write(0.5f) : rLed.write(0.0f);
+        }
+        
+    }
+}