Plz

Dependencies:   mbed CANMsg Adafruit_LEDBackpack

Committer:
fconboy
Date:
Tue Aug 20 17:06:10 2019 +0000
Revision:
11:6d6e8aea3112
Parent:
10:ea96b4c09854
Now reads CAN messages from Tritium, checks for 0x603 (speed) ones, and prints the speed to the 7-segment display. Tested with unpowered motor, apparently working.

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