hi

Dependencies:   CANMsg

Files at this revision

API Documentation at this revision

Comitter:
davidyu
Date:
Wed Dec 11 18:55:34 2019 +0000
Parent:
0:69bbc94cae9a
Commit message:
final bps sim and temp

Changed in this revision

canIds.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 69bbc94cae9a -r 983b2e230859 canIds.h
--- a/canIds.h	Thu Nov 14 03:20:27 2019 +0000
+++ b/canIds.h	Wed Dec 11 18:55:34 2019 +0000
@@ -29,4 +29,11 @@
 #define ODOMETER 0x60F
 
 // MPPT
-#define MPPT 0x77F
+#define MPPT_BASE_R     0x710 // MPPT Base ID (Request)
+#define MPPT_BASE_A     0x770 // MPPT Base ID (Answer)
+#define MPPT_OFF        0xF //MPPT Offset ID (set by DIP switch on board)
+
+// Array Temperature
+// WARNING: TEMPORARY IDS 
+#define ARRAY_TEMP_1    0x798
+#define ARRAY_TEMP_2    0x799
diff -r 69bbc94cae9a -r 983b2e230859 main.cpp
--- a/main.cpp	Thu Nov 14 03:20:27 2019 +0000
+++ b/main.cpp	Wed Dec 11 18:55:34 2019 +0000
@@ -4,7 +4,7 @@
 #include <stdint.h>
 #include <stdio.h>
 
-// #define BOARD1          // Comment this for one board, BOARD1 sends
+#define BOARD1          // Comment this for one board, BOARD1 sends
 
 #define CHAR16                      3
 #define CHAR32                      5
@@ -17,10 +17,24 @@
 #define MPPT_OFF        0xF //MPPT Offset ID (set by DIP switch on board)
  
 Serial          pc(USBTX, USBRX);
-// CAN             can(PA_11, PA_12);       // L432KC
-CAN             can(PD_0, PD_1);            // 429ZI
+CAN             can(PA_11, PA_12);       // L432KC
+// CAN             can(PD_0, PD_1);            // 429ZI
 DigitalOut      led(LED1);
 CANMessage      msg;
+ 
+ 
+// Array Temperatures
+AnalogIn Vtemp1(PA_3); // RTD temperature sensor (pin A2)
+AnalogIn Vtemp2(PA_4); // RTD temperature sensor (pin A3)
+double tempmeas1;
+double tempmeas2;
+double avgval;
+double n;
+double tempoff1 = -1.8; // Offset in deg C to calibrate RTD 1
+double tempoff2 = -1.8; // Offset in deg C to calibrate RTD 2
+double slope1 = 1.25; // Slope correction to RTD
+double slope2 = 1.25; // Slope correction to RTD
+
 int counter = 0;
 #ifdef BOARD1
     int CanID = 0x0000001;
@@ -29,6 +43,29 @@
 #endif
  
 // https://stackoverflow.com/questions/24420246/c-function-to-convert-float-to-byte-array
+// Receives temperatures from the array
+void receiveArrayTemp(){
+    // Temperature Measure 1
+    avgval = 0.0;
+    n=0;
+    while(n<100){
+        tempmeas1 = Vtemp1.read()*3.3;
+        avgval=avgval+tempmeas1;
+        n++;
+    }
+    tempmeas1 = tempoff1 + slope1*(avgval/100.0)*150.0/2.92;
+    
+    // Temperature Measure 2
+    avgval = 0.0;
+    n=0;
+    while(n<100){
+        tempmeas2 = Vtemp2.read()*3.3;
+        avgval=avgval+tempmeas2;
+        n++;
+    }
+    tempmeas2 = tempoff2 + slope2*(avgval/100.0)*150.0/2.92;
+}
+
 void float2Bytes(float val,uint8_t* bytes_array){
     uint8_t temp;
     // Create union of shared memory space
@@ -178,6 +215,28 @@
     
     // TODO: can error, bps command message?
     //wait(1.0);
+    // Send array temepratures
+    receiveArrayTemp();
+//    uint8_t arrayTemp1 = 0;
+//    uint8_t arrayTemp2 = 0;
+//    
+//    double tempmeas1;
+//    double tempmeas2;
+    float2Bytes((float) tempmeas1, &bytes[0]);
+    if(can.write(CANMessage(ARRAY_TEMP_1,(char*)&tempmeas1, 4))) {
+        pc.printf("Sent ARRAY_TEMP_1: %.1f\n", tempmeas1);    
+    } else {
+        printf("Cannot write to CAN\n");
+        NVIC_SystemReset();
+    }
+    
+    float2Bytes((float) tempmeas2, &bytes[0]);
+    if(can.write(CANMessage(ARRAY_TEMP_2,(char*)&tempmeas2, 4))) {
+        pc.printf("Sent ARRAY_TEMP_2: %.1f\n", tempmeas2);    
+    } else {
+        printf("Cannot write to CAN\n");
+        NVIC_SystemReset();
+    }
 } 
 
 float receiveCan(void) {