From 597c1a1be46669445f816d8352c0671783d47081 Mon Sep 17 00:00:00 2001 From: Kristof Date: Wed, 18 Nov 2026 10:59:16 +0100 Subject: [PATCH] Journal Bluetooth bench motor state changes The motor controller now journals connect, commanded speed/direction changes, stop requests (with reason: request, command_expired, disconnect), completed stops, disconnects and Bluetooth errors as linkhub.motor diagnostics, so they share the MAVLink timeline and land in experiment mavlog.json files. Lease renewals that keep the same target are not journaled. --- design/linkhub.md | 16 ++ linkhub/src/main.rs | 17 ++- linkhub/src/motor.rs | 342 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 320 insertions(+), 55 deletions(-) diff --git a/design/linkhub.md b/design/linkhub.md index 5b147f6..967a820 100644 --- a/design/linkhub.md +++ b/design/linkhub.md @@ -199,6 +199,22 @@ renewal and attempts a stop. A suspended browser must not revive an expired lease, and reopening the panel never starts or adopts a running motor. UI operation is described in the [motor panel guide](../linkhub-ui/README.md#bluetooth-motor-panel). +The controller journals its state changes as diagnostics with source +`linkhub.motor` and category `motor`, so they share the MAVLink timeline and +appear in an experiment's `mavlog.json`: + +| Event | Fields | When | +|---|---|---| +| `motor.connected` | `device`, `device_id` | A device connects (initialized off) | +| `motor.command` | `device_id`, `speed_percent`, `direction`, `timeout_ms` | A command starts the motor or changes its target or direction | +| `motor.stop_requested` | `reason` (`request`, `command_expired`, `disconnect`) | A controlled ramp-down of a running motor begins | +| `motor.stopped` | | The ramp reaches zero and the safe-stop command is written | +| `motor.disconnected` | `device`, `error` | A connected device is disconnected (warning when disconnect failed) | +| `motor.error` | `error` | A Bluetooth operation fails and motor state is unconfirmed (error level) | + +Lease renewals that keep the same target and direction are not journaled. +Query them with `linkhub query diagnostics --source linkhub.motor`. + Generic records, diagnostics, raw MAVLink frames, and decoded MAVLink messages are finite projections over the same cursor space. Every journal-backed GET projection accepts an explicit `after` cursor and returns: diff --git a/linkhub/src/main.rs b/linkhub/src/main.rs index 9eb284a..7d4ccbb 100644 --- a/linkhub/src/main.rs +++ b/linkhub/src/main.rs @@ -205,6 +205,7 @@ async fn main() -> Result<(), Box> { motor_scan_timeout_ms, motor_heartbeat_ms, motor_max_command_timeout_ms, + journal.clone(), ) .await?; @@ -382,18 +383,22 @@ async fn configure_motor( scan_timeout_ms: u64, heartbeat_ms: u64, maximum_command_timeout_ms: u64, + journal: JournalHandle, ) -> Result, Box> { use linkhub::motor::{BleMotorController, MotorConfig}; let Some(name_prefix) = name_prefix else { return Ok(None); }; - let controller = BleMotorController::bluetooth(MotorConfig { - name_prefix, - scan_timeout: Duration::from_millis(scan_timeout_ms), - heartbeat_interval: Duration::from_millis(heartbeat_ms), - maximum_command_timeout_ms, - }) + let controller = BleMotorController::bluetooth( + MotorConfig { + name_prefix, + scan_timeout: Duration::from_millis(scan_timeout_ms), + heartbeat_interval: Duration::from_millis(heartbeat_ms), + maximum_command_timeout_ms, + }, + journal, + ) .await?; Ok(Some(std::sync::Arc::new(controller))) } diff --git a/linkhub/src/motor.rs b/linkhub/src/motor.rs index a7fc065..6f50029 100644 --- a/linkhub/src/motor.rs +++ b/linkhub/src/motor.rs @@ -1,15 +1,20 @@ use std::collections::VecDeque; use std::future::Future; use std::pin::Pin; +use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex, MutexGuard, Weak}; use std::time::Duration; use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; use thiserror::Error; use tokio::sync::Mutex as AsyncMutex; use tokio::task::JoinHandle; use tokio::time::Instant; +use crate::journal::JournalHandle; +use crate::records::{DiagnosticEvent, DiagnosticLevel, wall_time_ns}; + pub const NUS_SERVICE_UUID: &str = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"; pub const NUS_RX_UUID: &str = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"; const SAFE_STOP_COMMAND: &str = "S:0;E:0;B:0\n"; @@ -269,12 +274,71 @@ impl Default for State { } } +/// Journals motor state changes (`linkhub query diagnostics --source +/// linkhub.motor`). Lease renewals that do not change speed or direction are +/// not journaled. +struct MotorEvents { + journal: Option, + sequence: AtomicU64, +} + +impl MotorEvents { + async fn emit(&self, level: DiagnosticLevel, event: &str, message: String, fields: Value) { + let Value::Object(fields) = fields else { + unreachable!("motor event fields are objects"); + }; + match level { + DiagnosticLevel::Error | DiagnosticLevel::Critical => { + tracing::error!(event, ?fields, "{message}"); + } + DiagnosticLevel::Warning => tracing::warn!(event, ?fields, "{message}"), + _ => tracing::info!(event, ?fields, "{message}"), + } + let Some(journal) = &self.journal else { + return; + }; + let diagnostic = DiagnosticEvent { + schema_version: 1, + run_id: journal.run_id(), + source: "linkhub.motor".to_owned(), + source_instance: "linkhub.motor".to_owned(), + source_sequence: self.sequence.fetch_add(1, Ordering::Relaxed) + 1, + source_wall_time_ns: wall_time_ns(), + source_monotonic_ns: None, + sim_time_ns: None, + sim_time_quality: None, + level, + category: "motor".to_owned(), + event: event.to_owned(), + message, + correlation_id: None, + causation_id: None, + fields, + related_records: Vec::new(), + }; + if let Err(error) = journal.append_diagnostic(diagnostic).await { + tracing::warn!(%error, "could not journal motor diagnostic"); + } + } + + async fn stop_requested(&self, reason: &str) { + self.emit( + DiagnosticLevel::Info, + "motor.stop_requested", + format!("Motor ramping to stop ({reason})"), + json!({ "reason": reason }), + ) + .await; + } +} + struct Inner { operation: AsyncMutex<()>, backend: AsyncMutex, config: MotorConfig, state: Mutex, supervisor: Mutex>>, + events: MotorEvents, } pub struct MotorController { @@ -311,7 +375,11 @@ pub trait MotorService: Send + Sync { } impl MotorController { - pub fn with_backend(backend: B, config: MotorConfig) -> Result { + pub fn with_backend( + backend: B, + config: MotorConfig, + journal: Option, + ) -> Result { config.validate()?; Ok(Self { inner: Arc::new(Inner { @@ -320,6 +388,10 @@ impl MotorController { config, state: Mutex::new(State::default()), supervisor: Mutex::new(None), + events: MotorEvents { + journal, + sequence: AtomicU64::new(0), + }, }), }) } @@ -372,11 +444,11 @@ impl MotorController { .await { Ok(device) => device, - Err(error) => return self.record_error(error), + Err(error) => return self.record_error(error).await, }; if let Err(error) = backend.write(SAFE_STOP_COMMAND).await { let _ = backend.disconnect().await; - return self.record_error(error); + return self.record_error(error).await; } device }; @@ -385,10 +457,19 @@ impl MotorController { let mut state = lock(&self.inner.state); state.connected = true; state.device = Some(result.name.clone()); - state.device_id = Some(result.id); + state.device_id = Some(result.id.clone()); mark_stopped(&mut state); state.error = None; } + self.inner + .events + .emit( + DiagnosticLevel::Info, + "motor.connected", + format!("Bluetooth motor {} connected", result.name), + json!({ "device": result.name, "device_id": result.id }), + ) + .await; self.start_supervisor(); Ok(result.name) } @@ -444,31 +525,62 @@ impl MotorController { if lock(&self.inner.state).device_id.as_deref() != Some(device_id) { return Err(MotorError::DeviceChanged); } - let mut state = lock(&self.inner.state); - if !state.connected { - return Err(MotorError::Disconnected); - } - if state.stop_epoch != epoch || state.stopping { - return Err(MotorError::Stopping); - } - if state - .expires_at - .is_some_and(|expiry| Instant::now() >= expiry) - { - begin_stop(&mut state); - return Err(MotorError::Stopping); + let changed = { + let mut state = lock(&self.inner.state); + if !state.connected { + return Err(MotorError::Disconnected); + } + if state.stop_epoch != epoch || state.stopping { + return Err(MotorError::Stopping); + } + if state + .expires_at + .is_some_and(|expiry| Instant::now() >= expiry) + { + Err(begin_stop(&mut state)) + } else { + let changed = !state.running + || state.profile.target != speed_percent + || state.direction != direction; + state.running = true; + state.profile.retarget(speed_percent); + state.direction = direction; + state.expires_at = Some(Instant::now() + Duration::from_millis(timeout_ms)); + state.error = None; + Ok(changed) + } + }; + match changed { + Err(began) => { + if began { + self.inner.events.stop_requested("command_expired").await; + } + Err(MotorError::Stopping) + } + Ok(true) => { + self.inner + .events + .emit( + DiagnosticLevel::Info, + "motor.command", + format!("Motor commanded to {speed_percent}%"), + json!({ + "device_id": device_id, + "speed_percent": speed_percent, + "direction": direction, + "timeout_ms": timeout_ms, + }), + ) + .await; + Ok(()) + } + Ok(false) => Ok(()), } - state.running = true; - state.profile.retarget(speed_percent); - state.direction = direction; - state.expires_at = Some(Instant::now() + Duration::from_millis(timeout_ms)); - state.error = None; - Ok(()) } pub async fn stop(&self) -> Result<(), MotorError> { let _operation = self.inner.operation.lock().await; - self.smooth_stop().await + self.smooth_stop("request").await } pub async fn reconnect(&self) -> Result { @@ -482,11 +594,14 @@ impl MotorController { self.disconnect().await } - async fn smooth_stop(&self) -> Result<(), MotorError> { + async fn smooth_stop(&self, reason: &str) -> Result<(), MotorError> { if !lock(&self.inner.state).connected { return Err(MotorError::Disconnected); } - begin_stop(&mut lock(&self.inner.state)); + let began = begin_stop(&mut lock(&self.inner.state)); + if began { + self.inner.events.stop_requested(reason).await; + } while lock(&self.inner.state).running { tokio::time::sleep(PROFILE_INTERVAL).await; advance_motor(&self.inner, PROFILE_INTERVAL).await?; @@ -495,8 +610,12 @@ impl MotorController { } async fn disconnect(&self) -> Result<(), MotorError> { - if lock(&self.inner.state).connected { - self.smooth_stop().await?; + let (was_connected, device) = { + let state = lock(&self.inner.state); + (state.connected, state.device.clone()) + }; + if was_connected { + self.smooth_stop("disconnect").await?; } self.stop_supervisor(); let result = self.inner.backend.lock().await.disconnect().await; @@ -508,11 +627,26 @@ impl MotorController { state.error = Some(error.to_string()); } } + if was_connected { + let (level, error) = match &result { + Ok(()) => (DiagnosticLevel::Info, None), + Err(error) => (DiagnosticLevel::Warning, Some(error.to_string())), + }; + self.inner + .events + .emit( + level, + "motor.disconnected", + "Bluetooth motor disconnected".to_owned(), + json!({ "device": device, "error": error }), + ) + .await; + } result } - fn record_error(&self, error: MotorError) -> Result { - record_motor_error(&self.inner, &error); + async fn record_error(&self, error: MotorError) -> Result { + record_motor_error(&self.inner, &error).await; Err(error) } @@ -587,7 +721,9 @@ fn mark_stopped(state: &mut State) { state.stopping = false; } -fn begin_stop(state: &mut State) { +/// Returns whether this call started ramping a running motor down. +fn begin_stop(state: &mut State) -> bool { + let began = state.running && !state.stopping; if !state.stopping { state.stop_epoch += 1; state.stopping = true; @@ -597,33 +733,42 @@ fn begin_stop(state: &mut State) { if !state.running { state.stopping = false; } + began } -fn record_motor_error(inner: &Inner, error: &MotorError) { - let mut state = lock(&inner.state); - state.connected = false; - state.expires_at = None; - state.error = Some(error.to_string()); - tracing::error!(%error, "Bluetooth motor state is unconfirmed"); +async fn record_motor_error(inner: &Inner, error: &MotorError) { + { + let mut state = lock(&inner.state); + state.connected = false; + state.expires_at = None; + state.error = Some(error.to_string()); + } + inner + .events + .emit( + DiagnosticLevel::Error, + "motor.error", + format!("Bluetooth motor state is unconfirmed: {error}"), + json!({ "error": error.to_string() }), + ) + .await; } async fn advance_motor( inner: &Inner, step: Duration, ) -> Result<(), MotorError> { - let (command, finished) = { + let (command, finished, expired) = { let mut state = lock(&inner.state); if !state.connected { return Err(MotorError::Disconnected); } - if state + let expired = state .expires_at .is_some_and(|expiry| Instant::now() >= expiry) - { - begin_stop(&mut state); - } + && begin_stop(&mut state); if !state.running { - (None, false) + (None, false, expired) } else { // Never catch up after a stalled task/link with a large command jump. let now = Instant::now(); @@ -643,9 +788,12 @@ async fn advance_motor( None }; state.speed_percent = speed; - (command, finished) + (command, finished, expired) } }; + if expired { + inner.events.stop_requested("command_expired").await; + } let result = async { let mut backend = inner.backend.lock().await; if let Some(command) = command { @@ -659,11 +807,20 @@ async fn advance_motor( } .await; if let Err(error) = result { - record_motor_error(inner, &error); + record_motor_error(inner, &error).await; return Err(error); } if finished { mark_stopped(&mut lock(&inner.state)); + inner + .events + .emit( + DiagnosticLevel::Info, + "motor.stopped", + "Motor stopped".to_owned(), + json!({}), + ) + .await; } Ok(()) } @@ -867,8 +1024,11 @@ pub use bluetooth::BtleplugBackend; pub type BleMotorController = MotorController; impl MotorController { - pub async fn bluetooth(config: MotorConfig) -> Result { - Self::with_backend(BtleplugBackend::new().await?, config) + pub async fn bluetooth( + config: MotorConfig, + journal: JournalHandle, + ) -> Result { + Self::with_backend(BtleplugBackend::new().await?, config, Some(journal)) } } @@ -941,7 +1101,7 @@ mod tests { ..MotorConfig::default() }; ( - MotorController::with_backend(backend, config).unwrap(), + MotorController::with_backend(backend, config, None).unwrap(), calls, ) } @@ -1221,7 +1381,7 @@ mod tests { let backend = MockBackend::default(); let failures = backend.fail_writes.clone(); let calls = backend.calls.clone(); - let motor = MotorController::with_backend(backend, MotorConfig::default()).unwrap(); + let motor = MotorController::with_backend(backend, MotorConfig::default(), None).unwrap(); motor.connect().await.unwrap(); motor .set_running("motor-1", 100, MotorDirection::Forward, 10_000) @@ -1443,6 +1603,90 @@ mod tests { assert!(!motor.status().running); } + #[tokio::test(start_paused = true)] + async fn journals_state_changes_but_not_lease_renewals() { + let directory = tempfile::TempDir::new().unwrap(); + let (journal, task) = crate::journal::JournalHandle::start( + crate::journal::JournalConfig::for_directory(directory.path(), uuid::Uuid::new_v4()), + ) + .await + .unwrap(); + let backend = MockBackend::default(); + let failures = backend.fail_writes.clone(); + let motor = + MotorController::with_backend(backend, MotorConfig::default(), Some(journal.clone())) + .unwrap(); + motor.connect().await.unwrap(); + for speed in [20, 20, 20, 40] { + motor + .set_running("motor-1", speed, MotorDirection::Forward, 1_000) + .await + .unwrap(); + } + elapse(4_000).await; + motor + .set_running("motor-1", 30, MotorDirection::Forward, 10_000) + .await + .unwrap(); + motor.stop().await.unwrap(); + motor + .set_running("motor-1", 30, MotorDirection::Forward, 10_000) + .await + .unwrap(); + motor.close().await.unwrap(); + motor.connect().await.unwrap(); + *lock(&failures) = true; + motor + .set_running("motor-1", 30, MotorDirection::Forward, 10_000) + .await + .unwrap(); + elapse(600).await; + + let events: Vec<_> = journal + .records_after(0) + .await + .unwrap() + .into_iter() + .filter_map(|record| match record.payload { + crate::records::RecordPayload::Diagnostic(event) => Some(event), + _ => None, + }) + .collect(); + let names: Vec<_> = events.iter().map(|event| event.event.as_str()).collect(); + assert_eq!( + names, + [ + "motor.connected", + "motor.command", + "motor.command", + "motor.stop_requested", + "motor.stopped", + "motor.command", + "motor.stop_requested", + "motor.stopped", + "motor.command", + "motor.stop_requested", + "motor.stopped", + "motor.disconnected", + "motor.connected", + "motor.command", + "motor.error", + ] + ); + let reasons: Vec<_> = events + .iter() + .filter(|event| event.event == "motor.stop_requested") + .map(|event| event.fields["reason"].as_str().unwrap()) + .collect(); + assert_eq!(reasons, ["command_expired", "request", "disconnect"]); + assert_eq!(events[2].fields["speed_percent"], 40); + assert_eq!(events[2].fields["direction"], "forward"); + assert_eq!(events[14].level, DiagnosticLevel::Error); + assert!(events.iter().all(|event| event.source == "linkhub.motor")); + journal.shutdown().await.unwrap(); + task.await.unwrap(); + } + #[tokio::test] async fn http_device_selection_and_leased_commands() { async fn call(app: &Router, method: &str, path: &str, body: Value) -> (StatusCode, Value) {