Plz

Dependencies:   mbed CANMsg Adafruit_LEDBackpack

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "CANMsg.h"
00003 #include "Adafruit_LEDBackpack.h"
00004 
00005 Ticker ticker;
00006 Ticker ticker2;
00007 Timer timer;
00008 AnalogIn currentPot(p15);
00009 float curr_val = 0;
00010 float curr_reading = 0;
00011 AnalogIn speedPot(p16);
00012 float speed_val = 0;
00013 float speed_reading = 0;
00014 float speed_reading_ms = 0;
00015 float speed_reading_kmh = 0;
00016 DigitalIn ignition(p21);
00017 DigitalIn regen(p22);
00018 DigitalIn rev(p23);
00019 DigitalIn brake(p24);
00020 DigitalIn accel(p25);
00021 float maxBusCurrent = 1.0;
00022 float actualSpeedms = 0;
00023 float actualSpeedrpm = 0;
00024 float actualSpeedkmh = 0;
00025 
00026 I2C i2c(p28,p27);
00027 Adafruit_LEDBackpack display(&i2c);
00028 
00029 DigitalOut debug1(LED1);
00030 DigitalOut debug2(LED2);
00031 DigitalOut debug3(LED3);
00032 DigitalOut debug4(LED4);
00033 
00034 //CAN can1(p9, p10);
00035 CAN can1(p30, p29);
00036 
00037 const uint16_t SIGNAL_ID = 0x501;
00038 const uint16_t BUS_ID = 0x502;
00039 const uint16_t TRITIUM_ID = 0x603;
00040 const uint8_t DISPLAY_ADDR = 0x70; 
00041 char counter = 0;
00042 uint8_t dig1 = 0;
00043 uint8_t dig2 = 0;
00044 
00045 Serial pc(USBTX, USBRX); // tx, rx
00046 
00047 CANMsg driverControls;
00048 CANMsg busCurrent;
00049 CANMsg msgReceived;
00050 
00051 //====================================================================
00052 
00053 void printMsg(CANMessage& msg)
00054 {
00055     pc.printf("-------------------------------------\r\n");
00056     pc.printf("  ID      = 0x%.3x\r\n", msg.id);
00057     pc.printf("  Type    = %d\r\n", msg.type);
00058     pc.printf("  Format  = %d\r\n", msg.format);
00059     pc.printf("  Length  = %d\r\n", msg.len);
00060     pc.printf("  Data    =");
00061     pc.printf("-------------------------------------\r\n");
00062     for(int i = 0; i < msg.len; i++)
00063         pc.printf(" 0x%.2X", msg.data[i]);
00064     pc.printf("\r\n");
00065 }
00066 
00067 //=====================================================================
00068 
00069 void onCanReceived()
00070 {
00071     can1.read(msgReceived);
00072 //    pc.printf("-------------------------------------\r\n");
00073 //    pc.printf("CAN message received\r\n");
00074 //    pc.printf("-------------------------------------\r\n");
00075     //printMsg(msgReceived);
00076     //wait(1);
00077     if (msgReceived.id == TRITIUM_ID) {
00078         // extract data from the received CAN message 
00079         // in the same order as it was added on the transmitter side
00080            
00081 //        pc.printf("  counter = %d\r\n", counter);
00082 //        pc.printf("  voltage = %e V\r\n", voltage);
00083         debug3 = !debug3;
00084         //printMsg(msgReceived);
00085         msgReceived >> actualSpeedrpm;
00086         //pc.printf("speed in m/s %f\r\n", actualSpeedms);
00087         msgReceived >> actualSpeedms; 
00088     }
00089     //timer.start(); // to transmit next message in main
00090 }
00091 
00092 //======================================================================
00093 
00094 void displayDigit(uint8_t digit, uint8_t position)
00095 {
00096     //    if (position > 4)
00097 //    {}
00098     //printf("digit = %d\t position = %d\r\n", digit, position);
00099     if (digit > 9) {
00100         display.displaybuffer[position] = 0;
00101     } else if (digit == 0) {
00102         display.displaybuffer[position] = 63;
00103     } else if (digit == 1) {
00104         display.displaybuffer[position] = 48;
00105     } else if (digit == 2) {
00106         display.displaybuffer[position] = 91;
00107     } else if (digit == 3) {
00108         display.displaybuffer[position] = 79;
00109     } else if (digit == 4) {
00110         display.displaybuffer[position] = 102;
00111     } else if (digit == 5) {
00112         display.displaybuffer[position] = 109;
00113     } else if (digit == 6) {
00114         display.displaybuffer[position] = 125;
00115     } else if (digit == 7) {
00116         display.displaybuffer[position] = 7;
00117     } else if (digit == 8) {
00118         display.displaybuffer[position] = 127;
00119     } else if (digit == 9) {
00120         display.displaybuffer[position] = 103;
00121     }
00122     //timer.start();
00123     display.writeDisplay();
00124     //timer.stop();
00125     //printf("Time to writeDisplay() is %dus\r\n", timer.read_us());
00126     //timer.reset();
00127 }
00128 
00129 void displayNumber(uint8_t number, bool position)
00130 {
00131     //timer.start();
00132     for (int n = 0; n < 100; (n = n + 10))
00133     {
00134         if (number >= n && number < n + 10)
00135         {
00136             dig1 = n/10;
00137             dig2 = number - n;
00138         }
00139     }
00140     
00141     if (number > 99)
00142     {dig1 = 0;
00143     dig2 = 0;}
00144 
00145     if (position) 
00146     {
00147         displayDigit(dig1, 3);
00148         displayDigit(dig2, 4);
00149     } 
00150     else if (!position) 
00151     {
00152         displayDigit(dig1, 0);
00153         displayDigit(dig2, 1);
00154     }
00155     //timer.stop();
00156     //printf("time taken to print number %d is %dus\r\n", number, timer.read_us());
00157     //timer.reset();
00158 }
00159 
00160 void setDriverControls()
00161 {
00162     driverControls.clear();
00163     driverControls.id = SIGNAL_ID;
00164     driverControls << speed_val;
00165     driverControls << curr_val;
00166     
00167     busCurrent.clear();
00168     busCurrent.id = BUS_ID;
00169     busCurrent << 0;
00170     busCurrent << maxBusCurrent;
00171 }
00172 
00173 void sendCAN()
00174 {
00175     setDriverControls();
00176     //printMsg(driverControls);
00177     //printMsg(busCurrent);
00178     if(can1.write(driverControls))
00179     {
00180         debug1 = !debug1;
00181     }
00182     else
00183     {
00184         //wait(0.5);
00185         
00186         
00187     }
00188     
00189     if(can1.write(busCurrent))
00190     {
00191         debug2 = !debug2;
00192     }
00193     else
00194     {
00195         //wait(0.5);
00196         
00197     }
00198 }
00199 
00200 void dispSpeed()
00201 {
00202     actualSpeedkmh = abs(actualSpeedms) * 3.6;
00203     displayNumber( (uint8_t) actualSpeedkmh , 1);
00204     displayNumber( (uint8_t) speed_reading_kmh , 0);
00205 }
00206 
00207 int main()
00208 {
00209     msgReceived = CANMsg();
00210     pc.baud(115200);
00211     can1.frequency(1000000);
00212     ticker.attach(&sendCAN, 0.1);
00213     ticker2.attach(&dispSpeed, 0.5);
00214     can1.attach(&onCanReceived); 
00215     ignition.mode(PullUp);
00216     regen.mode(PullUp);
00217     rev.mode(PullUp);
00218     brake.mode(PullUp);
00219     accel.mode(PullUp);
00220     display.begin(DISPLAY_ADDR);
00221     display.setBrightness(10);
00222     
00223     //pc.printf("-------------------------------------\r\n");
00224     //printf("Attempting to send a CAN message\n");
00225     //printf("Current and Speed: %f and %f\n", currentPot.read(), speedPot.read());
00226     //pc.printf("-------------------------------------\r\n");
00227     
00228     while(1) {
00229         
00230         //curr_val = 0.2;
00231         //speed_val = 5.0;
00232         
00233         curr_reading = currentPot.read();
00234         speed_reading = speedPot.read();
00235         speed_reading_ms = speed_reading * 30;
00236         speed_reading_kmh = speed_reading_ms*3.6;
00237         
00238         //displayNumber(1,1);
00239         //displayNumber(1,0);
00240 
00241         
00242         bool ignition_reading = ignition.read();
00243         bool regen_reading = regen.read();
00244         bool rev_reading = rev.read();
00245         bool brake_reading = brake.read();
00246         bool accel_reading = accel.read();
00247         
00248         //pc.printf("Current reading: %f\r\n", curr_reading);
00249         //pc.printf("Speed reading: %f\r\n", speed_reading);
00250         //pc.printf("Ignition reading: %d\r\n", ignition_reading);
00251         //pc.printf("Regen reading: %d\r\n", regen_reading);
00252         //pc.printf("Forward/reverse reading: %d\r\n", rev_reading);
00253         //pc.printf("Brake reading: %d\r\n", brake_reading);
00254         
00255         //pc.printf("Accelerator reading: %d\r\n\r\n", accel_reading);
00256         //wait(2);
00257         
00258         if (ignition)
00259         {
00260             curr_val = 0;
00261             speed_val = 0;
00262         }
00263         else
00264         {
00265             if (!brake && !regen)
00266             {
00267                 speed_val = 0;
00268                 curr_val = curr_reading;
00269             }
00270             else if(!brake && regen)
00271             {
00272                 speed_val = 0;
00273                 curr_val = 0;
00274             }
00275             else
00276             {
00277                 if (rev)
00278                 {
00279                     speed_val = (-1)*speed_reading_ms;
00280                 }
00281                 else
00282                 {
00283                     speed_val = speed_reading_ms;
00284                 }
00285                 
00286                 if (!accel)
00287                 {
00288                     curr_val = curr_reading;
00289                 }
00290                 else
00291                 {
00292                     curr_val = 0;
00293                 }
00294              }
00295             
00296         }
00297         //pc.printf("speed_val: %f\r\n", speed_val);
00298         //pc.printf("curr_val: %f\r\n\r\n", curr_val);
00299         //sendCAN();
00300         //pc.printf("\r\n\r\n");
00301         //wait(3);
00302     }
00303 }