Example software of using the mbed-rtos to control a simple vehicle's on board computer
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
main.cpp
- Committer:
- sk398
- Date:
- 2016-03-29
- Revision:
- 2:13a9394ba2e0
- Parent:
- 1:cdf851858518
- Child:
- 3:8192bbde17b3
File content as of revision 2:13a9394ba2e0:
#include "mbed.h" #include "rtos.h" #define TRUE 1 #define FALSE 0 #define HIGH 1 #define LOW 0 // ============================================================================ // MBED Pin Assignments // ============================================================================ AnalogIn Brake(p19); AnalogIn Accelerometer(p20); DigitalIn EngineState(p18); DigitalIn LeftIndicator(p17); DigitalIn RightIndicator(p16); DigitalIn SideLightIndicator(p15); DigitalOut EngineStateInd(LED1); DigitalOut SideLightInd(LED2); PwmOut LeftLightInd(LED3); PwmOut RightLightInd(LED4); PwmOut OverSpeedInd(p22); PwmOut AvSpeedWiper(p21); // ============================================================================ // Global Data Structure Declerations // ============================================================================ typedef struct { bool EngineState; float RawAccelerometer; float RawBraking; } CarRawParams; Mutex rawParamsMutex; CarRawParams rawParams; typedef struct { float rawAcceleration; float rawSpeed; } CarProcessedParams; Mutex processedParamsMutex; CarProcessedParams processedParams; typedef struct { float AverageSpeed; } CarFilteredParams; Mutex filteredParamsMutex; CarFilteredParams filteredParams; // ============================================================================ // Car Simulation // ============================================================================ void CarSimulator(void const *arg) { rawParamsMutex.lock(); float currentAccelerometer = rawParams.RawAccelerometer; float currentBrake = rawParams.RawBraking; bool currentEngineState = rawParams.EngineState; rawParamsMutex.unlock(); processedParamsMutex.lock(); float currentRawSpeed = processedParams.rawSpeed; processedParamsMutex.unlock(); if(currentEngineState) { if(currentRawSpeed >= 100) { processedParamsMutex.lock(); processedParams.rawSpeed = 100; processedParamsMutex.unlock(); } else { // Car Sim Model } } } // ============================================================================ // Control System Tasks // ============================================================================ void Task1_ReadRawData(void const *arg) { rawParamsMutex.lock(); rawParams.RawBraking = Brake.read(); rawParams.RawAccelerometer = Accelerometer.read(); rawParamsMutex.unlock(); } void Task2_ReadEngineState(void const *arg) { rawParamsMutex.lock(); bool currentEngineState = rawParams.EngineState; rawParamsMutex.unlock(); if(currentEngineState) { EngineStateInd = HIGH; } else { EngineStateInd = LOW; } } void Task3_UpdateRCWiper(void const *arg) { filteredParamsMutex.lock(); float currentAverageSpeed = filteredParams.AverageSpeed; filteredParamsMutex.unlock(); AvSpeedWiper.pulsewidth_ms(2*currentAverageSpeed); } void Task8_ReadSideLight(void const *arg) { if(SideLightIndicator) { SideLightInd = TRUE; } else { SideLightInd = FALSE; } } void Task9_ReadIndicatorLights(void const *arg) { // Left Indicator Only if(LeftIndicator && !RightIndicator) { LeftLightInd.period(1.0); LeftLightInd.pulsewidth(0.5); RightLightInd.period(1.0); RightLightInd.pulsewidth(0.0); } // Right Indicator Only else if(!LeftIndicator && RightIndicator) { LeftLightInd.period(1.0); LeftLightInd.pulsewidth(0.0); RightLightInd.period(1.0); RightLightInd.pulsewidth(0.5); } // Left and Right Indicators else if(LeftIndicator && RightIndicator) { LeftLightInd.period(0.5); RightLightInd.period(0.5); LeftLightInd.pulsewidth(0.25); RightLightInd.pulsewidth(0.25); } else { LeftLightInd.period(1.0); LeftLightInd.pulsewidth(0.0); RightLightInd.period(1.0); RightLightInd.pulsewidth(0.0); } } // ============================================================================ // Entry Point Thread // ============================================================================ int main() { AvSpeedWiper.period_ms(20); RtosTimer CarSim(CarSimulator,osTimerPeriodic); Thread::wait(2); RtosTimer Task1(Task1_ReadRawData,osTimerPeriodic); Thread::wait(2); RtosTimer Task2(Task2_ReadEngineState,osTimerPeriodic); Thread::wait(2); RtosTimer Task3(Task3_UpdateRCWiper,osTimerPeriodic); Thread::wait(2); RtosTimer Task8(Task8_ReadSideLight,osTimerPeriodic); Thread::wait(2); RtosTimer Task9(Task9_ReadIndicatorLights,osTimerPeriodic); Thread::wait(2); CarSim.start(50); Task1.start(100); Task2.start(500); Task3.start(1000); Task8.start(1000); Task9.start(2000); Thread::wait(osWaitForever); } // // //Mutex readvalues; //Semaphore read_s(1); // //Mail<value_t, 100> mail_box; // // //float readValue = 1.0; // //typedef struct //{ // float AccelerationRaw; // float BrakeRaw; //} value_t; // // //void readAnalogPins(void const *args) //{ // while(1) // { // value_t *ReadValue = mail_box.alloc(); // // ReadValue -> AccelerationrRaw = Accelerometer.read(); // ReadValue -> BrakeRaw = Brake.read(); // // mail_box.put(ReadValue); // Thread::wait(100); // } // //} // //void sendToPC(void const *args) //{ // while(1) // { // osEvent evt = mail_box.get(); // // if(evt.status == osEventMail) // { // value_t *ReadValue = (value_t*)evt.value.p; // printf("Value: %1.3f\r\n", ReadValue-> vale); // mail_box.free(ReadValue); // } // Thread::wait(5000); // } // //} // //int main() { // // Thread thread_1(readData); // Thread thread_2(sendToPC); // // while(1) { // // myled = 1; // wait(0.2); // myled = 0; // wait(0.2); // } //}