Skip to content

Commit

Permalink
Merge pull request snaptec#499 from dj3mu/slaveModeChanges
Browse files Browse the repository at this point in the history
Slave mode changes
  • Loading branch information
snaptec authored May 4, 2020
2 parents e87ca24 + 89a26ef commit f4d9707
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 42 deletions.
34 changes: 23 additions & 11 deletions rfidtag.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/bin/bash

# Allow 3 minutes between RFID scan and plugin - else the CP gets disabled again
MaximumSecondsAfterRfidScanToAssignCp=180
NumberOfSupportedChargePoints=2
declare -r MaximumSecondsAfterRfidScanToAssignCp=180

StartScanDataLocation="web/logging/data/startRfidScanData"
# lastmanagement == 1 means that it's on openWB duo
if (( lastmanagement > 0 )); then
declare -r InstalledChargePoints=2
else
declare -r InstalledChargePoints=1
fi

declare -r StartScanDataLocation="web/logging/data/startRfidScanData"

#
# the main script that is called from outside world
Expand Down Expand Up @@ -58,7 +64,7 @@ rfid() {
fi

# check all CPs that we support for whether the tag is valid for that CP
for ((currentCp=1; currentCp<=NumberOfSupportedChargePoints; currentCp++)); do
for ((currentCp=1; currentCp<=InstalledChargePoints; currentCp++)); do
checkTagValidAndSetStartScanData $currentCp
done

Expand All @@ -78,7 +84,13 @@ rfid() {
startData=$(<"${StartScanDataLocation}")
IFS=',' read -r -a startDataSegments <<< "$startData"

if (( pluggedLp > 0 )); then
if (( pluggedLp > 0 )) || ( (( InstalledChargePoints == 1 )) && (( plugstat == 1 )) && [[ ! -f "${StartScanDataLocation}Lp1" ]] ); then

local pluggedLpToUse=$pluggedLp
if (( pluggedLp == 0)); then
# happens for openWB single if already plugged in
pluggedLp=1
fi

startDataForPluggedLp="${startData}"
echo "$NowItIs: Charge point #${pluggedLp} has been plugged in - recording accounting start data"
Expand All @@ -94,7 +106,7 @@ rfid() {
echo "$NowItIs: Timeout (${secondsSinceRfidScan} > ${MaximumSecondsAfterRfidScanToAssignCp} seconds) waiting for plugin after RFID scan. Disabling the CP and deleting accounting data."

# in case of timeout we disable all CPs that are NOT plugged in right now
for ((currentCp=1; currentCp<=NumberOfSupportedChargePoints; currentCp++)); do
for ((currentCp=1; currentCp<=InstalledChargePoints; currentCp++)); do
if [[ "${lpsPlugStat[$currentCp]}" -ne "1" ]]; then
echo "$NowItIs: Disabling CP #${currentCp} as it's still unplugged after timeout of RFID tag scan has been exceeded"
mosquitto_pub -r -q 2 -t "openWB/set/lp${currentCp}/ChargePointEnabled" -m "0"
Expand All @@ -108,7 +120,7 @@ rfid() {
fi

# handle un-plug
for ((currentCp=1; currentCp<=NumberOfSupportedChargePoints; currentCp++)); do
for ((currentCp=1; currentCp<=InstalledChargePoints; currentCp++)); do
if (( unpluggedLps[$currentCp] > 0 )); then
echo "$NowItIs: Charge point #${currentCp} has been UNplugged - if running, stop sending accounting data (after one final transmission)"

Expand Down Expand Up @@ -221,10 +233,10 @@ checkTagValidAndSetStartScanData() {

local chargePoint=$1

# if we're in slave mode and the LP has not just been plugged in (in same control interval as the RFID scan)
# we completely ignore the scan.
if (( slavemode == 1 )) && (( lpsPlugStat[$chargePoint] > 0 )) && (( pluggedLps[$chargePoint] != 1 )); then
echo "$NowItIs: Ignoring RFID scan of tag '${lasttag}' for CP #${chargePoint} because that CP is not in 'unplugged' state (plugstatToUse == ${lpsPlugStat[$chargePoint]}, justPlugged == ${pluggedLps[$chargePoint]})"
# if we're in slave mode on an openWB dual and the LP has not just been plugged in (in same control interval as the RFID scan)
# we completely ignore the scan as we cannot associate it with a plugin operation
if (( slavemode == 1 )) && (( lpsPlugStat[$chargePoint] > 0 )) && (( pluggedLps[$chargePoint] != 1 )) && ( (( lastmanagement != 0 )) || (( chargePoint > 1 )) ); then
echo "$NowItIs: Ignoring RFID scan of tag '${lasttag}' for CP #${chargePoint} because that CP is not in 'unplugged' state (plugstatToUse == ${lpsPlugStat[$chargePoint]}, justPlugged == ${pluggedLps[$chargePoint]}, lastmanagement=${lastmanagement})"
return 0
fi

Expand Down
10 changes: 10 additions & 0 deletions runs/mqttsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,16 @@ def on_message(client, userdata, msg):
f = open('/var/www/html/openWB/ramdisk/AllowedPeakPower', 'w')
f.write(msg.payload.decode("utf-8"))
f.close()
if (msg.topic == "openWB/set/configure/FixedChargeCurrentCp1"):
if (int(msg.payload) >= -1 and int(msg.payload) <=32):
f = open('/var/www/html/openWB/ramdisk/FixedChargeCurrentCp1', 'w')
f.write(msg.payload.decode("utf-8"))
f.close()
if (msg.topic == "openWB/set/configure/FixedChargeCurrentCp2"):
if (int(msg.payload) >= -1 and int(msg.payload) <=32):
f = open('/var/www/html/openWB/ramdisk/FixedChargeCurrentCp2', 'w')
f.write(msg.payload.decode("utf-8"))
f.close()
if (msg.topic == "openWB/set/configure/AllowedRfidsForLp1"):
f = open('/var/www/html/openWB/ramdisk/AllowedRfidsForLp1', 'w')
f.write(msg.payload.decode("utf-8"))
Expand Down
83 changes: 52 additions & 31 deletions slavemode.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/bin/bash

HeartbeatTimeout=35
CurrentLimitAmpereForCpCharging=0.5
NumberOfSupportedChargePoints=2
LastChargingPhaseFile="ramdisk/lastChargingPhasesLp"
ExpectedChangeFile="ramdisk/expectedChangeLp"
SystemVoltage=240
MaxCurrentOffset=1.0
MinimumAdjustmentInterval=${slaveModeMinimumAdjustmentInterval:-15}
declare -r HeartbeatTimeout=35
declare -r CurrentLimitAmpereForCpCharging=0.5
declare -r LastChargingPhaseFile="ramdisk/lastChargingPhasesLp"
declare -r ExpectedChangeFile="ramdisk/expectedChangeLp"
declare -r SystemVoltage=240
declare -r MaxCurrentOffset=1.0
declare -r MinimumAdjustmentInterval=${slaveModeMinimumAdjustmentInterval:-15}

if (( lastmanagement > 0 )); then
declare -r -i NumberOfSupportedChargePoints=2
else
declare -r -i NumberOfSupportedChargePoints=1
fi

#
# the main entry point of the script that is called from outside
Expand Down Expand Up @@ -67,9 +72,20 @@ function computeAndSetCurrentForChargePoint() {

# the charge point that we're looking at is our first parameter
local chargePoint=$1
local expectedChangeFile="${ExpectedChangeFile}${chargePoint}"

# check if we're asked for a very fixed charge current (special cases superseding regular load management)
# needed especially to allow additional cars a charge start
if [ -f "ramdisk/FixedChargeCurrentCp${chargePoint}" ]; then
local fixedCurrent=$(<"ramdisk/FixedChargeCurrentCp${chargePoint}")
if (( fixedCurrent >= 0 )); then
$dbgWrite "$NowItIs: Slave Mode: Forced to ${fixedCurrent} A"
callSetCurrent $fixedCurrent $chargePoint
return 0
fi
fi

# check if the car has done the adjustment that it has last been asked for
local expectedChangeFile="${ExpectedChangeFile}${chargePoint}"
local expectedChangeTimestamp=NowItIs
local expectedCurrentPerPhase=-1
if [ -f "${expectedChangeFile}" ]; then
Expand All @@ -86,12 +102,6 @@ function computeAndSetCurrentForChargePoint() {

# we may adjust -- start calculating
declare -i chargingVehiclesAdjustedForThisCp=${ChargingVehiclesOnPhase[$ChargingPhaseWithMaximumTotalCurrent]}
if !(( CpIsCharging )); then

# add 1 for "ourself" as we need to calculate as if we were actually charging
chargingVehiclesAdjustedForThisCp=$((chargingVehiclesAdjustedForThisCp+1))
fi

if (( chargingVehiclesAdjustedForThisCp == 0 )); then
# this can happen in transient when master has not yet detected us as charging but we have already detected us as charging and no other car is charging
$dbgWrite "$NowItIs: Slave Mode: chargingVehiclesAdjustedForThisCp == 0 - forcing chargingVehiclesAdjustedForThisCp=1 for CP#${chargePoint}"
Expand Down Expand Up @@ -170,20 +180,6 @@ function computeAndSetCurrentForChargePoint() {
fi
fi

# finally limit to the configured min or max values
if (( llneu < minimalstromstaerke )) || ((LpEnabled == 0)); then
if ((LpEnabled != 0)); then
$dbgWrite "$NowItIs: Slave Mode Aktiv, LP akt., LpEnabled=$LpEnabled, llneu=$llneu < minmalstromstaerke=$minimalstromstaerke --> setze llneu=0"
else
$dbgWrite "$NowItIs: Slave Mode Aktiv, LP deakt. --> setze llneu=0"
fi
llneu=0
fi
if (( llneu > maximalstromstaerke )); then
$dbgWrite "$NowItIs: Slave Mode Aktiv, llneu=$llneu < maximalstromstaerke=$maximalstromstaerke --> setze llneu=$maximalstromstaerke"
llneu=$maximalstromstaerke
fi

callSetCurrent $llneu $chargePoint

if (( PreviousExpectedChargeCurrent != llneu )); then
Expand Down Expand Up @@ -411,31 +407,56 @@ function checkControllerHeartbeat() {
function callSetCurrent() {

# the new current to set is our first parameter
declare -i -r currentToSet=$1
declare -i currentToSet=$1

# the charge point that we're looking at is the second parameter
# numeric, value of 0 means "all"
local chargePoint=$2

# we have to do a slightly ugly if-else-cascade to set the charge point selector for set-current.sh
# Note: There's currently only one current limit (min/max) per box - so setting same for all CPs
if (( chargePoint == 0 )); then
local chargePointString="all"
local minimumCurrentPossible=$minimalstromstaerke
local maximumCurrentPossible=$maximalstromstaerke
elif (( chargePoint == 1 )); then
local chargePointString="m"
local minimumCurrentPossible=$minimalstromstaerke
local maximumCurrentPossible=$maximalstromstaerke
elif (( chargePoint == 2 )); then
local chargePointString="s1"
local minimumCurrentPossible=$minimalstromstaerke
local maximumCurrentPossible=$maximalstromstaerke
elif (( chargePoint == 3 )); then
local chargePointString="s2"
local minimumCurrentPossible=$minimalstromstaerke
local maximumCurrentPossible=$maximalstromstaerke
elif (( chargePoint >= 4 )); then
local chargePointString="lp${chargePoint}"
local minimumCurrentPossible=$minimalstromstaerke
local maximumCurrentPossible=$maximalstromstaerke
else
echo "$NowItIs: Slave Mode charge current set ERROR: Charge Point #${chargePoint} is not supported"
return 1
fi

$dbgWrite "$NowItIs: callSetCurrent(${currentToSet}, ${chargePoint}): Calling runs/set-current.sh ${currentToSet} ${chargePointString}"
# finally limit to the configured min or max values
if ( (( currentToSet < minimumCurrentPossible )) || ((LpEnabled == 0)) ) && (( currentToSet != 0 )); then
if ((LpEnabled != 0)); then
$dbgWrite "$NowItIs: Slave Mode Aktiv, LP akt., LpEnabled=$LpEnabled, currentToSet=$currentToSet < minimumCurrentPossible=$minimumCurrentPossible --> setze currentToSet=0"
else
$dbgWrite "$NowItIs: Slave Mode Aktiv, LP deakt. --> setze currentToSet=0"
fi
currentToSet=0
fi
if (( currentToSet > maximumCurrentPossible )); then
$dbgWrite "$NowItIs: Slave Mode Aktiv, currentToSet=$currentToSet < maximumCurrentPossible=$maximumCurrentPossible --> setze currentToSet=$maximumCurrentPossible"
currentToSet=$maximumCurrentPossible
fi

if (( PreviousExpectedChargeCurrent != currentToSet )); then

$dbgWrite "$NowItIs: Setting current to ${currentToSet} A for CP#${chargePoint}"
echo "$NowItIs,$currentToSet" > "${ExpectedChangeFile}${chargePoint}"
fi

Expand Down

0 comments on commit f4d9707

Please sign in to comment.