Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Branch:
Metric
Revision:
120:041edeec08f5
Parent:
119:0e2b641e98a2
Child:
121:553faf139a20
--- a/main.cpp	Fri Jul 05 16:55:04 2013 +0000
+++ b/main.cpp	Mon Jul 08 02:08:08 2013 +0000
@@ -23,7 +23,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "119"; // gg - revision string, max 6 characters
+char revStr[7] = "120"; // gg - revision string, max 6 characters
 
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -47,8 +47,10 @@
 InterruptIn touchpad(p17);
 CAN can1(p9, p10);      // CAN1 (EV) uses pins 9 and 10 (rx, tx) and pin 8 (rs)
 DigitalOut can1SleepMode(p8);     // Use pin 8 to control the sleep mode of can2
+//DigitalInOut can1SleepMode(p8);     // Use pin 8 to control the sleep mode of can2
 CAN can2(p30, p29);     // CAN2 (CAR) uses pins 30 and 29 (rx, tx) and pin 28 (rs)
 DigitalOut can2SleepMode(p28);     // Use pin 28 to control the sleep mode of can1
+//DigitalInOut can2SleepMode(p28);     // Use pin 28 to control the sleep mode of can1
 AnalogIn mon12V(p15);
 TOUCH_TFTx2 tt(p16, p17, p19, p20, p11, p12, p13, p6, p7, p5, "TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs0, cs1, reset
 PwmOut dled(p23);
@@ -123,7 +125,7 @@
 float mph[39]={0};
 float kW[39]={0};
 float mpkWh[39]={0};
-float unloadedVavg_x2,unloadedV_x2,Resr,Rlms,redRlms,incRlms;
+float unloadedV_x2,Resr,curRmax,curRmin,redRmax,redRmin,incRmax,incRmin;
 // Logarithmic division scale (roughly - snapped to common units of time)
 float timeConstant[39] = {1, 1.58, 2.51, 3.98, 6.31, 10, 15.8, 25.1, 39.8, 60, // 1 minute
                      60*1.58, 60*2.51, 60*3.98, 60*6.31, 60*10, 60*15.8, 60*25.1, 60*39.8, 60*60, // 1 hour
@@ -140,6 +142,8 @@
 bool metric = false;
 
 int main() {
+    //can1SleepMode.mode(OpenDrain);
+    //can2SleepMode.mode(OpenDrain);
     char sTemp[40];
     unsigned long secs;
     unsigned char i,j,display=0,lwt=0;
@@ -153,7 +157,7 @@
     touchpad.rise(&touch_ISR);
     tt.wfi();               // enable interrupt on touch
     dled = 0.8; // turn on display LED 80%
-    Resr = 0.1; // initial guess of Resr
+    Resr = 0.075; // initial guess of Resr
     timer.start() ;
     RTC_Init(); // start the RTC Interrupts that sync the timer
     struct tm t; // pointer to a static tm structure
@@ -212,8 +216,8 @@
     can2.monitor(true); // set to snoop mode
     can1.frequency(500000);
     can2.frequency(500000);
-    can1SleepMode = 1;         // Turn on Monitor_only Mode
-    can2SleepMode = 1;         // Turn on Monitor_only Mode
+    can1SleepMode = VP230Sleep;         // Turn on Monitor_only Mode
+    can2SleepMode = VP230Sleep;         // Turn on Monitor_only Mode
     can1.attach(&recieve1);
     can2.attach(&recieve2);
 
@@ -725,15 +729,6 @@
                 if (mpkWh[0]<0) {
                     mpkWh[0]=99;// negative means inf.
                 }
-                unloadedV_x2 = unloadedVavg_x2/numWsamples;
-                //Rlms /= numWsamples; //unecessary
-                //redRlms /= numWsamples;
-                //incRlms /= numWsamples;
-                if (redRlms<Rlms) {
-                    Resr-=0.001;
-                } else if (incRlms<Rlms) {
-                    Resr+=0.001;
-                }
             } else {
                 kW[0]=0;
                 mpkWh[0]=0;
@@ -744,10 +739,23 @@
             motorRPM=0;
             numSsamples=0;
             mWs_x4=0;
-            unloadedVavg_x2=0;
-            Rlms=0;
-            redRlms=0;
-            incRlms=0;
+            if((curRmax-curRmin)<10){ // At least 5V change?
+                // do nothing - insufficient delta_V to measure resistance
+            }else if ((redRmax-redRmin)<(curRmax-curRmin)) {
+                Resr-=0.001;
+                unloadedV_x2 = (redRmax+redRmin)/2;
+            } else if ((incRmax-incRmin)<(curRmax-curRmin)) {
+                Resr+=0.001;
+                unloadedV_x2 = (incRmax+incRmin)/2;
+            } else {
+                unloadedV_x2 = (curRmax+curRmin)/2;
+            }
+            curRmin=1000;
+            curRmax=0;
+            incRmin=1000;
+            incRmax=0;
+            redRmin=1000;
+            redRmax=0;
             numWsamples=0;
             if(accOn||playbackEn){
                 for(i=1;i<39;i++){