Plz

Dependencies:   mbed CANMsg Adafruit_LEDBackpack

Files at this revision

API Documentation at this revision

Comitter:
fconboy
Date:
Tue Aug 20 17:06:10 2019 +0000
Parent:
10:ea96b4c09854
Commit message:
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.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ea96b4c09854 -r 6d6e8aea3112 main.cpp
--- a/main.cpp	Fri Aug 16 16:01:22 2019 +0000
+++ b/main.cpp	Tue Aug 20 17:06:10 2019 +0000
@@ -3,6 +3,7 @@
 #include "Adafruit_LEDBackpack.h"
 
 Ticker ticker;
+Ticker ticker2;
 Timer timer;
 AnalogIn currentPot(p15);
 float curr_val = 0;
@@ -18,6 +19,9 @@
 DigitalIn brake(p24);
 DigitalIn accel(p25);
 float maxBusCurrent = 1.0;
+float actualSpeedms = 0;
+float actualSpeedrpm = 0;
+float actualSpeedkmh = 0;
 
 I2C i2c(p28,p27);
 Adafruit_LEDBackpack display(&i2c);
@@ -32,12 +36,17 @@
 
 const uint16_t SIGNAL_ID = 0x501;
 const uint16_t BUS_ID = 0x502;
-//const uint8_t DISPLAY_ADDR = 0x70; 
+const uint16_t TRITIUM_ID = 0x603;
+const uint8_t DISPLAY_ADDR = 0x70; 
 char counter = 0;
+uint8_t dig1 = 0;
+uint8_t dig2 = 0;
+
 Serial pc(USBTX, USBRX); // tx, rx
 
 CANMsg driverControls;
 CANMsg busCurrent;
+CANMsg msgReceived;
 
 //====================================================================
 
@@ -56,26 +65,30 @@
 }
 
 //=====================================================================
-/*
-void onCanReceived(CANMessage& msg)
+
+void onCanReceived()
 {
-    can1.read(msg);
-    pc.printf("-------------------------------------\r\n");
-    pc.printf("CAN message received\r\n");
-    pc.printf("-------------------------------------\r\n");
-    printMsg(msg);
- 
-    if (msg.id == MY_ID) {
+    can1.read(msgReceived);
+//    pc.printf("-------------------------------------\r\n");
+//    pc.printf("CAN message received\r\n");
+//    pc.printf("-------------------------------------\r\n");
+    //printMsg(msgReceived);
+    //wait(1);
+    if (msgReceived.id == TRITIUM_ID) {
         // extract data from the received CAN message 
         // in the same order as it was added on the transmitter side
-        msg >> counter;
-        msg >> voltage;    
-        pc.printf("  counter = %d\r\n", counter);
-        pc.printf("  voltage = %e V\r\n", voltage);
+           
+//        pc.printf("  counter = %d\r\n", counter);
+//        pc.printf("  voltage = %e V\r\n", voltage);
+        debug3 = !debug3;
+        //printMsg(msgReceived);
+        msgReceived >> actualSpeedrpm;
+        //pc.printf("speed in m/s %f\r\n", actualSpeedms);
+        msgReceived >> actualSpeedms; 
     }
-    timer.start(); // to transmit next message in main
+    //timer.start(); // to transmit next message in main
 }
-*/
+
 //======================================================================
 
 void displayDigit(uint8_t digit, uint8_t position)
@@ -106,11 +119,11 @@
     } else if (digit == 9) {
         display.displaybuffer[position] = 103;
     }
-    timer.start();
+    //timer.start();
     display.writeDisplay();
-    timer.stop();
-    printf("Time to writeDisplay() is %dus\r\n", timer.read_us());
-    timer.reset();
+    //timer.stop();
+    //printf("Time to writeDisplay() is %dus\r\n", timer.read_us());
+    //timer.reset();
 }
 
 void displayNumber(uint8_t number, bool position)
@@ -144,35 +157,6 @@
     //timer.reset();
 }
 
-
-void flash(int light)   //Turns on one of the 4 mbed LEDs
-{   if (light == 1)
-    {debug1 = 1;
-    wait(0.2);
-    debug1 = 0;}
-    else if (light == 2)
-    {debug2 = 1;
-    wait(0.2);
-    debug2 = 0;}
-    else if (light == 3)
-    {debug3 = 1;
-    wait(0.2);
-    debug3 = 0;}
-    else if (light == 4)
-    {debug4 = 1;
-    wait(0.2);
-    debug4 = 0;}
-    else if (light == 5)
-    {debug1 = 1;
-    debug2 = 1;
-    debug3 = 1;
-    debug4 = 1;
-    wait(0.2);
-    debug1 = 0;
-    debug2 = 0;
-    debug3 = 0;
-    debug4 = 0;}}
-
 void setDriverControls()
 {
     driverControls.clear();
@@ -193,31 +177,41 @@
     //printMsg(busCurrent);
     if(can1.write(driverControls))
     {
-        //debug1 = !debug1;
+        debug1 = !debug1;
     }
     else
     {
         //wait(0.5);
-        flash(3);
+        
+        
     }
     
     if(can1.write(busCurrent))
     {
-        //debug2 = !debug2;
+        debug2 = !debug2;
     }
     else
     {
         //wait(0.5);
-        flash(4);
+        
     }
 }
 
+void dispSpeed()
+{
+    actualSpeedkmh = abs(actualSpeedms) * 3.6;
+    displayNumber( (uint8_t) actualSpeedkmh , 1);
+    displayNumber( (uint8_t) speed_reading_kmh , 0);
+}
+
 int main()
 {
-
-    pc.baud(9600);
+    msgReceived = CANMsg();
+    pc.baud(115200);
     can1.frequency(1000000);
     ticker.attach(&sendCAN, 0.1);
+    ticker2.attach(&dispSpeed, 0.5);
+    can1.attach(&onCanReceived); 
     ignition.mode(PullUp);
     regen.mode(PullUp);
     rev.mode(PullUp);
@@ -241,7 +235,9 @@
         speed_reading_ms = speed_reading * 30;
         speed_reading_kmh = speed_reading_ms*3.6;
         
-        
+        //displayNumber(1,1);
+        //displayNumber(1,0);
+
         
         bool ignition_reading = ignition.read();
         bool regen_reading = regen.read();
@@ -255,7 +251,7 @@
         //pc.printf("Regen reading: %d\r\n", regen_reading);
         //pc.printf("Forward/reverse reading: %d\r\n", rev_reading);
         //pc.printf("Brake reading: %d\r\n", brake_reading);
-        //flash(1);
+        
         //pc.printf("Accelerator reading: %d\r\n\r\n", accel_reading);
         //wait(2);