From af822abb6f104b12c3325328661aef79bc661600 Mon Sep 17 00:00:00 2001 From: didi Date: Wed, 15 Jul 2026 13:25:19 +0300 Subject: [PATCH] Clamp EMS/GVS/ET params in /execute to the bounds manual_control_app.py already enforces --- utils/receiver.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/utils/receiver.py b/utils/receiver.py index 77bed37..0934f3c 100644 --- a/utils/receiver.py +++ b/utils/receiver.py @@ -126,6 +126,12 @@ def stim_gvs(self, channel, amplitude, polarity, duration): def stim_et(self, channel, amplitude, polarity, freq, pulse_width, duration): print(f"⚠️ [SKIPPED ET - STIM DISABLED] ch={channel} amp={amplitude} pol={polarity} freq={freq} pw={pulse_width} dur={duration}") +def _clamp(value, lo, hi, label): + if value < lo or value > hi: + print(f"⚠️ [CLAMPED] {label}={value} outside [{lo}, {hi}], clamping") + return max(lo, min(hi, value)) + return value + def find_serial_ports(): ports = serial.tools.list_ports.comports() valid_ports = [] @@ -222,10 +228,12 @@ def execute_sequence(): # Process Stimulation Commands else: chan = int(cmd.get("channel", 1)) - amp = float(cmd.get("amplitude", 6)) - dur = float(cmd.get("duration", 0.5)) - freq = int(cmd.get("frequency", 100)) - pw = int(cmd.get("pulse_width", 300)) + # Same hardcoded bounds manual_control_app.py enforces via its sliders, + # applied here so /execute can't drive the stimulator outside them. + amp = _clamp(float(cmd.get("amplitude", 6)), 0, 60, "amplitude") + dur = _clamp(float(cmd.get("duration", 0.5)), 0, 10, "duration") + freq = _clamp(int(cmd.get("frequency", 100)), 0, 100, "frequency") + pw = _clamp(int(cmd.get("pulse_width", 300)), 0, 1000, "pulse_width") pol = int(cmd.get("polarity", 0)) if ctype == "EMS": @@ -248,4 +256,4 @@ def execute_sequence(): print(f"🚀 Receiver starting on port 5001...") print(f"🛠️ Stimulator Port: {stim_port or 'NONE'}") print(f"🛠️ Relay Port: {relay_port or 'NONE'}") - app.run(host='0.0.0.0', port=5001) \ No newline at end of file + app.run(host='0.0.0.0', port=5001)