diff --git a/robot/control/robot_control.py b/robot/control/robot_control.py index 174e729..790bb02 100644 --- a/robot/control/robot_control.py +++ b/robot/control/robot_control.py @@ -156,6 +156,12 @@ def on_set_pressure_set_point(self, data): pressure = data["pressure"] self.pid_group.set_force_setpoint(pressure) + # Recalculate stability with the new setpoint + self._pressure_pid_done_for_current_target = False + self._set_pressure_pid_active(True) + if hasattr(self, "pressure_sensor") and self.pressure_sensor: + self.pressure_sensor.buffer.clear() + def on_set_tracker_fiducials(self, data): # TODO: This shouldn't call the constructor again but instead a separate reset method. self.process_tracker.__init__(robot_config=self.robot_config) @@ -922,7 +928,10 @@ def setup_sensors(self): if not isinstance(self.pressure_sensor, BufferedPressureSensorReader): print("Enabling Pressure Sensor...") self.pressure_sensor = BufferedPressureSensorReader( - self.config, 115200, buffer_size=100 + self.config, + 115200, + buffer_size=100, + sample_interval=self.config.get("pressure_sample_interval", 0.05) ) else: if isinstance(self.pressure_sensor, BufferedPressureSensorReader): @@ -1246,6 +1255,19 @@ def update_state_variables(self): else None ) + # Continuous check: if stability was reached, but force spikes beyond a threshold, + # reactivate the PID to retreat and recalculate stability. + if self._pressure_pid_done_for_current_target and self.feedback_pressure_sensor is not None: + if self.pid_group: + setpoint = self.pid_group.get_force_setpoint() + spike_multiplier = self.config.get("force_spike_threshold_multiplier", 1.4) + if setpoint is not None and self.feedback_pressure_sensor <= spike_multiplier * setpoint: + print(f"[!] Force spike detected ({self.feedback_pressure_sensor:.2f} <= {spike_multiplier} * {setpoint:.2f}). Reactivating pressure PID to retreat.") + self._pressure_pid_done_for_current_target = False + self._set_pressure_pid_active(True) + if self.pressure_sensor: + self.pressure_sensor.buffer.clear() + def update_navigation_variables(self, warning): now = time.time() if warning: diff --git a/robot/sensors/pressure_sensor.py b/robot/sensors/pressure_sensor.py index f793820..b7fecd1 100644 --- a/robot/sensors/pressure_sensor.py +++ b/robot/sensors/pressure_sensor.py @@ -8,11 +8,12 @@ class BufferedPressureSensorReader: - def __init__(self, config, baudrate=115200, buffer_size=100, reconnect_interval=1): + def __init__(self, config, baudrate=115200, buffer_size=100, reconnect_interval=1, sample_interval=0.0): self.config = config self.baudrate = baudrate self.buffer_size = buffer_size self.reconnect_interval = reconnect_interval + self.sample_interval = sample_interval self.connect_attempts = 0 # Track how many times we've tried to connect self.buffer = deque(maxlen=buffer_size) @@ -49,6 +50,10 @@ def _serial_loop(self): continue try: + if self.sample_interval > 0: + self.serial.reset_input_buffer() + self.serial.readline() # Discard partial line resulting from the flush + line = self.serial.readline().decode("utf-8", errors="ignore").strip() if line: try: @@ -67,6 +72,10 @@ def _serial_loop(self): if not self.started: self.started = True print("[✓] Pressure data started.") + + if self.sample_interval > 0: + time.sleep(self.sample_interval) + except ValueError: print(f"[!] Invalid float: {line}") except serial.SerialException as e: @@ -78,6 +87,8 @@ def _try_connect(self): return False try: self.serial = serial.Serial(self.config['com_port_pressure_sensor'], self.baudrate, timeout=1) + self.serial.dtr = False + self.serial.rts = False self.ready = True return True except serial.SerialException as e: