Changed voltage spike to use rate of change.
authoraidan@aidan-laptop
Thu, 27 May 2010 14:36:36 +0100
changeset 17 24e1b1c89f6a
parent 16 53ed13f721a5
child 18 01c2efa497f9
Changed voltage spike to use rate of change. Added more logging.
pbmd
pbmd.c
stateManager.c
stateManager.h
Binary file pbmd has changed
--- a/pbmd.c	Wed May 26 13:05:35 2010 +0100
+++ b/pbmd.c	Thu May 27 14:36:36 2010 +0100
@@ -46,6 +46,7 @@
     openlog( progName, LOG_CONS, LOG_USER );
     
     validateArgs( argc, argv );       
+    syslog ( LOG_NOTICE, "Power States: AMBER=%d GREEN=%d", AMBER, GREEN );
     
     // extract args    
     logFile = fopen( argv[1], "a" );    // Open log file
--- a/stateManager.c	Wed May 26 13:05:35 2010 +0100
+++ b/stateManager.c	Thu May 27 14:36:36 2010 +0100
@@ -61,8 +61,8 @@
     CPhidgetInterfaceKit_getSensorRawValue(IFK, VOLTINDEX, &Value);  // Voltmeter
 
     // Calibration
-    // when the phidget is not connected to anything it returns a voltage of -5mV. 
-    // this must be adjusted to zero by adding 5mV
+    // when the phidget is not connected to anything it returns a voltage of -5V. 
+    // this must be adjusted to zero by adding 5V
     Value += 5;
 
     // get an average over 5 readings to allow for sensor noise. 1 per second for 5 seconds
@@ -109,14 +109,15 @@
     return fValue * -1;                             // invert so + is charging and - is amp draw
 }
 
-int updateSnapshotfile(const char *snapFileName, int Voltage, int Amps,  int state)
+int updateSnapshotfile(const char *snapFileName, int Voltage, int Amps,  int state, int spiking)
 {
     // Snapshot file is a single line file, overwritten each time with the current and latest values
     time_t rawtime;
     struct tm * timeinfo;
     char timeStr [80];
     char snapEntry [200];
-    
+    char * spikingStr = spiking ? " SPIKE" : "";
+        
     time ( &rawtime );
     timeinfo = localtime ( &rawtime );
 
@@ -131,7 +132,7 @@
     }
     else 
     {
-        snprintf( snapEntry, 200, "%s %dmV %dmA %s\n", timeStr, Voltage, Amps, getStateDesc(state) );                                                           
+        snprintf( snapEntry, 200, "%s %dV%s %dmA %s\n", timeStr, Voltage, spikingStr, Amps, getStateDesc(state) );                                                           
         fputs( snapEntry, snapfile );
         fflush( snapfile );
         fclose( snapfile );
@@ -139,13 +140,14 @@
     }        
 }
 
