Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MCP23017 WattBob_TextLCD mbed-rtos
main.cpp
00001 /******************************************************************************* 00002 00003 filename :: main.cpp 00004 ====================== 00005 00006 Description 00007 Assignment 3 of the B31DG Embedded Software Course. 00008 It consists in writing a simple microcontroller based on the MBED RTOS 00009 library for the LPC1768 board. The repetitive tasks have been implemented 00010 by using RTOS Timers, 4 to be specific. One for the car simulator, one 00011 launching tasks whose frequency is either 5 Hz or 10 Hz, one launching 00012 tasks whose frequency is either 2 Hz, 1 Hz or 0.5 Hz and the last one 00013 is launching tasks running at a frequency of 0.2 Hz and 0.5Hz. 00014 00015 Version 00016 Antoine V1.3 February 2018 00017 00018 History 00019 v1.2 Wrote documentation 00020 v1.1 Added timers calling tasks multiple of their execution frequency 00021 v1.0 First Complete Version (all tasks done and 11 Timers) 00022 ... 00023 v0.2 Build main structure of program 00024 v0.1 Initial Version 00025 ........................................................................ 00026 00027 Detailed Description 00028 11 tasks were to be performed: 00029 - SIMULATOR - 20 Hz 00030 Write a simple car simulator which will compute the car speed based 00031 on the values of the accelerator (%), the brake (%) and the state 00032 of the engine. 00033 - TASK 1 - 10 Hz 00034 Read the value of 2 analog inputs which correspond to the values of 00035 acceleration and brake. 00036 - TASK 2 - 2 Hz 00037 Read a digital input switch corresponding to the state of the engine 00038 - TASK 3 - 5 Hz 00039 Filter the speed with an averaging filter. 00040 - TASK 4 - 2 Hz 00041 Show the use of a brake with a digital output 00042 - TASK 5 - 1 Hz 00043 Monitor the speed, and if it goes above a given threshold, switch on 00044 a digital output 00045 - TASK 6 - 2 Hz 00046 Display on the screen travelled distance & the last average speed 00047 - TASK 7 - 0.2 Hz 00048 Send into a MAIL queue object the values of speed, accelerometer and 00049 brake. The queue has a size of 100. 00050 - TASK 8 - 0.05 Hz 00051 Dump the the content of the queue into the serial port. 00052 - TASK 9 - 1 Hz 00053 Read a digital switch and set a digital output based on its state 00054 - TASK 10 - 0.5 Hz 00055 Read two switches and set two LEDs according to their state. They 00056 blink at 1 Hz if only one of the switch is on, at 2 Hz is they 00057 are both on. 00058 00059 ......................................................................... 00060 00061 Personnal Values None 00062 .............................. 00063 00064 Method 00065 I decided not to use global variables between the different functions called 00066 by the RTOS timer as a challenge and because it is not safe to use global 00067 variables in most of the case because you lose control on the origin of 00068 a modification on them. 00069 Hence, I declared a structure which is used by the different tasks and 00070 contains pointers to every variables, structures or object needed. 00071 To make it work, I had to slightly modify the RTOSTimer library and remove 00072 the 'const' keyword protecting the given pointer to function calls. 00073 00074 The functions used by for the different task execution are located in the 00075 included files "my_tasks" while "my_tools" contains functions called by the 00076 tasks in order to simplify their code. 00077 00078 The header "my_stucture" contains 3 small structures: 00079 - engineRAWValues which stores the RAW values of the car 00080 - carStatistics which stores values about speed and distance 00081 - mailStruct which is the structure required by the mail Queue object 00082 00083 .......................................................................... 00084 00085 00086 00087 *******************************************************************************/ 00088 00089 00090 #include "mbed.h" 00091 #include "rtos.h" 00092 #include "my_tools.h" 00093 #include "my_structures.h" 00094 #include "my_tasks.h" 00095 00096 #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(ON,BL_BIT) 00097 #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(OFF,BL_BIT) 00098 00099 /******************************************************************************* 00100 * PIN DECLARATION 00101 ******************************************************************************/ 00102 #define ANALOG_INPUT_1 p20 00103 #define ANALOG_INPUT_2 p18 00104 00105 #define DIGITAL_IN_ENGINE p15 00106 #define DIGITAL_IN_SIDE p16 00107 #define DIGITAL_IN_LEFT p11 00108 #define DIGITAL_IN_RIGHT p14 00109 00110 #define DIGITAL_OUT_OVERSPEED p26 00111 #define DIGITAL_OUT_BRAKE p25 00112 00113 using namespace std; 00114 00115 // =============== 00116 // System Inputs 00117 // =============== 00118 AnalogIn ACCELERATOR(ANALOG_INPUT_1); 00119 AnalogIn BRAKE(ANALOG_INPUT_2); 00120 DigitalIn ENGINE_SWITCH(DIGITAL_IN_ENGINE); 00121 DigitalIn SIDE_LIGHT_SWITCH(DIGITAL_IN_SIDE); 00122 DigitalIn LEFT_SWITCH(DIGITAL_IN_LEFT); 00123 DigitalIn RIGHT_SWITCH(DIGITAL_IN_RIGHT); 00124 00125 // ================ 00126 // System Outputs 00127 // ================ 00128 DigitalOut SIDE_LIGHT_INDICATOR(LED1); 00129 PwmOut LEFT_LIGHT(LED2); 00130 PwmOut RIGHT_LIGHT(LED3); 00131 DigitalOut ENGINE_LIGHT(LED4); 00132 DigitalOut OVERSPEED_LIGHT(DIGITAL_OUT_OVERSPEED); 00133 DigitalOut BRAKE_LIGHT(DIGITAL_OUT_BRAKE); 00134 00135 /******************************************************************************* 00136 * FUNCTION PROTOTYPES 00137 ******************************************************************************/ 00138 00139 /** Initialize the main structure used in the program 00140 * 00141 * @param: Set a arguments used to initalize the structure, all pointers 00142 */ 00143 void initializeStructure(carStructure &car, 00144 engineRAWValues *rawValues, 00145 carStatistics *carStat, 00146 Mail<mailStruct,100> *mailQueue, 00147 Mutex *rawMutex, 00148 Mutex *speedMutex, 00149 Mutex *mailMutex, 00150 MCP23017 *par_port, 00151 WattBob_TextLCD *lcd, 00152 Serial *pcSerial 00153 ); 00154 00155 /******************************************************************************* 00156 * MAIN FUNCTION 00157 ******************************************************************************/ 00158 int main() 00159 { 00160 /**************************** 00161 * MAIN SETUP 00162 ****************************/ 00163 // ---------------------------------------------------------------------- // 00164 // Screen Variables 00165 MCP23017 *par_port; 00166 WattBob_TextLCD *lcd; 00167 par_port = new MCP23017(p9, p10, 0x40); // initialise 16-bit I/O chip 00168 lcd = new WattBob_TextLCD(par_port); // initialise 2*26 char display 00169 00170 // Turn LCD backlight ON 00171 par_port->write_bit(ON,BL_BIT); 00172 00173 // Init Message on Screen 00174 lcd->cls(); 00175 lcd->locate(0,0); 00176 lcd->printf("Init..."); 00177 wait(0.5); 00178 00179 00180 // ---------------------------------------------------------------------- // 00181 // USB Connection to PC 00182 Serial PCSerial(USBTX,USBRX); 00183 // Increase Baud Rate of Serial Connection 00184 PCSerial.baud(115200); 00185 00186 /**************************** 00187 * DECLARE STRUCTURES 00188 ****************************/ 00189 // ---------------------------------------------------------------------- // 00190 // ENGINE RAW VALUES 00191 Mutex rawMutex; 00192 engineRAWValues rawValues; 00193 initRAWValuesStruct(rawValues); 00194 // ---------------------------------------------------------------------- // 00195 // CAR STATISTICS VALUES 00196 vector<float> speedVec(SIZE_SPEED_VECTOR); 00197 Mutex speedMutex; 00198 carStatistics carStats; 00199 initCarStatisticsStruct(carStats, &speedVec); 00200 00201 // ---------------------------------------------------------------------- // 00202 // MAIL QUEUE STRUCTURE 00203 Mail<mailStruct,100> mailQueue; 00204 Mutex mailMutex; 00205 00206 // ---------------------------------------------------------------------- // 00207 // Declare structure to be used by the different task functions 00208 carStructure myCar; 00209 initializeStructure(myCar, 00210 &rawValues, &carStats, 00211 &mailQueue, 00212 &rawMutex, &speedMutex, &mailMutex, 00213 par_port, lcd, 00214 &PCSerial 00215 ); 00216 00217 // ---------------------------------------------------------------------- // 00218 // ---------------------------------------------------------------------- // 00219 /**************************** 00220 * DECLARE RTOS TIMER 00221 ****************************/ 00222 RtosTimer carSimulator(task0_carSim, osTimerPeriodic, (void *) &myCar); 00223 RtosTimer timer_1(timer1, osTimerPeriodic, (void *) &myCar); 00224 RtosTimer timer_2(timer2, osTimerPeriodic, (void *) &myCar); 00225 RtosTimer timer_3(timer3, osTimerPeriodic, (void *) &myCar); 00226 00227 00228 /**************************** 00229 * START RTOS TIMER 00230 ****************************/ 00231 carSimulator.start(50); // 20Hz 00232 timer_1.start(100); // 10 Hz 00233 timer_2.start(500); // 2 Hz 00234 timer_3.start(5000); // 0.2 Hz 00235 00236 // RtosTimer will run forever 00237 Thread::wait(osWaitForever); 00238 00239 return 0; 00240 } 00241 00242 00243 void initializeStructure(carStructure &car, 00244 engineRAWValues *rawValues, 00245 carStatistics *carStat, 00246 Mail<mailStruct,100> *mailQueue, 00247 Mutex *rawMutex, 00248 Mutex *speedMutex, 00249 Mutex *mailMutex, 00250 MCP23017 *par_port, 00251 WattBob_TextLCD *lcd, 00252 Serial *pcSerial 00253 ) 00254 { 00255 // System Inputs 00256 car.p_accelerator = &ACCELERATOR ; 00257 car.p_brake = &BRAKE ; 00258 car.p_engineSwitch = &ENGINE_SWITCH ; 00259 car.p_sideLightSwitch = &SIDE_LIGHT_SWITCH ; 00260 car.p_leftSwitch = &LEFT_SWITCH ; 00261 car.p_rightSwitch = &RIGHT_SWITCH ; 00262 00263 // System Outputs 00264 car.p_sideLightIndicator = &SIDE_LIGHT_INDICATOR ; 00265 car.p_leftLight = &LEFT_LIGHT ; 00266 car.p_rightLight = &RIGHT_LIGHT ; 00267 car.p_engineLight = &ENGINE_LIGHT ; 00268 car.p_overspeedLight = &OVERSPEED_LIGHT ; 00269 car.p_brakeLight = &BRAKE_LIGHT ; 00270 00271 // Values Structure; 00272 car.p_rawValues = rawValues; 00273 car.p_carStats = carStat; 00274 car.p_mailQueue = mailQueue; 00275 car.nbElementInQueue = 0; 00276 00277 // Mutex 00278 car.p_rawMutex = rawMutex; 00279 car.p_statMutex = speedMutex; 00280 car.p_mailMutex = mailMutex; 00281 00282 // Serial Connection 00283 car.p_PC = pcSerial; 00284 00285 // Screen variables 00286 car.p_par_port = par_port; 00287 car.p_lcd = lcd; 00288 }
Generated on Wed Jul 13 2022 17:41:33 by
1.7.2