Thomas Milburn
/
FBRDash-tom
can't push chnages :(
Fork of FBRDash by
main.cpp@5:177520d43c87, 2012-10-14 (annotated)
- Committer:
- tomontoast
- Date:
- Sun Oct 14 21:09:49 2012 +0000
- Revision:
- 5:177520d43c87
- Parent:
- 4:53b28844b594
14/10/12
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
intrinseca |
0:1f422ed56e0f | 1 | #include "mbed.h" |
intrinseca |
0:1f422ed56e0f | 2 | #include "TextLCD.h" |
intrinseca | 1:b3907b8d9f65 | 3 | #include "PinDetect.h" |
intrinseca | 1:b3907b8d9f65 | 4 | #include "Menu.h" |
intrinseca | 1:b3907b8d9f65 | 5 | #include "Comms.h" |
intrinseca | 1:b3907b8d9f65 | 6 | #include "PCComms.h" |
intrinseca | 1:b3907b8d9f65 | 7 | #include "Gears.h" |
intrinseca | 1:b3907b8d9f65 | 8 | #include "LEDS.h" |
intrinseca | 1:b3907b8d9f65 | 9 | #include "bigchar.h" |
tomontoast | 4:53b28844b594 | 10 | //Test comment 2 |
intrinseca | 2:825f572902c6 | 11 | //Refresh interval for LCD |
intrinseca | 1:b3907b8d9f65 | 12 | #define LCD_REFRESH_TIME 150000 |
intrinseca | 2:825f572902c6 | 13 | |
intrinseca | 2:825f572902c6 | 14 | //Refresh interval for rev LEDs |
intrinseca | 1:b3907b8d9f65 | 15 | #define REV_REFRESH_TIME 50000 |
intrinseca | 1:b3907b8d9f65 | 16 | |
intrinseca | 2:825f572902c6 | 17 | //Warning lights |
intrinseca | 1:b3907b8d9f65 | 18 | DigitalOut warn[] = { (p20), (p19), (p27), (p18) }; |
intrinseca | 2:825f572902c6 | 19 | |
intrinseca | 2:825f572902c6 | 20 | //mBED LEDs |
intrinseca | 1:b3907b8d9f65 | 21 | DigitalOut debug[] = { (LED1), (LED2), (LED3) }; |
intrinseca |
0:1f422ed56e0f | 22 | |
intrinseca | 2:825f572902c6 | 23 | //Heartbeat LED |
intrinseca | 1:b3907b8d9f65 | 24 | DigitalOut heartbeat(LED4); |
intrinseca | 1:b3907b8d9f65 | 25 | |
intrinseca | 2:825f572902c6 | 26 | //Rev LEDS |
intrinseca | 1:b3907b8d9f65 | 27 | PwmOut leds[] = { (p24), (p25), (p26), (p23), (p22), (p21) }; |
intrinseca | 1:b3907b8d9f65 | 28 | |
intrinseca | 2:825f572902c6 | 29 | //LCD |
intrinseca | 1:b3907b8d9f65 | 30 | TextLCD lcd(p5, p6, p7, p8, p9, p10, p11); |
intrinseca |
0:1f422ed56e0f | 31 | |
intrinseca | 2:825f572902c6 | 32 | //Tickers for refreshing |
intrinseca | 1:b3907b8d9f65 | 33 | Ticker lcdRefreshTicker; |
intrinseca | 1:b3907b8d9f65 | 34 | Ticker revRefreshTicker; |
intrinseca | 2:825f572902c6 | 35 | |
intrinseca | 2:825f572902c6 | 36 | //Used to animate LEDs for testing |
intrinseca | 1:b3907b8d9f65 | 37 | Ticker increment; |
intrinseca | 1:b3907b8d9f65 | 38 | |
intrinseca | 2:825f572902c6 | 39 | //Main car state structure |
intrinseca | 1:b3907b8d9f65 | 40 | State car; |
intrinseca |
0:1f422ed56e0f | 41 | |
tomontoast | 5:177520d43c87 | 42 | //Initialise CAN |
tomontoast | 5:177520d43c87 | 43 | CAN can(p30, p29); |
tomontoast | 5:177520d43c87 | 44 | |
intrinseca | 2:825f572902c6 | 45 | //Classes for various parts of the firmware |
tomontoast | 5:177520d43c87 | 46 | Menu dashMenu(&lcd, p16, p17, p12); //*LCD, OK, Left, Right |
intrinseca | 1:b3907b8d9f65 | 47 | PCComms pc(&car); |
tomontoast | 5:177520d43c87 | 48 | Gears gearButtons(p14, p13, p15, &car.gear, &pc); //Up, Neutral, Down, *Current Gear |
intrinseca | 1:b3907b8d9f65 | 49 | LEDS revs(leds); |
intrinseca |
0:1f422ed56e0f | 50 | |
intrinseca | 2:825f572902c6 | 51 | //Refresh the rev LEDs and warning LEDs |
intrinseca | 1:b3907b8d9f65 | 52 | void revRefresh() |
intrinseca |
0:1f422ed56e0f | 53 | { |
tomontoast | 5:177520d43c87 | 54 | CANMessage msg; |
intrinseca | 1:b3907b8d9f65 | 55 | revs.refresh(car.rpm); |
intrinseca | 1:b3907b8d9f65 | 56 | |
tomontoast | 5:177520d43c87 | 57 | if(car.voltage<12){ |
tomontoast | 5:177520d43c87 | 58 | warn[0]=1; |
tomontoast | 5:177520d43c87 | 59 | }else{ |
tomontoast | 5:177520d43c87 | 60 | warn[0]=0; |
tomontoast | 5:177520d43c87 | 61 | } |
tomontoast | 5:177520d43c87 | 62 | if(car.coolant_temp>110){ |
tomontoast | 5:177520d43c87 | 63 | warn[1]=1; |
tomontoast | 5:177520d43c87 | 64 | }else{ |
tomontoast | 5:177520d43c87 | 65 | warn[1]=0; |
tomontoast | 5:177520d43c87 | 66 | } |
tomontoast | 5:177520d43c87 | 67 | if(car.rpm==0 and car.gear!=0){ |
tomontoast | 5:177520d43c87 | 68 | warn[2]=1; |
tomontoast | 5:177520d43c87 | 69 | }else{ |
tomontoast | 5:177520d43c87 | 70 | warn[2]=0; |
intrinseca | 1:b3907b8d9f65 | 71 | } |
tomontoast | 5:177520d43c87 | 72 | if(can.read(msg)) { |
tomontoast | 5:177520d43c87 | 73 | if(msg.id==100 and msg.len==8){ |
tomontoast | 5:177520d43c87 | 74 | car.rpm = msg.data[0] + (msg.data[1] << 8); |
tomontoast | 5:177520d43c87 | 75 | car.throttle_pos = msg.data[2]; |
tomontoast | 5:177520d43c87 | 76 | car.manifold_pres = msg.data[3]; |
tomontoast | 5:177520d43c87 | 77 | car.air_temp = msg.data[4]; |
tomontoast | 5:177520d43c87 | 78 | car.coolant_temp = msg.data[5]; |
tomontoast | 5:177520d43c87 | 79 | car.lambda = msg.data[6]; |
tomontoast | 5:177520d43c87 | 80 | } |
tomontoast | 5:177520d43c87 | 81 | else if(msg.id==200 and msg.len==8){ |
tomontoast | 5:177520d43c87 | 82 | car.speed = msg.data[0]; |
tomontoast | 5:177520d43c87 | 83 | car.accel_x = msg.data[1]; |
tomontoast | 5:177520d43c87 | 84 | car.accel_y = msg.data[2]; |
tomontoast | 5:177520d43c87 | 85 | car.gear = msg.data[3]; |
tomontoast | 5:177520d43c87 | 86 | car.oil_temp = msg.data[4]; |
tomontoast | 5:177520d43c87 | 87 | car.voltage = msg.data[5]/16.0; |
tomontoast | 5:177520d43c87 | 88 | } |
tomontoast | 5:177520d43c87 | 89 | } |
tomontoast | 5:177520d43c87 | 90 | |
intrinseca |
0:1f422ed56e0f | 91 | } |
intrinseca |
0:1f422ed56e0f | 92 | |
intrinseca | 2:825f572902c6 | 93 | //Refresh the LCD |
intrinseca | 1:b3907b8d9f65 | 94 | void lcdRefresh() |
intrinseca |
0:1f422ed56e0f | 95 | { |
intrinseca | 2:825f572902c6 | 96 | //If menu off screen, display HUD |
intrinseca | 1:b3907b8d9f65 | 97 | if(dashMenu.display == false) |
intrinseca | 1:b3907b8d9f65 | 98 | { |
intrinseca | 1:b3907b8d9f65 | 99 | lcd.locate(0, 0); |
tomontoast | 5:177520d43c87 | 100 | lcd.printf("%3.0fC%5.1dRPM", car.coolant_temp, car.rpm); |
intrinseca | 1:b3907b8d9f65 | 101 | lcd.locate(0, 1); |
tomontoast | 5:177520d43c87 | 102 | lcd.printf("%2dMPH %3.1fV", car.speed, car.voltage); |
intrinseca | 1:b3907b8d9f65 | 103 | |
intrinseca | 1:b3907b8d9f65 | 104 | write_bigchar(&lcd, 13, car.gear); |
intrinseca | 1:b3907b8d9f65 | 105 | } |
intrinseca | 2:825f572902c6 | 106 | //Otherwise show menu |
intrinseca | 1:b3907b8d9f65 | 107 | else |
intrinseca | 1:b3907b8d9f65 | 108 | { |
intrinseca | 1:b3907b8d9f65 | 109 | dashMenu.refresh(); |
intrinseca | 1:b3907b8d9f65 | 110 | } |
intrinseca | 1:b3907b8d9f65 | 111 | |
intrinseca | 2:825f572902c6 | 112 | //Blink heartbeat |
intrinseca | 1:b3907b8d9f65 | 113 | heartbeat = !heartbeat; |
intrinseca |
0:1f422ed56e0f | 114 | } |
intrinseca |
0:1f422ed56e0f | 115 | |
intrinseca | 2:825f572902c6 | 116 | |
intrinseca | 2:825f572902c6 | 117 | //Used to animate LEDs for testing |
intrinseca | 1:b3907b8d9f65 | 118 | /*void doIncrement() |
intrinseca | 1:b3907b8d9f65 | 119 | { |
intrinseca | 1:b3907b8d9f65 | 120 | if(car.rpm < LIMIT && car.gear > 0) |
intrinseca | 1:b3907b8d9f65 | 121 | car.rpm++; |
intrinseca | 1:b3907b8d9f65 | 122 | }*/ |
intrinseca |
0:1f422ed56e0f | 123 | |
intrinseca | 2:825f572902c6 | 124 | //Startup animation |
intrinseca | 1:b3907b8d9f65 | 125 | void selfTest() |
intrinseca | 1:b3907b8d9f65 | 126 | { |
intrinseca | 1:b3907b8d9f65 | 127 | lcd.printf(" FBR 2012"); |
tomontoast | 5:177520d43c87 | 128 | lcd.locate(0,1); |
tomontoast | 5:177520d43c87 | 129 | lcd.printf(" Ready to drive"); |
tomontoast | 5:177520d43c87 | 130 | int offset = 0; |
tomontoast | 5:177520d43c87 | 131 | int type=1; |
tomontoast | 5:177520d43c87 | 132 | int blk=7; |
tomontoast | 5:177520d43c87 | 133 | int from=1; |
tomontoast | 5:177520d43c87 | 134 | int to=0; |
tomontoast | 5:177520d43c87 | 135 | int id = offset*262144+type*32768+from*2048+to*128+blk*8; |
tomontoast | 5:177520d43c87 | 136 | CANMessage msg; |
tomontoast | 5:177520d43c87 | 137 | msg = CANMessage(id,0,8,CANData,CANExtended); |
tomontoast | 5:177520d43c87 | 138 | lcd.cls(); |
tomontoast | 5:177520d43c87 | 139 | lcd.printf("%x",id); |
tomontoast | 5:177520d43c87 | 140 | can.reset(); |
tomontoast | 5:177520d43c87 | 141 | if(can.write(msg)){ |
tomontoast | 5:177520d43c87 | 142 | wait(0.01); |
tomontoast | 5:177520d43c87 | 143 | lcd.printf(" %d errors",can.tderror()); |
tomontoast | 5:177520d43c87 | 144 | can.reset(); |
tomontoast | 5:177520d43c87 | 145 | while(can.read(msg)==false){ |
tomontoast | 5:177520d43c87 | 146 | wait(0.1); |
tomontoast | 5:177520d43c87 | 147 | } |
tomontoast | 5:177520d43c87 | 148 | wait(0.5); |
tomontoast | 5:177520d43c87 | 149 | lcd.printf(" %d errors",can.tderror()); |
tomontoast | 5:177520d43c87 | 150 | } |
intrinseca | 2:825f572902c6 | 151 | //Light up LEDs |
intrinseca | 2:825f572902c6 | 152 | for(int i = 0; i < LEDS::NUM_LEDS; i++) |
intrinseca | 1:b3907b8d9f65 | 153 | { |
intrinseca | 1:b3907b8d9f65 | 154 | leds[i] = true; |
intrinseca | 1:b3907b8d9f65 | 155 | if(i < 4) |
intrinseca | 1:b3907b8d9f65 | 156 | warn[i] = true; |
tomontoast | 5:177520d43c87 | 157 | wait(0.2); |
intrinseca | 1:b3907b8d9f65 | 158 | } |
intrinseca |
0:1f422ed56e0f | 159 | |
intrinseca | 2:825f572902c6 | 160 | //Turn off LEDs |
intrinseca | 1:b3907b8d9f65 | 161 | for(int i = LEDS::NUM_LEDS - 1; i >= 0; i--) |
intrinseca | 1:b3907b8d9f65 | 162 | { |
intrinseca | 1:b3907b8d9f65 | 163 | leds[i] = false; |
intrinseca | 1:b3907b8d9f65 | 164 | if(i < 4) |
intrinseca | 1:b3907b8d9f65 | 165 | warn[i] = false; |
tomontoast | 5:177520d43c87 | 166 | wait(0.2); |
intrinseca | 1:b3907b8d9f65 | 167 | } |
intrinseca |
0:1f422ed56e0f | 168 | |
intrinseca | 1:b3907b8d9f65 | 169 | lcd.cls(); |
intrinseca | 1:b3907b8d9f65 | 170 | } |
intrinseca | 1:b3907b8d9f65 | 171 | |
intrinseca | 1:b3907b8d9f65 | 172 | int main() |
intrinseca | 2:825f572902c6 | 173 | { |
tomontoast | 5:177520d43c87 | 174 | //Initialise CAN |
tomontoast | 5:177520d43c87 | 175 | can.frequency(500000); |
intrinseca | 2:825f572902c6 | 176 | //Initialise state |
tomontoast | 5:177520d43c87 | 177 | car.rpm = 0; |
tomontoast | 5:177520d43c87 | 178 | car.gear = 2; |
tomontoast | 5:177520d43c87 | 179 | car.speed = 0; |
tomontoast | 5:177520d43c87 | 180 | car.coolant_temp = 0; |
tomontoast | 5:177520d43c87 | 181 | car.throttle_pos = 0; |
tomontoast | 5:177520d43c87 | 182 | car.manifold_pres = 0; |
tomontoast | 5:177520d43c87 | 183 | car.air_temp = 0; |
tomontoast | 5:177520d43c87 | 184 | car.lambda = 0; |
tomontoast | 5:177520d43c87 | 185 | car.accel_x = 0; |
tomontoast | 5:177520d43c87 | 186 | car.accel_y = 0; |
tomontoast | 5:177520d43c87 | 187 | car.oil_temp = 0; |
tomontoast | 5:177520d43c87 | 188 | car.voltage = 0; |
intrinseca | 1:b3907b8d9f65 | 189 | |
intrinseca | 2:825f572902c6 | 190 | //Set up menu |
intrinseca | 1:b3907b8d9f65 | 191 | dashMenu.addItem<float>("Coolant Temp ", "%12.1f\xDF\x43", &car.coolant_temp); // \xDF\x43 -> �C . Need code for C as otherwise it gets taken as hex digit. |
intrinseca | 1:b3907b8d9f65 | 192 | dashMenu.addItem<unsigned char>("Air Temp ", "%12d\xDF\x43", &car.air_temp); |
intrinseca | 1:b3907b8d9f65 | 193 | dashMenu.addItem<unsigned char>("Throttle Pos ", "%13d\xDF", &car.throttle_pos); |
intrinseca | 1:b3907b8d9f65 | 194 | dashMenu.addItem<unsigned char>("Manifold Pres ", "%10d psi", &car.manifold_pres); |
intrinseca | 2:825f572902c6 | 195 | dashMenu.addItem<unsigned char>("Lambda ", "%14d", &car.lambda); |
intrinseca | 1:b3907b8d9f65 | 196 | dashMenu.addItem<unsigned char>("Oil Temp ", "%12d\xDF\x43", &car.oil_temp); |
intrinseca | 1:b3907b8d9f65 | 197 | |
intrinseca | 2:825f572902c6 | 198 | //Set up characters on LCS |
intrinseca | 1:b3907b8d9f65 | 199 | setup_bigchar(&lcd); |
intrinseca | 2:825f572902c6 | 200 | |
intrinseca | 2:825f572902c6 | 201 | //Do bootup animation |
intrinseca | 1:b3907b8d9f65 | 202 | selfTest(); |
intrinseca | 1:b3907b8d9f65 | 203 | |
intrinseca | 2:825f572902c6 | 204 | //Start refresh tickers |
intrinseca | 1:b3907b8d9f65 | 205 | lcdRefreshTicker.attach_us(&lcdRefresh, LCD_REFRESH_TIME); |
intrinseca | 1:b3907b8d9f65 | 206 | revRefreshTicker.attach_us(&revRefresh, REV_REFRESH_TIME); |
intrinseca | 1:b3907b8d9f65 | 207 | //increment.attach(&doIncrement, 0.0005); |
intrinseca | 1:b3907b8d9f65 | 208 | |
intrinseca | 2:825f572902c6 | 209 | //Infinite loop - program is interrupt driven |
intrinseca | 1:b3907b8d9f65 | 210 | while(true) |
intrinseca | 1:b3907b8d9f65 | 211 | { |
intrinseca | 1:b3907b8d9f65 | 212 | __WFI(); |
intrinseca |
0:1f422ed56e0f | 213 | } |
intrinseca | 1:b3907b8d9f65 | 214 | } |