crazyflie_lib/subsystems/
high_level_commander.rs

1//! # High-level commander subsystem
2//!
3//! This subsystem is responsible for managing high-level commands and setpoints for the Crazyflie.
4//! It builds on top of the (low-level) [`crate::subsystems::commander::Commander`] subsystem and provides a more user-friendly interface
5//! for controlling the drone's behavior.
6
7use crazyflie_link::Packet;
8use flume::Sender;
9use tokio::time::{sleep, Duration};
10
11use crate::{Error, Result};
12
13use crate::crazyflie::HL_COMMANDER_PORT;
14
15
16// Command type identifiers
17const COMMAND_SET_GROUP_MASK: u8 = 0;
18const COMMAND_STOP: u8 = 3;
19const COMMAND_DEFINE_TRAJECTORY: u8 = 6;
20const COMMAND_TAKEOFF_2: u8 = 7;
21const COMMAND_LAND_2: u8 = 8;
22const COMMAND_SPIRAL: u8 = 11;
23const COMMAND_GO_TO_2: u8 = 12;
24const COMMAND_START_TRAJECTORY_2: u8 = 13;
25
26/// This mask is used to specify that all Crazyflies should respond to the command.
27pub const ALL_GROUPS: u8 = 0;
28
29const TRAJECTORY_LOCATION_MEM: u8 = 1;
30
31/// 4D polynomial trajectory
32pub const TRAJECTORY_TYPE_POLY4D: u8 = 0;
33/// Compressed 4D polynomial trajectory
34pub const TRAJECTORY_TYPE_POLY4D_COMPRESSED: u8 = 1;
35
36
37/// High-level commander interface for a Crazyflie.
38///
39/// The high-level commander is a firmware module that generates smooth
40/// position setpoints from high-level actions such as *take-off*, *go-to*,
41/// *spiral*, and *land*. Internally it plans trajectories (polynomial-based)
42/// that are executed by the Crazyflie.
43///
44/// This Rust type provides an asynchronous, remote client for that module:
45/// it builds and sends the required packets over the high-level commander port,
46/// exposing a small set of ergonomic methods. When using trajectory functions,
47/// ensure the trajectory data has been uploaded to the Crazyflie's memory first.
48///
49/// # Command execution model
50/// Movement commands ([`take_off`](Self::take_off), [`land`](Self::land),
51/// [`go_to`](Self::go_to), [`spiral`](Self::spiral)) immediately trigger execution
52/// on the Crazyflie and then block for their `duration` to simplify command sequencing.
53/// The Crazyflie executes these commands autonomously—if the Rust future is cancelled
54/// or the connection drops, the drone will continue executing the command until completion.
55///
56/// # Notes
57/// The high-level commander can be preempted at any time by setpoints from the commander.
58/// To return control to the high-level commander, see [`crate::subsystems::commander::Commander::notify_setpoint_stop`].
59///
60/// A `HighLevelCommander` is typically obtained from a [`crate::Crazyflie`] instance.
61///
62/// # Safe usage pattern
63/// ```no_run
64/// # use crazyflie_link::LinkContext;
65/// # use crazyflie_lib::Crazyflie;
66/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
67/// # let context = LinkContext::new();
68/// # let cf = Crazyflie::connect_from_uri(&context, "radio://0/80/2M/E7E7E7E7E7").await?;
69/// // Continue flight sequence even if commands fail
70/// if let Err(e) = cf.high_level_commander.take_off(0.5, None, 2.0, None).await {
71///     eprintln!("Take-off failed: {e}");
72/// }
73///
74/// if let Err(e) = cf.high_level_commander.land(0.0, None, 2.0, None).await {
75///     eprintln!("Landing failed: {e}");
76/// }
77/// # Ok(())
78/// # }
79/// ```
80#[derive(Debug)]
81pub struct HighLevelCommander {
82    uplink: Sender<Packet>,
83}
84
85/// Constructor methods.
86impl HighLevelCommander {
87    /// Create a new HighLevelCommander
88    pub fn new(uplink: Sender<Packet>) -> Self {
89        Self { uplink }
90    }
91}
92
93/// Group mask related commands.
94impl HighLevelCommander {
95    /// Set the group mask for the high-level commander.
96    ///
97    /// # Arguments
98    /// * `group_mask` - The group mask to set. Use `ALL_GROUPS` to set the mask for all Crazyflies.
99    pub async fn set_group_mask(&self, group_mask: u8) -> Result<()> {
100        let mut payload = Vec::with_capacity(2);
101        payload.push(COMMAND_SET_GROUP_MASK);
102        payload.push(group_mask);
103
104        let pk = Packet::new(HL_COMMANDER_PORT, 0, payload);
105
106        self.uplink
107            .send_async(pk)
108            .await
109            .map_err(|_| Error::Disconnected)?;
110        Ok(())
111    }
112}
113
114/// High-level movement commands.
115///
116/// # Warning
117/// Avoid overlapping movement commands. When a command is sent to a Crazyflie
118/// while another is currently executing, the generated polynomial can take
119/// unexpected routes and have high peaks.
120impl HighLevelCommander {
121    /// Take off vertically from the current x-y position to the given target height.
122    ///
123    /// # Arguments
124    /// * `height` - Target height (meters) above the world origin.
125    /// * `yaw` - Target yaw (radians). Use `None` to maintain the current yaw.
126    /// * `duration` - Time (seconds) to reach the target height. This method blocks for this duration.
127    /// * `group_mask` - Bitmask selecting which Crazyflies to command. Use `None` for all Crazyflies.
128    pub async fn take_off(&self, height: f32, yaw: Option<f32>, duration: f32, group_mask: Option<u8>) -> Result<()> {
129        let use_current_yaw = yaw.is_none();
130        let target_yaw = yaw.unwrap_or(0.0);
131
132        let group_mask_value = group_mask.unwrap_or(ALL_GROUPS);
133
134        let mut payload = Vec::with_capacity(3 + 3 * 4);
135        payload.push(COMMAND_TAKEOFF_2);
136        payload.push(group_mask_value);
137        payload.extend_from_slice(&height.to_le_bytes());
138        payload.extend_from_slice(&target_yaw.to_le_bytes());
139        payload.push(use_current_yaw as u8);
140        payload.extend_from_slice(&duration.to_le_bytes());
141
142        let pk = Packet::new(HL_COMMANDER_PORT, 0, payload);
143
144        self.uplink
145            .send_async(pk)
146            .await
147           .map_err(|_| Error::Disconnected)?;
148
149        sleep(Duration::from_secs_f32(duration)).await;
150        Ok(())
151    }
152
153    /// Land vertically from the current x-y position to the given target height.
154    ///
155    /// # Arguments
156    /// * `height` - Target height (meters) above the world origin.
157    /// * `yaw` - Target yaw (radians). Use `None` to maintain the current yaw.
158    /// * `duration` - Time (seconds) to reach the target height. This method blocks for this duration.
159    /// * `group_mask` - Bitmask selecting which Crazyflies to command. Use `None` for all Crazyflies.
160    pub async fn land(&self, height: f32, yaw: Option<f32>, duration: f32, group_mask: Option<u8>) -> Result<()> {
161        let use_current_yaw = yaw.is_none();
162        let target_yaw = yaw.unwrap_or(0.0);
163
164        let group_mask_value = group_mask.unwrap_or(ALL_GROUPS);
165
166        let mut payload = Vec::with_capacity(3 + 3 * 4);
167        payload.push(COMMAND_LAND_2);
168        payload.push(group_mask_value);
169        payload.extend_from_slice(&height.to_le_bytes());
170        payload.extend_from_slice(&target_yaw.to_le_bytes());
171        payload.push(use_current_yaw as u8);
172        payload.extend_from_slice(&duration.to_le_bytes());
173
174        let pk = Packet::new(HL_COMMANDER_PORT, 0, payload);
175
176        self.uplink
177            .send_async(pk)
178            .await
179            .map_err(|_| Error::Disconnected)?;
180
181        sleep(Duration::from_secs_f32(duration)).await;
182        Ok(())
183    }
184
185    /// Stop the current high-level command and disable motors.
186    ///
187    /// This immediately halts any active high-level command (takeoff, land, go_to, spiral, 
188    /// or trajectory execution) and stops motor output.
189    ///
190    /// # Arguments
191    /// * `group_mask` - Bitmask selecting which Crazyflies to command. Use `None` for all Crazyflies.
192    pub async fn stop(&self, group_mask: Option<u8>) -> Result<()> {
193        let group_mask_value = group_mask.unwrap_or(ALL_GROUPS);
194
195        let mut payload = Vec::with_capacity(2);
196        payload.push(COMMAND_STOP);
197        payload.push(group_mask_value);
198
199        let pk = Packet::new(HL_COMMANDER_PORT, 0, payload);
200
201        self.uplink
202            .send_async(pk)
203            .await
204            .map_err(|_| Error::Disconnected)?;
205        Ok(())
206    }
207
208    /// Move to an absolute or relative position with smooth path planning.
209    ///
210    /// The path is designed to transition smoothly from the current state to the target
211    /// position, gradually decelerating at the goal with minimal overshoot. When the 
212    /// system is at hover, the path will be a straight line, but if there is any initial
213    /// velocity, the path will be a smooth curve.
214    ///
215    /// The trajectory is derived by solving for a unique 7th-degree polynomial that
216    /// satisfies the initial conditions of position, velocity, and acceleration, and
217    /// ends at the goal with zero velocity and acceleration. Additionally, the jerk
218    /// (derivative of acceleration) is constrained to be zero at both the starting
219    /// and ending points.
220    ///
221    /// # Arguments
222    /// * `x` - Target x-position in meters
223    /// * `y` - Target y-position in meters
224    /// * `z` - Target z-position in meters
225    /// * `yaw` - Target yaw angle in radians
226    /// * `duration` - Time in seconds to reach the target position. This method blocks for this duration.
227    /// * `relative` - If `true`, positions and yaw are relative to current position; if `false`, absolute
228    /// * `linear` - If `true`, use linear interpolation; if `false`, use polynomial trajectory
229    /// * `group_mask` - Bitmask selecting which Crazyflies to command. Use `None` for all Crazyflies.
230    pub async fn go_to(&self, x: f32, y: f32, z: f32, yaw: f32, duration: f32, relative: bool, linear: bool, group_mask: Option<u8>) -> Result<()> {
231        let group_mask_value = group_mask.unwrap_or(ALL_GROUPS);
232
233        let mut payload = Vec::with_capacity(4 + 5 * 4);
234        payload.push(COMMAND_GO_TO_2);
235        payload.push(group_mask_value);
236        payload.push(relative as u8);
237        payload.push(linear as u8);
238        payload.extend_from_slice(&x.to_le_bytes());
239        payload.extend_from_slice(&y.to_le_bytes());
240        payload.extend_from_slice(&z.to_le_bytes());
241        payload.extend_from_slice(&yaw.to_le_bytes());
242        payload.extend_from_slice(&duration.to_le_bytes());
243
244        let pk = Packet::new(HL_COMMANDER_PORT, 0, payload);
245
246        self.uplink
247            .send_async(pk)
248            .await
249            .map_err(|_| Error::Disconnected)?;
250
251        sleep(Duration::from_secs_f32(duration)).await;
252        Ok(())
253    }
254
255    /// Fly a spiral segment.
256    ///
257    /// The Crazyflie moves along an arc around a computed center point, sweeping
258    /// through an angle of up to ±2π (one full turn). While sweeping, the radius
259    /// changes linearly from `initial_radius` to `final_radius`. If the radii are
260    /// equal, the path is a circular arc; if they differ, the path spirals inward
261    /// or outward accordingly. Altitude changes linearly by `altitude_gain` over
262    /// the duration.
263    ///
264    /// # Center placement
265    /// The spiral center is placed differently depending on `sideways` and `clockwise`:
266    /// * `sideways = false`
267    ///   * `clockwise = true`  → center lies to the **right** of the current heading.
268    ///   * `clockwise = false` → center lies to the **left** of the current heading.
269    /// * `sideways = true`
270    ///   * `clockwise = true`  → center lies **ahead** of the current heading.
271    ///   * `clockwise = false` → center lies **behind** the current heading.
272    ///
273    /// # Orientation
274    /// * `sideways = false`: the Crazyflie’s heading follows the tangent of the
275    ///   spiral (flies forward along the path).
276    /// * `sideways = true`: the Crazyflie’s heading points toward the spiral center
277    ///   while circling around it (flies sideways along the path).
278    ///
279    /// # Direction conventions
280    /// * `clockwise` chooses on which side the center is placed.
281    /// * The **sign of `angle`** sets the travel direction along the arc:
282    ///   `angle > 0` sweeps one way; `angle < 0` traverses the arc in the opposite
283    ///   direction (i.e., “backwards”). This can make some combinations appear
284    ///   counterintuitive—for example, `sideways = false`, `clockwise = true`,
285    ///   `angle < 0` will *look* counter-clockwise from above.
286    ///
287    /// # Arguments
288    /// * `angle` - Total spiral angle in radians (limited to ±2π).
289    /// * `initial_radius` - Starting radius in meters (≥ 0).
290    /// * `final_radius` - Ending radius in meters (≥ 0).
291    /// * `altitude_gain` - Vertical displacement in meters (positive = climb,
292    ///   negative = descent).
293    /// * `duration` - Time in seconds to complete the spiral. This method blocks for this duration.
294    /// * `sideways` - If `true`, heading points toward the spiral center;
295    ///   if `false`, heading follows the spiral tangent.
296    /// * `clockwise` - If `true`, fly clockwise; otherwise counter-clockwise.
297    /// * `group_mask` - Bitmask selecting which Crazyflies this applies to.
298    ///
299    /// # Errors
300    /// Returns [`Error::InvalidArgument`] if any parameters are out of range,
301    /// or [`Error::Disconnected`] if the command cannot be sent.
302    pub async fn spiral(&self, angle: f32, initial_radius: f32, final_radius: f32, altitude_gain: f32, duration: f32, sideways: bool, clockwise: bool, group_mask: Option<u8>) -> Result<()> {
303        // Check if all arguments are within range
304        if angle.abs() > 2.0 * std::f32::consts::PI {
305            return Err(Error::InvalidArgument("angle out of range".to_string()));
306        }
307        if initial_radius < 0.0 {
308            return Err(Error::InvalidArgument("initial_radius must be >= 0".to_string()));
309        }
310        if final_radius < 0.0 {
311            return Err(Error::InvalidArgument("final_radius must be >= 0".to_string()));
312        }
313
314        let group_mask_value = group_mask.unwrap_or(ALL_GROUPS);
315
316        let mut payload = Vec::with_capacity(4 + 5 * 4);
317        payload.push(COMMAND_SPIRAL);
318        payload.push(group_mask_value);
319        payload.push(sideways as u8);
320        payload.push(clockwise as u8);
321        payload.extend_from_slice(&angle.to_le_bytes());
322        payload.extend_from_slice(&initial_radius.to_le_bytes());
323        payload.extend_from_slice(&final_radius.to_le_bytes());
324        payload.extend_from_slice(&altitude_gain.to_le_bytes());
325        payload.extend_from_slice(&duration.to_le_bytes());
326
327        let pk = Packet::new(HL_COMMANDER_PORT, 0, payload);
328
329        self.uplink
330            .send_async(pk)
331            .await
332            .map_err(|_| Error::Disconnected)?;
333
334        sleep(Duration::from_secs_f32(duration)).await;
335        Ok(())
336    }
337}
338
339
340/// Trajectory implementations
341impl HighLevelCommander {
342    /// Define a trajectory previously uploaded to memory.
343    ///
344    /// # Arguments
345    /// * `trajectory_id` - Identifier used to reference this trajectory later.
346    /// * `memory_offset` - Byte offset into trajectory memory where the data begins.
347    /// * `piece_count` - Number of segments (pieces) in the trajectory.
348    /// * `trajectory_type` - Type of the trajectory data (e.g. Poly4D).
349    ///
350    /// # Errors
351    /// Returns [`Error::Disconnected`] if the command cannot be sent.
352    pub async fn define_trajectory(&self, trajectory_id: u8, memory_offset: u32, num_pieces: u8, trajectory_type: Option<u8>) -> Result<()> {
353        let trajectory_type_value = trajectory_type.unwrap_or(TRAJECTORY_TYPE_POLY4D);
354
355        let mut payload = Vec::with_capacity(5 + 1 * 4);
356        payload.push(COMMAND_DEFINE_TRAJECTORY);
357        payload.push(trajectory_id);
358        payload.push(TRAJECTORY_LOCATION_MEM);
359        payload.push(trajectory_type_value);
360        payload.extend_from_slice(&memory_offset.to_le_bytes());
361        payload.push(num_pieces);
362
363        let pk = Packet::new(HL_COMMANDER_PORT, 0, payload);
364
365        self.uplink
366            .send_async(pk)
367            .await
368            .map_err(|_| Error::Disconnected)?;
369        Ok(())
370    }
371
372    /// Start executing a previously defined trajectory.
373    ///
374    /// The trajectory is identified by `trajectory_id` and can be modified
375    /// at execution time by scaling its speed, shifting its position, aligning
376    /// its yaw, or running it in reverse.
377    ///
378    /// # Arguments
379    /// * `trajectory_id` - Identifier of the trajectory (as defined with [`HighLevelCommander::define_trajectory`]).
380    /// * `time_scale` - Time scaling factor; `1.0` = original speed,
381    ///   values >1.0 slow down, values <1.0 speed up.
382    /// * `relative_position` - If `true`, shift trajectory to the current setpoint position.
383    /// * `relative_yaw` - If `true`, align trajectory yaw to the current yaw.
384    /// * `reversed` - If `true`, execute the trajectory in reverse.
385    /// * `group_mask` - Mask selecting which Crazyflies this applies to.
386    ///   If `None`, defaults to all Crazyflies.
387    ///
388    /// # Errors
389    /// Returns [`Error::Disconnected`] if the command cannot be sent.
390    pub async fn start_trajectory(&self, trajectory_id: u8, time_scale: f32, relative_position: bool, relative_yaw: bool, reversed: bool, group_mask: Option<u8>) -> Result<()> {
391        let group_mask_value = group_mask.unwrap_or(ALL_GROUPS);
392
393        let mut payload = Vec::with_capacity(5 + 1 * 4);
394        payload.push(COMMAND_START_TRAJECTORY_2);
395        payload.push(group_mask_value);
396        payload.push(relative_position as u8);
397        payload.push(relative_yaw as u8);
398        payload.push(reversed as u8);
399        payload.push(trajectory_id);
400        payload.extend_from_slice(&time_scale.to_le_bytes());
401
402        let pk = Packet::new(HL_COMMANDER_PORT, 0, payload);
403
404        self.uplink
405            .send_async(pk)
406            .await
407            .map_err(|_| Error::Disconnected)?;
408        Ok(())
409    }
410}