-int updateLogfile(FILE *logfile, int Voltage, int Amps, int state, const char *progName)
+int updateLogfile(FILE *logfile, int Voltage, int Amps, int state, int spiking, const char *progName)
 {   
     time_t rawTime;
     struct tm * timeInfo;
     char timeStr [80];
     char hostName [80];    
     char logEntry [200];
+    char * spikingStr = spiking ? " SPIKE" : "";
     
     time ( &rawTime );
     timeInfo = localtime ( &rawTime );
@@ -160,7 +162,7 @@
     }
     else
     {
-        snprintf( logEntry, 200, "%s %s %s %dmV %dmA %s\n", timeStr, hostName, progName, Voltage, Amps, getStateDesc(state) );
+        snprintf( logEntry, 200, "%s %s %s %dV%s %dmA %s\n", timeStr, hostName, progName, Voltage, spikingStr, Amps, getStateDesc(state) );
         fputs( logEntry, logfile );
         fflush( logfile );
         return 0;
@@ -211,10 +213,10 @@
           
         
     if ( *newState != localState )
-        syslog ( LOG_INFO, "Moving from state %s to state %s, bootflag=%s, voltage=%d", 
+        syslog ( LOG_INFO, "Moving from state %s to state %s, bootflag=%s, voltage=%dV", 
                 getStateDesc(*newState), getStateDesc(localState), bootflag, Voltage );
     else
-        syslog ( LOG_DEBUG, "Remaining in same state %s, bootflag=%s, voltage=%d", 
+        syslog ( LOG_DEBUG, "Remaining in same state %s, bootflag=%s, voltage=%dV", 
                 getStateDesc(localState), bootflag, Voltage );
 
     *newState = localState; 
@@ -228,26 +230,25 @@
     return;
 }         
 
-//if the change in voltage is greater than the max allowable change in the given time step, a spike has occured
-//if spike:  set voltage to prevVoltage and return 1
+//if the rate of change in voltage is greater than the max allowable, a spike has occured
+//if spike:  set voltage & voltageTime to prev value and return 1
 //otherwise: update prevVoltage to voltage and return 0
 int testVoltageSpike(int *prevVoltage, time_t *prevVoltageTime, int *Voltage, time_t *VoltageTime)
 {
-    const int timeStep = 120;            
-    const int maxDeltaVoltage = 1000;        
-    
-    int deltaVoltage = (*Voltage) - (*prevVoltage); 
-    deltaVoltage = deltaVoltage < 0 ? -1 * deltaVoltage : deltaVoltage;     //use abs value
+    const float maxRateOfChange = 1;
     
-    int deltaTime = (*VoltageTime) - (*prevVoltageTime);
-
-    if( deltaVoltage > maxDeltaVoltage && deltaTime < timeStep ) 
+    float deltaSeconds = (*VoltageTime) - (*prevVoltageTime);    
+    float deltaVoltage = (*Voltage) - (*prevVoltage); 
+    deltaVoltage = deltaVoltage < 0 ? -1 * deltaVoltage : deltaVoltage;     //use abs value
+        
+    // eg. if the voltage changes by more than 1 volt over 100 sec a spike has occured
+    if( deltaVoltage / deltaSeconds > maxRateOfChange )
     {
-        syslog ( LOG_ERR, "Voltage spike from: %dv to: %dv in %d seconds. Using prevVoltage: %d", *prevVoltage, *Voltage, deltaTime, *prevVoltage ); 
+        syslog ( LOG_ERR, "Voltage spike from: %dV to: %dV in %d seconds. Using prevVoltage: %dV", *prevVoltage, *Voltage, (int)deltaSeconds, *prevVoltage ); 
 	    *Voltage=*prevVoltage;
 	    *VoltageTime=*prevVoltageTime; 
 	    return 1;           
-    } 
+    }  
     else 
     {
 	    *prevVoltage = *Voltage;
@@ -286,6 +287,8 @@
     int tts;
     int newState = 0;
     int ret;
+    int spiking;
+    int spikecount;
     
     time_t timenow, prevVoltageTime, VoltageTime;
     
@@ -351,16 +354,19 @@
     time (&prevVoltageTime);            //initialise previous voltage time 
     prevVoltage = getVoltage(IFK);      //ininitalise voltage        
         
-    int spiking = 1;
-    int spikecount = 0;
-          
-    while (spiking) 
+    spiking = 1;
+    spikecount = 0;
+    while ( spiking ) 
     {
         sleep(10);
+        time (&VoltageTime);
         Voltage = getVoltage(IFK); 
-	    spiking = testVoltageSpike(&prevVoltage, &prevVoltageTime, &Voltage, &VoltageTime);        
-        
-        if (spikecount++ > SPIKELIMIT) 
+	    spiking = testVoltageSpike(&prevVoltage, &prevVoltageTime, &Voltage, &VoltageTime);   
+	    
+	    if ( spiking ) 
+    	    syslog ( LOG_ERR, "Initial voltage spike occured - acquiring new voltage to retest" );    
+            
+        if ( spikecount++ > SPIKELIMIT ) 
         {
             syslog ( LOG_ERR, "Error initial voltage spike continuing after %d tests, closing program", spikecount );
                            
@@ -380,14 +386,14 @@
     {          
         time (&VoltageTime);
         Voltage = getVoltage(IFK);             
-        testVoltageSpike(&prevVoltage, &prevVoltageTime, &Voltage, &VoltageTime);
+        spiking = testVoltageSpike(&prevVoltage, &prevVoltageTime, &Voltage, &VoltageTime);
         
         Amps = getAmps(IFK);
 
         getnewState (Voltage, bootflag, &newState);
 
-        updateLogfile (logfile, Voltage, Amps, newState, progName);
-        updateSnapshotfile (snapFileName, Voltage, Amps, newState);
+        updateLogfile (logfile, Voltage, Amps, newState, spiking, progName);
+        updateSnapshotfile (snapFileName, Voltage, Amps, newState, spiking);
 
         // Now Update the LCD
         getTimeString (0, wakeTimeStr);
@@ -404,7 +410,7 @@
         {
             // sleep for 3 hours, then wake, reboot
             getTimeString (STBYLONG_TIME, wakeTimeStr);
-            snprintf (buffer1, bufferSize, "%-s Back@%-s", getStateDesc(newState), wakeTimeStr);
+            snprintf (buffer1, bufferSize, "%-s Wake@%-s", getStateDesc(newState), wakeTimeStr);
             
             CPhidgetTextLCD_setDisplayString (LCD, 1, buffer1);
         
--- a/stateManager.h	Wed May 26 13:05:35 2010 +0100
+++ b/stateManager.h	Thu May 27 14:36:36 2010 +0100
@@ -50,8 +50,8 @@
 int getVoltage(CPhidgetInterfaceKitHandle IFK);
 int getAmps(CPhidgetInterfaceKitHandle IFK);
 
-int updateSnapshotfile(const char *snapFileName, int Voltage, int Amps, int state);
-int updateLogfile(FILE *logfile, int Voltage, int Amps, int state, const char *progName);
+int updateSnapshotfile(const char *snapFileName, int Voltage, int Amps, int state, int spiking);
+int updateLogfile(FILE *logfile, int Voltage, int Amps, int state, int spiking, const char *progName);
 
 void getTimeString (int wakeTime, char *wakeTimeStr);
 void getnewState(int Voltage, const char *bootflag, int *newState);