Plz

Dependencies:   mbed CANMsg Adafruit_LEDBackpack

Committer:
fconboy
Date:
Fri Aug 16 16:01:22 2019 +0000
Revision:
10:ea96b4c09854
Parent:
9:42ded201dcdd
Child:
11:6d6e8aea3112
With printing to 7-segment display included but not tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
StrawberryAubrey 0:db7d127e9819 1 #include "mbed.h"
StrawberryAubrey 0:db7d127e9819 2 #include "CANMsg.h"
fconboy 7:060be032c57a 3 #include "Adafruit_LEDBackpack.h"
StrawberryAubrey 0:db7d127e9819 4
fconboy 6:be28839a8221 5 Ticker ticker;
fconboy 6:be28839a8221 6 Timer timer;
fconboy 1:6c993d149edc 7 AnalogIn currentPot(p15);
fconboy 7:060be032c57a 8 float curr_val = 0;
fconboy 7:060be032c57a 9 float curr_reading = 0;
fconboy 1:6c993d149edc 10 AnalogIn speedPot(p16);
fconboy 7:060be032c57a 11 float speed_val = 0;
fconboy 7:060be032c57a 12 float speed_reading = 0;
fconboy 7:060be032c57a 13 float speed_reading_ms = 0;
fconboy 7:060be032c57a 14 float speed_reading_kmh = 0;
fconboy 4:e687888fa056 15 DigitalIn ignition(p21);
fconboy 4:e687888fa056 16 DigitalIn regen(p22);
fconboy 7:060be032c57a 17 DigitalIn rev(p23);
fconboy 7:060be032c57a 18 DigitalIn brake(p24);
fconboy 7:060be032c57a 19 DigitalIn accel(p25);
fconboy 7:060be032c57a 20 float maxBusCurrent = 1.0;
fconboy 7:060be032c57a 21
fconboy 7:060be032c57a 22 I2C i2c(p28,p27);
fconboy 7:060be032c57a 23 Adafruit_LEDBackpack display(&i2c);
StrawberryAubrey 0:db7d127e9819 24
fconboy 7:060be032c57a 25 DigitalOut debug1(LED1);
fconboy 7:060be032c57a 26 DigitalOut debug2(LED2);
fconboy 7:060be032c57a 27 DigitalOut debug3(LED3);
fconboy 7:060be032c57a 28 DigitalOut debug4(LED4);
fconboy 7:060be032c57a 29
fconboy 7:060be032c57a 30 //CAN can1(p9, p10);
fconboy 7:060be032c57a 31 CAN can1(p30, p29);
fconboy 7:060be032c57a 32
StrawberryAubrey 0:db7d127e9819 33 const uint16_t SIGNAL_ID = 0x501;
StrawberryAubrey 0:db7d127e9819 34 const uint16_t BUS_ID = 0x502;
fconboy 10:ea96b4c09854 35 //const uint8_t DISPLAY_ADDR = 0x70;
StrawberryAubrey 0:db7d127e9819 36 char counter = 0;
StrawberryAubrey 0:db7d127e9819 37 Serial pc(USBTX, USBRX); // tx, rx
StrawberryAubrey 0:db7d127e9819 38
StrawberryAubrey 0:db7d127e9819 39 CANMsg driverControls;
StrawberryAubrey 0:db7d127e9819 40 CANMsg busCurrent;
StrawberryAubrey 0:db7d127e9819 41
StrawberryAubrey 0:db7d127e9819 42 //====================================================================
StrawberryAubrey 0:db7d127e9819 43
StrawberryAubrey 0:db7d127e9819 44 void printMsg(CANMessage& msg)
StrawberryAubrey 0:db7d127e9819 45 {
StrawberryAubrey 0:db7d127e9819 46 pc.printf("-------------------------------------\r\n");
StrawberryAubrey 0:db7d127e9819 47 pc.printf(" ID = 0x%.3x\r\n", msg.id);
StrawberryAubrey 0:db7d127e9819 48 pc.printf(" Type = %d\r\n", msg.type);
StrawberryAubrey 0:db7d127e9819 49 pc.printf(" Format = %d\r\n", msg.format);
StrawberryAubrey 0:db7d127e9819 50 pc.printf(" Length = %d\r\n", msg.len);
StrawberryAubrey 0:db7d127e9819 51 pc.printf(" Data =");
StrawberryAubrey 0:db7d127e9819 52 pc.printf("-------------------------------------\r\n");
StrawberryAubrey 0:db7d127e9819 53 for(int i = 0; i < msg.len; i++)
StrawberryAubrey 0:db7d127e9819 54 pc.printf(" 0x%.2X", msg.data[i]);
StrawberryAubrey 0:db7d127e9819 55 pc.printf("\r\n");
StrawberryAubrey 0:db7d127e9819 56 }
StrawberryAubrey 0:db7d127e9819 57
StrawberryAubrey 0:db7d127e9819 58 //=====================================================================
StrawberryAubrey 0:db7d127e9819 59 /*
StrawberryAubrey 0:db7d127e9819 60 void onCanReceived(CANMessage& msg)
StrawberryAubrey 0:db7d127e9819 61 {
StrawberryAubrey 0:db7d127e9819 62 can1.read(msg);
StrawberryAubrey 0:db7d127e9819 63 pc.printf("-------------------------------------\r\n");
StrawberryAubrey 0:db7d127e9819 64 pc.printf("CAN message received\r\n");
StrawberryAubrey 0:db7d127e9819 65 pc.printf("-------------------------------------\r\n");
StrawberryAubrey 0:db7d127e9819 66 printMsg(msg);
StrawberryAubrey 0:db7d127e9819 67
StrawberryAubrey 0:db7d127e9819 68 if (msg.id == MY_ID) {
StrawberryAubrey 0:db7d127e9819 69 // extract data from the received CAN message
StrawberryAubrey 0:db7d127e9819 70 // in the same order as it was added on the transmitter side
StrawberryAubrey 0:db7d127e9819 71 msg >> counter;
StrawberryAubrey 0:db7d127e9819 72 msg >> voltage;
StrawberryAubrey 0:db7d127e9819 73 pc.printf(" counter = %d\r\n", counter);
StrawberryAubrey 0:db7d127e9819 74 pc.printf(" voltage = %e V\r\n", voltage);
StrawberryAubrey 0:db7d127e9819 75 }
StrawberryAubrey 0:db7d127e9819 76 timer.start(); // to transmit next message in main
StrawberryAubrey 0:db7d127e9819 77 }
StrawberryAubrey 0:db7d127e9819 78 */
StrawberryAubrey 0:db7d127e9819 79 //======================================================================
StrawberryAubrey 0:db7d127e9819 80
fconboy 10:ea96b4c09854 81 void displayDigit(uint8_t digit, uint8_t position)
fconboy 10:ea96b4c09854 82 {
fconboy 10:ea96b4c09854 83 // if (position > 4)
fconboy 10:ea96b4c09854 84 // {}
fconboy 10:ea96b4c09854 85 //printf("digit = %d\t position = %d\r\n", digit, position);
fconboy 10:ea96b4c09854 86 if (digit > 9) {
fconboy 10:ea96b4c09854 87 display.displaybuffer[position] = 0;
fconboy 10:ea96b4c09854 88 } else if (digit == 0) {
fconboy 10:ea96b4c09854 89 display.displaybuffer[position] = 63;
fconboy 10:ea96b4c09854 90 } else if (digit == 1) {
fconboy 10:ea96b4c09854 91 display.displaybuffer[position] = 48;
fconboy 10:ea96b4c09854 92 } else if (digit == 2) {
fconboy 10:ea96b4c09854 93 display.displaybuffer[position] = 91;
fconboy 10:ea96b4c09854 94 } else if (digit == 3) {
fconboy 10:ea96b4c09854 95 display.displaybuffer[position] = 79;
fconboy 10:ea96b4c09854 96 } else if (digit == 4) {
fconboy 10:ea96b4c09854 97 display.displaybuffer[position] = 102;
fconboy 10:ea96b4c09854 98 } else if (digit == 5) {
fconboy 10:ea96b4c09854 99 display.displaybuffer[position] = 109;
fconboy 10:ea96b4c09854 100 } else if (digit == 6) {
fconboy 10:ea96b4c09854 101 display.displaybuffer[position] = 125;
fconboy 10:ea96b4c09854 102 } else if (digit == 7) {
fconboy 10:ea96b4c09854 103 display.displaybuffer[position] = 7;
fconboy 10:ea96b4c09854 104 } else if (digit == 8) {
fconboy 10:ea96b4c09854 105 display.displaybuffer[position] = 127;
fconboy 10:ea96b4c09854 106 } else if (digit == 9) {
fconboy 10:ea96b4c09854 107 display.displaybuffer[position] = 103;
fconboy 10:ea96b4c09854 108 }
fconboy 10:ea96b4c09854 109 timer.start();
fconboy 10:ea96b4c09854 110 display.writeDisplay();
fconboy 10:ea96b4c09854 111 timer.stop();
fconboy 10:ea96b4c09854 112 printf("Time to writeDisplay() is %dus\r\n", timer.read_us());
fconboy 10:ea96b4c09854 113 timer.reset();
fconboy 10:ea96b4c09854 114 }
fconboy 10:ea96b4c09854 115
fconboy 10:ea96b4c09854 116 void displayNumber(uint8_t number, bool position)
fconboy 10:ea96b4c09854 117 {
fconboy 10:ea96b4c09854 118 //timer.start();
fconboy 10:ea96b4c09854 119 for (int n = 0; n < 100; (n = n + 10))
fconboy 10:ea96b4c09854 120 {
fconboy 10:ea96b4c09854 121 if (number >= n && number < n + 10)
fconboy 10:ea96b4c09854 122 {
fconboy 10:ea96b4c09854 123 dig1 = n/10;
fconboy 10:ea96b4c09854 124 dig2 = number - n;
fconboy 10:ea96b4c09854 125 }
fconboy 10:ea96b4c09854 126 }
fconboy 10:ea96b4c09854 127
fconboy 10:ea96b4c09854 128 if (number > 99)
fconboy 10:ea96b4c09854 129 {dig1 = 0;
fconboy 10:ea96b4c09854 130 dig2 = 0;}
fconboy 10:ea96b4c09854 131
fconboy 10:ea96b4c09854 132 if (position)
fconboy 10:ea96b4c09854 133 {
fconboy 10:ea96b4c09854 134 displayDigit(dig1, 3);
fconboy 10:ea96b4c09854 135 displayDigit(dig2, 4);
fconboy 10:ea96b4c09854 136 }
fconboy 10:ea96b4c09854 137 else if (!position)
fconboy 10:ea96b4c09854 138 {
fconboy 10:ea96b4c09854 139 displayDigit(dig1, 0);
fconboy 10:ea96b4c09854 140 displayDigit(dig2, 1);
fconboy 10:ea96b4c09854 141 }
fconboy 10:ea96b4c09854 142 //timer.stop();
fconboy 10:ea96b4c09854 143 //printf("time taken to print number %d is %dus\r\n", number, timer.read_us());
fconboy 10:ea96b4c09854 144 //timer.reset();
fconboy 10:ea96b4c09854 145 }
fconboy 10:ea96b4c09854 146
fconboy 10:ea96b4c09854 147
fconboy 7:060be032c57a 148 void flash(int light) //Turns on one of the 4 mbed LEDs
fconboy 7:060be032c57a 149 { if (light == 1)
fconboy 7:060be032c57a 150 {debug1 = 1;
fconboy 7:060be032c57a 151 wait(0.2);
fconboy 7:060be032c57a 152 debug1 = 0;}
fconboy 7:060be032c57a 153 else if (light == 2)
fconboy 7:060be032c57a 154 {debug2 = 1;
fconboy 7:060be032c57a 155 wait(0.2);
fconboy 7:060be032c57a 156 debug2 = 0;}
fconboy 7:060be032c57a 157 else if (light == 3)
fconboy 7:060be032c57a 158 {debug3 = 1;
fconboy 7:060be032c57a 159 wait(0.2);
fconboy 7:060be032c57a 160 debug3 = 0;}
fconboy 7:060be032c57a 161 else if (light == 4)
fconboy 7:060be032c57a 162 {debug4 = 1;
fconboy 7:060be032c57a 163 wait(0.2);
fconboy 7:060be032c57a 164 debug4 = 0;}
fconboy 7:060be032c57a 165 else if (light == 5)
fconboy 7:060be032c57a 166 {debug1 = 1;
fconboy 7:060be032c57a 167 debug2 = 1;
fconboy 7:060be032c57a 168 debug3 = 1;
fconboy 7:060be032c57a 169 debug4 = 1;
fconboy 7:060be032c57a 170 wait(0.2);
fconboy 7:060be032c57a 171 debug1 = 0;
fconboy 7:060be032c57a 172 debug2 = 0;
fconboy 7:060be032c57a 173 debug3 = 0;
fconboy 7:060be032c57a 174 debug4 = 0;}}
fconboy 7:060be032c57a 175
fconboy 6:be28839a8221 176 void setDriverControls()
fconboy 6:be28839a8221 177 {
fconboy 6:be28839a8221 178 driverControls.clear();
fconboy 6:be28839a8221 179 driverControls.id = SIGNAL_ID;
fconboy 6:be28839a8221 180 driverControls << speed_val;
fconboy 6:be28839a8221 181 driverControls << curr_val;
fconboy 7:060be032c57a 182
fconboy 7:060be032c57a 183 busCurrent.clear();
fconboy 7:060be032c57a 184 busCurrent.id = BUS_ID;
fconboy 7:060be032c57a 185 busCurrent << 0;
fconboy 7:060be032c57a 186 busCurrent << maxBusCurrent;
fconboy 6:be28839a8221 187 }
fconboy 6:be28839a8221 188
fconboy 7:060be032c57a 189 void sendCAN()
StrawberryAubrey 0:db7d127e9819 190 {
fconboy 6:be28839a8221 191 setDriverControls();
fconboy 7:060be032c57a 192 //printMsg(driverControls);
fconboy 7:060be032c57a 193 //printMsg(busCurrent);
StrawberryAubrey 0:db7d127e9819 194 if(can1.write(driverControls))
fconboy 7:060be032c57a 195 {
fconboy 9:42ded201dcdd 196 //debug1 = !debug1;
fconboy 7:060be032c57a 197 }
StrawberryAubrey 0:db7d127e9819 198 else
fconboy 7:060be032c57a 199 {
fconboy 7:060be032c57a 200 //wait(0.5);
fconboy 8:3eb4fcab8ede 201 flash(3);
fconboy 7:060be032c57a 202 }
fconboy 7:060be032c57a 203
fconboy 9:42ded201dcdd 204 if(can1.write(busCurrent))
fconboy 9:42ded201dcdd 205 {
fconboy 9:42ded201dcdd 206 //debug2 = !debug2;
fconboy 9:42ded201dcdd 207 }
fconboy 9:42ded201dcdd 208 else
fconboy 9:42ded201dcdd 209 {
fconboy 7:060be032c57a 210 //wait(0.5);
fconboy 9:42ded201dcdd 211 flash(4);
fconboy 9:42ded201dcdd 212 }
StrawberryAubrey 0:db7d127e9819 213 }
StrawberryAubrey 0:db7d127e9819 214
StrawberryAubrey 0:db7d127e9819 215 int main()
StrawberryAubrey 0:db7d127e9819 216 {
fconboy 7:060be032c57a 217
StrawberryAubrey 0:db7d127e9819 218 pc.baud(9600);
StrawberryAubrey 0:db7d127e9819 219 can1.frequency(1000000);
fconboy 8:3eb4fcab8ede 220 ticker.attach(&sendCAN, 0.1);
fconboy 7:060be032c57a 221 ignition.mode(PullUp);
fconboy 7:060be032c57a 222 regen.mode(PullUp);
fconboy 7:060be032c57a 223 rev.mode(PullUp);
fconboy 7:060be032c57a 224 brake.mode(PullUp);
fconboy 7:060be032c57a 225 accel.mode(PullUp);
fconboy 10:ea96b4c09854 226 display.begin(DISPLAY_ADDR);
fconboy 10:ea96b4c09854 227 display.setBrightness(10);
fconboy 7:060be032c57a 228
fconboy 7:060be032c57a 229 //pc.printf("-------------------------------------\r\n");
fconboy 7:060be032c57a 230 //printf("Attempting to send a CAN message\n");
fconboy 7:060be032c57a 231 //printf("Current and Speed: %f and %f\n", currentPot.read(), speedPot.read());
fconboy 7:060be032c57a 232 //pc.printf("-------------------------------------\r\n");
fconboy 8:3eb4fcab8ede 233
fconboy 8:3eb4fcab8ede 234 while(1) {
fconboy 7:060be032c57a 235
fconboy 9:42ded201dcdd 236 //curr_val = 0.2;
fconboy 9:42ded201dcdd 237 //speed_val = 5.0;
fconboy 10:ea96b4c09854 238
fconboy 7:060be032c57a 239 curr_reading = currentPot.read();
fconboy 7:060be032c57a 240 speed_reading = speedPot.read();
fconboy 7:060be032c57a 241 speed_reading_ms = speed_reading * 30;
fconboy 7:060be032c57a 242 speed_reading_kmh = speed_reading_ms*3.6;
fconboy 7:060be032c57a 243
fconboy 10:ea96b4c09854 244
fconboy 10:ea96b4c09854 245
fconboy 7:060be032c57a 246 bool ignition_reading = ignition.read();
fconboy 7:060be032c57a 247 bool regen_reading = regen.read();
fconboy 7:060be032c57a 248 bool rev_reading = rev.read();
fconboy 7:060be032c57a 249 bool brake_reading = brake.read();
fconboy 7:060be032c57a 250 bool accel_reading = accel.read();
fconboy 7:060be032c57a 251
fconboy 7:060be032c57a 252 //pc.printf("Current reading: %f\r\n", curr_reading);
fconboy 7:060be032c57a 253 //pc.printf("Speed reading: %f\r\n", speed_reading);
fconboy 7:060be032c57a 254 //pc.printf("Ignition reading: %d\r\n", ignition_reading);
fconboy 7:060be032c57a 255 //pc.printf("Regen reading: %d\r\n", regen_reading);
fconboy 7:060be032c57a 256 //pc.printf("Forward/reverse reading: %d\r\n", rev_reading);
fconboy 7:060be032c57a 257 //pc.printf("Brake reading: %d\r\n", brake_reading);
fconboy 10:ea96b4c09854 258 //flash(1);
fconboy 7:060be032c57a 259 //pc.printf("Accelerator reading: %d\r\n\r\n", accel_reading);
fconboy 7:060be032c57a 260 //wait(2);
fconboy 7:060be032c57a 261
fconboy 7:060be032c57a 262 if (ignition)
fconboy 7:060be032c57a 263 {
fconboy 7:060be032c57a 264 curr_val = 0;
fconboy 7:060be032c57a 265 speed_val = 0;
fconboy 7:060be032c57a 266 }
fconboy 7:060be032c57a 267 else
StrawberryAubrey 0:db7d127e9819 268 {
fconboy 7:060be032c57a 269 if (!brake && !regen)
fconboy 7:060be032c57a 270 {
fconboy 7:060be032c57a 271 speed_val = 0;
fconboy 7:060be032c57a 272 curr_val = curr_reading;
fconboy 7:060be032c57a 273 }
fconboy 7:060be032c57a 274 else if(!brake && regen)
fconboy 7:060be032c57a 275 {
fconboy 7:060be032c57a 276 speed_val = 0;
fconboy 7:060be032c57a 277 curr_val = 0;
fconboy 7:060be032c57a 278 }
fconboy 7:060be032c57a 279 else
fconboy 7:060be032c57a 280 {
fconboy 9:42ded201dcdd 281 if (rev)
fconboy 7:060be032c57a 282 {
fconboy 7:060be032c57a 283 speed_val = (-1)*speed_reading_ms;
fconboy 7:060be032c57a 284 }
fconboy 7:060be032c57a 285 else
fconboy 7:060be032c57a 286 {
fconboy 7:060be032c57a 287 speed_val = speed_reading_ms;
fconboy 7:060be032c57a 288 }
fconboy 7:060be032c57a 289
fconboy 7:060be032c57a 290 if (!accel)
fconboy 7:060be032c57a 291 {
fconboy 7:060be032c57a 292 curr_val = curr_reading;
fconboy 7:060be032c57a 293 }
fconboy 7:060be032c57a 294 else
fconboy 7:060be032c57a 295 {
fconboy 7:060be032c57a 296 curr_val = 0;
fconboy 7:060be032c57a 297 }
fconboy 9:42ded201dcdd 298 }
fconboy 9:42ded201dcdd 299
StrawberryAubrey 0:db7d127e9819 300 }
fconboy 7:060be032c57a 301 //pc.printf("speed_val: %f\r\n", speed_val);
fconboy 7:060be032c57a 302 //pc.printf("curr_val: %f\r\n\r\n", curr_val);
fconboy 7:060be032c57a 303 //sendCAN();
fconboy 7:060be032c57a 304 //pc.printf("\r\n\r\n");
fconboy 7:060be032c57a 305 //wait(3);
fconboy 8:3eb4fcab8ede 306 }
StrawberryAubrey 0:db7d127e9819 307 }