Synchronize stress readings after clutch changes

This commit is contained in:
2026-07-17 01:43:07 +02:00
parent 5a24e96217
commit 03a88fb2c5
2 changed files with 24 additions and 7 deletions
+21 -4
View File
@@ -2,12 +2,12 @@
-- Two Create Item Vaults are discovered by their contents and remembered.
local REFRESH_SECONDS = 1
local SENSOR_SETTLE_SECONDS = 0.15
local MIN_WIDTH = 28
local MIN_HEIGHT = 13
local CLUTCH_OUTPUT_SIDE = "left"
-- This installation drives the Clutch directly: powered means stopped.
-- Change to true if a redstone-torch inverter is installed later.
local OUTPUT_LEVEL_WHEN_RUNNING = false
-- The external redstone-torch inverter makes output ON release the Clutch.
local OUTPUT_LEVEL_WHEN_RUNNING = true
local STATE_FILE = "create_andesite_state.txt"
local DEVICE_FILE = "create_andesite_devices.txt"
@@ -41,10 +41,14 @@ end
local requestedRunning = loadText(STATE_FILE) == "RUNNING"
local devices = textutils.unserialize(loadText(DEVICE_FILE) or "") or {}
local capacityCache = {}
local lastAppliedRunning = nil
local function setMachineRunning(running)
local level = running and OUTPUT_LEVEL_WHEN_RUNNING or not OUTPUT_LEVEL_WHEN_RUNNING
redstone.setOutput(CLUTCH_OUTPUT_SIDE, level)
local changed = lastAppliedRunning ~= running
lastAppliedRunning = running
return changed
end
-- Fail safe while peripherals are being discovered.
@@ -249,7 +253,20 @@ local function evaluate()
local allowed = not missing and not noMaterial and not outputFull and not overstressed
local effectiveRunning = requestedRunning and allowed
setMachineRunning(effectiveRunning)
local outputChanged = setMachineRunning(effectiveRunning)
-- Create updates the kinetic network after the redstone event. Without a
-- short settling period, the first frame after a touch shows the previous
-- state and therefore looks inverted.
if outputChanged then
sleep(SENSOR_SETTLE_SECONDS)
speed, capacity, usage, load = discoverStressometer()
overstressed = capacity and usage and usage > capacity
if overstressed and effectiveRunning then
effectiveRunning = false
setMachineRunning(false)
end
end
local status, color = "STOPPED", colors.red
if requestedRunning then