Skip to content

Commit

Permalink
Allow desiredAmpsOffered to be rounded to half and quart
Browse files Browse the repository at this point in the history
  • Loading branch information
RichieB2B committed Jun 8, 2023
1 parent 23eaea2 commit 383c2e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions etc/twcmanager/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
# rates really does.
"minAmpsPerTWC": 12,

# By default TWCManager will limit charging to whole Amps only and never more
# than the PV surplus. This strategy can be changed by setting roundingAmps
# to "half" or "quart". Or to allow rounding to the nearest number (that
# might be more than the PV surplus) use "round", "roundHalf" or "roundQuart".
"roundingAmps": "int",

# If the system rapidly changes its mind about whether to start or stop charging,
# this parameter keeps charging / not charging until the decision remains stable
# for this many seconds.
Expand Down
24 changes: 19 additions & 5 deletions lib/TWCManager/TWCSlave.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(self, TWCID, maxAmps, config, master):
"useFlexAmpsToStartCharge", False
)
self.startStopDelay = self.configConfig.get("startStopDelay", 60)
self.roundingAmps = self.configConfig.get("roundingAmps", "int")

def print_status(self, heartbeatData):
try:
Expand Down Expand Up @@ -806,7 +807,7 @@ def receive_slave_heartbeat(self, heartbeatData):
# Call the Tesla API to set the charge rate for vehicle connected to this TWC
# TODO: Identify vehicle
if (
int(desiredAmpsOffered) == self.__lastAPIAmpsValue
self.rounding(desiredAmpsOffered) == self.__lastAPIAmpsValue
and (
self.__lastAPIAmpsRepeat > 50 or self.__lastAPIAmpsRepeat % 10 != 0
)
Expand All @@ -819,15 +820,15 @@ def receive_slave_heartbeat(self, heartbeatData):
else:
self.__lastAPIAmpsRequest = time.time()
self.__lastAPIAmpsRepeat = 0
self.__lastAPIAmpsValue = int(desiredAmpsOffered)
self.__lastAPIAmpsValue = self.rounding(desiredAmpsOffered)

# Determine vehicle to control
targetVehicle = (
None if self.getLastVehicle() is None else self.getLastVehicle()
)

self.master.getModuleByName("TeslaAPI").setChargeRate(
int(desiredAmpsOffered), targetVehicle
int(self.rounding(desiredAmpsOffered)), targetVehicle
)

desiredAmpsOffered = int(self.configConfig.get("wiringMaxAmpsPerTWC", 6))
Expand All @@ -840,7 +841,7 @@ def receive_slave_heartbeat(self, heartbeatData):
# one second and 12.0A the next second, the car reduces its power
# use to ~5.14-5.23A and refuses to go higher. So it seems best to
# stick with whole amps.
desiredAmpsOffered = int(desiredAmpsOffered)
desiredAmpsOffered = self.rounding(desiredAmpsOffered)

# Mid Oct 2017, Tesla pushed a firmware update to their cars
# that seems to create the following bug:
Expand Down Expand Up @@ -1018,7 +1019,7 @@ def receive_slave_heartbeat(self, heartbeatData):
# look like this:
# S 032e 0.25/0.00A: 03 0000 0019 0000 M: 05 0000 0000 0000
if self.reportedAmpsMax != desiredAmpsOffered or desiredAmpsOffered == 0:
desiredHundredthsOfAmps = int(desiredAmpsOffered * 100)
desiredHundredthsOfAmps = int(self.rounding(desiredAmpsOffered) * 100)
self.masterHeartbeatData = bytearray(
[
(0x09 if self.protocolVersion == 2 else 0x05),
Expand Down Expand Up @@ -1165,3 +1166,16 @@ def getLastVehicle(self):
if lastVehicle != None:
return lastVehicle
return None

def rounding(self, value):
if self.roundingAmps == 'half':
return int(value * 2) / 2
if self.roundingAmps == 'quart':
return int(value * 4) / 4
if self.roundingAmps == 'round':
return round(value)
if self.roundingAmps == 'roundHalf':
return round(value * 2) / 2
if self.roundingAmps == 'roundQuart':
return round(value * 4) / 4
return int(value)

0 comments on commit 383c2e1

Please sign in to comment.