Added comments explaining get voltage / amps formula
authoraidan@aidan-laptop
Wed, 26 May 2010 13:05:35 +0100
changeset 16 53ed13f721a5
parent 15 94374ea96d47
child 17 24e1b1c89f6a
Added comments explaining get voltage / amps formula Need to test the spike code on bench
pbmd
stateManager.c
stateManager.h
Binary file pbmd has changed
--- a/stateManager.c	Wed May 26 11:41:00 2010 +0100
+++ b/stateManager.c	Wed May 26 13:05:35 2010 +0100
@@ -60,13 +60,20 @@
     sleep(2);                                                        // wait at least 800ms to settle
     CPhidgetInterfaceKit_getSensorRawValue(IFK, VOLTINDEX, &Value);  // Voltmeter
 
-    // Calibration - this is zero point calibration.
+    // Calibration
+    // when the phidget is not connected to anything it returns a voltage of -5mV. 
+    // this must be adjusted to zero by adding 5mV
     Value += 5;
-    // End calibration
 
     // get an average over 5 readings to allow for sensor noise. 1 per second for 5 seconds
     for (i = 0; i < 5 ; i++)
-    {
+    { 
+        // http://www.phidgets.com/documentation/Phidgets/1123.pdf
+        // If you want maximum accuracy, you can use the RawSensorValue property from the PhidgetInterfaceKit. 
+        // To adjust a formula, substitute (SensorValue) with (RawSensorValue / 4.095)
+        //
+        // The Formula to translate SensorValue into Voltage is:
+        // Voltage (in volts) = (SensorValue x 0.06) - 30        
         fValue = Value;
         fValue = (((fValue/4.095)*0.06)-30)*100;    // multiply raw value and up by 100 to keep
         avgValue += fValue;
@@ -86,12 +93,20 @@
     CPhidgetInterfaceKit_getSensorRawValue(IFK, AMPINDEX, &Value);   // Ampmeter
 
     // Calibration
+    // when the phidget is not connected to anything it returns a voltage of -2mA. 
+    // this must be adjusted to zero by adding 2mA
     Value += 2;
-    // End calibration    
+
 
+    // http://www.phidgets.com/documentation/Phidgets/1122.pdf
+    // If you want maximum accuracy, you can use the RawSensorValue property from the PhidgetInterfaceKit. 
+    // To adjust a formula, substitute (SensorValue) with (RawSensorValue / 4.095)
+    //
+    // The formula to translate SensorValue into Current is:
+    // DC Amps = (SensorValue / 13.2) - 37.8787    
     fValue = Value;
     fValue = (((fValue/4.095)/13.2)-37.8787)*1000;  // multiply raw value to get amps
-    return fValue * -1;                            // invert so + is charging and - is amp draw
+    return fValue * -1;                             // invert so + is charging and - is amp draw
 }
 
 int updateSnapshotfile(const char *snapFileName, int Voltage, int Amps,  int state)
@@ -333,9 +348,34 @@
     
     /******************************************************************************************/
     
-    prevVoltageTime = boottime;                     //initialise previous voltage time 
-    prevVoltage = getVoltage(IFK);                  //ininitalise voltage
+    time (&prevVoltageTime);            //initialise previous voltage time 
+    prevVoltage = getVoltage(IFK);      //ininitalise voltage        
+        
+    int spiking = 1;
+    int spikecount = 0;
+          
+    while (spiking) 
+    {
+        sleep(10);
+        Voltage = getVoltage(IFK); 
+	    spiking = testVoltageSpike(&prevVoltage, &prevVoltageTime, &Voltage, &VoltageTime);        
         
+        if (spikecount++ > SPIKELIMIT) 
+        {
+            syslog ( LOG_ERR, "Error initial voltage spike continuing after %d tests, closing program", spikecount );
+                           
+            snprintf (buffer0, bufferSize, "Error Reading Voltage");
+            snprintf (buffer1, bufferSize, "Power Management Restarting");
+
+            CPhidgetTextLCD_setDisplayString (LCD, 0, buffer0); 
+            CPhidgetTextLCD_setDisplayString (LCD, 1, buffer1);  
+            
+            closeCPhidget((CPhidgetHandle)IFK);
+            closeCPhidget((CPhidgetHandle)LCD); 
+            return 1;
+        }         
+    }
+    
     while(1)
     {          
         time (&VoltageTime);
--- a/stateManager.h	Wed May 26 11:41:00 2010 +0100
+++ b/stateManager.h	Wed May 26 13:05:35 2010 +0100
@@ -37,6 +37,8 @@
 #define SLEEPHOUREND      6         // Time to end sleeping
 #define SLEEPTIMEENDSTR   "06:00"   // Time to end sleeping
 
+#define SPIKELIMIT        20
+
 #define BOOTFLAG_MAN_RESTART    "X"
 #define BOOTFLAG_MAN_OVERRIDE   "N"
 #define BOOTFLAG_NORMAL         "S"