diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b1be0d..83f0b91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -194,6 +194,111 @@ back. Tesla's Fleet API accepts a command only when it is signed with a key paired to the car, and a Lua driver cannot sign. To start a Tesla's charge, add `tesla_vehicle` (BLE proxy). No behaviour changes. +### etrel 1.1.0 + +The driver now controls the charger, so FTW can use it as a loadpoint. It +emits `ev` instead of `v2x_charger` and declares `capabilities = { "ev" }`. + +- Registers come from Etrel's INCH SmartHome Modbus TCP register list. The old + map (holding 300-312) was in no Etrel document and is gone. Readings are + Float32 input registers 0-31; identity is 990-1019. +- An order becomes a current per phase: power over voltage and phases. It is + one phase only when config says `phases: 1` and the car has not drawn on + more than one phase since it was plugged in, or when Core orders `1p` and + the car draws on exactly one. Otherwise, idle or just started too, it is + three, so a three-phase car never gets a one-phase sum on each phase. The + current never exceeds Core's `max_amps_per_phase` or `cap_amps_per_phase`, + the installer's custom max current or 32 A. It rounds down to 0.1 A, and + below 6 A it writes 0 A. +- Pause writes a 0 A setpoint. Core sends resume and start without a budget, + so resume writes an order back only when it is under 30 s old. Otherwise it + writes 6 A, and only when the limit named with the last positive order + allows 6 A (a 0 W order carries no cap, so it does not replace that limit). + With a limit below 6 A it writes nothing and returns false. With no limit + known, after a reload for example, it writes nothing, accepts the resume and + logs once that the charger waits for Core's next order. The pause and stop + registers are one-way triggers with no documented undo, so they are never + written. +- Default mode acts only after a command wrote in this run. Core runs it at + every start, so a charger FTW never steered keeps its own setpoint and a + charge already running goes on, as in heishamon 0.8.1. After that it writes + 0 A once, and returns true only when that lands. A refused stand-down + returns false and is tried again at most once a minute. +- A charger that holds the charge paused against an order for 30 s reports + `command_stalled`, and the next order writes the setpoint again, at most + every 30 s. +- A failed read is left out, not reported as 0. Session energy of 0 is left + out, since some firmware never moves it (evcc-io/evcc#5346). +- The second connector of an INCH Duo is not covered. + +Not yet confirmed on hardware. + +### keba 1.1.0 + +The driver now controls a KeContact P30, so FTW can use it as a loadpoint. It +emits `ev` instead of `v2x_charger` and declares `capabilities = { "ev" }`. + +- Registers follow KEBA's P30 Modbus TCP Programmers Guide V1.07. The old map + read the error code as L1 current, lifetime energy as session energy, and + voltages from the current, serial and product registers. Each value is now + read on its own, as the guide requires. The unit ID default is 255. +- An order writes 5004 (mA) and then enables with 5014. Phases, caps, + rounding, resume and the default-mode rule work as in etrel 1.1.0, with the + hardware limit from 1110. A resume of an old order writes 5004 = 6000 mA + before the enable. Below 6 A, pause and default mode disable with + 5014 = 0, since 5004 takes nothing under 6000 mA. +- Phases count as measured only when all three phase currents answered in + that poll; otherwise orders use three. A phase current that stops answering + is tried again once a minute. +- A charger that offers more than the order, or charges while disabled, gets + the order again after 30 s. One that stays suspended against an enable for + 30 s gets the enable again, at most every 30 s. +- A configured failsafe timeout is refreshed at half its length while FTW + steers, paused or not, and while FTW holds the charger disabled after a + stand-down, so the failsafe current cannot start a charge FTW stopped. + After three refused refreshes it is tried once a minute. Whether the + failsafe current overrides a disable once FTW stops is not verified. +- A charger whose product code is not a P30 is not controlled. +- The UDP interface is not used: FTW's driver host has no UDP, and KEBA + forbids using both. A c-series below firmware 3.10.16 needs an update. + +Not yet confirmed on hardware. + +### schrack_ev 1.1.0 + +The driver now controls a CION Home or Semipublic, so FTW can use it as a +loadpoint. It emits `ev` instead of `v2x_charger` and declares +`capabilities = { "ev" }`. + +- Registers follow Schrack's CION Home and Semipublic Modbus table. The old map + read the charging authorisation as a state and power from registers Schrack + does not list. These chargers speak Modbus RTU, so FTW needs a Modbus TCP to + RTU gateway. +- An order writes the current to 101 (whole A) and then authorises with + 100 = 1. A void authorisation is cleared with 100 = 0 first, as the table + says, at most every 30 s while the charger calls it void. With no car + plugged in only the current is set. + Below 6 A, pause and default mode write 100 = 0. Resume and default mode + work as in etrel 1.1.0; a resume of an old order writes 101 = 6 first. +- The charger reports no power and no phase currents, so `w` stays empty and + orders are split over the phases in config only (`phases`, default 3). +- The CION Pro is not covered: it speaks OCPP, which FTW handles itself. + +Not yet confirmed on hardware. + +### wallbox 1.1.0 + +The driver says it cannot control the charger: `read_only = true`, with the +reason in its description. We found no documented local Modbus interface for +the Pulsar Plus, Pulsar Max, Commander 2 or Copper SB, and the registers this +driver reads come from no Wallbox document, so its power and energy are +unverified. Wallbox documents OCPP 1.6J, and FTW's built-in OCPP central +system reads and controls these chargers without a driver. + +The driver emits `ev` instead of `v2x_charger`, and only fields whose register +answered. It no longer reports 0 W, 0 Wh or 0 A for registers that did not +answer. + ### heishamon 0.8.1 Default mode and cleanup reset `Z1_Heat_Request_Temp` to `safe_offset` only diff --git a/SUPPORT_STATUS.md b/SUPPORT_STATUS.md index 940a359..94c3c60 100644 --- a/SUPPORT_STATUS.md +++ b/SUPPORT_STATUS.md @@ -48,8 +48,8 @@ Catalog source is not proof that a target can install or run a driver. | easee_cloud | 1.4.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | esphome_dsmr | 1.0.7 | ftw-core | not_assessed | — | not_recorded | not_assessed | | esphome_dsmr | 1.0.7 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| etrel | 1.0.3 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| etrel | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| etrel | 1.1.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| etrel | 1.1.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | ferroamp | 2.1.2 | ftw-core | not_assessed | — | not_recorded | not_assessed | | ferroamp | 2.1.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | ferroamp_dc2_v2x | 2.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed | @@ -84,8 +84,8 @@ Catalog source is not proof that a target can install or run a driver. | huawei | 2.1.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | janitza | 1.0.5 | ftw-core | not_assessed | — | not_recorded | not_assessed | | janitza | 1.0.5 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| keba | 1.0.3 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| keba | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| keba | 1.1.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| keba | 1.1.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | konja-261-svk | 0.3.2 | ftw-core | not_assessed | — | not_recorded | not_assessed | | konja-261-svk | 0.3.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | kostal | 2.1.2 | ftw-core | not_assessed | — | not_recorded | not_assessed | @@ -124,8 +124,8 @@ Catalog source is not proof that a target can install or run a driver. | saj | 1.2.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | schneider_meter | 1.0.5 | ftw-core | not_assessed | — | not_recorded | not_assessed | | schneider_meter | 1.0.5 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| schrack_ev | 1.0.3 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| schrack_ev | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| schrack_ev | 1.1.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| schrack_ev | 1.1.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | sdm630 | 2.0.2 | ftw-core | not_assessed | — | not_recorded | not_assessed | | sdm630 | 2.0.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | shelly | 1.1.2 | ftw-core | not_assessed | — | not_recorded | not_assessed | @@ -178,8 +178,8 @@ Catalog source is not proof that a target can install or run a driver. | victron | 2.1.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | victron_mqtt | 1.0.3 | ftw-core | not_assessed | — | not_recorded | not_assessed | | victron_mqtt | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| wallbox | 1.0.3 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| wallbox | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| wallbox | 1.1.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| wallbox | 1.1.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | zap | 3.1.3 | ftw-core | not_assessed | — | not_recorded | not_assessed | | zap | 3.1.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | zaptec_cloud | 0.2.2 | ftw-core | not_assessed | — | not_recorded | not_assessed | diff --git a/devices.yaml b/devices.yaml index fcddc17..e198c0d 100644 --- a/devices.yaml +++ b/devices.yaml @@ -348,16 +348,16 @@ manufacturers: - name: "Etrel" model_families: - name: "INCH" - variants: [INCH DUO, INCH HOME, INCH PRO] + variants: [INCH Duo, INCH Home, INCH Pro] regions: [] protocols: - protocol: modbus driver: "etrel" - version: "1.0.3" - ders: [v2x_charger] - control: false + version: "1.1.0" + ders: [ev] + control: true firmware_versions: "" - notes: "Community driver, untested" + notes: "Control written from Etrel's INCH SmartHome Modbus TCP register list; not yet confirmed on hardware. Duo: connector 1 only." - name: "Ferroamp" model_families: - name: "DC2 V2X" @@ -838,11 +838,11 @@ manufacturers: protocols: - protocol: modbus driver: "keba" - version: "1.0.3" - ders: [v2x_charger] - control: false + version: "1.1.0" + ders: [ev] + control: true firmware_versions: "" - notes: "Community driver, untested" + notes: "Control written from KEBA's P30 Modbus TCP Programmers Guide V1.07; not yet confirmed on hardware. Needs c-series firmware 3.10.16 or x-series 1.11, DIP switch DSW1.3 ON and Modbus unit ID 255." - name: "Konja Power" model_families: - name: "MG series" @@ -1251,17 +1251,17 @@ manufacturers: notes: "Community driver, untested" - name: "Schrack" model_families: - - name: "i-CHARGE" - variants: [i-CHARGE CION, i-CHARGE CION PRO] + - name: "i-CHARGE CION" + variants: [i-CHARGE CION Home, i-CHARGE CION Semipublic] regions: [] protocols: - protocol: modbus driver: "schrack_ev" - version: "1.0.3" - ders: [v2x_charger] - control: false + version: "1.1.0" + ders: [ev] + control: true firmware_versions: "" - notes: "Community driver, untested" + notes: "Control written from Schrack's CION Home and Semipublic Modbus register table; not yet confirmed on hardware. Modbus RTU through a Modbus TCP gateway. No power reading. CION Pro not covered: use OCPP." - name: "Shelly" model_families: - name: "1PM Gen3" @@ -2101,11 +2101,11 @@ manufacturers: protocols: - protocol: modbus driver: "wallbox" - version: "1.0.3" - ders: [v2x_charger] + version: "1.1.0" + ders: [ev] control: false firmware_versions: "" - notes: "Community driver, untested" + notes: "Cannot control: we found no documented local Modbus interface for these chargers. The power and energy this driver reports come from an unverified register map. Use FTW's built-in OCPP central system (Wallbox supports OCPP 1.6J) to read and control them." - name: "Zaptec" model_families: - name: "Zaptec Cloud" diff --git a/drivers/lua/etrel.lua b/drivers/lua/etrel.lua index ebc2908..b5329b8 100644 --- a/drivers/lua/etrel.lua +++ b/drivers/lua/etrel.lua @@ -1,27 +1,123 @@ --- Etrel INCH EV Charger Driver (community, untested) --- Emits: V2X Charger --- Register type: HOLDING (FC 0x03) --- Modbus TCP interface, port 502 --- Note: Currents and voltages are U16 with 0.1 scaling factor +-- Etrel INCH EV charger (Modbus TCP) +-- Emits: ev +-- Controls: charging current per phase, pause and resume. +-- +-- Register map: Etrel's "INCH SmartHome Modbus TCP Registers" list. The copy +-- this driver was written from is the 2023 sheet kept in +-- github.com/RustyDust/sonnen-charger. Etrel now publishes V6.0 (August 2025) +-- in its Knowledge Centre (page "Modbus Communication with INCH products"), +-- which needs firmware 7.1.1 for every register. V6.0 was not checked here. +-- +-- Input registers, function code 4, big-endian words and bytes: +-- 0 connector status (Int16, see STATUS below) +-- 4..5 target current from power management or Modbus (Float32, A) +-- 8..13 L1..L3 voltage (Float32, V) +-- 14..19 L1..L3 current (Float32, A) +-- 26..27 total active power (Float32, kW) +-- 30..31 energy of the running session (Float32, kWh) +-- 990 serial number (10 registers, ASCII) +-- 1000 model (10 registers, ASCII) +-- 1015 software version (5 registers, ASCII) +-- 1028..9 custom max current (Float32, A), set by the installer +-- +-- Holding registers, function code 16 only: +-- 8..9 set current setpoint (Float32, A). Exception 12 means +-- "current setpoint too high". +-- +-- The list also has "pause charging" (2) and "stop charging" (1). Both are +-- one-way triggers, and the list names no register that undoes them, so this +-- driver never writes them. It pauses by writing a 0 A setpoint, as evcc's +-- Etrel charger does, and resumes by writing a current again. "Cancel current +-- setpoint" (10) hands the charger back to its own limit, which is the full +-- installation current, so it is not a safe stand-down either. +-- +-- Not covered: the second connector of an INCH Duo. evcc and the Home +-- Assistant integration ha-etrel-inch address it 100 registers higher, but the +-- register list used here does not say so. +-- +-- Session energy: evcc found register 30 stays 0 on some firmware +-- (evcc-io/evcc#5346). A zero is left out rather than reported, since it +-- cannot be told from a real one. +-- +-- Watchdog: ha-etrel-inch reports two charger settings, a monitoring interval +-- and a fallback current the charger takes when Modbus goes quiet. This driver +-- polls every 5 s. Set the monitoring interval above that if it is used. +-- +-- Config: `phases: 1` on a one-phase installation. Otherwise an order is +-- split over three phases unless Core orders one phase and the car draws on +-- exactly one. +-- +-- Sign convention: an EVSE is a one-way load, so `w` is positive while the car +-- charges, as the charger reports it. + +DRIVER = { + host_api_min = 1, + host_api_max = 1, + id = "etrel", + name = "Etrel INCH", + manufacturer = "Etrel", + version = "1.1.0", + protocols = { "modbus" }, + capabilities = { "ev" }, + description = "Etrel INCH Home, Pro and Duo (connector 1) over Modbus TCP. Reads power, phase currents and charger state, and sets the charging current. Enable Modbus in the charger's web interface.", + homepage = "https://etrel.com", + authors = { "FTW contributors" }, + tested_models = { "INCH Home", "INCH Pro", "INCH Duo" }, + verification_status = "experimental", + verification_notes = "Control written from Etrel's INCH SmartHome Modbus TCP register list; not yet confirmed on hardware.", + connection_defaults = { + port = 502, + unit_id = 1, + }, +} PROTOCOL = "modbus" --- Reading a register the charger does not have costs a failed read on every --- poll forever, and the host counts those against the poll whether or not Lua --- caught the error. Enough of them and the site is marked offline and reports --- nothing at all, which is worse than reporting one field less. So stop asking --- once a register has proved it is not there. Three tries, because one failure --- proves nothing -- the link may just have been slow. --- --- Safe to bound every read here: this driver has no driver_command, so no read --- below is on a control path where giving up could veto a later command. +local REG_BLOCK = 0 -- input 0..31, read in one request +local BLOCK_COUNT = 32 +local REG_SERIAL = 990 -- input, 10 registers +local REG_MODEL = 1000 -- input, 10 registers +local REG_SW_VERSION = 1015 -- input, 5 registers +local REG_CUSTOM_MAX = 1028 -- input, Float32 A +local REG_SET_CURRENT = 8 -- holding, Float32 A, function code 16 + +local MIN_A = 6 -- IEC 61851 minimum; below it the car gets 0 A +local MAX_A = 32 -- the largest INCH model +local STEP_A = 0.1 -- the setpoint is a float; keep tenths + +local STATUS = { + [0] = "unknown", + [1] = "socket_available", + [2] = "waiting_for_vehicle", + [3] = "waiting_for_vehicle_to_start", + [4] = "charging", + [5] = "paused_by_vehicle", + [6] = "paused_by_charger", + [7] = "charging_ended", + [8] = "fault", + [9] = "resuming", + [10] = "unavailable", +} +-- A car is plugged in for these. 1 and 2 mean no car. 0, 8 and 10 say +-- nothing about the cable, so connected stays unknown for them. +local CONNECTED = { [3] = true, [4] = true, [5] = true, [6] = true, [7] = true, [9] = true } + +---------------------------------------------------------------------------- +-- Reads +---------------------------------------------------------------------------- + +-- The host counts every failed modbus_read against the poll whether or not +-- Lua caught it. A register the charger does not answer therefore costs a +-- failed read on every poll, and the site goes offline. Three tries, then +-- leave it alone. Core reloads the driver once nothing is left to read, so an +-- outage does not silence it for good. local GIVE_UP_AFTER = 3 local read_failures = {} local function probe_read(addr, count, kind) if (read_failures[addr] or 0) >= GIVE_UP_AFTER then return nil end local ok, regs = pcall(host.modbus_read, addr, count, kind) - if ok and regs and regs[1] ~= nil then + if ok and type(regs) == "table" and regs[count] ~= nil then read_failures[addr] = nil return regs end @@ -35,75 +131,404 @@ local function probe_read(addr, count, kind) return nil end +-- IEEE-754 binary32 from two big-endian registers. Works on the 16-bit +-- halves so a build with 32-bit integers cannot flip the sign. NaN and +-- infinity come back nil: a number that is not finite is not a reading. +local function decode_f32_be(hi, lo) + if type(hi) ~= "number" or type(lo) ~= "number" then return nil end + local sign = 1 + if hi >= 0x8000 then sign = -1; hi = hi - 0x8000 end + local exponent = math.floor(hi / 128) + local mantissa = (hi % 128) * 65536 + lo + if exponent == 0xFF then return nil end + if exponent == 0 then + if mantissa == 0 then return 0 end + return sign * mantissa * 2^-149 + end + return sign * (1 + mantissa / 0x800000) * 2^(exponent - 127) +end + +-- The two registers of a binary32, for the current setpoint only: 0 or a +-- current between 0.1 and MAX_A. +local function encode_f32_be(value) + if value <= 0 then return 0, 0 end + local exponent = 0 + local m = value + while m >= 2 do m = m / 2; exponent = exponent + 1 end + while m < 1 do m = m * 2; exponent = exponent - 1 end + local fraction = math.floor((m - 1) * 0x800000 + 0.5) + if fraction >= 0x800000 then fraction = 0; exponent = exponent + 1 end + local hi = (exponent + 127) * 128 + math.floor(fraction / 65536) + local lo = fraction % 65536 + return hi, lo +end + +local function ascii(regs, first, count) + local ok, text = pcall(host.decode_string, regs, first, count) + if not ok or type(text) ~= "string" then return "" end + return (text:gsub("[^%w%-%._/ ]", "")) +end + +---------------------------------------------------------------------------- +-- State +---------------------------------------------------------------------------- + +local identity_read = false +local device_limit_a, device_limit_ms = nil, nil +local device_limit_due_ms = nil + +-- What the charger last took from this driver. nil means unknown: the next +-- order writes whatever it is. +local written_a = nil +local written_ms = nil +-- The last order: its current (nil for 0 A), when it came, and the lowest +-- per-phase limit Core named with it. Core sends ev_resume and ev_start with +-- no budget, so a resume trusts only a fresh order and that limit. +local order_a, order_ms = nil, nil +local last_cap_a = nil +local resume_wait_logged = false +-- True from a successful command write until the stand-down lands. While it +-- is true the poll checks that the charger still follows the order. +local steering = false +-- True while a current above zero that this driver wrote may still stand. +local order_landed = false +-- True once a command has tried to write in this run. Core runs default mode +-- at every start, so until then the stand-down leaves the charger alone: a +-- charger FTW never steered keeps its own setpoint, and a charge already +-- running goes on. The same rule as heishamon 0.8.1. +local ftw_wrote = false + +-- Core treats a default mode that returns true as a charger stood down, so +-- true means confirmed (or nothing of FTW's to take back). A refused +-- stand-down returns false and is tried again at most once a minute, without +-- writing in between. +local STANDDOWN_RETRY_MS = 60000 +local standdown_tried_ms = nil + +-- Config `phases`: 1 for a one-phase installation, otherwise three. +local installed_phases = 3 +-- Phases the car drew on at the latest poll, and the most it drew on since +-- it was plugged in. +local drawing_phases = 0 +local seen_phases = 0 + +-- Fresh readings from the latest poll, for the command path. +local last_status = nil +local last_connected = nil +local stalled_since_ms = nil +local stall_rewrite_ms = nil + +local ORDER_SETTLE_MS = 30000 +local ORDER_FRESH_MS = 30000 +local DEVICE_LIMIT_EVERY_MS = 60000 + +---------------------------------------------------------------------------- +-- Orders +---------------------------------------------------------------------------- + +local function finite_positive(v) + return type(v) == "number" and v == v and v > 0 and v < math.huge +end + +-- Three phases, unless one phase is certain: +-- * the installation is one-phase (config) and the car has not drawn on +-- more than one phase since it was plugged in, or +-- * Core orders one phase and the car draws on exactly one. +-- An idle or just-started charger counts as three. Core budgets only L1 for a +-- one-phase order, so a three-phase car must never get that sum per phase. +local function phases_for(cmd) + if installed_phases == 1 and seen_phases <= 1 then return 1 end + if type(cmd) == "table" and cmd.phase_mode == "1p" and + seen_phases == 1 and drawing_phases == 1 then + return 1 + end + return 3 +end + +-- The lowest per-phase limit an order names, or nil when it names none. +local function cap_of(cmd) + local cap = nil + if type(cmd) == "table" then + for _, key in ipairs({ "max_amps_per_phase", "cap_amps_per_phase" }) do + local v = cmd[key] + if finite_positive(v) and (cap == nil or v < cap) then cap = v end + end + end + return cap +end + +-- Core orders total power. The charger takes a current per phase. +-- +-- phases see phases_for +-- amps power / (voltage * phases), never above Core's max and cap per +-- phase or the charger's own limit, rounded down to 0.1 A +-- below MIN_A gives 0 A. A current is never rounded up to the minimum. +local function amps_for(power_w, cmd) + local phases = phases_for(cmd) + if type(power_w) ~= "number" or power_w ~= power_w or power_w <= 0 then + return 0, phases + end + local voltage = 230 + if type(cmd) == "table" and finite_positive(cmd.voltage) then voltage = cmd.voltage end + local ceiling = MAX_A + if device_limit_a and device_limit_a > 0 and device_limit_a < ceiling then + ceiling = device_limit_a + end + if type(cmd) == "table" then + for _, key in ipairs({ "max_amps_per_phase", "cap_amps_per_phase" }) do + local v = cmd[key] + if finite_positive(v) and v < ceiling then ceiling = v end + end + end + local amps = power_w / (voltage * phases) + if amps > ceiling then amps = ceiling end + -- The small allowance absorbs float error in a division Core meant to be + -- exact; it is a ten-thousandth of an ampere, not a rounding up. + amps = math.floor(amps / STEP_A + 0.001) * STEP_A + if amps < MIN_A then return 0, phases end + return amps, phases +end + +local function write_current(amps) + if written_a ~= nil and math.abs(written_a - amps) < 0.05 then return true end + local hi, lo = encode_f32_be(amps) + -- Default mode writes only once this is set, so only a command sets it. + ftw_wrote = true + local ok, err = pcall(host.modbus_write_multi, REG_SET_CURRENT, { hi, lo }) + if not ok or (err ~= nil and err ~= true and err ~= "") then + -- The charger may hold the old setpoint or the new one. + written_a = nil + host.log("warn", "Etrel: current setpoint " .. tostring(amps) .. + " A not written: " .. tostring(ok and err or "error")) + return false + end + written_a, written_ms = amps, host.millis() + order_landed = amps > 0 + return true +end + +---------------------------------------------------------------------------- +-- Driver interface +---------------------------------------------------------------------------- + function driver_init(config) host.set_make("Etrel") + if type(config) == "table" and tonumber(config.phases) == 1 then + installed_phases = 1 + end +end + +local function read_identity() + if identity_read then return end + local regs = probe_read(REG_SERIAL, 30, "input") + if not regs then return end + identity_read = true + local serial = ascii(regs, 1, 10) + local model = ascii(regs, REG_MODEL - REG_SERIAL + 1, 10) + local software = ascii(regs, REG_SW_VERSION - REG_SERIAL + 1, 5) + if #serial > 0 then host.set_sn(serial) end + if #model > 0 then host.set_model(model) end + if #software > 0 then host.log("info", "Etrel: software " .. software) end +end + +local function read_device_limit(now) + if device_limit_due_ms and now < device_limit_due_ms then return end + device_limit_due_ms = now + DEVICE_LIMIT_EVERY_MS + local regs = probe_read(REG_CUSTOM_MAX, 2, "input") + local limit = regs and decode_f32_be(regs[1], regs[2]) + if limit and limit > 0 and limit <= 80 then + device_limit_a, device_limit_ms = limit, now + end end function driver_poll() - -- Active power: 300 (U32, W) - local w_regs = probe_read(300, 2, "holding") - local power_w = 0 - if w_regs then - power_w = host.decode_u32_be(w_regs[1], w_regs[2]) - end - - -- Session energy: 304 (U32, Wh) - local se_regs = probe_read(304, 2, "holding") - local session_wh = 0 - if se_regs then - session_wh = host.decode_u32_be(se_regs[1], se_regs[2]) - end - - -- L1 current: 306 (U16, 0.1A), L2: 307, L3: 308 - local a_regs = probe_read(306, 3, "holding") - local l1_a, l2_a, l3_a = 0, 0, 0 - if a_regs then - l1_a = a_regs[1] * 0.1 - l2_a = a_regs[2] * 0.1 - l3_a = a_regs[3] * 0.1 - end - - -- L1 voltage: 309 (U16, 0.1V), L2: 310, L3: 311 - local v_regs = probe_read(309, 3, "holding") - local l1_v, l2_v, l3_v = 0, 0, 0 - if v_regs then - l1_v = v_regs[1] * 0.1 - l2_v = v_regs[2] * 0.1 - l3_v = v_regs[3] * 0.1 - end - - -- Charger state: 312 (U16: 0=idle, 1=connected, 2=charging, 3=error) - local st_regs = probe_read(312, 1, "holding") - local raw_state = 0 - local state = 0 - if st_regs then - raw_state = st_regs[1] - -- Map Etrel states to standard: 0=idle, 1=connected, 2=charging, 3=error - if raw_state == 0 then - state = 0 -- idle -> idle - elseif raw_state == 1 then - state = 1 -- connected -> connected - elseif raw_state == 2 then - state = 2 -- charging -> charging - elseif raw_state == 3 then - state = 3 -- error -> error + local now = host.millis() + read_identity() + read_device_limit(now) + + local regs = probe_read(REG_BLOCK, BLOCK_COUNT, "input") + if not regs then + last_status, last_connected, drawing_phases = nil, nil, 0 + return 5000 + end + + local status = regs[1] + if status ~= nil and STATUS[status] == nil then status = nil end + local target_a = decode_f32_be(regs[5], regs[6]) + local volts, amps = {}, {} + for i = 1, 3 do + volts[i] = decode_f32_be(regs[9 + 2 * (i - 1)], regs[10 + 2 * (i - 1)]) + amps[i] = decode_f32_be(regs[15 + 2 * (i - 1)], regs[16 + 2 * (i - 1)]) + end + local kw = decode_f32_be(regs[27], regs[28]) + local session_kwh = decode_f32_be(regs[31], regs[32]) + + local w = kw and kw * 1000 or nil + local connected = nil + if status == 1 or status == 2 then connected = false end + if status and CONNECTED[status] then connected = true end + local charging = nil + if status ~= nil and status ~= 0 then charging = status == 4 end + + local measured = 0 + for i = 1, 3 do + if amps[i] and amps[i] >= 1 then measured = measured + 1 end + end + last_status, last_connected = status, connected + -- A new car starts unseen: a one-phase car after a three-phase one may + -- use one phase again. + if connected == false then seen_phases = 0 end + drawing_phases = measured + if measured > seen_phases then seen_phases = measured end + + -- A charger that lets the car take more than this driver ordered has lost + -- the order, after a restart for example. Forget what was written so the + -- next order writes it again. Only once the order has had time to land. + if steering and written_a ~= nil and target_a and connected and + written_ms and now - written_ms >= ORDER_SETTLE_MS and + target_a > written_a + 0.5 then + host.log("warn", string.format( + "Etrel: charger allows %.1f A against an order of %.1f A; writing it again", + target_a, written_a)) + written_a = nil + end + + -- The current the charger lets the car take now, when it is below the + -- order and the order has stood long enough to be followed. + local current_limit_a = nil + if charging and target_a and written_a and written_a > 0 and written_ms and + now - written_ms >= ORDER_SETTLE_MS and target_a + 0.5 < written_a then + current_limit_a = target_a + end + + -- Stalled: a current of ours stands, a car is plugged in, and the charger + -- itself holds the charge paused. Once that has lasted 30 s, forget the + -- setpoint so the next order writes it again, at most every 30 s. + local command_stalled = false + if steering and order_landed and connected and status == 6 then + stalled_since_ms = stalled_since_ms or now + command_stalled = now - stalled_since_ms >= ORDER_SETTLE_MS + if command_stalled and (not stall_rewrite_ms or now - stall_rewrite_ms >= ORDER_SETTLE_MS) then + stall_rewrite_ms = now + written_a = nil end + else + stalled_since_ms, stall_rewrite_ms = nil, nil end - -- Emit V2X charger telemetry - host.emit("v2x_charger", { - w = power_w, - session_charge_wh = session_wh, - l1_v = l1_v, - l2_v = l2_v, - l3_v = l3_v, - l1_a = l1_a, - l2_a = l2_a, - l3_a = l3_a, + local reason = nil + if connected and charging == false then reason = STATUS[status] end + -- Paused by the car or ended: the car does not ask for current. + local request_active = nil + if connected then request_active = not (status == 5 or status == 7) end + + host.emit("ev", { + w = w, + connected = connected, + charging = charging, + request_active = request_active, + session_wh = (session_kwh and session_kwh > 0) and session_kwh * 1000 or nil, + l1_v = volts[1], l2_v = volts[2], l3_v = volts[3], + l1_a = amps[1], l2_a = amps[2], l3_a = amps[3], + phases = measured > 0 and measured or nil, + max_a = target_a, + current_limit_a = current_limit_a, + device_limit_a = device_limit_a, + device_limit_age_s = device_limit_ms and (now - device_limit_ms) / 1000 or nil, + state_label = status and STATUS[status] or nil, + reason_no_current_label = reason, + command_stalled = command_stalled, }) return 5000 end +-- Core sends ev_resume and ev_start with no budget. A fresh order is written +-- again. Otherwise only MIN_A, and only when the last limit Core named allows +-- it; with no such limit nothing is written and the next order sets the level. +local function resume() + local now = host.millis() + local amps = nil + if order_a and order_ms and now - order_ms <= ORDER_FRESH_MS then + amps = order_a + elseif last_cap_a and last_cap_a >= MIN_A then + amps = MIN_A + written_a = nil + end + if not amps and last_cap_a == nil then + -- No limit known, after a reload for example. Nothing was refused and + -- nothing written; Core's next order sets the level. + if not resume_wait_logged then + resume_wait_logged = true + host.log("info", "Etrel: no fresh order and no known limit; " .. + "the charger waits for Core's next order") + end + return true + end + if not amps then + host.log("info", "Etrel: the last limit Core named leaves no room for 6 A; " .. + "resume waits for the next order") + return false + end + local ok = write_current(amps) + if ok then steering = true end + return ok +end + +function driver_command(action, power_w, cmd) + if action == "init" then return true end + if action == "deinit" then return driver_default_mode() end + + if action == "ev_set_current" then + local amps, phases = amps_for(power_w, cmd) + host.log("debug", string.format("Etrel: %s W on %d phase(s) -> %.1f A", + tostring(power_w), phases, amps)) + -- Core names cap_amps_per_phase only with a positive order, so a 0 W + -- order must not replace the cap with the site maximum. + if type(power_w) == "number" and power_w > 0 then last_cap_a = cap_of(cmd) end + resume_wait_logged = false + order_a, order_ms = (amps > 0) and amps or nil, host.millis() + local ok = write_current(amps) + if ok then steering = true end + return ok + end + + if action == "ev_pause" then + local ok = write_current(0) + if ok then steering = true end + return ok + end + + if action == "ev_resume" or action == "ev_start" then + return resume() + end + + host.log("warn", "Etrel: unknown action " .. tostring(action)) + return false +end + +-- Stand the charger down: a 0 A setpoint, once a command wrote in this run. +-- Repeated calls write nothing once that has landed. A refused stand-down +-- returns false and is tried again at most once a minute. +function driver_default_mode() + if not ftw_wrote then return true end + if written_a == 0 then + steering = false + return true + end + local now = host.millis() + if standdown_tried_ms and now - standdown_tried_ms < STANDDOWN_RETRY_MS then + return false + end + standdown_tried_ms = now + if write_current(0) then + standdown_tried_ms = nil + steering = false + return true + end + host.log("warn", "Etrel: stand-down refused; trying again in a minute") + return false +end + function driver_cleanup() end diff --git a/drivers/lua/keba.lua b/drivers/lua/keba.lua index 8928e00..76f88b7 100644 --- a/drivers/lua/keba.lua +++ b/drivers/lua/keba.lua @@ -1,142 +1,597 @@ --- Keba KeContact P30 EV Charger Driver (community, untested) --- Emits: V2X Charger --- Register type: HOLDING (FC 0x03) --- Keba Modbus TCP interface --- Note: Most values are U32 in milliunit format +-- KEBA KeContact P30 EV charger (Modbus TCP) +-- Emits: ev +-- Controls: charging current per phase, pause and resume. +-- +-- Register map: KEBA "KeContact P30 Charging Station Modbus TCP Programmers +-- Guide" V1.07 (document 132578, 12.2025). +-- +-- Needs a P30 c-series with firmware 3.10.16 or later, or a P30 x-series +-- with software 1.11 or later, and DIP switch DSW1.3 set to ON. Unit ID 255, +-- port 502. Function codes 3 (read) and 6 (write) only. Each read is one +-- UINT32 value of two registers; the guide forbids reading more at once. +-- KEBA asks for at least 5 s between writes. +-- +-- Read (function code 3, UINT32 big-endian): +-- 1000 charging state 0 start-up, 1 not ready, 2 ready, 3 charging, +-- 4 error, 5 suspended (temperature or disabled) +-- 1004 cable state 5 and 7: plugged into the car +-- 1008/1010/1012 L1..L3 current, mA +-- 1014 serial number +-- 1016 product type e.g. 304111: P30, cable 0, 32 A, c-series, +-- energy meter 1, RFID 1. Meter digit 0: none. +-- 1020 active power, mW +-- 1036 total energy, 0.1 Wh +-- 1040/1042/1044 L1..L3 voltage, V +-- 1100 max charging current, mA: what the charger offers the car now +-- 1110 max supported current, mA: DIP switches, cable and temperature +-- 1502 charged energy of the session, 0.1 Wh +-- 1602 failsafe timeout, s +-- +-- Write (function code 6, UINT16): +-- 5004 set charging current, mA, 6000..63000. Holds until a reboot. +-- 5014 1 enable, 0 disable (suspended). Stops an active charge. +-- 5018 failsafe timeout, s. Resets the failsafe timer. +-- +-- The guide names no way to set 5004 below 6000, so pause and every order +-- below 6 A disable the charger with 5014 instead. +-- +-- Failsafe: when Modbus commands stop for the failsafe timeout, the charger +-- takes its failsafe current (5016). The guide does not say whether that +-- current overrides a disable (5014 = 0), and it is not verified here. So +-- while FTW steers, and while it holds the charger disabled after a +-- stand-down, the driver keeps refreshing the timer (5018). Once FTW stops, +-- the charger follows its own failsafe settings. +-- +-- The UDP interface (port 7090: `currtime`, `ena`, `report 2/3`) reaches the +-- same charger, but FTW's driver host has no UDP. KEBA forbids using both +-- interfaces together, so this driver uses Modbus TCP only. A c-series still +-- on firmware older than 3.10.16 speaks only UDP: update its firmware. +-- The P40 has its own guide and is not covered. +-- +-- Config: `phases: 1` on a one-phase installation. Otherwise an order is +-- split over three phases unless Core orders one phase and the car draws on +-- exactly one. +-- +-- Sign convention: an EVSE is a one-way load, so `w` is positive while the car +-- charges, as the charger reports it. + +DRIVER = { + host_api_min = 1, + host_api_max = 1, + id = "keba", + name = "KEBA KeContact P30", + manufacturer = "KEBA", + version = "1.1.0", + protocols = { "modbus" }, + capabilities = { "ev" }, + description = "KEBA KeContact P30 c-series (firmware 3.10.16 or later) and x-series (1.11 or later) over Modbus TCP. Reads power, phase currents and charger state, and sets the charging current. Set DIP switch DSW1.3 to ON and the Modbus unit ID to 255; the UDP interface must stay off.", + homepage = "https://www.keba.com/emobility", + authors = { "FTW contributors" }, + tested_models = { "P30 c-series", "P30 x-series" }, + verification_status = "experimental", + verification_notes = "Control written from KEBA's P30 Modbus TCP Programmers Guide V1.07; not yet confirmed on hardware.", + connection_defaults = { + port = 502, + unit_id = 255, + }, +} PROTOCOL = "modbus" +local REG_STATE = 1000 +local REG_CABLE = 1004 +local REG_CURRENT = { 1008, 1010, 1012 } +local REG_SERIAL = 1014 +local REG_PRODUCT = 1016 +local REG_POWER = 1020 +local REG_TOTAL_ENERGY = 1036 +local REG_VOLTAGE = { 1040, 1042, 1044 } +local REG_MAX_CURRENT = 1100 +local REG_HW_CURRENT = 1110 +local REG_SESSION = 1502 +local REG_FS_TIMEOUT = 1602 + +local REG_SET_CURRENT = 5004 +local REG_ENABLE = 5014 +local REG_SET_FS_TMO = 5018 + +local MIN_A = 6 -- 5004 takes nothing below 6000 mA +local MAX_A = 32 -- the largest P30 hardware +local STEP_A = 0.1 + +local STATE = { + [0] = "starting", + [1] = "not_ready", + [2] = "waiting_for_vehicle", + [3] = "charging", + [4] = "error", + [5] = "suspended", +} + ---------------------------------------------------------------------------- --- Absent-register guard +-- Reads ---------------------------------------------------------------------------- + -- The host counts every failed modbus_read against the poll whether or not --- Lua caught it. So a register this driver can live without must stop being --- read once the device has made clear it will not answer — otherwise the --- poll keeps failing, the stale-telemetry watchdog marks the charger --- offline, and the site reports nothing at all instead of one field less. +-- Lua caught it. A register the charger does not answer costs a failed read +-- on every poll and takes the site offline. Three tries, then leave it alone. +-- Core reloads the driver once nothing is left to read. -- --- Three strikes rather than one: a single miss is more often a slow link --- than a missing register. A restart re-probes. +-- The phase currents are the exception. Every P30 has all three, and while +-- one is missing the driver must divide by three phases, so a lost phase +-- current is tried once a minute instead of never again. local GIVE_UP_AFTER = 3 +local RETRY_AFTER_MS = 60000 +local RETRIED = { [1008] = true, [1010] = true, [1012] = true } local read_failures = {} +local retry_at = {} local function probe_read(addr, count, kind) - if (read_failures[addr] or 0) >= GIVE_UP_AFTER then return nil end + if (read_failures[addr] or 0) >= GIVE_UP_AFTER then + if not RETRIED[addr] then return nil end + local now = host.millis() + if now < (retry_at[addr] or 0) then return nil end + retry_at[addr] = now + RETRY_AFTER_MS + end local ok, regs = pcall(host.modbus_read, addr, count, kind) - if ok and regs and regs[1] ~= nil then - read_failures[addr] = nil + if ok and type(regs) == "table" and regs[count] ~= nil then + read_failures[addr], retry_at[addr] = nil, nil return regs end local failures = (read_failures[addr] or 0) + 1 read_failures[addr] = failures if failures == GIVE_UP_AFTER then + retry_at[addr] = host.millis() + RETRY_AFTER_MS host.log("info", string.format( - "Keba: register %d did not answer %d times; leaving it alone " .. - "until restart", addr, GIVE_UP_AFTER)) + "KEBA: register %d did not answer %d times; %s", addr, GIVE_UP_AFTER, + RETRIED[addr] and "trying it once a minute" or "leaving it alone until restart")) end return nil end -function driver_init(config) - host.set_make("Keba") +-- One UINT32 register pair, high word first. +local function read_u32(addr) + local regs = probe_read(addr, 2, "holding") + if not regs then return nil end + return host.decode_u32_be(regs[1], regs[2]) end -function driver_poll() - -- Charger state: 1000 (U32: 0=startup, 1=not ready, 2=ready, 3=charging, 4=error, 5=suspended) - local st_regs = probe_read(1000, 2, "holding") - local raw_state = 0 - local state = 0 - if st_regs then - raw_state = host.decode_u32_be(st_regs[1], st_regs[2]) - -- Map Keba states to standard: 0=idle, 1=connected, 2=charging, 3=error - if raw_state == 0 then - state = 0 -- startup -> idle - elseif raw_state == 1 then - state = 0 -- not ready -> idle - elseif raw_state == 2 then - state = 0 -- ready -> idle - elseif raw_state == 3 then - state = 2 -- charging -> charging - elseif raw_state == 4 then - state = 3 -- error -> error - elseif raw_state == 5 then - state = 1 -- suspended -> connected +---------------------------------------------------------------------------- +-- State +---------------------------------------------------------------------------- + +local identity_read = false +local product = nil -- product code digits as a string, once read +local is_p30 = nil -- nil until the product code answers +local has_meter = true -- false only when the product code says so + +-- What the charger last took from this driver. nil means unknown. +local written_ma = nil +local written_ms = nil +local enabled = nil -- 5014 is write-only, so this is ours to track +local enabled_ms = nil +-- The last order: its current (nil for 0 A), when it came, and the lowest +-- per-phase limit Core named with it. Core sends ev_resume and ev_start with +-- no budget, so a resume trusts only a fresh order and that limit. +local order_a, order_ms = nil, nil +local last_cap_a = nil +local resume_wait_logged = false +local steering = false -- a command landed and no stand-down since +local order_landed = false -- an enable of ours may still stand +-- True once a command has tried to write in this run. Core runs default mode +-- at every start, so until then the stand-down leaves the charger alone: a +-- charger FTW never steered keeps its own state, and a charge already running +-- goes on. The same rule as heishamon 0.8.1. +local ftw_wrote = false + +-- Core treats a default mode that returns true as a charger stood down, so +-- true means confirmed (or nothing of FTW's to take back). A refused +-- stand-down returns false and is tried again at most once a minute, without +-- writing in between. +local STANDDOWN_RETRY_MS = 60000 +local standdown_tried_ms = nil + +local failsafe_timeout_s = nil +local failsafe_refreshed_ms = nil +local FAILSAFE_ATTEMPTS = 3 +local FAILSAFE_BACKOFF_MS = 60000 +local failsafe_refusals = 0 + +-- Config `phases`: 1 for a one-phase installation, otherwise three. +local installed_phases = 3 +-- Phases the car drew on at the latest poll, and the most it drew on since +-- it was plugged in. +local drawing_phases = 0 +local seen_phases = 0 +local device_limit_a, device_limit_ms = nil, nil +local stalled_since_ms = nil +local stall_rewrite_ms = nil + +local ORDER_SETTLE_MS = 30000 +local ORDER_FRESH_MS = 30000 + +---------------------------------------------------------------------------- +-- Writes +---------------------------------------------------------------------------- + +local function write(addr, value, what) + local ok, err = pcall(host.modbus_write, addr, value) + if not ok or (err ~= nil and err ~= true and err ~= "") then + host.log("warn", "KEBA: " .. what .. " not written: " .. + tostring(ok and err or "error")) + return false + end + return true +end + +local function write_current_ma(ma) + if written_ma == ma then return true end + -- Default mode writes only once this is set, so only a command sets it. + ftw_wrote = true + if not write(REG_SET_CURRENT, ma, "charging current " .. tostring(ma) .. " mA") then + written_ma = nil + return false + end + written_ma, written_ms = ma, host.millis() + return true +end + +local function set_enabled(on) + if enabled == on then return true end + ftw_wrote = true + if not write(REG_ENABLE, on and 1 or 0, on and "enable" or "disable") then + enabled = nil + return false + end + enabled, enabled_ms = on, host.millis() + order_landed = on + return true +end + +---------------------------------------------------------------------------- +-- Orders +---------------------------------------------------------------------------- + +local function finite_positive(v) + return type(v) == "number" and v == v and v > 0 and v < math.huge +end + +-- Three phases, unless one phase is certain: +-- * the installation is one-phase (config) and the car has not drawn on +-- more than one phase since it was plugged in, or +-- * Core orders one phase and the car draws on exactly one. +-- An idle or just-started charger counts as three. Core budgets only L1 for a +-- one-phase order, so a three-phase car must never get that sum per phase. +local function phases_for(cmd) + if installed_phases == 1 and seen_phases <= 1 then return 1 end + if type(cmd) == "table" and cmd.phase_mode == "1p" and + seen_phases == 1 and drawing_phases == 1 then + return 1 + end + return 3 +end + +-- The lowest per-phase limit an order names, or nil when it names none. +local function cap_of(cmd) + local cap = nil + if type(cmd) == "table" then + for _, key in ipairs({ "max_amps_per_phase", "cap_amps_per_phase" }) do + local v = cmd[key] + if finite_positive(v) and (cap == nil or v < cap) then cap = v end end end + return cap +end - -- L1 current: 1006 (U32, mA), L2: 1008, L3: 1010 - local a1_regs = probe_read(1006, 2, "holding") - local l1_a = 0 - if a1_regs then - l1_a = host.decode_u32_be(a1_regs[1], a1_regs[2]) * 0.001 +-- Core orders total power. The charger takes a current per phase. +-- +-- phases see phases_for +-- amps power / (voltage * phases), never above Core's max and cap per +-- phase or the charger's own limit, rounded down to 0.1 A +-- below MIN_A gives 0 A. A current is never rounded up to the minimum. +local function amps_for(power_w, cmd) + local phases = phases_for(cmd) + if type(power_w) ~= "number" or power_w ~= power_w or power_w <= 0 then + return 0, phases + end + local voltage = 230 + if type(cmd) == "table" and finite_positive(cmd.voltage) then voltage = cmd.voltage end + local ceiling = MAX_A + if device_limit_a and device_limit_a > 0 and device_limit_a < ceiling then + ceiling = device_limit_a end + if type(cmd) == "table" then + for _, key in ipairs({ "max_amps_per_phase", "cap_amps_per_phase" }) do + local v = cmd[key] + if finite_positive(v) and v < ceiling then ceiling = v end + end + end + local amps = power_w / (voltage * phases) + if amps > ceiling then amps = ceiling end + -- The allowance absorbs float error in a division Core meant to be exact. + amps = math.floor(amps / STEP_A + 0.001) * STEP_A + if amps < MIN_A then return 0, phases end + return amps, phases +end + +local function to_ma(amps) + return math.floor(amps * 10 + 0.5) * 100 +end + +-- Set the current first, then enable, so a charger that was disabled never +-- starts on an older, higher current. +local function charge_at(amps) + if not write_current_ma(to_ma(amps)) then return false end + return set_enabled(true) +end - local a2_regs = probe_read(1008, 2, "holding") - local l2_a = 0 - if a2_regs then - l2_a = host.decode_u32_be(a2_regs[1], a2_regs[2]) * 0.001 +local function control_allowed() + if is_p30 == false then + host.log("warn", "KEBA: product code " .. tostring(product) .. + " is not a P30; this driver does not control it") + return false end + return true +end + +---------------------------------------------------------------------------- +-- Driver interface +---------------------------------------------------------------------------- - local a3_regs = probe_read(1010, 2, "holding") - local l3_a = 0 - if a3_regs then - l3_a = host.decode_u32_be(a3_regs[1], a3_regs[2]) * 0.001 +function driver_init(config) + host.set_make("Keba") + if type(config) == "table" and tonumber(config.phases) == 1 then + installed_phases = 1 end +end + +local function read_identity() + if identity_read then return end + local code = read_u32(REG_PRODUCT) + if not code then return end + identity_read = true + product = string.format("%.0f", code) + -- P30 product codes have six digits and start with 3. + is_p30 = #product == 6 and product:sub(1, 1) == "3" + if is_p30 then + has_meter = product:sub(5, 5) ~= "0" + host.set_model("KeContact P30 " .. + (product:sub(4, 4) == "1" and "c-series" or "x-series")) + end + local serial = read_u32(REG_SERIAL) + if serial then host.set_sn(string.format("%.0f", serial)) end +end + +local function milliamps(addr) + local v = read_u32(addr) + return v and v / 1000 or nil +end - -- L1 voltage: 1012 (U32, mV), L2: 1014, L3: 1016 - local v1_regs = probe_read(1012, 2, "holding") - local l1_v = 0 - if v1_regs then - l1_v = host.decode_u32_be(v1_regs[1], v1_regs[2]) * 0.001 +function driver_poll() + local now = host.millis() + read_identity() + + local state = read_u32(REG_STATE) + if state ~= nil and STATE[state] == nil then state = nil end + local cable = read_u32(REG_CABLE) + + local amps, volts = {}, {} + local w, session_wh, lifetime_wh = nil, nil, nil + if has_meter then + for i = 1, 3 do amps[i] = milliamps(REG_CURRENT[i]) end + local mw = read_u32(REG_POWER) + w = mw and mw / 1000 or nil + local total = read_u32(REG_TOTAL_ENERGY) + lifetime_wh = total and total / 10 or nil + local session = read_u32(REG_SESSION) + session_wh = session and session / 10 or nil + for i = 1, 3 do volts[i] = read_u32(REG_VOLTAGE[i]) end end + local offered_a = milliamps(REG_MAX_CURRENT) + local hw_a = milliamps(REG_HW_CURRENT) + if hw_a and hw_a > 0 and hw_a <= 63 then device_limit_a, device_limit_ms = hw_a, now end + failsafe_timeout_s = read_u32(REG_FS_TIMEOUT) + + if state == nil and cable == nil and w == nil then + -- Nothing answered: there is no fresh reading to report. + drawing_phases = 0 + return 5000 + end + + local connected = nil + if cable ~= nil then connected = cable == 5 or cable == 7 end + local charging = nil + if state ~= nil and state ~= 0 then charging = state == 3 end - local v2_regs = probe_read(1014, 2, "holding") - local l2_v = 0 - if v2_regs then - l2_v = host.decode_u32_be(v2_regs[1], v2_regs[2]) * 0.001 + -- Phases count as measured only when all three currents answered this + -- poll. Otherwise the count is unknown and orders use three phases. + local measured = nil + if amps[1] and amps[2] and amps[3] then + measured = 0 + for i = 1, 3 do + if amps[i] >= 1 then measured = measured + 1 end + end end + -- A new car starts unseen: a one-phase car after a three-phase one may + -- use one phase again. + if connected == false then seen_phases = 0 end + drawing_phases = measured + if measured and measured > seen_phases then seen_phases = measured end - local v3_regs = probe_read(1016, 2, "holding") - local l3_v = 0 - if v3_regs then - l3_v = host.decode_u32_be(v3_regs[1], v3_regs[2]) * 0.001 + -- A charger that offers more than this driver ordered, or charges while + -- this driver disabled it, has lost the order: a reboot clears 5004. + -- Forget what was written so the next order writes it again, once the + -- write has had time to land (KEBA takes 6-7 s to apply a current). + if steering and connected then + if written_ma and offered_a and written_ms and now - written_ms >= ORDER_SETTLE_MS and + offered_a > written_ma / 1000 + 0.5 then + host.log("warn", string.format( + "KEBA: charger offers %.1f A against an order of %.1f A; writing it again", + offered_a, written_ma / 1000)) + written_ma = nil + end + if enabled == false and charging and enabled_ms and now - enabled_ms >= ORDER_SETTLE_MS then + host.log("warn", "KEBA: charging although disabled; disabling again") + enabled = nil + end end - -- Active power: 1020 (U32, mW) - local w_regs = probe_read(1020, 2, "holding") - local power_w = 0 - if w_regs then - power_w = host.decode_u32_be(w_regs[1], w_regs[2]) * 0.001 + -- The failsafe sets its own current (5016) when Modbus commands stop for + -- its timeout. While FTW steers, paused or not, and while it holds the + -- charger disabled after a stand-down, refresh it at half the timeout, + -- never faster than KEBA's 5 s between writes; otherwise a failsafe + -- current could start a charge FTW stopped. After three refusals try once + -- a minute, and start over after a success. + if (steering or (ftw_wrote and enabled == false)) and failsafe_timeout_s and failsafe_timeout_s >= 5 and + failsafe_timeout_s <= 600 then + local every_ms = math.max(5, failsafe_timeout_s / 2) * 1000 + if failsafe_refusals >= FAILSAFE_ATTEMPTS then + every_ms = math.max(every_ms, FAILSAFE_BACKOFF_MS) + end + if not failsafe_refreshed_ms or now - failsafe_refreshed_ms >= every_ms then + failsafe_refreshed_ms = now + if write(REG_SET_FS_TMO, failsafe_timeout_s, "failsafe refresh") then + failsafe_refusals = 0 + else + failsafe_refusals = failsafe_refusals + 1 + end + end end - -- Session energy: 1036 (U32, 0.1Wh) - local se_regs = probe_read(1036, 2, "holding") - local session_wh = 0 - if se_regs then - session_wh = host.decode_u32_be(se_regs[1], se_regs[2]) * 0.1 + local current_limit_a = nil + if charging and offered_a and written_ma and enabled and written_ms and + now - written_ms >= ORDER_SETTLE_MS and offered_a + 0.5 < written_ma / 1000 then + current_limit_a = offered_a end - -- Max current: 5004 (U16, mA) -- read/write register - local mc_regs = probe_read(5004, 1, "holding") - local max_a = 0 - if mc_regs then - max_a = mc_regs[1] * 0.001 + -- Stalled: an enable of ours stands, a car is plugged in, and the charger + -- stays suspended. Once that has lasted 30 s, forget the enable so the + -- next order writes it again, at most every 30 s. + local command_stalled = false + if steering and order_landed and connected and state == 5 then + stalled_since_ms = stalled_since_ms or now + command_stalled = now - stalled_since_ms >= ORDER_SETTLE_MS + if command_stalled and (not stall_rewrite_ms or now - stall_rewrite_ms >= ORDER_SETTLE_MS) then + stall_rewrite_ms = now + enabled = nil + end + else + stalled_since_ms, stall_rewrite_ms = nil, nil end - -- Emit V2X charger telemetry - host.emit("v2x_charger", { - w = power_w, - session_charge_wh = session_wh, - l1_v = l1_v, - l2_v = l2_v, - l3_v = l3_v, - l1_a = l1_a, - l2_a = l2_a, - l3_a = l3_a, + local reason = nil + if connected and charging == false then reason = STATE[state] end + + host.emit("ev", { + w = w, + connected = connected, + charging = charging, + session_wh = session_wh, + lifetime_wh = lifetime_wh, + l1_v = volts[1], l2_v = volts[2], l3_v = volts[3], + l1_a = amps[1], l2_a = amps[2], l3_a = amps[3], + phases = (measured and measured > 0) and measured or nil, + max_a = offered_a, + current_limit_a = current_limit_a, + device_limit_a = device_limit_a, + device_limit_age_s = device_limit_ms and (now - device_limit_ms) / 1000 or nil, + state_label = state and STATE[state] or nil, + reason_no_current_label = reason, + command_stalled = command_stalled, }) return 5000 end +function driver_command(action, power_w, cmd) + if action == "init" then return true end + if action == "deinit" then return driver_default_mode() end + if not control_allowed() then return false end + + if action == "ev_set_current" then + local amps, phases = amps_for(power_w, cmd) + host.log("debug", string.format("KEBA: %s W on %d phase(s) -> %.1f A", + tostring(power_w), phases, amps)) + -- Core names cap_amps_per_phase only with a positive order, so a 0 W + -- order must not replace the cap with the site maximum. + if type(power_w) == "number" and power_w > 0 then last_cap_a = cap_of(cmd) end + resume_wait_logged = false + order_a, order_ms = (amps > 0) and amps or nil, host.millis() + local ok + if amps <= 0 then + ok = set_enabled(false) + else + ok = charge_at(amps) + end + if ok then steering = true end + return ok + end + + if action == "ev_pause" then + local ok = set_enabled(false) + if ok then steering = true end + return ok + end + + if action == "ev_resume" or action == "ev_start" then + -- No budget comes with these. A fresh order is written again. + -- Otherwise 5004 = 6000 mA first, and only when the last limit Core + -- named allows 6 A. With no limit known nothing is written and + -- Core's next order sets the level. + local now = host.millis() + local amps = nil + if order_a and order_ms and now - order_ms <= ORDER_FRESH_MS then + amps = order_a + elseif last_cap_a and last_cap_a >= MIN_A then + amps = MIN_A + written_ma = nil + end + if not amps and last_cap_a == nil then + -- No limit known, after a reload for example. Nothing was refused and + -- nothing written; Core's next order sets the level. + if not resume_wait_logged then + resume_wait_logged = true + host.log("info", "KEBA: no fresh order and no known limit; " .. + "the charger waits for Core's next order") + end + return true + end + if not amps then + host.log("info", "KEBA: the last limit Core named leaves no room for 6 A; " .. + "resume waits for the next order") + return false + end + local ok = charge_at(amps) + if ok then steering = true end + return ok + end + + host.log("warn", "KEBA: unknown action " .. tostring(action)) + return false +end + +-- Stand the charger down: disable it, once a command wrote in this run. +-- Repeated calls write nothing once that has landed. A refused stand-down +-- returns false and is tried again at most once a minute. +function driver_default_mode() + if not ftw_wrote then return true end + if enabled == false then + steering = false + return true + end + local now = host.millis() + if standdown_tried_ms and now - standdown_tried_ms < STANDDOWN_RETRY_MS then + return false + end + standdown_tried_ms = now + if set_enabled(false) then + standdown_tried_ms = nil + steering = false + return true + end + host.log("warn", "KEBA: stand-down refused; trying again in a minute") + return false +end + function driver_cleanup() end diff --git a/drivers/lua/schrack_ev.lua b/drivers/lua/schrack_ev.lua index b8717e6..fa995c5 100644 --- a/drivers/lua/schrack_ev.lua +++ b/drivers/lua/schrack_ev.lua @@ -1,28 +1,99 @@ --- Schrack i-CHARGE EV Charger Driver (community, untested) --- Emits: V2X Charger --- Register type: HOLDING (FC 0x03) --- Modbus TCP interface, port 502 --- Note: Currents and voltages are U16 with 0.1 scaling factor +-- Schrack i-CHARGE CION Home and Semipublic EV charger (Modbus RTU) +-- Emits: ev +-- Controls: charging current per phase, pause and resume. +-- +-- Register map: Schrack "Modbus Register Tabelle CION Home und Semipublic" +-- (versions of 13.04.2021, 04.08.2022 and 09.02.2023) and table 15 of the +-- "i-CHARGE CION" operating manual V2.2. +-- +-- The CION Home (EMCIONH...) and Semipublic (EMCIONS...) speak Modbus RTU on +-- RS485, 57600 baud, 8N1, with DIP switches 1 and 2 OFF. DIP switches 3 and 4 +-- set the bus address (Schrack's 2023 table: OFF/ON = 1, ON/OFF = 2, +-- ON/ON = 3). FTW speaks Modbus TCP, so it reaches the charger through a +-- Modbus TCP to RTU gateway. Set the driver's unit_id to the bus address. +-- +-- The CION Pro (EMCIONP...) is a different controller with OCPP 1.5/1.6 and +-- is not covered here. Connect it to FTW's built-in OCPP central system. +-- +-- Holding registers (function code 3 to read, 6 to write): +-- 100 charging authorisation 0 not allowed, 1 allowed (r/w). +-- RFID or input E1 set it to 1; unplugging +-- the car sets it back to 0. +-- 101 target charging current A, 6..32 (r/w). Schrack: "can and should +-- be set during a charging process". +-- 120 authorisation void 1 when 100 cannot take effect (fault, no +-- car, no cable). Writing 100 = 0 clears it. +-- 121 status bits bit 0 cable plugged, bit 1 contactor 1 on, +-- bit 6 group fault +-- 126 current Mode 3 current A, offered to the car now +-- 127 max Mode 3 current A, configured maximum +-- 128 cable current capacity A: 0, 13, 20, 32, 63 +-- 139 CP state ASCII 'A'..'F', 'U' +-- 150 error word non-zero is a fault +-- 816 device name 16 registers ASCII +-- 832 firmware version 16 registers ASCII +-- +-- The register list has no power or energy, so `w` and `session_wh` stay +-- empty. FTW can order the charger but cannot see what the car draws from it. +-- +-- The charger reports no phase currents, so an order is split over the +-- phases set in config (`phases`, default 3) and nothing else. Core's +-- phase_mode cannot be checked against the car here: a one-phase order on a +-- three-phase installation is still divided by three, so a three-phase car +-- never gets the whole order on each phase. On a one-phase installation set +-- `phases: 1`. + +DRIVER = { + host_api_min = 1, + host_api_max = 1, + id = "schrack_ev", + name = "Schrack i-CHARGE CION", + manufacturer = "Schrack", + version = "1.1.0", + protocols = { "modbus" }, + capabilities = { "ev" }, + description = "Schrack i-CHARGE CION Home and Semipublic over Modbus RTU, through a Modbus TCP to RTU gateway. Reads charger state and sets the charging current. The charger reports no power, so FTW cannot see what the car draws. The CION Pro is not covered: connect it to FTW's OCPP central system.", + homepage = "https://www.schrack.com/services/cion-docu", + authors = { "FTW contributors" }, + tested_models = { "i-CHARGE CION Home", "i-CHARGE CION Semipublic" }, + verification_status = "experimental", + verification_notes = "Control written from Schrack's CION Home and Semipublic Modbus register table; not yet confirmed on hardware.", + connection_defaults = { + port = 502, + unit_id = 1, + }, +} PROTOCOL = "modbus" --- A register the charger never answers costs a failed read on every poll for --- as long as the driver keeps asking. The host counts those failures against --- the poll whether or not the pcall caught them, so a driver that keeps --- reporting telemetry while permanently failing one read gets the whole site --- marked offline. Give a register three chances, then leave it alone until --- restart. Three rather than one: a single failure is not proof, the link may --- just have been slow. --- --- This driver is read-only — every read below is on the poll path, so none of --- them can veto a later command. +local REG_AUTH = 100 -- with 101: two registers +local REG_SET_CURRENT = 101 +local REG_VOID = 120 -- with 121: two registers +local REG_OFFERED = 126 -- with 127, 128: three registers +local REG_CP_STATE = 139 +local REG_ERROR = 150 +local REG_IDENTITY = 816 -- device name and firmware: 32 registers + +local MIN_A = 6 +local MAX_A = 32 + +local CP = { [65] = "A", [66] = "B", [67] = "C", [68] = "D", [69] = "E", [70] = "F", [85] = "U" } + +---------------------------------------------------------------------------- +-- Reads +---------------------------------------------------------------------------- + +-- The host counts every failed modbus_read against the poll whether or not +-- Lua caught it. A register the charger does not answer costs a failed read +-- on every poll and takes the site offline. Three tries, then leave it alone. +-- Core reloads the driver once nothing is left to read. local GIVE_UP_AFTER = 3 local read_failures = {} local function probe_read(addr, count, kind) if (read_failures[addr] or 0) >= GIVE_UP_AFTER then return nil end local ok, regs = pcall(host.modbus_read, addr, count, kind) - if ok and regs and regs[1] ~= nil then + if ok and type(regs) == "table" and regs[count] ~= nil then read_failures[addr] = nil return regs end @@ -36,75 +107,374 @@ local function probe_read(addr, count, kind) return nil end +local function ascii(regs, first, count) + local ok, text = pcall(host.decode_string, regs, first, count) + if not ok or type(text) ~= "string" then return "" end + return (text:gsub("[^%w%-%._/ ]", "")) +end + +---------------------------------------------------------------------------- +-- State +---------------------------------------------------------------------------- + +local installed_phases = 3 +local identity_read = false + +-- What the charger last took from this driver. nil means unknown. +local written_a, written_a_ms = nil, nil +local written_auth, written_auth_ms = nil, nil +-- The last order: its current (nil for 0 A), when it came, and the lowest +-- per-phase limit Core named with it. Core sends ev_resume and ev_start with +-- no budget, so a resume trusts only a fresh order and that limit. +local order_a, order_ms = nil, nil +local last_cap_a = nil +local resume_wait_logged = false +local steering = false +-- True once a command has tried to write in this run. Core runs default mode +-- at every start, so until then the stand-down leaves the charger alone: a +-- charger FTW never steered keeps its own state, and a charge already running +-- goes on. The same rule as heishamon 0.8.1. +local ftw_wrote = false + +-- Core treats a default mode that returns true as a charger stood down, so +-- true means confirmed (or nothing of FTW's to take back). A refused +-- stand-down returns false and is tried again at most once a minute, without +-- writing in between. +local STANDDOWN_RETRY_MS = 60000 +local standdown_tried_ms = nil + +-- Fresh readings from the latest poll, for the command path. +local last_connected = nil +local last_void = nil +-- When a void authorisation was last reset. A fault that is still there +-- makes the reset void again at once, and void then stays set after the +-- fault clears, so the reset is repeated at most every VOID_RESET_EVERY_MS +-- while void lasts. Cleared when the charger stops calling it void. +local void_reset_ms = nil +local VOID_RESET_EVERY_MS = 30000 +local device_limit_a, device_limit_ms = nil, nil +local stalled_since_ms = nil + +local ORDER_SETTLE_MS = 10000 +local STALL_MS = 30000 +local ORDER_FRESH_MS = 30000 + +---------------------------------------------------------------------------- +-- Writes +---------------------------------------------------------------------------- + +local function write(addr, value, what) + -- Default mode writes only once this is set, so only a command sets it. + ftw_wrote = true + local ok, err = pcall(host.modbus_write, addr, value) + if not ok or (err ~= nil and err ~= true and err ~= "") then + host.log("warn", "Schrack: " .. what .. " not written: " .. + tostring(ok and err or "error")) + return false + end + return true +end + +local function write_current(amps) + if written_a == amps then return true end + if not write(REG_SET_CURRENT, amps, "charging current " .. tostring(amps) .. " A") then + written_a = nil + return false + end + written_a, written_a_ms = amps, host.millis() + return true +end + +local function write_auth(value) + if written_auth == value then return true end + if not write(REG_AUTH, value, "charging authorisation " .. tostring(value)) then + written_auth = nil + return false + end + written_auth, written_auth_ms = value, host.millis() + return true +end + +---------------------------------------------------------------------------- +-- Orders +---------------------------------------------------------------------------- + +local function finite_positive(v) + return type(v) == "number" and v == v and v > 0 and v < math.huge +end + +-- The lowest per-phase limit an order names, or nil when it names none. +local function cap_of(cmd) + local cap = nil + if type(cmd) == "table" then + for _, key in ipairs({ "max_amps_per_phase", "cap_amps_per_phase" }) do + local v = cmd[key] + if finite_positive(v) and (cap == nil or v < cap) then cap = v end + end + end + return cap +end + +-- Core orders total power. The charger takes whole amperes per phase. +-- +-- phases the installed phases from config only, since the charger +-- measures none +-- amps power / (voltage * phases), never above Core's max and cap per +-- phase or the charger's own limit, rounded down to 1 A +-- below MIN_A gives 0 A. A current is never rounded up to the minimum. +local function amps_for(power_w, cmd) + local phases = installed_phases + if type(power_w) ~= "number" or power_w ~= power_w or power_w <= 0 then + return 0, phases + end + local voltage = 230 + if type(cmd) == "table" and finite_positive(cmd.voltage) then voltage = cmd.voltage end + local ceiling = MAX_A + if device_limit_a and device_limit_a > 0 and device_limit_a < ceiling then + ceiling = device_limit_a + end + if type(cmd) == "table" then + for _, key in ipairs({ "max_amps_per_phase", "cap_amps_per_phase" }) do + local v = cmd[key] + if finite_positive(v) and v < ceiling then ceiling = v end + end + end + local amps = power_w / (voltage * phases) + if amps > ceiling then amps = ceiling end + -- The allowance absorbs float error in a division Core meant to be exact. + amps = math.floor(amps + 0.0001) + if amps < MIN_A then return 0, phases end + return amps, phases +end + +-- Set the current first, then allow charging, so the car never starts on an +-- older, higher current. With no car plugged in, authorisation would only +-- turn void, so it waits for the next order after the car arrives. +local function charge_at(amps) + if not write_current(amps) then return false end + if last_connected == false then return true end + local now = host.millis() + if last_void == 1 and (not void_reset_ms or now - void_reset_ms >= VOID_RESET_EVERY_MS) then + -- Schrack: a void authorisation is cleared by writing 0 first. At + -- most every 30 s, so a car that stays unready is not cycled on every + -- order. + if not write(REG_AUTH, 0, "authorisation reset") then + written_auth = nil + return false + end + void_reset_ms = now + written_auth = 0 + end + return write_auth(1) +end + +---------------------------------------------------------------------------- +-- Driver interface +---------------------------------------------------------------------------- + function driver_init(config) host.set_make("Schrack") + if type(config) == "table" and tonumber(config.phases) == 1 then + installed_phases = 1 + end +end + +local function read_identity() + if identity_read then return end + local regs = probe_read(REG_IDENTITY, 32, "holding") + if not regs then return end + identity_read = true + local name = ascii(regs, 1, 16) + local firmware = ascii(regs, 17, 16) + if #name > 0 then host.set_model(name) end + if #firmware > 0 then host.log("info", "Schrack: firmware " .. firmware) end end function driver_poll() - -- Charger state: 100 (U16: 0=idle, 1=connected, 2=charging, 3=error) - local st_regs = probe_read(100, 1, "holding") - local raw_state = 0 - local state = 0 - if st_regs then - raw_state = st_regs[1] - -- Map Schrack states to standard: 0=idle, 1=connected, 2=charging, 3=error - if raw_state == 0 then - state = 0 -- idle -> idle - elseif raw_state == 1 then - state = 1 -- connected -> connected - elseif raw_state == 2 then - state = 2 -- charging -> charging - elseif raw_state == 3 then - state = 3 -- error -> error + local now = host.millis() + read_identity() + + local ctl = probe_read(REG_AUTH, 2, "holding") + local st = probe_read(REG_VOID, 2, "holding") + local cur = probe_read(REG_OFFERED, 3, "holding") + local cp_regs = probe_read(REG_CP_STATE, 1, "holding") + local err_regs = probe_read(REG_ERROR, 1, "holding") + + local cp = cp_regs and CP[cp_regs[1]] or nil + if not ctl and not cp then + -- Neither the order nor the car state answered: nothing fresh to say. + last_connected, last_void = nil, nil + return 5000 + end + + local auth = ctl and ctl[1] or nil + local target_a = ctl and ctl[2] or nil + local void = st and st[1] or nil + local bits = st and st[2] or nil + local offered_a = cur and cur[1] or nil + local max_a = cur and cur[2] or nil + local cable_a = cur and cur[3] or nil + local fault = err_regs and err_regs[1] ~= 0 + + local connected = nil + if cp == "A" then connected = false end + if cp == "B" or cp == "C" or cp == "D" then connected = true end + local charging = nil + if cp then + charging = (cp == "C" or cp == "D") + -- Bit 1 of 121 is contactor 1: without it no current flows. + if charging and bits and math.floor(bits / 2) % 2 == 0 then charging = false end + end + last_connected, last_void = connected, void + if void == 0 then void_reset_ms = nil end + + -- The charger's own ceiling: its configured maximum and the cable. + local limit = nil + if max_a and max_a >= MIN_A and max_a <= 63 then limit = max_a end + if cable_a and cable_a > 0 and (limit == nil or cable_a < limit) then limit = cable_a end + if limit then device_limit_a, device_limit_ms = limit, now end + + -- A charger that no longer holds this driver's order forgets it: a + -- current that differs, or an authorisation that was cleared, by the car + -- leaving for example. The next order writes it again. + if steering then + if written_a and target_a and target_a ~= written_a and + written_a_ms and now - written_a_ms >= ORDER_SETTLE_MS then + written_a = nil + end + if written_auth and auth and auth ~= written_auth and + written_auth_ms and now - written_auth_ms >= ORDER_SETTLE_MS then + written_auth = nil end end - -- L1 current: 200 (U16, 0.1A), L2: 201, L3: 202 - local a_regs = probe_read(200, 3, "holding") - local l1_a, l2_a, l3_a = 0, 0, 0 - if a_regs then - l1_a = a_regs[1] * 0.1 - l2_a = a_regs[2] * 0.1 - l3_a = a_regs[3] * 0.1 - end - - -- L1 voltage: 203 (U16, 0.1V), L2: 204, L3: 205 - local v_regs = probe_read(203, 3, "holding") - local l1_v, l2_v, l3_v = 0, 0, 0 - if v_regs then - l1_v = v_regs[1] * 0.1 - l2_v = v_regs[2] * 0.1 - l3_v = v_regs[3] * 0.1 - end - - -- Active power: 206 (U32, W) - local w_regs = probe_read(206, 2, "holding") - local power_w = 0 - if w_regs then - power_w = host.decode_u32_be(w_regs[1], w_regs[2]) - end - - -- Session energy: 210 (U32, Wh) - local se_regs = probe_read(210, 2, "holding") - local session_wh = 0 - if se_regs then - session_wh = host.decode_u32_be(se_regs[1], se_regs[2]) - end - - -- Emit V2X charger telemetry - host.emit("v2x_charger", { - w = power_w, - session_charge_wh = session_wh, - l1_v = l1_v, - l2_v = l2_v, - l3_v = l3_v, - l1_a = l1_a, - l2_a = l2_a, - l3_a = l3_a, + local current_limit_a = nil + if charging and offered_a and written_a and written_a > 0 and written_a_ms and + now - written_a_ms >= STALL_MS and offered_a + 0.5 < written_a then + current_limit_a = offered_a + end + + -- Stalled: charging is ordered, a car is plugged in, and the charger has + -- dropped the authorisation or calls it void. + local command_stalled = false + if steering and written_auth == 1 and connected and (auth == 0 or void == 1) then + stalled_since_ms = stalled_since_ms or now + command_stalled = now - stalled_since_ms >= STALL_MS + else + stalled_since_ms = nil + end + + local reason = nil + if connected and charging == false then + if fault then reason = "fault" + elseif void == 1 then reason = "authorisation_void" + elseif auth == 0 then reason = "not_authorised" + else reason = "waiting_for_vehicle" end + end + + host.emit("ev", { + connected = connected, + charging = charging, + max_a = offered_a, + current_limit_a = current_limit_a, + device_limit_a = device_limit_a, + device_limit_age_s = device_limit_ms and (now - device_limit_ms) / 1000 or nil, + state_label = cp and ("cp_" .. cp) or nil, + reason_no_current_label = reason, + command_stalled = command_stalled, }) return 5000 end +function driver_command(action, power_w, cmd) + if action == "init" then return true end + if action == "deinit" then return driver_default_mode() end + + if action == "ev_set_current" then + local amps, phases = amps_for(power_w, cmd) + host.log("debug", string.format("Schrack: %s W on %d phase(s) -> %d A", + tostring(power_w), phases, amps)) + -- Core names cap_amps_per_phase only with a positive order, so a 0 W + -- order must not replace the cap with the site maximum. + if type(power_w) == "number" and power_w > 0 then last_cap_a = cap_of(cmd) end + resume_wait_logged = false + order_a, order_ms = (amps > 0) and amps or nil, host.millis() + local ok + if amps <= 0 then + ok = write_auth(0) + else + ok = charge_at(amps) + end + if ok then steering = true end + return ok + end + + if action == "ev_pause" then + local ok = write_auth(0) + if ok then steering = true end + return ok + end + + if action == "ev_resume" or action == "ev_start" then + -- No budget comes with these. A fresh order is written again. + -- Otherwise 101 = 6 A first, and only when the last limit Core named + -- allows 6 A. With no limit known nothing is written and Core's next + -- order sets the level. + local now = host.millis() + local amps = nil + if order_a and order_ms and now - order_ms <= ORDER_FRESH_MS then + amps = order_a + elseif last_cap_a and last_cap_a >= MIN_A then + amps = MIN_A + written_a = nil + end + if not amps and last_cap_a == nil then + -- No limit known, after a reload for example. Nothing was refused and + -- nothing written; Core's next order sets the level. + if not resume_wait_logged then + resume_wait_logged = true + host.log("info", "Schrack: no fresh order and no known limit; " .. + "the charger waits for Core's next order") + end + return true + end + if not amps then + host.log("info", "Schrack: the last limit Core named leaves no room for 6 A; " .. + "resume waits for the next order") + return false + end + local ok = charge_at(amps) + if ok then steering = true end + return ok + end + + host.log("warn", "Schrack: unknown action " .. tostring(action)) + return false +end + +-- Stand the charger down: withdraw the charging authorisation, once a command wrote in this run. +-- Repeated calls write nothing once that has landed. A refused stand-down +-- returns false and is tried again at most once a minute. +function driver_default_mode() + if not ftw_wrote then return true end + if written_auth == 0 then + steering = false + return true + end + local now = host.millis() + if standdown_tried_ms and now - standdown_tried_ms < STANDDOWN_RETRY_MS then + return false + end + standdown_tried_ms = now + if write_auth(0) then + standdown_tried_ms = nil + steering = false + return true + end + host.log("warn", "Schrack: stand-down refused; trying again in a minute") + return false +end + function driver_cleanup() end diff --git a/drivers/lua/wallbox.lua b/drivers/lua/wallbox.lua index 936a636..88465df 100644 --- a/drivers/lua/wallbox.lua +++ b/drivers/lua/wallbox.lua @@ -1,24 +1,56 @@ --- Wallbox Commander 2 EV Charger Driver (community, untested) --- Emits: V2X Charger --- Register type: HOLDING (FC 0x03) --- Note: Limited register set -- no per-phase voltage/current available +-- Wallbox Pulsar Plus, Pulsar Max, Commander 2 and Copper SB +-- Emits: ev +-- Reads only. This driver cannot control the charger. +-- +-- Why it cannot: we found no documented local Modbus TCP register map for +-- these chargers. The Modbus that Wallbox's installation guides describe is +-- for its own load management (a Power Boost meter, power sharing between +-- chargers), not a server an energy manager can command. The control route +-- Wallbox documents is OCPP 1.6J ("OCPP activation and setup guide", +-- support.wallbox.com), and evcc controls these models over OCPP only. We +-- found no published Wallbox cloud API either. +-- +-- Use FTW's built-in OCPP central system instead. It reads and controls these +-- chargers without any driver: set FTW's OCPP address in the myWallbox app +-- under Settings, External management, OCPP. See FTW's docs/ocpp.md. +-- +-- What this driver reads: holding registers 4-5 (power), 7-8 (session +-- energy) and 9 (maximum current) on unit 1. They come from an early +-- community sketch, not from any Wallbox document, and are unverified: the +-- power and energy it reports may be wrong or absent. A value is reported only +-- when its register answered, and nothing is reported as 0 W that was not +-- read. + +DRIVER = { + host_api_min = 1, + host_api_max = 1, + id = "wallbox", + name = "Wallbox (Modbus, reads only)", + manufacturer = "Wallbox", + version = "1.1.0", + protocols = { "modbus" }, + capabilities = { "ev" }, + read_only = true, + description = "Cannot control: we found no documented local Modbus interface for the Pulsar Plus, Pulsar Max, Commander 2 or Copper SB. The power and energy this driver reports come from registers no Wallbox document lists, and are unverified. Connect the charger to FTW's built-in OCPP central system instead; Wallbox supports OCPP 1.6J, and FTW reads and controls it that way without a driver.", + homepage = "https://support.wallbox.com/en/knowledge-base/ocpp-activation-and-setup-guide/", + authors = { "FTW contributors" }, + tested_models = { "Commander 2", "Pulsar Plus" }, + verification_status = "experimental", +} PROTOCOL = "modbus" --- Bounded register probe. --- The host counts every failed host.modbus_read against the poll, even one --- pcall caught here. A driver that keeps emitting while a register keeps --- failing is marked offline by the stale-telemetry watchdog, and the site --- then reports nothing at all. So: three tries, then leave the register --- alone. Three rather than one because a single failure is not proof the --- register is missing -- the link may just have been slow. +-- Bounded register probe. The host counts every failed host.modbus_read +-- against the poll, even one pcall caught here. A driver that keeps reading a +-- register that never answers is marked offline by the stale-telemetry +-- watchdog. So: three tries, then leave the register alone. local GIVE_UP_AFTER = 3 local read_failures = {} local function probe_read(addr, count, kind) if (read_failures[addr] or 0) >= GIVE_UP_AFTER then return nil end local ok, regs = pcall(host.modbus_read, addr, count, kind) - if ok and regs and regs[1] ~= nil then + if ok and type(regs) == "table" and regs[count] ~= nil then read_failures[addr] = nil return regs end @@ -34,52 +66,25 @@ end function driver_init(config) host.set_make("Wallbox") + host.log("info", "Wallbox: reads only, from an unverified register map. " .. + "Use FTW's OCPP central system to read and control this charger.") end function driver_poll() - -- Charger state: 0 (U16: 0=ready, 1=charging, 2=waiting, 3=disconnected) - local st_regs = probe_read(0, 1, "holding") - local raw_state = 0 - local state = 0 - if st_regs then - raw_state = st_regs[1] - -- Map Wallbox states to standard: 0=idle, 1=connected, 2=charging, 3=error - if raw_state == 0 then - state = 1 -- ready -> connected - elseif raw_state == 1 then - state = 2 -- charging -> charging - elseif raw_state == 2 then - state = 1 -- waiting -> connected - elseif raw_state == 3 then - state = 0 -- disconnected -> idle - end - end - - -- Active power: 4-5 (U32, W) + -- Unverified: power in W at 4-5 (U32), session energy in Wh at 7-8 (U32), + -- maximum current in A at 9, state at 0. local w_regs = probe_read(4, 2, "holding") - local power_w = 0 - if w_regs then - power_w = host.decode_u32_be(w_regs[1], w_regs[2]) + if not w_regs then + -- Power is this stream's defining reading. Without it, say nothing. + return 5000 end - - -- Session energy: 7 (U32, Wh) local se_regs = probe_read(7, 2, "holding") - local session_wh = 0 - if se_regs then - session_wh = host.decode_u32_be(se_regs[1], se_regs[2]) - end - - -- Max current: 9 (U16, A) -- writable local mc_regs = probe_read(9, 1, "holding") - local max_a = 0 - if mc_regs then - max_a = mc_regs[1] - end - -- Emit V2X charger telemetry - host.emit("v2x_charger", { - w = power_w, - session_charge_wh = session_wh, + host.emit("ev", { + w = host.decode_u32_be(w_regs[1], w_regs[2]), + session_wh = se_regs and host.decode_u32_be(se_regs[1], se_regs[2]) or nil, + max_a = mc_regs and mc_regs[1] or nil, }) return 5000 diff --git a/drivers/tests/invented-number-baseline.json b/drivers/tests/invented-number-baseline.json index 63710da..9f0f22a 100644 --- a/drivers/tests/invented-number-baseline.json +++ b/drivers/tests/invented-number-baseline.json @@ -23,7 +23,6 @@ "ctek_hybrid": 9, "ctek_v2": 9, "deye": 28, - "etrel": 8, "ferroamp_modbus": 18, "fronius": 11, "goe": 8, @@ -31,11 +30,9 @@ "growatt": 16, "hello": 3, "janitza": 13, - "keba": 8, "kostal": 22, "kstar": 9, "schneider_meter": 13, - "schrack_ev": 8, "siemens_pac": 13, "sigenergy": 5, "sma": 28, @@ -49,7 +46,6 @@ "solis_string": 21, "sungrow": 20, "varta": 5, - "victron": 10, - "wallbox": 2 + "victron": 10 } } diff --git a/drivers/tests/lua_harness/test_etrel_control.lua b/drivers/tests/lua_harness/test_etrel_control.lua new file mode 100644 index 0000000..b701dd5 --- /dev/null +++ b/drivers/tests/lua_harness/test_etrel_control.lua @@ -0,0 +1,261 @@ +-- Etrel INCH: an order becomes a Float32 current setpoint at holding 8, per +-- phase, rounded down, within every cap. Pause writes 0 A; resume writes a +-- fresh order back, or only 6 A when the order is old. Default mode acts only +-- after FTW wrote, returns true only once the stand-down lands, and retries a +-- refused one at most once a minute. A refused write returns false. +dofile("drivers/tests/lua_harness/host_mock.lua") +local DRIVER_PATH = "drivers/lua/etrel.lua" + +-- Test data encoder, checked against the mock's own decoder below. +local function enc(v) + if v == 0 then return 0, 0 end + local e, m = 0, v + while m >= 2 do m = m / 2; e = e + 1 end + while m < 1 do m = m * 2; e = e - 1 end + local f = math.floor((m - 1) * 0x800000 + 0.5) + return (e + 127) * 128 + math.floor(f / 65536), f % 65536 +end +for _, v in ipairs({ 0.5, 6, 16, 230, 7.36 }) do + local hi, lo = enc(v) + assert(math.abs(host.decode_f32_be(hi, lo) - v) < 1e-3, "test encoder is wrong for " .. v) +end + +local function set_f32(addr, v) + local hi, lo = enc(v) + host._modbus_registers.input[addr] = hi + host._modbus_registers.input[addr + 1] = lo +end + +-- status: Etrel connector status; amps: per-phase current; kw: total power; +-- target: the current the charger lets the car take. +local function charger(status, amps, kw, target) + host._modbus_registers.input[0] = status + for i = 1, 3 do set_f32(8 + 2 * (i - 1), 230) end + for i = 1, 3 do set_f32(14 + 2 * (i - 1), amps[i] or 0) end + set_f32(26, kw or 0) + set_f32(4, target or 0) +end + +local function boot(config) + host.reset() + driver_init, driver_poll, driver_command, driver_default_mode = nil, nil, nil, nil + dofile(DRIVER_PATH) + driver_init(config or {}) + charger(3, {}, 0, 32) + driver_poll() +end + +local function setpoint_writes() + local out = {} + for _, call in ipairs(host._calls) do + if call.func == "modbus_write_multiple" then + assert(call.args[1] == 8, "wrote holding " .. tostring(call.args[1])) + local v = call.args[2] + table.insert(out, host.decode_f32_be(v[1], v[2])) + end + end + return out +end +local function last_setpoint() local w = setpoint_writes(); return w[#w] end +local function near(a, b) return a ~= nil and math.abs(a - b) < 0.01 end +local function later(ms) host._millis_counter = host._millis_counter + ms end +local THREE = { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25 } +local ONE = { phase_mode = "1p", voltage = 230, max_amps_per_phase = 25 } +local function order(w, cmd) return driver_command("ev_set_current", w, cmd or THREE) end + +-- 1. Power to amps per phase. +boot() +assert(order(11040)); assert(near(last_setpoint(), 16), "11040 W on 3p is not 16 A") + +-- 2. Round down to 0.1 A, never up. +boot() +assert(order(4500)); assert(near(last_setpoint(), 6.5), "4500 W did not round down to 6.5 A") + +-- 3. Below 6 A gives 0 A, not 6 A. +boot() +assert(order(4000)); assert(near(last_setpoint(), 0), "5.8 A was not written as 0 A") + +-- 4. One phase only when it is certain. A 1p order on an idle three-phase +-- installation gives P/(3*V), so a three-phase car never gets the L1 sum. +boot() +assert(order(7000, ONE)); assert(near(last_setpoint(), 10.1), "an idle 1p order was not split over three") +-- ...and on a car that draws on three phases. +boot() +charger(4, { 10, 10, 10 }, 6.9, 10) +driver_poll() +assert(order(7000, ONE)); assert(near(last_setpoint(), 10.1), "measured phases were ignored") +-- A 1p order on a car that draws on exactly one phase gives P/V. +boot() +charger(4, { 10, 0, 0 }, 2.3, 10) +driver_poll() +assert(order(3680, ONE)); assert(near(last_setpoint(), 16), "a one-phase car did not get P/V") +-- A car once seen on three phases stays three until it is unplugged. +boot() +charger(4, { 10, 10, 10 }, 6.9, 10) +driver_poll() +charger(4, { 10, 0, 0 }, 2.3, 10) +driver_poll() +assert(order(7000, ONE)); assert(near(last_setpoint(), 10.1), "a three-phase car got P/V") +charger(1, {}, 0, 0) +driver_poll() +charger(4, { 10, 0, 0 }, 2.3, 10) +driver_poll() +assert(order(3680, ONE)); assert(near(last_setpoint(), 16), "unplugging did not forget the old car") +-- A one-phase installation (config) gives P/V, idle or not, unless the car +-- draws on more than one phase. +boot({ phases = 1 }) +assert(order(3680)); assert(near(last_setpoint(), 16), "phases: 1 was ignored") +charger(4, { 10, 10, 10 }, 6.9, 10) +driver_poll() +assert(order(7000)); assert(near(last_setpoint(), 10.1), "phases: 1 overrode three measured phases") + +-- 5. Caps: Core's max and cap per phase, and the charger's own limit. +boot() +assert(order(22000, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 10 })) +assert(near(last_setpoint(), 10), "max_amps_per_phase was exceeded") +assert(order(22000, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25, cap_amps_per_phase = 8 })) +assert(near(last_setpoint(), 8), "cap_amps_per_phase was exceeded") +assert(order(1e12)); assert(near(last_setpoint(), 25), "a huge order passed the fuse") +assert(order(0 / 0)); assert(near(last_setpoint(), 0), "NaN did not give 0 A") +host.reset() +dofile(DRIVER_PATH) +driver_init({}) +set_f32(1028, 13) +charger(3, {}, 0, 13) +driver_poll() +assert(order(11040)); assert(near(last_setpoint(), 13), "the charger's custom max current was exceeded") + +-- 6. Write only on change; a decrease lands at once. +boot() +assert(order(11040)); assert(order(11040)) +assert(#setpoint_writes() == 1, "an unchanged order was written again") +assert(order(5520)); assert(near(last_setpoint(), 8), "a decrease was not written at once") +assert(#setpoint_writes() == 2) + +-- 7. Pause writes 0 A, resume brings the order back. +boot() +assert(order(11040)) +assert(driver_command("ev_pause", 0, {})); assert(near(last_setpoint(), 0), "pause did not write 0 A") +assert(driver_command("ev_resume", 0, {})); assert(near(last_setpoint(), 16), "resume lost the order") +-- Resume carries no budget. With no order and no limit on record, after a +-- reload for example, nothing is written and the start is accepted: Core's +-- next order sets the level. The wait is logged once. +boot() +assert(driver_command("ev_start", 0, {}) == true, "a start with no known limit was refused") +assert(driver_command("ev_resume", 0, {}) == true) +assert(#setpoint_writes() == 0, "start without an order wrote") +local waits = 0 +for _, line in ipairs(host._logs) do + if line:find("waits for Core's next order", 1, true) then waits = waits + 1 end +end +assert(waits == 1, "the wait was logged " .. waits .. " times") +-- An order an hour old is not trusted: resume offers only 6 A... +boot() +assert(order(11040)) +assert(driver_command("ev_pause", 0, {})) +later(3600000) +assert(driver_command("ev_resume", 0, {})); assert(near(last_setpoint(), 6), "resume wrote an hour-old order") +-- ...and nothing when the last positive order left no room for 6 A. A later +-- 0 W order names only the site maximum and must not replace that cap. +boot() +assert(order(11040, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25, cap_amps_per_phase = 4 })) +assert(order(0, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25 })) +later(3600000) +local resumed_before = #setpoint_writes() +assert(driver_command("ev_resume", 0, {}) == false) +assert(#setpoint_writes() == resumed_before, "resumed without room for 6 A") + +-- 8. Core runs default mode at every start: a charger FTW never wrote to in +-- this run is left alone. +boot() +charger(4, { 16, 16, 16 }, 11, 16) +driver_poll() +for _ = 1, 3 do assert(driver_default_mode() == true) end +assert(#setpoint_writes() == 0, "default mode wrote to a charger FTW never steered") +-- Once FTW wrote, default mode stands down once, then writes nothing. +boot() +assert(order(11040)) +assert(driver_default_mode() == true); assert(near(last_setpoint(), 0), "default mode did not write 0 A") +local before = #setpoint_writes() +for _ = 1, 5 do assert(driver_default_mode() == true) end +assert(#setpoint_writes() == before, "default mode kept writing") + +-- 9. A refused write returns false, and the next order tries again. +boot() +host._modbus_write_error = "modbus exception function=0x10 code=0x0c" +assert(order(11040) == false, "a refused write returned true") +host._modbus_write_error = nil +assert(order(11040)); assert(near(last_setpoint(), 16), "the order was not retried") + +-- 10. Once FTW wrote, a refused stand-down returns false. It is tried again +-- at most once a minute, with no write in between, and true only once it +-- lands. +boot() +assert(order(11040)) +host._modbus_write_error = "modbus exception function=0x10 code=0x02" +local function default_writes() + local n = #setpoint_writes() + local result = driver_default_mode() + return #setpoint_writes() - n, result +end +local counts = {} +for i = 1, 4 do + local count, result = default_writes() + counts[i] = count + assert(result == false, "a refused stand-down returned " .. tostring(result)) +end +assert(table.concat(counts, ",") == "1,0,0,0", "stand-down retried too often: " .. table.concat(counts, ",")) +later(61000) +local count, result = default_writes() +assert(count == 1 and result == false, "the stand-down was not retried after a minute") +host._modbus_write_error = nil +later(61000) +count, result = default_writes() +assert(count == 1 and result == true, "a landed stand-down was not confirmed") +count, result = default_writes() +assert(count == 0 and result == true, "a confirmed stand-down was written again") + +-- 10b. A charger that keeps the charge paused against the order for 30 s gets +-- the setpoint again on the next order, at most every 30 s. +boot() +assert(order(11040)) +charger(6, {}, 0, 16) +driver_poll() +later(31000) +driver_poll() +assert(host._emitted.ev[#host._emitted.ev].command_stalled == true, "a stalled order was not reported") +local stalled_before = #setpoint_writes() +assert(order(11040)); assert(#setpoint_writes() == stalled_before + 1, "a stalled order was not written again") +later(5000) +driver_poll() +assert(order(11040)); assert(#setpoint_writes() == stalled_before + 1, "a stalled order was written again too soon") + +-- 11. A charger that allows more than the order has lost it: the next order +-- writes it again, once the order has had time to land. +boot() +assert(order(11040)) +charger(4, { 16, 16, 16 }, 11, 32) +driver_poll() +assert(order(11040)); assert(#setpoint_writes() == 1, "rewrote before the order could land") +later(31000) +driver_poll() +assert(order(11040)); assert(#setpoint_writes() == 2, "a lost order was not written again") + +-- 12. Telemetry: power in W, state, the charger's own offer. +boot() +charger(4, { 16, 16, 15.9 }, 11.04, 16) +driver_poll() +local ev = host._emitted.ev[#host._emitted.ev] +assert(math.abs(ev.w - 11040) < 1, "power is not in W") +assert(ev.connected == true and ev.charging == true) +assert(near(ev.max_a, 16) and ev.phases == 3) +charger(5, {}, 0, 16) +driver_poll() +ev = host._emitted.ev[#host._emitted.ev] +assert(ev.charging == false and ev.request_active == false and ev.reason_no_current_label == "paused_by_vehicle") +host._modbus_read_error = "timeout" +local count = #host._emitted.ev +driver_poll() +assert(#host._emitted.ev == count, "a failed read was reported") + +print("etrel control: ok") diff --git a/drivers/tests/lua_harness/test_keba_control.lua b/drivers/tests/lua_harness/test_keba_control.lua new file mode 100644 index 0000000..df3e4be --- /dev/null +++ b/drivers/tests/lua_harness/test_keba_control.lua @@ -0,0 +1,320 @@ +-- KEBA P30: an order becomes 5004 (mA, function code 6) then 5014 = 1, per +-- phase, rounded down to 0.1 A, within every cap. Below 6 A and pause write +-- 5014 = 0; resume writes a fresh order back, or only 6 A when the order is +-- old. Default mode acts only after FTW wrote, returns true only once the +-- stand-down lands, and retries a refused one at most once a minute. A +-- refused write returns false. The failsafe timer is refreshed while FTW +-- steers. +dofile("drivers/tests/lua_harness/host_mock.lua") +local DRIVER_PATH = "drivers/lua/keba.lua" + +local function set_u32(addr, v) + host._modbus_registers.holding[addr] = math.floor(v / 65536) + host._modbus_registers.holding[addr + 1] = v % 65536 +end + +-- state: KEBA charging state; cable: cable state; ma: per-phase current; +-- offered: register 1100 in mA. +local function charger(state, cable, ma, offered, extra) + set_u32(1000, state) + set_u32(1004, cable) + for i, addr in ipairs({ 1008, 1010, 1012 }) do set_u32(addr, ma[i] or 0) end + for _, addr in ipairs({ 1040, 1042, 1044 }) do set_u32(addr, 230) end + local total = 0 + for i = 1, 3 do total = total + (ma[i] or 0) * 230 end + set_u32(1020, total) -- mW: mA * V + set_u32(1100, offered or 32000) + set_u32(1110, (extra and extra.hw) or 32000) + set_u32(1502, 12345) + set_u32(1602, (extra and extra.failsafe) or 0) +end + +local function boot(product, config) + host.reset() + driver_init, driver_poll, driver_command, driver_default_mode = nil, nil, nil, nil + dofile(DRIVER_PATH) + driver_init(config or {}) + set_u32(1016, product or 304111) + set_u32(1014, 18416854) + charger(2, 7, {}, 32000) + driver_poll() +end + +local function writes(addr) + local out = {} + for _, call in ipairs(host._calls) do + if call.func == "modbus_write" and (addr == nil or call.args[1] == addr) then + table.insert(out, addr and call.args[2] or (call.args[1] .. "=" .. call.args[2])) + end + end + return out +end +local function last(addr) local w = writes(addr); return w[#w] end +local function later(ms) host._millis_counter = host._millis_counter + ms end +local THREE = { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25 } +local ONE = { phase_mode = "1p", voltage = 230, max_amps_per_phase = 25 } +local function order(w, cmd) return driver_command("ev_set_current", w, cmd or THREE) end + +-- 1. Current first, then enable. +boot() +assert(order(11040)) +assert(table.concat(writes(), ",") == "5004=16000,5014=1", "writes were " .. table.concat(writes(), ",")) + +-- 2. Round down to 0.1 A, never up. +boot() +assert(order(4500)); assert(last(5004) == 6500, "4500 W did not round down to 6.5 A") + +-- 3. Below 6 A disables; 5004 takes nothing under 6000 mA. +boot() +assert(order(4000)) +assert(table.concat(writes(), ",") == "5014=0", "below 6 A wrote " .. table.concat(writes(), ",")) + +-- 4. One phase only when it is certain. A 1p order on an idle three-phase +-- installation gives P/(3*V), so a three-phase car never gets the L1 sum. +boot() +assert(order(7000, ONE)); assert(last(5004) == 10100, "an idle 1p order was not split over three") +-- ...and on a car that draws on three phases. +boot() +charger(3, 7, { 10000, 10000, 10000 }, 10000) +driver_poll() +assert(order(7000, ONE)); assert(last(5004) == 10100, "measured phases were ignored") +-- A 1p order on a car that draws on exactly one phase gives P/V. +boot() +charger(3, 7, { 10000, 0, 0 }, 10000) +driver_poll() +assert(order(3680, ONE)); assert(last(5004) == 16000, "a one-phase car did not get P/V") +-- A car once seen on three phases stays three until it is unplugged. +boot() +charger(3, 7, { 10000, 10000, 10000 }, 10000) +driver_poll() +charger(3, 7, { 10000, 0, 0 }, 10000) +driver_poll() +assert(order(7000, ONE)); assert(last(5004) == 10100, "a three-phase car got P/V") +charger(1, 0, {}, 10000) +driver_poll() +charger(3, 7, { 10000, 0, 0 }, 10000) +driver_poll() +assert(order(3680, ONE)); assert(last(5004) == 16000, "unplugging did not forget the old car") +-- A one-phase installation (config) gives P/V, idle or not, unless the car +-- draws on more than one phase. +boot(nil, { phases = 1 }) +assert(order(3680)); assert(last(5004) == 16000, "phases: 1 was ignored") +charger(3, 7, { 10000, 10000, 10000 }, 10000) +driver_poll() +assert(order(7000)); assert(last(5004) == 10100, "phases: 1 overrode three measured phases") + +-- 5. Caps: Core's max and cap per phase, and the charger's hardware limit. +boot() +assert(order(22000, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 10 })) +assert(last(5004) == 10000, "max_amps_per_phase was exceeded") +assert(order(22000, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25, cap_amps_per_phase = 8 })) +assert(last(5004) == 8000, "cap_amps_per_phase was exceeded") +assert(order(1e12)); assert(last(5004) == 25000, "a huge order passed the fuse") +boot() +charger(2, 7, {}, 13000, { hw = 13000 }) +driver_poll() +assert(order(11040)); assert(last(5004) == 13000, "the charger's hardware limit was exceeded") + +-- 6. Write only on change; a decrease lands at once. +boot() +assert(order(11040)); assert(order(11040)) +assert(#writes() == 2, "an unchanged order was written again") +assert(order(5520)); assert(last(5004) == 8000, "a decrease was not written at once") +assert(#writes() == 3) + +-- 7. Pause disables, resume enables at the same current. +boot() +assert(order(11040)) +assert(driver_command("ev_pause", 0, {})); assert(last(5014) == 0, "pause did not disable") +assert(driver_command("ev_resume", 0, {})); assert(last(5014) == 1, "resume did not enable") +assert(#writes(5004) == 1, "resume rewrote an unchanged current") +-- Resume carries no budget. With no order and no limit on record, after a +-- reload for example, nothing is written and the start is accepted: Core's +-- next order sets the level. The wait is logged once. +boot() +assert(driver_command("ev_start", 0, {}) == true, "a start with no known limit was refused") +assert(driver_command("ev_resume", 0, {}) == true) +assert(#writes() == 0, "start without an order wrote") +local waits = 0 +for _, line in ipairs(host._logs) do + if line:find("waits for Core's next order", 1, true) then waits = waits + 1 end +end +assert(waits == 1, "the wait was logged " .. waits .. " times") +-- An order an hour old is not trusted: 5004 = 6000 mA first, then enable. +boot() +assert(order(11040)) +assert(driver_command("ev_pause", 0, {})) +later(3600000) +local resumed_before = #writes() +assert(driver_command("ev_resume", 0, {})) +local resumed = {} +for i = resumed_before + 1, #writes() do resumed[#resumed + 1] = writes()[i] end +assert(table.concat(resumed, ",") == "5004=6000,5014=1", "an old order resumed as " .. table.concat(resumed, ",")) +-- ...and nothing when the last positive order left no room for 6 A. A later +-- 0 W order names only the site maximum and must not replace that cap. +boot() +assert(order(11040, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25, cap_amps_per_phase = 4 })) +assert(order(0, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25 })) +later(3600000) +resumed_before = #writes() +assert(driver_command("ev_resume", 0, {}) == false) +assert(#writes() == resumed_before, "resumed without room for 6 A") + +-- 8. Core runs default mode at every start: a charger FTW never wrote to in +-- this run is left alone, and a charge already running goes on. +boot() +charger(3, 7, { 16000, 16000, 16000 }, 16000) +driver_poll() +for _ = 1, 3 do assert(driver_default_mode() == true) end +assert(#writes() == 0, "default mode wrote to a charger FTW never steered") +-- Once FTW wrote, default mode disables once, then writes nothing. +boot() +assert(order(11040)) +assert(driver_default_mode() == true); assert(last(5014) == 0, "default mode did not disable") +local before = #writes() +for _ = 1, 5 do assert(driver_default_mode() == true) end +assert(#writes() == before, "default mode kept writing") + +-- 9. A refused write returns false, and the next order tries again. +boot() +host._modbus_write_error = "modbus exception function=0x06 code=0x02" +assert(order(11040) == false, "a refused write returned true") +host._modbus_write_error = nil +assert(order(11040)); assert(last(5004) == 16000 and last(5014) == 1, "the order was not retried") +-- A refused enable fails the order too. +boot() +host._modbus_write_fail_at = 2 +assert(order(11040) == false, "a refused enable returned true") + +-- 10. Once FTW wrote, a refused stand-down returns false. It is tried again +-- at most once a minute, with no write in between, and true only once it +-- lands. +boot() +assert(order(11040)) +host._modbus_write_error = "modbus exception function=0x06 code=0x02" +local function default_writes() + local n = #writes() + local result = driver_default_mode() + return #writes() - n, result +end +local counts = {} +for i = 1, 4 do + local count, result = default_writes() + counts[i] = count + assert(result == false, "a refused stand-down returned " .. tostring(result)) +end +assert(table.concat(counts, ",") == "1,0,0,0", "stand-down retried too often: " .. table.concat(counts, ",")) +later(61000) +local count, result = default_writes() +assert(count == 1 and result == false, "the stand-down was not retried after a minute") +host._modbus_write_error = nil +later(61000) +count, result = default_writes() +assert(count == 1 and result == true and last(5014) == 0, "a landed stand-down was not confirmed") +count, result = default_writes() +assert(count == 0 and result == true, "a confirmed stand-down was written again") + +-- 10b. A charger that stays suspended against an enable for 30 s gets the +-- enable again on the next order, at most every 30 s. +boot() +assert(order(11040)) +charger(5, 7, {}, 16000) +driver_poll() +later(31000) +driver_poll() +assert(host._emitted.ev[#host._emitted.ev].command_stalled == true, "a stalled enable was not reported") +assert(order(11040)); assert(#writes(5014) == 2, "a stalled enable was not written again") +later(5000) +driver_poll() +assert(order(11040)); assert(#writes(5014) == 2, "a stalled enable was written again too soon") + +-- 11. Not a P30: no control, no writes. +boot(4000110) +assert(order(11040) == false, "controlled a charger that is not a P30") +assert(driver_default_mode() == true) +assert(#writes() == 0, "wrote to a charger that is not a P30") + +-- 12. A charger that offers more than the order, or charges while disabled, +-- has lost it: the next order writes it again, once the order could land. +boot() +assert(order(11040)) +charger(3, 7, { 16000, 16000, 16000 }, 32000) +driver_poll() +assert(order(11040)); assert(#writes(5004) == 1, "rewrote before the order could land") +later(31000) +driver_poll() +assert(order(11040)); assert(#writes(5004) == 2, "a lost current was not written again") +boot() +assert(order(4000)) +charger(3, 7, { 16000, 16000, 16000 }, 16000) +later(31000) +driver_poll() +assert(order(4000)); assert(#writes(5014) == 2, "charging while disabled was not disabled again") + +-- 13. The failsafe timer is refreshed at half its timeout while FTW steers, +-- paused or not, and while FTW holds the charger disabled. After three +-- refusals it is tried once a minute, and the count starts over after a +-- success. +boot() +charger(2, 7, {}, 32000, { failsafe = 20 }) +assert(order(11040)) +driver_poll(); assert(#writes(5018) == 1 and last(5018) == 20, "failsafe was not refreshed") +later(4000); driver_poll(); assert(#writes(5018) == 1, "failsafe refreshed too often") +later(7000); driver_poll(); assert(#writes(5018) == 2, "failsafe refresh stopped") +assert(driver_command("ev_pause", 0, {})) +later(11000); driver_poll(); assert(#writes(5018) == 3, "failsafe refresh stopped during a pause") +host._modbus_write_error = "modbus exception function=0x06 code=0x02" +for n = 4, 6 do + later(11000); driver_poll(); assert(#writes(5018) == n, "a refused refresh was not retried") +end +later(11000); driver_poll(); assert(#writes(5018) == 6, "no back-off after three refusals") +later(50000); driver_poll(); assert(#writes(5018) == 7, "the refresh did not retry after a minute") +host._modbus_write_error = nil +later(61000); driver_poll(); assert(#writes(5018) == 8, "the refresh did not retry after a minute") +later(11000); driver_poll(); assert(#writes(5018) == 9, "a success did not reset the back-off") +-- While FTW holds the charger disabled after a stand-down, the refresh goes +-- on, so a failsafe current cannot start a charge FTW stopped. +assert(driver_default_mode() == true) +later(11000); driver_poll(); assert(#writes(5018) == 10, "failsafe refresh stopped while FTW holds the charger disabled") + +-- 13b. With L2 or L3 unreadable the phase count is unknown: a 1p order is +-- divided by three. The lost registers are tried again once a minute. +boot() +charger(3, 7, { 10000, 0, 0 }, 10000) +host._modbus_read_fail_addresses[1010] = "timeout" +host._modbus_read_fail_addresses[1012] = "timeout" +for _ = 1, 4 do driver_poll() end +assert(order(7000, ONE)); assert(last(5004) == 10100, "an unknown phase count was not treated as three") +local function reads(addr) + local n = 0 + for _, call in ipairs(host._calls) do + if call.func == "modbus_read" and call.args[1] == addr then n = n + 1 end + end + return n +end +local tries = reads(1010) +driver_poll(); assert(reads(1010) == tries, "a lost phase current was read on every poll") +host._modbus_read_fail_addresses[1010] = nil +host._modbus_read_fail_addresses[1012] = nil +later(61000) +driver_poll(); assert(reads(1010) == tries + 1, "a lost phase current was not tried again") +assert(order(3680, ONE)); assert(last(5004) == 16000, "a one-phase car did not get P/V once all three answered") + +-- 14. Telemetry: power in W, session energy, state, the charger's offer. +boot() +charger(3, 7, { 16000, 16000, 16000 }, 16000) +driver_poll() +local ev = host._emitted.ev[#host._emitted.ev] +assert(math.abs(ev.w - 11040) < 1, "power is not in W") +assert(ev.connected == true and ev.charging == true and ev.phases == 3) +assert(ev.max_a == 16 and ev.session_wh == 1234.5) +charger(5, 7, {}, 16000) +driver_poll() +ev = host._emitted.ev[#host._emitted.ev] +assert(ev.charging == false and ev.reason_no_current_label == "suspended") +host._modbus_read_error = "timeout" +local count = #host._emitted.ev +driver_poll() +assert(#host._emitted.ev == count, "a failed read was reported") + +print("keba control: ok") diff --git a/drivers/tests/lua_harness/test_schrack_ev_control.lua b/drivers/tests/lua_harness/test_schrack_ev_control.lua new file mode 100644 index 0000000..2cd0a0e --- /dev/null +++ b/drivers/tests/lua_harness/test_schrack_ev_control.lua @@ -0,0 +1,261 @@ +-- Schrack i-CHARGE CION: an order becomes 101 (whole A) then 100 = 1, per +-- phase, rounded down, within every cap. Below 6 A and pause write 100 = 0. +-- A void authorisation is cleared with 0 before 1, once per void episode. +-- Resume writes a fresh order back, or only 6 A when the order is old. +-- Default mode acts only after FTW wrote, returns true only once the +-- stand-down lands, and retries a refused one at most once a minute. A +-- refused write returns false. +dofile("drivers/tests/lua_harness/host_mock.lua") +local DRIVER_PATH = "drivers/lua/schrack_ev.lua" + +local CP_A, CP_B, CP_C = 65, 66, 67 + +-- cp: CP state; auth/current: registers 100/101; offered: 126; max: 127; +-- cable: 128; void: 120; bits: 121. +local function charger(s) + local h = host._modbus_registers.holding + h[100] = s.auth or 0 + h[101] = s.current or 16 + h[120] = s.void or 0 + h[121] = s.bits or 0 + h[126] = s.offered or 0 + h[127] = s.max or 32 + h[128] = s.cable or 32 + h[139] = s.cp or CP_B + h[150] = s.error or 0 +end + +local function boot(config) + host.reset() + driver_init, driver_poll, driver_command, driver_default_mode = nil, nil, nil, nil + dofile(DRIVER_PATH) + driver_init(config or {}) + charger({ cp = CP_B }) + driver_poll() +end + +local function writes(addr) + local out = {} + for _, call in ipairs(host._calls) do + if call.func == "modbus_write" and (addr == nil or call.args[1] == addr) then + table.insert(out, addr and call.args[2] or (call.args[1] .. "=" .. call.args[2])) + end + end + return out +end +local function last(addr) local w = writes(addr); return w[#w] end +local function later(ms) host._millis_counter = host._millis_counter + ms end +local THREE = { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25 } +local ONE = { phase_mode = "1p", voltage = 230, max_amps_per_phase = 25 } +local function order(w, cmd) return driver_command("ev_set_current", w, cmd or THREE) end + +-- 1. Current first, then authorise. +boot() +assert(order(11040)) +assert(table.concat(writes(), ",") == "101=16,100=1", "writes were " .. table.concat(writes(), ",")) + +-- 2. Whole amperes, rounded down, never up. +boot() +assert(order(4500)); assert(last(101) == 6, "4500 W did not round down to 6 A") + +-- 3. Below 6 A withdraws the authorisation. +boot() +assert(order(4000)) +assert(table.concat(writes(), ",") == "100=0", "below 6 A wrote " .. table.concat(writes(), ",")) + +-- 4. The charger measures no phases, so only config decides. A 1p order on +-- an idle three-phase installation gives P/(3*V). +boot() +assert(order(7000, ONE)); assert(last(101) == 10, "an idle 1p order was not split over three") +assert(order(3680, ONE)); assert(last(100) == 0, "a 1p order reached 16 A on a three-phase installation") +boot({ phases = 1 }) +assert(order(3680, ONE)); assert(last(101) == 16, "phases: 1 was ignored") +-- On a one-phase installation every order is one phase, within the fuse. +assert(order(11040)); assert(last(101) == 25, "phases: 1 did not decide a 3p order") + +-- 5. Caps: Core's max and cap per phase, the charger's maximum and the cable. +boot() +assert(order(22000, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 10 })) +assert(last(101) == 10, "max_amps_per_phase was exceeded") +assert(order(22000, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25, cap_amps_per_phase = 8 })) +assert(last(101) == 8, "cap_amps_per_phase was exceeded") +assert(order(1e12)); assert(last(101) == 25, "a huge order passed the fuse") +boot() +charger({ cp = CP_B, max = 13 }) +driver_poll() +assert(order(11040)); assert(last(101) == 13, "the charger's maximum was exceeded") +boot() +charger({ cp = CP_B, max = 32, cable = 13 }) +driver_poll() +assert(order(11040)); assert(last(101) == 13, "the cable's capacity was exceeded") + +-- 6. Write only on change; a decrease lands at once. +boot() +assert(order(11040)); assert(order(11040)) +assert(#writes() == 2, "an unchanged order was written again") +assert(order(5520)); assert(last(101) == 8, "a decrease was not written at once") +assert(#writes() == 3) + +-- 7. With no car, the current is set but authorisation waits for the car. +boot() +charger({ cp = CP_A }) +driver_poll() +assert(order(11040)) +assert(table.concat(writes(), ",") == "101=16", "authorised with no car: " .. table.concat(writes(), ",")) +charger({ cp = CP_B, current = 16 }) +driver_poll() +assert(order(11040)); assert(last(100) == 1, "did not authorise once the car arrived") + +-- 8. A void authorisation is cleared with 0 before 1. +boot() +charger({ cp = CP_B, void = 1 }) +driver_poll() +assert(order(11040)) +assert(table.concat(writes(), ",") == "101=16,100=0,100=1", "void not cleared: " .. table.concat(writes(), ",")) +-- A fault keeps the reset void. The reset is repeated at most every 30 s, so +-- a car that stays unready is not cycled on every order... +charger({ cp = CP_B, void = 1, auth = 1, current = 16, error = 1 }) +driver_poll() +assert(order(11040)) +assert(table.concat(writes(), ",") == "101=16,100=0,100=1", "void reset repeated within 30 s: " .. table.concat(writes(), ",")) +-- ...and once the fault clears while void still reads 1, a later order resets +-- it again and charging resumes. +charger({ cp = CP_B, void = 1, auth = 1, current = 16 }) +later(31000) +driver_poll() +assert(order(11040)) +assert(table.concat(writes(), ",") == "101=16,100=0,100=1,100=0,100=1", "no reset after the fault cleared: " .. table.concat(writes(), ",")) +charger({ cp = CP_C, void = 0, auth = 1, current = 16, bits = 3, offered = 16 }) +driver_poll() +assert(host._emitted.ev[#host._emitted.ev].charging == true, "charging did not resume") +-- A new void episode gets its own reset at once. +charger({ cp = CP_B, void = 1, auth = 1, current = 16 }) +driver_poll() +assert(order(11040)) +assert(table.concat(writes(), ",") == "101=16,100=0,100=1,100=0,100=1,100=0,100=1", "a new void episode was not reset") +-- A refused reset does not count: the next order tries it again at once. +boot() +charger({ cp = CP_B, void = 1 }) +driver_poll() +host._modbus_write_fail_at = 2 +assert(order(11040) == false, "a refused reset returned true") +assert(order(11040)) +assert(table.concat(writes(), ",") == "101=16,100=0,100=0,100=1", "a refused reset was not retried: " .. table.concat(writes(), ",")) + +-- 9. Pause withdraws the authorisation, resume gives it back. +boot() +assert(order(11040)) +assert(driver_command("ev_pause", 0, {})); assert(last(100) == 0, "pause did not withdraw") +assert(driver_command("ev_resume", 0, {})); assert(last(100) == 1, "resume did not authorise") +assert(#writes(101) == 1, "resume rewrote an unchanged current") + +-- 9b. Resume carries no budget. With no order and no limit on record, after +-- a reload for example, nothing is written and the start is accepted: Core's +-- next order sets the level. The wait is logged once. +boot() +assert(driver_command("ev_start", 0, {}) == true, "a start with no known limit was refused") +assert(driver_command("ev_resume", 0, {}) == true) +assert(#writes() == 0, "start without an order wrote") +local waits = 0 +for _, line in ipairs(host._logs) do + if line:find("waits for Core's next order", 1, true) then waits = waits + 1 end +end +assert(waits == 1, "the wait was logged " .. waits .. " times") +-- An order an hour old is not trusted: 101 = 6 A first, then authorise. +boot() +assert(order(11040)) +assert(driver_command("ev_pause", 0, {})) +later(3600000) +local resumed_before = #writes() +assert(driver_command("ev_resume", 0, {})) +local resumed = {} +for i = resumed_before + 1, #writes() do resumed[#resumed + 1] = writes()[i] end +assert(table.concat(resumed, ",") == "101=6,100=1", "an old order resumed as " .. table.concat(resumed, ",")) +-- ...and nothing when the last positive order left no room for 6 A. A later +-- 0 W order names only the site maximum and must not replace that cap. +boot() +assert(order(11040, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25, cap_amps_per_phase = 4 })) +assert(order(0, { phase_mode = "3p", voltage = 230, max_amps_per_phase = 25 })) +later(3600000) +resumed_before = #writes() +assert(driver_command("ev_resume", 0, {}) == false) +assert(#writes() == resumed_before, "resumed without room for 6 A") + +-- 10. Core runs default mode at every start: a charger FTW never wrote to in +-- this run is left alone, and a charge already running goes on. +boot() +charger({ cp = CP_C, auth = 1, current = 16, bits = 3, offered = 16 }) +driver_poll() +for _ = 1, 3 do assert(driver_default_mode() == true) end +assert(#writes() == 0, "default mode wrote to a charger FTW never steered") +-- Once FTW wrote, default mode withdraws once, then writes nothing. +boot() +assert(order(11040)) +assert(driver_default_mode() == true); assert(last(100) == 0, "default mode did not withdraw") +local before = #writes() +for _ = 1, 5 do assert(driver_default_mode() == true) end +assert(#writes() == before, "default mode kept writing") + +-- 11. A refused write returns false, and the next order tries again. +boot() +host._modbus_write_error = "modbus exception function=0x06 code=0x02" +assert(order(11040) == false, "a refused write returned true") +host._modbus_write_error = nil +assert(order(11040)); assert(last(101) == 16 and last(100) == 1, "the order was not retried") + +-- 12. Once FTW wrote, a refused stand-down returns false. It is tried again +-- at most once a minute, with no write in between, and true only once it +-- lands. +boot() +assert(order(11040)) +host._modbus_write_error = "modbus exception function=0x06 code=0x02" +local function default_writes() + local n = #writes() + local result = driver_default_mode() + return #writes() - n, result +end +local counts = {} +for i = 1, 4 do + local count, result = default_writes() + counts[i] = count + assert(result == false, "a refused stand-down returned " .. tostring(result)) +end +assert(table.concat(counts, ",") == "1,0,0,0", "stand-down retried too often: " .. table.concat(counts, ",")) +later(61000) +local count, result = default_writes() +assert(count == 1 and result == false, "the stand-down was not retried after a minute") +host._modbus_write_error = nil +later(61000) +count, result = default_writes() +assert(count == 1 and result == true and last(100) == 0, "a landed stand-down was not confirmed") +count, result = default_writes() +assert(count == 0 and result == true, "a confirmed stand-down was written again") + +-- 13. A charger that no longer holds the order is written again once the +-- order could land. +boot() +assert(order(11040)) +charger({ cp = CP_C, auth = 1, current = 32, bits = 3, offered = 32 }) +driver_poll() +assert(order(11040)); assert(#writes(101) == 1, "rewrote before the order could land") +later(11000) +driver_poll() +assert(order(11040)); assert(#writes(101) == 2, "a lost current was not written again") + +-- 14. Telemetry: state, the charger's offer, no invented power. +boot() +charger({ cp = CP_C, auth = 1, current = 16, bits = 3, offered = 16 }) +driver_poll() +local ev = host._emitted.ev[#host._emitted.ev] +assert(ev.connected == true and ev.charging == true and ev.max_a == 16) +assert(ev.w == nil and ev.session_wh == nil, "reported power the charger does not measure") +charger({ cp = CP_B, auth = 0 }) +driver_poll() +ev = host._emitted.ev[#host._emitted.ev] +assert(ev.charging == false and ev.reason_no_current_label == "not_authorised") +host._modbus_read_error = "timeout" +local count = #host._emitted.ev +driver_poll() +assert(#host._emitted.ev == count, "a failed read was reported") + +print("schrack_ev control: ok") diff --git a/drivers/tests/test_etrel_control.py b/drivers/tests/test_etrel_control.py new file mode 100644 index 0000000..9134c8e --- /dev/null +++ b/drivers/tests/test_etrel_control.py @@ -0,0 +1,12 @@ +"""Etrel INCH turns orders into a safe current setpoint, pauses, resumes and stands down once.""" +from pathlib import Path +import subprocess + + +def test_etrel_control(): + root = Path(__file__).resolve().parents[2] + result = subprocess.run( + [str(root / "lua55"), "drivers/tests/lua_harness/test_etrel_control.lua"], + cwd=root, text=True, capture_output=True, check=False, + ) + assert result.returncode == 0, result.stdout + result.stderr diff --git a/drivers/tests/test_keba_control.py b/drivers/tests/test_keba_control.py new file mode 100644 index 0000000..d564829 --- /dev/null +++ b/drivers/tests/test_keba_control.py @@ -0,0 +1,12 @@ +"""KEBA P30 turns orders into a safe current and enable, pauses, resumes and stands down once.""" +from pathlib import Path +import subprocess + + +def test_keba_control(): + root = Path(__file__).resolve().parents[2] + result = subprocess.run( + [str(root / "lua55"), "drivers/tests/lua_harness/test_keba_control.lua"], + cwd=root, text=True, capture_output=True, check=False, + ) + assert result.returncode == 0, result.stdout + result.stderr diff --git a/drivers/tests/test_schrack_ev_control.py b/drivers/tests/test_schrack_ev_control.py new file mode 100644 index 0000000..011fefe --- /dev/null +++ b/drivers/tests/test_schrack_ev_control.py @@ -0,0 +1,12 @@ +"""Schrack CION turns orders into a safe current and authorisation, pauses, resumes and stands down once.""" +from pathlib import Path +import subprocess + + +def test_schrack_ev_control(): + root = Path(__file__).resolve().parents[2] + result = subprocess.run( + [str(root / "lua55"), "drivers/tests/lua_harness/test_schrack_ev_control.lua"], + cwd=root, text=True, capture_output=True, check=False, + ) + assert result.returncode == 0, result.stdout + result.stderr diff --git a/index.yaml b/index.yaml index 183f6ec..28ebba7 100644 --- a/index.yaml +++ b/index.yaml @@ -206,14 +206,15 @@ drivers: size_bytes: 17074 sha256: "4fabecf0f5f2f276e7393d104330314d3453f19ad2905a51d1ffa306372a71ad" - name: "etrel" - version: "1.0.3" + version: "1.1.0" tier: community protocol: modbus connectivity: local - ders: [v2x_charger] - control: false - size_bytes: 3545 - sha256: "fdf3c3cae6ca45f9b546a97cf16a34b047172293711db2dfa759286f065d77c8" + setup: [device_ui] + ders: [ev] + control: true + size_bytes: 21562 + sha256: "7ab3c7f28f3df5d75c491d589a751183dc5c2deac0f6b96212f79c39be668d9c" - name: "ferroamp" version: "2.1.2" tier: core @@ -371,14 +372,15 @@ drivers: size_bytes: 4524 sha256: "e3594504efd1315d86ced6480b8f526cfd0f940e992a5743858af8f772199eae" - name: "keba" - version: "1.0.3" + version: "1.1.0" tier: community protocol: modbus connectivity: local - ders: [v2x_charger] - control: false - size_bytes: 4620 - sha256: "8c8f2685dbd9a67b422c134c9d1afc53d073233f0b2c10cca75c8b30fefaad91" + setup: [installer] + ders: [ev] + control: true + size_bytes: 23439 + sha256: "1cb5b73fe49525d1d2500eca0d3d18b340e83fc7a0943fb63c3c568113069f49" - name: "konja-261-svk" version: "0.3.2" tier: community @@ -558,14 +560,15 @@ drivers: size_bytes: 4615 sha256: "cfe95da259940699c7403e349c6402e10aad9dd358dc864f14d102326a1a39e7" - name: "schrack_ev" - version: "1.0.3" + version: "1.1.0" tier: community protocol: modbus connectivity: local - ders: [v2x_charger] - control: false - size_bytes: 3554 - sha256: "652db1569e74d8a451ddfcefb4f55d897e144b9a955608d20b4278ed6b9a2fdd" + setup: [bridge, installer] + ders: [ev] + control: true + size_bytes: 19195 + sha256: "2efa740660f9205aa2d85a210c9bfc98e6bcb00b4fa75f67df7d4f230b8d1f90" - name: "sdm630" version: "2.0.2" tier: core @@ -808,14 +811,14 @@ drivers: size_bytes: 5779 sha256: "deb4dba9ef525a59c023939d95686f4b533ee5a9d39a34598e499323afb6535f" - name: "wallbox" - version: "1.0.3" + version: "1.1.0" tier: community protocol: modbus connectivity: local - ders: [v2x_charger] + ders: [ev] control: false - size_bytes: 2842 - sha256: "1c0362e0a25f1fe297de838a85089eb7676b25e78a7d6d1d03ccea1258b94403" + size_bytes: 4047 + sha256: "239ac3e22c0b0ed528fe813a10aee58dacf192d350e1ee5b942f53896fd6727f" - name: "zap" version: "3.1.3" tier: core diff --git a/manifests/etrel.yaml b/manifests/etrel.yaml index b908e63..8be3a88 100644 --- a/manifests/etrel.yaml +++ b/manifests/etrel.yaml @@ -1,26 +1,37 @@ name: "etrel" -version: "1.0.3" +version: "1.1.0" tier: community author: "Sourceful Labs AB" protocol: modbus connectivity: local -ders: [v2x_charger] -control: false +setup: [device_ui] +ders: [ev] +control: true tested_devices: - manufacturer: "Etrel" model_family: "INCH" - variants: [INCH HOME, INCH DUO, INCH PRO] + variants: [INCH Home, INCH Pro, INCH Duo] regions: [] firmware_versions: "" - notes: "Community driver, untested" + notes: "Control written from Etrel's INCH SmartHome Modbus TCP register list; not yet confirmed on hardware. Duo: connector 1 only." min_driver_version: "1.0.0" min_host_version: "2.0.0" -size_bytes: 3545 +size_bytes: 21562 dkb_id: "" -sha256: "fdf3c3cae6ca45f9b546a97cf16a34b047172293711db2dfa759286f065d77c8" +sha256: "7ab3c7f28f3df5d75c491d589a751183dc5c2deac0f6b96212f79c39be668d9c" signature: "" bytecode_sha256: "" bytecode_signature: "" bytecode_size: 0 changelog: "" + +upstream_docs: + - url: "https://etrel.atlassian.net/wiki/spaces/EKC/pages/3485665834" + title: "Etrel Knowledge Centre: Modbus communication with INCH products" + kind: other + url_stability: unknown + - url: "https://raw.githubusercontent.com/RustyDust/sonnen-charger/main/Etrel%20INCH%20SmartHome%20Modbus%20TCPRegisters.xlsx" + title: "Etrel INCH SmartHome Modbus TCP registers (2023 copy)" + kind: register_map + url_stability: stable diff --git a/manifests/keba.yaml b/manifests/keba.yaml index 101f35d..eb49882 100644 --- a/manifests/keba.yaml +++ b/manifests/keba.yaml @@ -1,26 +1,37 @@ name: "keba" -version: "1.0.3" +version: "1.1.0" tier: community author: "Sourceful Labs AB" protocol: modbus connectivity: local -ders: [v2x_charger] -control: false +setup: [installer] +ders: [ev] +control: true tested_devices: - manufacturer: "Keba" model_family: "KeContact P30" variants: [P30 c-series, P30 x-series] regions: [] firmware_versions: "" - notes: "Community driver, untested" + notes: "Control written from KEBA's P30 Modbus TCP Programmers Guide V1.07; not yet confirmed on hardware. Needs c-series firmware 3.10.16 or x-series 1.11, DIP switch DSW1.3 ON and Modbus unit ID 255." min_driver_version: "1.0.0" min_host_version: "2.0.0" -size_bytes: 4620 +size_bytes: 23439 dkb_id: "" -sha256: "8c8f2685dbd9a67b422c134c9d1afc53d073233f0b2c10cca75c8b30fefaad91" +sha256: "1cb5b73fe49525d1d2500eca0d3d18b340e83fc7a0943fb63c3c568113069f49" signature: "" bytecode_sha256: "" bytecode_signature: "" bytecode_size: 0 changelog: "" + +upstream_docs: + - url: "https://www.keba.com/download/x/44932c2bc8/kecontactp30modbustcp_pgen.pdf" + title: "KeContact P30 Modbus TCP Programmers Guide" + kind: register_map + url_stability: stable + - url: "https://www.keba.com/download/x/4aab015706/kecontactp30udp_pgen.pdf" + title: "KeContact P30 UDP Programmers Guide" + kind: api_docs + url_stability: stable diff --git a/manifests/schrack_ev.yaml b/manifests/schrack_ev.yaml index be851a8..08e44e9 100644 --- a/manifests/schrack_ev.yaml +++ b/manifests/schrack_ev.yaml @@ -1,26 +1,41 @@ name: "schrack_ev" -version: "1.0.3" +version: "1.1.0" tier: community author: "Sourceful Labs AB" protocol: modbus connectivity: local -ders: [v2x_charger] -control: false +setup: [bridge, installer] +ders: [ev] +control: true tested_devices: - manufacturer: "Schrack" - model_family: "i-CHARGE" - variants: [i-CHARGE CION, i-CHARGE CION PRO] + model_family: "i-CHARGE CION" + variants: [i-CHARGE CION Home, i-CHARGE CION Semipublic] regions: [] firmware_versions: "" - notes: "Community driver, untested" + notes: "Control written from Schrack's CION Home and Semipublic Modbus register table; not yet confirmed on hardware. Modbus RTU through a Modbus TCP gateway. No power reading. CION Pro not covered: use OCPP." min_driver_version: "1.0.0" min_host_version: "2.0.0" -size_bytes: 3554 +size_bytes: 19195 dkb_id: "" -sha256: "652db1569e74d8a451ddfcefb4f55d897e144b9a955608d20b4278ed6b9a2fdd" +sha256: "2efa740660f9205aa2d85a210c9bfc98e6bcb00b4fa75f67df7d4f230b8d1f90" signature: "" bytecode_sha256: "" bytecode_signature: "" bytecode_size: 0 changelog: "" + +upstream_docs: + - url: "https://image.schrackcdn.com/bedienungsanleitungen/a_emcionxxxx_modbus_lookup_table_de.pdf" + title: "Schrack CION Home and Semipublic Modbus register table (2023)" + kind: register_map + url_stability: stable + - url: "https://image.schrack.com/bedienungsanleitungen/a_emcion_homecontroller_modbus_lookup_table_de.pdf" + title: "Schrack CION Home and Semipublic Modbus register table (2022)" + kind: register_map + url_stability: stable + - url: "https://image.schrackcdn.com/bedienungsanleitungen/a_emcionxxxx_icc_v2.2_en.pdf" + title: "Schrack i-CHARGE CION operating manual V2.2" + kind: manual + url_stability: stable diff --git a/manifests/wallbox.yaml b/manifests/wallbox.yaml index d705fa1..4227c33 100644 --- a/manifests/wallbox.yaml +++ b/manifests/wallbox.yaml @@ -1,10 +1,10 @@ name: "wallbox" -version: "1.0.3" +version: "1.1.0" tier: community author: "Sourceful Labs AB" protocol: modbus connectivity: local -ders: [v2x_charger] +ders: [ev] control: false tested_devices: - manufacturer: "Wallbox" @@ -12,15 +12,21 @@ tested_devices: variants: [Commander 2, Pulsar Plus] regions: [] firmware_versions: "" - notes: "Community driver, untested" + notes: "Cannot control: we found no documented local Modbus interface for these chargers. The power and energy this driver reports come from an unverified register map. Use FTW's built-in OCPP central system (Wallbox supports OCPP 1.6J) to read and control them." min_driver_version: "1.0.0" min_host_version: "2.0.0" -size_bytes: 2842 +size_bytes: 4047 dkb_id: "" -sha256: "1c0362e0a25f1fe297de838a85089eb7676b25e78a7d6d1d03ccea1258b94403" +sha256: "239ac3e22c0b0ed528fe813a10aee58dacf192d350e1ee5b942f53896fd6727f" signature: "" bytecode_sha256: "" bytecode_signature: "" bytecode_size: 0 changelog: "" + +upstream_docs: + - url: "https://support.wallbox.com/en/knowledge-base/ocpp-activation-and-setup-guide/" + title: "Wallbox OCPP activation and setup guide" + kind: manual + url_stability: stable diff --git a/support-status.json b/support-status.json index 77cbb57..86df4a1 100644 --- a/support-status.json +++ b/support-status.json @@ -464,7 +464,7 @@ }, { "catalog_source": true, - "catalog_version": "1.0.3", + "catalog_version": "1.1.0", "driver_id": "etrel", "targets": { "blixt-l1": { @@ -842,7 +842,7 @@ }, { "catalog_source": true, - "catalog_version": "1.0.3", + "catalog_version": "1.1.0", "driver_id": "keba", "targets": { "blixt-l1": { @@ -1262,7 +1262,7 @@ }, { "catalog_source": true, - "catalog_version": "1.0.3", + "catalog_version": "1.1.0", "driver_id": "schrack_ev", "targets": { "blixt-l1": { @@ -1829,7 +1829,7 @@ }, { "catalog_source": true, - "catalog_version": "1.0.3", + "catalog_version": "1.1.0", "driver_id": "wallbox", "targets": { "blixt-l1": {