Aaron Stevenson / Mbed 2 deprecated Assignment3

Dependencies:   mbed MCP23017 mbed-rtos WattBob_TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 
00004 #include "MCP23017.h"
00005 #include "WattBob_TextLCD.h"
00006 
00007 #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT)
00008 #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT)
00009 
00010 MCP23017 *par_port;             // pointer to 16-bit parallel I/O object
00011 WattBob_TextLCD *lcd;           // pointer to 2*16 chacater LCD object
00012 
00013 AnalogIn accel(p15);            // Analog in
00014 AnalogIn brake(p16);
00015 
00016 float speed;                    // IO values 
00017 float kmph;
00018 float aveSpeed;
00019 float acc;
00020 float br;
00021 float distance = 0;
00022 
00023 DigitalOut sideLights(LED1);    // mbed out
00024 DigitalOut lIndicator(LED2);
00025 DigitalOut rIndicator(LED3);
00026 DigitalOut engLight(LED4);
00027 
00028 DigitalOut brakeLights(p6);     // redbox out
00029 DigitalOut fluxCapacitor(p7);
00030 
00031 DigitalIn  engine(p5);          // switches in
00032 DigitalIn  lightSwitch(p8);
00033 DigitalIn  lIndicate(p11);
00034 DigitalIn  rIndicate(p12);
00035 
00036 int l, r, lr;                   // indicator values
00037 
00038 Serial pc(USBTX, USBRX);        // serial tx, rx
00039 
00040 Thread sp;                      // threads. Speed
00041 Thread task1;
00042 Thread task246;
00043 Thread task3;
00044 Thread task59;
00045 Thread task7;
00046 Thread task8;
00047 Thread task10;
00048 Thread indi;                    // indicators
00049 
00050 typedef struct {                //  mail
00051     float m_speed;
00052     float m_acc;
00053     float m_br;
00054 } mail_t;
00055 
00056 Mail<mail_t, 100> mail_box;
00057 
00058 void acceleration()                                 //  1.  read brake and accelerator          10
00059 {
00060     while (1) {
00061         acc = accel.read()*3.3;
00062         br = brake.read()*3.3;
00063         
00064         Thread::wait(100);
00065     }
00066 }
00067 void getSpeed()                                     // calculation for speed & distance thread  20
00068 {
00069     while (1) {
00070         speed = (acc - br)*16.835;                      // speed in m/s. Max = 55
00071         if (speed < 0) {
00072             speed = 0;
00073         }
00074         kmph = speed*3.6;                               // convert speed to km/ph. Change to *2.24 for ~mph
00075         distance = distance + (speed*0.05)/1000;        // distance = speed*time /1000 for m to km (/1609.34 for miles)
00076         
00077         Thread::wait(50);
00078     }
00079 }
00080 void ignition()                                     //  2.   Read engine on/off show LED  subroutine    2
00081 {
00082     if (engine.read() > 0) {
00083         engLight = 1;
00084     } else {
00085         engLight = 0;
00086         aveSpeed = 0;
00087         acc = 0;
00088         br = 0;
00089     }
00090 }
00091 void speedo()                                       //  3.   Average last n speed readings  thread      5
00092 {
00093     while (1) {
00094         for (int i = 0; i<3; i++) {
00095             aveSpeed = kmph + aveSpeed;                 // in km/h
00096         }
00097         aveSpeed = aveSpeed/4;
00098 
00099         Thread::wait(200);
00100     }
00101 }
00102 void braking()                                      //  4.   Brake indicated by LED   subroutine        2
00103 {
00104     if ( br > 0) {
00105         brakeLights = 1;
00106     } else {
00107         brakeLights = 0;
00108     }
00109 }
00110 void greatScott()                                   //  5.   if speed > 88 LED on  subroutine           1
00111 {
00112     if (kmph > 141.6) {                                 // 141.6 km = 88 miles
00113         fluxCapacitor = 1;
00114     } else {
00115         fluxCapacitor = 0;
00116     }
00117 }
00118 void LCD()                                          //  6.   display odometer and speed  subroutine     2
00119 {
00120     lcd->locate(0,0);
00121     lcd->printf("KM:%0.1f",distance);
00122     lcd->locate(1,0);
00123     lcd->printf("KM/H:%.1f",aveSpeed);
00124 }
00125 void send_thread (void)                             //  7.  speed, acc, brake MAILq thread              0.2
00126 {
00127     while (true) {
00128         mail_t *mail = mail_box.alloc();
00129         mail->m_speed = aveSpeed;
00130         mail->m_acc = acc;
00131         mail->m_br = br;
00132         mail_box.put(mail);
00133 
00134         Thread::wait(5000);
00135     }
00136 }
00137 void toSerial()                                     //  8.   MAILq to serial PC    thread               0.05
00138 {
00139     while (1) {
00140         osEvent evt = mail_box.get();
00141         mail_t *mail = (mail_t*)evt.value.p;
00142         pc.printf("\nSpeed: %.2f \n\r", mail->m_speed);
00143         pc.printf("Acceleration: %.2f \n\r", mail->m_acc);
00144         pc.printf("Braking: %.2f \n\r", mail->m_br);
00145         
00146         Thread::wait(20000);
00147     }
00148 }
00149 void lights()                                       //  9.   set side lights  subroutine                1
00150 {
00151         if (lightSwitch.read() == 1) {                  // If light switch on
00152             sideLights = 1;                             // turn on side lights
00153         } else {
00154             sideLights = 0;
00155         }
00156 }
00157 void indicators()                                   //  10.  check indicators  thread                   0.5
00158 {
00159     while (1) {
00160 
00161         if ((lIndicate == 1) && (rIndicate == 1)) {         //  If both switch on
00162             lr = 1;                                         //      both LED at 2Hz
00163         } else if ((lIndicate == 1) && (rIndicate == 0)) {  //  if left switch on
00164             l = 1;                                          //      left LED at 1Hz
00165             r = 0;
00166             lr = 0;
00167         } else if ((lIndicate == 0) && (rIndicate == 1)) {  //  if right switch on
00168             r = 1;                                          //      right LED at 1Hz
00169             l = 0;
00170             lr = 0;
00171         } else if ((lIndicate == 0) && (rIndicate == 0)) {  // both off
00172             r = 0;
00173             l = 0;
00174             lr = 0;
00175         }
00176 
00177         Thread::wait(2000);
00178     }
00179 }
00180 
00181 void indicate()                                     // flash indicators
00182 {
00183     while (1) {
00184         int x = 1000;
00185         if (lr == 1) {                  //  If both switch on
00186             lIndicator = !lIndicator;     //      both LED at 2Hz
00187             rIndicator = !rIndicator;
00188             x = 500;
00189         } else if (l == 1) {            //  if left switch on
00190             lIndicator = !lIndicator;     //      left LED at 1Hz
00191             rIndicator = 0;
00192             x = 1000;
00193         } else if (r == 1) {           //  if right switch on
00194             rIndicator = !rIndicator;     //      right LED at 1Hz
00195             lIndicator = 0;
00196             x = 1000;
00197         }
00198 
00199         Thread::wait(x);
00200     }
00201 }
00202 void t59()                                   //  thread for 5 & 9           1Hz
00203 {
00204     while (1) {
00205         greatScott();
00206         lights();
00207 
00208         Thread::wait(1000);
00209     }
00210 }
00211 void t246()                                     //  thread for 2, 4, & 6     2Hz
00212 {
00213     while (1) {
00214         ignition();
00215         LCD();
00216         braking();
00217 
00218         Thread::wait(500);
00219     }
00220 }
00221 
00222 int main()
00223 {
00224     par_port = new MCP23017(p9, p10, 0x40);             // initialise 16-bit I/O chip
00225     lcd = new WattBob_TextLCD(par_port);                // initialise 2*26 char display
00226     par_port->write_bit(1,BL_BIT);                      // turn LCD backlight ON
00227                                         //Hz
00228     sp.start(getSpeed);                 //20
00229     task1.start(acceleration);          //10
00230     task246.start(t246);                //2
00231     task3.start(speedo);                //5
00232     task59.start(t59);                  //1
00233     task7.start(callback(send_thread)); //0.2
00234     task8.start(toSerial);              //0.05
00235     task10.start(indicators);           //0.5
00236     indi.start(indicate);               // 1-2
00237 
00238 }