Files
BikeControl/crates/ble/src/lib.rs
T
dtourolleandClaude Opus 5 7b511db3dc Ride the drivetrain, command the load in watts
Speed now comes from the drivetrain and the load from the road, which is
the way round a bike actually works.

Speed is cadence x development, filtered lightly. Power, not cadence,
decides whether the rider is driving it: on a direct-drive trainer the
flywheel keeps the cranks turning after they stop, so cadence alone reads
a healthy 80 rpm for someone doing nothing. Below 15 W the speed runs
down to whatever the gradient sustains on no power - zero uphill, a real
freewheeling speed on a descent. Stopping on a 3.5% climb used to settle
at 22 km/h and stay there, because the model wanted to decelerate and a
blend toward the flywheel speed outvoted it; that blend is gone.

The D100 sends no cadence over FTMS - it is a rebadged Magene T110 with
cadence disabled in firmware (qdomyos-zwift#3282) - so it is inferred
from wheel speed, which one sprocket and no freewheel make exact. Its
Zwift channel does carry cadence, and is now greeted with RideOn and
subscribed on every notifying characteristic, so a measured value is used
where one arrives.

The load is commanded as power, not gradient. The trainer declares
50-600 W in 1 W steps against 0-6% inclination in 0.1% steps refusing
negatives, and whether it acts on 0x11 at all is still unconfirmed. Its
power target is a ceiling rather than a setpoint, which is very nearly
what a road is: exceed it and the surplus becomes speed. Gravity travels
on the same channel as watts, so nothing is lost by leaving 0x11 alone.
LoadChannel keeps the gradient path selectable and tested.

Virtual shifting reaches the trainer for the first time. The physics
load model was written but never called, and a paddle press both shifted
a gear in Rust and nudged the gradient in the webview - the shift
silently, the tilt visibly, so the paddles looked like a gradient trim.

Also: a fixed 12 W drivetrain loss, held as a power because that is how
it presents; crank length, so a gear can be reported as the force it puts
under the foot; gear and pedal force on the ride screen; a drag-race
profile for testing gearing on the flat.

Two readout bugs fixed on the way. The rolling windows were trimmed by
timestamp but fed on a fixed timer, so every second spent on the ride
screen before starting pushed samples at t=0 that could never expire -
speed read a fraction of the truth for the first 45 s. And the headline
speed was a 45 s mean, which took most of a minute to show a gear change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 18:21:08 +02:00

79 lines
2.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! FTMS client and BLE transport. See REQUIREMENTS.md §5.15.2.
//!
//! # Layout
//!
//! The crate is split so that everything protocol-shaped is a pure function
//! over bytes, and only [`client`] and [`scan`] touch a radio. That is what
//! lets the whole wire format be tested without a trainer on the desk:
//!
//! | Module | Contents | Needs hardware |
//! |--------|----------|----------------|
//! | [`uuids`] | FTMS assigned numbers | no |
//! | [`indoor_bike_data`] | `0x2AD2` decoder | no |
//! | [`control_point`] | `0x2AD9` encoders and response decoding | no |
//! | [`capabilities`] | `0x2ACC`/`0x2AD5`/`0x2AD6`/`0x2AD8` decoding, and the safety gate | no |
//! | [`zwift`] | Zwift's proprietary protocol (§2.3.1) | no |
//! | [`scan`] | discovery | yes |
//! | [`client`] | the connection actor | yes |
//!
//! # Usage
//!
//! ```no_run
//! use bikecontrol_ble::{FtmsClient, FtmsConfig, TrainerSelector};
//! use bikecontrol_core::types::ControlTarget;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = FtmsClient::connect(TrainerSelector::Any, FtmsConfig::default()).await?;
//!
//! let mut telemetry = client.telemetry();
//! tokio::spawn(async move {
//! while let Ok(sample) = telemetry.recv().await {
//! println!("{:?} W", sample.power_w);
//! }
//! });
//!
//! client.set_target(ControlTarget::Gradient { percent: 4.0 }).await?;
//!
//! // SAF-2: always leave the trainer at zero.
//! client.shutdown().await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Attribution
//!
//! The FTMS field ordering, scaling and control-point encodings are ported from
//! [`obostjancic/smart-trainer-control`](https://github.com/obostjancic/smart-trainer-control),
//! MIT licensed, Copyright (c) 2025 Ogi — a working Van Rysel D100 client
//! (REQUIREMENTS.md §3.2). Per-module attribution notes mark where.
pub mod capabilities;
pub mod click;
pub mod client;
pub mod control_point;
pub mod error;
pub mod indoor_bike_data;
pub mod scan;
pub mod uuids;
pub mod zwift;
pub use click::{ClickClient, ClickConfig, ClickEvent, PodSelector};
pub use capabilities::{
FitnessMachineFeature, InclinationRange, PowerRange, ResistanceLevelRange, TrainerCapabilities,
UnsupportedTarget,
};
pub use client::{
safety_reset_commands, Backoff, ControlOutcome, FtmsClient, FtmsConfig, FtmsEvent, Procedure,
};
pub use control_point::{ControlPointResponse, OpCode, ResultCode, SimulationParameters, StopOrPause};
pub use error::FtmsError;
pub use indoor_bike_data::{DecodeError, IndoorBikeData};
pub use scan::{
default_adapter, scan, scan_trainers, DiscoveredDevice, ScanKind, TrainerSelector,
};
pub use uuids::FITNESS_MACHINE_SERVICE;
pub use zwift::{
Button, ButtonBitmask, ClickButtons, DeviceKind as ZwiftDeviceKind,
MessageType as ZwiftMessageType, PodId,
};