Skip to content

Commit

Permalink
Avoid using floating point in the heartbeat code
Browse files Browse the repository at this point in the history
Prior work in 2015 in this repository (commit 1c8ae71 and 52f3151)
had removed all uses of floating point, saving the flash memory needed
for floating point software routines.

More recently, floating point is used if HEARTBEAT_SUPERVISION is
enabled. By making a few small changes and adjusting things to use
integer math, we can save 600+ bytes of flash.

This also removes the deprecated function `HsDeactivate` which isn't
called from anywhere.
  • Loading branch information
toofishes committed Dec 10, 2020
1 parent 00e837d commit 5800407
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
3 changes: 2 additions & 1 deletion firmware/open_evse/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Change Log

202012 toofishes
20201209 toofishes
- various code size optimizations for a smaller binary
- recode energy meter usage calculation for better accuracy using integer math
- remove Time.h and Time.cpp libraries, not needed as we track time in other ways
- remove SERIALCLI, it hasn't been maintained in years and is replaced by RAPI
- avoid using floating point in heartbeat code to save 600+ bytes of Flash

20201001 SCL
- move MV_FOR_xx from EnergyMeter.h to openevse.h, and outside of KWH_RECORDING
Expand Down
30 changes: 9 additions & 21 deletions firmware/open_evse/J1772EvseController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,7 @@ int J1772EVSEController::HsPulse() {
int rc = 1;
#ifdef DEBUG_HS
Serial.print(F("HsPulse called. Time interval before reset: "));
Serial.println((unsigned long)((millis() - m_HsLastPulse)/1000.0));
Serial.println((millis() - m_HsLastPulse)/1000);
#endif
if ((m_HsTriggered == HS_MISSEDPULSE_NOACK)) { //We were in a state of missed pulse
rc = 1; //We have been triggered but have not been acknowledged. Therfore respond with NK.
Expand All @@ -2050,13 +2050,12 @@ int J1772EVSEController::HsPulse() {
m_HsLastPulse = millis(); //We just had a heartbeat so reset the HEARTBEAT SUPERVISION timeout interval;
#ifdef DEBUG_HS
Serial.print(F(" Time interval after reset: "));
Serial.println((unsigned long)((millis() - m_HsLastPulse)/1000.0));
Serial.println((millis() - m_HsLastPulse)/1000);
#endif
return rc;
}

int J1772EVSEController::HsRestoreAmpacity() {
UNION4B u1,u2,u3,u4;
#ifdef DEBUG_HS
Serial.println(F("HsRestoreAmpacity called"));
#endif
Expand All @@ -2065,14 +2064,14 @@ int J1772EVSEController::HsRestoreAmpacity() {
#ifdef DEBUG_HS
Serial.println(F("HEARTBEAT_SUPERVISION was previously triggered - checking if OK to restore ampacity"));
#endif
u3.u8 = g_EvseController.GetMaxCurrentCapacity(); //We get the ceiling for how high we can set ampacity
uint8_t maxCapacity = g_EvseController.GetMaxCurrentCapacity(); //We get the ceiling for how high we can set ampacity
#ifdef TEMPERATURE_MONITORING
if (!g_TempMonitor.OverTemperature()) { //We need to ensure that OverTemperature is not active before we raise current capacity
#ifdef DEBUG_HS
Serial.print(F("Not over temp - OK to restore ampacity to: "));
Serial.println(u3.u8);
Serial.println(maxCapacity);
#endif
rc = g_EvseController.SetCurrentCapacity(u3.u8,1,1); //We are not writing EEPROM, but we are setting current to maximum capacity
rc = g_EvseController.SetCurrentCapacity(maxCapacity,1,1); //We are not writing EEPROM, but we are setting current to maximum capacity
}
else {
#ifdef DEBUG_HS
Expand All @@ -2081,7 +2080,7 @@ int J1772EVSEController::HsRestoreAmpacity() {
rc = 1; //Fail. Cannnot restore ampacity, as TEMPERATURE_MONITORING OverTemperature() is still in force
}
#else // !TEMPERATURE_MONITORING
rc = g_EvseController.SetCurrentCapacity(u3.u8,1,1); //Do not write this value to EEPROM
rc = g_EvseController.SetCurrentCapacity(maxCapacity,1,1); //Do not write this value to EEPROM
#endif // TEMPERATURE_MONITORING
}
else {
Expand All @@ -2091,9 +2090,9 @@ int J1772EVSEController::HsRestoreAmpacity() {
}

int J1772EVSEController::HsExpirationCheck() {
unsigned long sinceLastPulse = (unsigned long)((millis() - m_HsLastPulse)/1000.0); //convert ms to seconds
unsigned long sinceLastPulse = millis() - m_HsLastPulse;
int rc=1;
if ((m_HsInterval != 0) && (sinceLastPulse > m_HsInterval)) { //HEARTBEAT_SUPERVISION is currently active and the Heartbeat interval has timed out
if (m_HsInterval != 0 && sinceLastPulse > (m_HsInterval * 1000)) { //HEARTBEAT_SUPERVISION is currently active and the Heartbeat interval has timed out
#ifdef DEBUG_HS
Serial.println(F("HsExpirationCheck: Heartbeat timer expired account late or no pulse"));
#endif
Expand All @@ -2103,7 +2102,7 @@ int J1772EVSEController::HsExpirationCheck() {
#endif
rc=SetCurrentCapacity(m_IFallback,1,1); //Drop the current, update the display, and do not write it to EEPROM
}
//m_HsInterval timed out, and as a result we may have to perturb the system ampacity setting.
//m_HsInterval timed out, and as a result we may have to perturb the system ampacity setting.
//We flag that here by setting m_HsTriggered = HS_MISSEDPULSE_NOACK
m_HsTriggered = HS_MISSEDPULSE_NOACK; //Flag the fact that HEARTBEAT_SUPERVISION had a missed pulse and report same when pulsed (via nack), until acknowledged
m_HsLastPulse = millis(); //Reset the timer so we don't check again until the next interval
Expand Down Expand Up @@ -2145,17 +2144,6 @@ int J1772EVSEController::HsAckMissedPulse(uint8_t ack) {
return rc;
}

int J1772EVSEController::HsDeactivate() {
//DEPRECATED USE HeartbeatSupervision set to 0 interval instead so update EEPROM!
#ifdef DEBUG_HS
Serial.println(F("HsDeactivate called - BUT I AM DEPRECATED!"));
#endif
int rc=1;
m_HsInterval = 0;
rc=(int)m_HsInterval;
return rc;
}

int J1772EVSEController::GetHearbeatInterval() {
return (int)m_HsInterval;
}
Expand Down
1 change: 0 additions & 1 deletion firmware/open_evse/J1772EvseController.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ int HsPulse();
int HsRestoreAmpacity();
int HsExpirationCheck();
int HsAckMissedPulse(uint8_t ack);
int HsDeactivate();
int GetHearbeatInterval();
int GetHearbeatCurrent();
int GetHearbeatTrigger();
Expand Down

0 comments on commit 5800407

Please sign in to comment.