Plz

Dependencies:   mbed CANMsg Adafruit_LEDBackpack

Committer:
fconboy
Date:
Mon Aug 05 16:50:20 2019 +0000
Revision:
7:060be032c57a
Parent:
6:be28839a8221
Child:
8:3eb4fcab8ede
Updated program flow, some code for the 7-segment display added.

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 7:060be032c57a 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 7:060be032c57a 81 void flash(int light) //Turns on one of the 4 mbed LEDs
fconboy 7:060be032c57a 82 { if (light == 1)
fconboy 7:060be032c57a 83 {debug1 = 1;
fconboy 7:060be032c57a 84 wait(0.2);
fconboy 7:060be032c57a 85 debug1 = 0;}
fconboy 7:060be032c57a 86 else if (light == 2)
fconboy 7:060be032c57a 87 {debug2 = 1;
fconboy 7:060be032c57a 88 wait(0.2);
fconboy 7:060be032c57a 89 debug2 = 0;}
fconboy 7:060be032c57a 90 else if (light == 3)
fconboy 7:060be032c57a 91 {debug3 = 1;
fconboy 7:060be032c57a 92 wait(0.2);
fconboy 7:060be032c57a 93 debug3 = 0;}
fconboy 7:060be032c57a 94 else if (light == 4)
fconboy 7:060be032c57a 95 {debug4 = 1;
fconboy 7:060be032c57a 96 wait(0.2);
fconboy 7:060be032c57a 97 debug4 = 0;}
fconboy 7:060be032c57a 98 else if (light == 5)
fconboy 7:060be032c57a 99 {debug1 = 1;
fconboy 7:060be032c57a 100 debug2 = 1;
fconboy 7:060be032c57a 101 debug3 = 1;
fconboy 7:060be032c57a 102 debug4 = 1;
fconboy 7:060be032c57a 103 wait(0.2);
fconboy 7:060be032c57a 104 debug1 = 0;
fconboy 7:060be032c57a 105 debug2 = 0;
fconboy 7:060be032c57a 106 debug3 = 0;
fconboy 7:060be032c57a 107 debug4 = 0;}}
fconboy 7:060be032c57a 108
fconboy 6:be28839a8221 109 void setDriverControls()
fconboy 6:be28839a8221 110 {
fconboy 6:be28839a8221 111 driverControls.clear();
fconboy 6:be28839a8221 112 driverControls.id = SIGNAL_ID;
fconboy 6:be28839a8221 113 driverControls << speed_val;
fconboy 6:be28839a8221 114 driverControls << curr_val;
fconboy 7:060be032c57a 115
fconboy 7:060be032c57a 116 busCurrent.clear();
fconboy 7:060be032c57a 117 busCurrent.id = BUS_ID;
fconboy 7:060be032c57a 118 busCurrent << 0;
fconboy 7:060be032c57a 119 busCurrent << maxBusCurrent;
fconboy 6:be28839a8221 120 }
fconboy 6:be28839a8221 121
fconboy 7:060be032c57a 122 void sendCAN()
StrawberryAubrey 0:db7d127e9819 123 {
fconboy 6:be28839a8221 124 setDriverControls();
fconboy 7:060be032c57a 125 //printMsg(driverControls);
fconboy 7:060be032c57a 126 //printMsg(busCurrent);
StrawberryAubrey 0:db7d127e9819 127 if(can1.write(driverControls))
fconboy 7:060be032c57a 128 {
fconboy 7:060be032c57a 129 debug1 = !debug1;
fconboy 7:060be032c57a 130 }
StrawberryAubrey 0:db7d127e9819 131 else
fconboy 7:060be032c57a 132 {
fconboy 7:060be032c57a 133 //wait(0.5);
fconboy 7:060be032c57a 134 //flash(3);
fconboy 7:060be032c57a 135 }
fconboy 7:060be032c57a 136
fconboy 7:060be032c57a 137 //if(can1.write(busCurrent))
fconboy 7:060be032c57a 138 //{
fconboy 7:060be032c57a 139 // debug2 = !debug2;
fconboy 7:060be032c57a 140 //}
fconboy 7:060be032c57a 141 //else
fconboy 7:060be032c57a 142 //{
fconboy 7:060be032c57a 143 //wait(0.5);
fconboy 7:060be032c57a 144 //}
StrawberryAubrey 0:db7d127e9819 145 }
StrawberryAubrey 0:db7d127e9819 146
StrawberryAubrey 0:db7d127e9819 147 int main()
StrawberryAubrey 0:db7d127e9819 148 {
fconboy 7:060be032c57a 149
StrawberryAubrey 0:db7d127e9819 150 pc.baud(9600);
StrawberryAubrey 0:db7d127e9819 151 can1.frequency(1000000);
fconboy 7:060be032c57a 152 ticker.attach(&sendCAN, 1);
fconboy 7:060be032c57a 153 ignition.mode(PullUp);
fconboy 7:060be032c57a 154 regen.mode(PullUp);
fconboy 7:060be032c57a 155 rev.mode(PullUp);
fconboy 7:060be032c57a 156 brake.mode(PullUp);
fconboy 7:060be032c57a 157 accel.mode(PullUp);
fconboy 7:060be032c57a 158
fconboy 7:060be032c57a 159
fconboy 7:060be032c57a 160 // display.begin(DISPLAY_ADDR);
fconboy 7:060be032c57a 161 // for (int n = 0; n<16; n++)
fconboy 7:060be032c57a 162 // {
fconboy 7:060be032c57a 163 // display.setBrightness(n);
fconboy 7:060be032c57a 164 // for (int i = 0; i<10; i++)
fconboy 7:060be032c57a 165 // {
fconboy 7:060be032c57a 166 // display.displaybuffer[0] = i;
fconboy 7:060be032c57a 167 // display.displaybuffer[1] = i;
fconboy 7:060be032c57a 168 // display.displaybuffer[2] = 0;
fconboy 7:060be032c57a 169 // display.displaybuffer[3] = 0;
fconboy 7:060be032c57a 170 // display.displaybuffer[4] = 0;
fconboy 7:060be032c57a 171 // display.displaybuffer[5] = 0;
fconboy 7:060be032c57a 172 // display.displaybuffer[6] = 0;
fconboy 7:060be032c57a 173 // display.displaybuffer[7] = 0;
fconboy 7:060be032c57a 174 // display.writeDisplay();
fconboy 7:060be032c57a 175 // wait(0.5);
fconboy 7:060be032c57a 176 // }
fconboy 7:060be032c57a 177 // }
fconboy 7:060be032c57a 178
fconboy 7:060be032c57a 179
fconboy 7:060be032c57a 180 //pc.printf("-------------------------------------\r\n");
fconboy 7:060be032c57a 181 //printf("Attempting to send a CAN message\n");
fconboy 7:060be032c57a 182 //printf("Current and Speed: %f and %f\n", currentPot.read(), speedPot.read());
fconboy 7:060be032c57a 183 //pc.printf("-------------------------------------\r\n");
fconboy 2:36e6eccdfc1e 184 //while(1) {
fconboy 7:060be032c57a 185
fconboy 7:060be032c57a 186 //curr_val = 0.0;
fconboy 7:060be032c57a 187 //speed_val = 0.0;
fconboy 7:060be032c57a 188 curr_reading = currentPot.read();
fconboy 7:060be032c57a 189 speed_reading = speedPot.read();
fconboy 7:060be032c57a 190 speed_reading_ms = speed_reading * 30;
fconboy 7:060be032c57a 191 speed_reading_kmh = speed_reading_ms*3.6;
fconboy 7:060be032c57a 192
fconboy 7:060be032c57a 193 bool ignition_reading = ignition.read();
fconboy 7:060be032c57a 194 bool regen_reading = regen.read();
fconboy 7:060be032c57a 195 bool rev_reading = rev.read();
fconboy 7:060be032c57a 196 bool brake_reading = brake.read();
fconboy 7:060be032c57a 197 bool accel_reading = accel.read();
fconboy 7:060be032c57a 198
fconboy 7:060be032c57a 199 //pc.printf("Current reading: %f\r\n", curr_reading);
fconboy 7:060be032c57a 200 //pc.printf("Speed reading: %f\r\n", speed_reading);
fconboy 7:060be032c57a 201 //pc.printf("Ignition reading: %d\r\n", ignition_reading);
fconboy 7:060be032c57a 202 //pc.printf("Regen reading: %d\r\n", regen_reading);
fconboy 7:060be032c57a 203 //pc.printf("Forward/reverse reading: %d\r\n", rev_reading);
fconboy 7:060be032c57a 204 //pc.printf("Brake reading: %d\r\n", brake_reading);
fconboy 7:060be032c57a 205 //pc.printf("Accelerator reading: %d\r\n\r\n", accel_reading);
fconboy 7:060be032c57a 206 //wait(2);
fconboy 7:060be032c57a 207
fconboy 7:060be032c57a 208 if (ignition)
fconboy 7:060be032c57a 209 {
fconboy 7:060be032c57a 210 curr_val = 0;
fconboy 7:060be032c57a 211 speed_val = 0;
fconboy 7:060be032c57a 212 }
fconboy 7:060be032c57a 213 else
StrawberryAubrey 0:db7d127e9819 214 {
fconboy 7:060be032c57a 215 if (!brake && !regen)
fconboy 7:060be032c57a 216 {
fconboy 7:060be032c57a 217 speed_val = 0;
fconboy 7:060be032c57a 218 curr_val = curr_reading;
fconboy 7:060be032c57a 219 }
fconboy 7:060be032c57a 220 else if(!brake && regen)
fconboy 7:060be032c57a 221 {
fconboy 7:060be032c57a 222 speed_val = 0;
fconboy 7:060be032c57a 223 curr_val = 0;
fconboy 7:060be032c57a 224 }
fconboy 7:060be032c57a 225 else
fconboy 7:060be032c57a 226 {
fconboy 7:060be032c57a 227 if (!rev)
fconboy 7:060be032c57a 228 {
fconboy 7:060be032c57a 229 speed_val = (-1)*speed_reading_ms;
fconboy 7:060be032c57a 230 }
fconboy 7:060be032c57a 231 else
fconboy 7:060be032c57a 232 {
fconboy 7:060be032c57a 233 speed_val = speed_reading_ms;
fconboy 7:060be032c57a 234 }
fconboy 7:060be032c57a 235
fconboy 7:060be032c57a 236 if (!accel)
fconboy 7:060be032c57a 237 {
fconboy 7:060be032c57a 238 curr_val = curr_reading;
fconboy 7:060be032c57a 239 }
fconboy 7:060be032c57a 240 else
fconboy 7:060be032c57a 241 {
fconboy 7:060be032c57a 242 curr_val = 0;
fconboy 7:060be032c57a 243 }
fconboy 7:060be032c57a 244 }
StrawberryAubrey 0:db7d127e9819 245 }
fconboy 7:060be032c57a 246 //pc.printf("speed_val: %f\r\n", speed_val);
fconboy 7:060be032c57a 247 //pc.printf("curr_val: %f\r\n\r\n", curr_val);
fconboy 7:060be032c57a 248 //sendCAN();
fconboy 7:060be032c57a 249 //pc.printf("\r\n\r\n");
fconboy 7:060be032c57a 250 //wait(3);
fconboy 2:36e6eccdfc1e 251 //}
StrawberryAubrey 0:db7d127e9819 252 }