Add script to regulate Technic power networks

This commit is contained in:
Christopher Head 2018-08-25 10:49:41 -07:00
parent 37ba1dcfdb
commit adbe89a740
No known key found for this signature in database
GPG Key ID: 4B14D664A35D3E0B
2 changed files with 60 additions and 0 deletions

31
power-regulator.lua Normal file
View File

@ -0,0 +1,31 @@
local battery_boxes = "lv_batteries"
local supply_converter = "lv_converter"
local poll_interval = 10
local min_charge = 25
local max_charge = 75
if event.type == "program" or (event.type == "interrupt" and event.iid == "start") then
-- Step 1: ask the battery boxes for their data.
digiline_send(battery_boxes, "get")
interrupt(1, "finish")
mem.current = 0
mem.total = 0
elseif event.type == "digiline" and event.channel == battery_boxes then
-- Step 2: collect data from battery boxes.
mem.current = mem.current + event.msg.charge
mem.total = mem.total + event.msg.max_charge
elseif event.type == "interrupt" and event.iid == "finish" then
-- Step 3: all data received; decide what to do and sleep until next update.
local percent
if mem.total ~= 0 then
percent = mem.current * 100 / mem.total
else
percent = 0
end
if percent <= min_charge then
digiline_send(supply_converter, "on")
elseif percent >= max_charge then
digiline_send(supply_converter, "off")
end
interrupt(poll_interval - 1, "start")
end

29
power-regulator.md Normal file
View File

@ -0,0 +1,29 @@
Technic Power Network Regulator
===============================
The power network regulator script manages a Technic supply converter to
convert power from one voltage to another without wasting power. The regulator
script monitors the charge level of a set of battery boxes on the output
network and controls a supply converter. The supply converter is turned on when
the battery charge level gets low, to keep the network running, and turned off
when the battery charge level gets high, to avoid wasting power on the input
network by running the supply converter when not needed.
Configuration
-------------
The battery_boxes string is the name of the Digiline channel on which all the
battery boxes are listening.
The supply_converter string is the name of the Digiline channel on which the
supply converter is listening.
The poll_interval integer is the number of seconds between updates. This number
must be greater than 1.
The min_charge integer is the percentage (from 0 to 100) at which the supply
converter should turn on.
The max_charge integer is the percentage (from 0 to 100) at which the supply
converter should turn off.