crazyflie-lib 0.7.1

Crazyflie quadcopter control lib
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Trajectory memory for the Crazyflie high level commander
//!
//! This module provides types and functionality for defining and uploading
//! trajectories to the Crazyflie's trajectory memory. Trajectories can be
//! either uncompressed (using `Poly4D`) or compressed (using `CompressedStart`
//! and `CompressedSegment`).

use crate::{Error, Result, subsystems::memory::{MemoryBackend, memory_types}};
use memory_types::{FromMemoryBackend, MemoryType};

/// Encode a spatial coordinate (meters) to millimeters as i16
///
/// Valid range: approximately -32.767 to +32.767 meters
fn encode_spatial(coordinate: f32) -> Result<i16> {
    let scaled = coordinate * 1000.0;
    if scaled < i16::MIN as f32 || scaled > i16::MAX as f32 {
        return Err(Error::InvalidArgument(
            format!("Spatial coordinate {:.3}m out of representable range ({:.3}m to {:.3}m)",
                coordinate, i16::MIN as f32 / 1000.0, i16::MAX as f32 / 1000.0)
        ));
    }
    Ok(scaled as i16)
}

/// Encode a yaw angle (radians) to 1/10th degrees as i16
///
/// Valid range: approximately -573.2 to +573.2 degrees (-10.0 to +10.0 radians)
fn encode_yaw(angle_rad: f32) -> Result<i16> {
    let scaled = angle_rad.to_degrees() * 10.0;
    if scaled < i16::MIN as f32 || scaled > i16::MAX as f32 {
        return Err(Error::InvalidArgument(
            format!("Yaw angle {:.3} rad out of representable range", angle_rad)
        ));
    }
    Ok(scaled as i16)
}

/// A polynomial with up to 8 coefficients
#[derive(Debug, Clone)]
pub struct Poly {
    /// The polynomial coefficients (up to 8)
    pub values: [f32; 8],
}

impl Default for Poly {
    fn default() -> Self {
        Self { values: [0.0; 8] }
    }
}

impl Poly {
    /// Create a new polynomial with the given coefficients
    pub fn new(values: [f32; 8]) -> Self {
        Self { values }
    }

    /// Create a polynomial from a slice of values
    ///
    /// If the slice has fewer than 8 values, the remaining coefficients are set to 0.
    /// If the slice has more than 8 values, only the first 8 are used.
    pub fn from_slice(values: &[f32]) -> Self {
        let mut poly = Self::default();
        let len = values.len().min(8);
        poly.values[..len].copy_from_slice(&values[..len]);
        poly
    }
}

/// A 4D polynomial trajectory segment (uncompressed format)
///
/// This represents a single segment of a trajectory defined by polynomials
/// for x, y, z, and yaw coordinates over a duration of time.
#[derive(Debug, Clone)]
pub struct Poly4D {
    /// Duration of this segment in seconds
    pub duration: f32,
    /// Polynomial for x coordinate
    pub x: Poly,
    /// Polynomial for y coordinate
    pub y: Poly,
    /// Polynomial for z coordinate
    pub z: Poly,
    /// Polynomial for yaw angle
    pub yaw: Poly,
}

impl Poly4D {
    /// Create a new Poly4D trajectory segment
    pub fn new(duration: f32, x: Poly, y: Poly, z: Poly, yaw: Poly) -> Self {
        Self { duration, x, y, z, yaw }
    }

    /// Pack this segment into bytes for transmission
    pub fn pack(&self) -> Vec<u8> {
        let mut data = Vec::with_capacity(132); // 8*4*4 + 4 = 132 bytes

        // Pack x coefficients (8 * f32)
        for &v in &self.x.values {
            data.extend_from_slice(&v.to_le_bytes());
        }
        // Pack y coefficients (8 * f32)
        for &v in &self.y.values {
            data.extend_from_slice(&v.to_le_bytes());
        }
        // Pack z coefficients (8 * f32)
        for &v in &self.z.values {
            data.extend_from_slice(&v.to_le_bytes());
        }
        // Pack yaw coefficients (8 * f32)
        for &v in &self.yaw.values {
            data.extend_from_slice(&v.to_le_bytes());
        }
        // Pack duration (f32)
        data.extend_from_slice(&self.duration.to_le_bytes());

        data
    }
}

/// Starting point for a compressed trajectory
///
/// Compressed trajectories begin with a `CompressedStart` that defines
/// the initial position and yaw, followed by `CompressedSegment`s.
#[derive(Debug, Clone)]
pub struct CompressedStart {
    /// X coordinate in meters
    pub x: f32,
    /// Y coordinate in meters
    pub y: f32,
    /// Z coordinate in meters
    pub z: f32,
    /// Yaw angle in radians
    pub yaw: f32,
}

impl CompressedStart {
    /// Create a new compressed start point
    pub fn new(x: f32, y: f32, z: f32, yaw: f32) -> Self {
        Self { x, y, z, yaw }
    }

    /// Pack this start point into bytes for transmission
    ///
    /// Returns an error if any coordinate is out of the representable range.
    pub fn pack(&self) -> Result<Vec<u8>> {
        let mut data = Vec::with_capacity(8);

        data.extend_from_slice(&encode_spatial(self.x)?.to_le_bytes());
        data.extend_from_slice(&encode_spatial(self.y)?.to_le_bytes());
        data.extend_from_slice(&encode_spatial(self.z)?.to_le_bytes());
        data.extend_from_slice(&encode_yaw(self.yaw)?.to_le_bytes());

        Ok(data)
    }
}

/// Type of polynomial element in a compressed segment
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ElementType {
    /// No movement (0 coefficients)
    Constant = 0,
    /// Linear (1 coefficient)
    Linear = 1,
    /// Quadratic (3 coefficients)
    Quadratic = 2,
    /// Full (7 coefficients)
    Full = 3,
}

impl ElementType {
    fn from_len(len: usize) -> Option<Self> {
        match len {
            0 => Some(ElementType::Constant),
            1 => Some(ElementType::Linear),
            3 => Some(ElementType::Quadratic),
            7 => Some(ElementType::Full),
            _ => None,
        }
    }
}

/// A segment in a compressed trajectory
///
/// Compressed segments use variable-length encoding where each axis
/// can have 0, 1, 3, or 7 coefficients depending on the complexity
/// of motion along that axis.
#[derive(Debug, Clone)]
pub struct CompressedSegment {
    duration: f32,
    x: Vec<f32>,
    y: Vec<f32>,
    z: Vec<f32>,
    yaw: Vec<f32>,
}

impl CompressedSegment {
    /// Create a new compressed segment
    ///
    /// # Arguments
    /// * `duration` - Duration of this segment in seconds
    /// * `x` - X polynomial coefficients (must be 0, 1, 3, or 7 elements)
    /// * `y` - Y polynomial coefficients (must be 0, 1, 3, or 7 elements)
    /// * `z` - Z polynomial coefficients (must be 0, 1, 3, or 7 elements)
    /// * `yaw` - Yaw polynomial coefficients (must be 0, 1, 3, or 7 elements)
    ///
    /// # Errors
    /// Returns an error if any element vector has an invalid length
    pub fn new(duration: f32, x: Vec<f32>, y: Vec<f32>, z: Vec<f32>, yaw: Vec<f32>) -> Result<Self> {
        Self::validate(&x)?;
        Self::validate(&y)?;
        Self::validate(&z)?;
        Self::validate(&yaw)?;

        Ok(Self { duration, x, y, z, yaw })
    }

    fn validate(element: &[f32]) -> Result<()> {
        let len = element.len();
        if len != 0 && len != 1 && len != 3 && len != 7 {
            return Err(Error::InvalidArgument(
                "Element length must be 0, 1, 3, or 7".to_owned()
            ));
        }
        Ok(())
    }

    fn encode_type(element: &[f32]) -> u8 {
        // Safe: fields are validated in new()
        ElementType::from_len(element.len()).unwrap() as u8
    }

    fn pack_spatial_element(element: &[f32]) -> Result<Vec<u8>> {
        let mut data = Vec::new();
        for &v in element {
            data.extend_from_slice(&encode_spatial(v)?.to_le_bytes());
        }
        Ok(data)
    }

    fn pack_yaw_element(element: &[f32]) -> Result<Vec<u8>> {
        let mut data = Vec::new();
        for &v in element {
            data.extend_from_slice(&encode_yaw(v)?.to_le_bytes());
        }
        Ok(data)
    }

    /// Pack this segment into bytes for transmission
    ///
    /// Returns an error if any coordinate is out of the representable range.
    pub fn pack(&self) -> Result<Vec<u8>> {
        let element_types = Self::encode_type(&self.x)
            | (Self::encode_type(&self.y) << 2)
            | (Self::encode_type(&self.z) << 4)
            | (Self::encode_type(&self.yaw) << 6);
        let duration_ms = (self.duration * 1000.0) as u16;

        let mut data = Vec::new();

        data.push(element_types);
        data.extend_from_slice(&duration_ms.to_le_bytes());
        data.extend(Self::pack_spatial_element(&self.x)?);
        data.extend(Self::pack_spatial_element(&self.y)?);
        data.extend(Self::pack_spatial_element(&self.z)?);
        data.extend(Self::pack_yaw_element(&self.yaw)?);

        Ok(data)
    }
}

/// Memory interface for trajectories used by the high level commander
///
/// Trajectories can be either uncompressed (using `Poly4D` segments) or
/// compressed (using `CompressedStart` followed by `CompressedSegment`s).
/// Use `write_uncompressed` for Poly4D trajectories and `write_compressed`
/// for compressed trajectories.
#[derive(Debug)]
pub struct TrajectoryMemory {
    memory: MemoryBackend,
}

impl FromMemoryBackend for TrajectoryMemory {
    async fn from_memory_backend(memory: MemoryBackend) -> Result<Self> {
        if memory.memory_type == MemoryType::Trajectory {
            Ok(Self { memory })
        } else {
            Err(Error::MemoryError("Wrong type of memory!".to_owned()))
        }
    }

    async fn initialize_memory_backend(memory: MemoryBackend) -> Result<Self> {
        if memory.memory_type == MemoryType::Trajectory {
            Ok(Self { memory })
        } else {
            Err(Error::MemoryError("Wrong type of memory!".to_owned()))
        }
    }

    fn close_memory(self) -> MemoryBackend {
        self.memory
    }
}

impl TrajectoryMemory {
    /// Write an uncompressed trajectory (Poly4D segments) to the Crazyflie
    ///
    /// # Arguments
    /// * `segments` - A slice of Poly4D trajectory segments
    /// * `start_addr` - The address in trajectory memory to upload to (0 by default)
    ///
    /// # Returns
    /// The number of bytes written
    pub async fn write_uncompressed(
        &self,
        segments: &[Poly4D],
        start_addr: usize,
    ) -> Result<usize> {
        let mut data = Vec::new();
        for segment in segments {
            data.extend(segment.pack());
        }

        self.memory.write::<fn(usize, usize)>(start_addr, &data, None).await?;
        Ok(data.len())
    }

    /// Write an uncompressed trajectory with progress reporting
    ///
    /// # Arguments
    /// * `segments` - A slice of Poly4D trajectory segments
    /// * `start_addr` - The address in trajectory memory to upload to (0 by default)
    /// * `progress_callback` - Called with (bytes_written, total_bytes)
    ///
    /// # Returns
    /// The number of bytes written
    pub async fn write_uncompressed_with_progress<F>(
        &self,
        segments: &[Poly4D],
        start_addr: usize,
        progress_callback: F,
    ) -> Result<usize>
    where
        F: FnMut(usize, usize),
    {
        let mut data = Vec::new();
        for segment in segments {
            data.extend(segment.pack());
        }

        self.memory.write(start_addr, &data, Some(progress_callback)).await?;
        Ok(data.len())
    }

    /// Write a compressed trajectory to the Crazyflie
    ///
    /// Compressed trajectories must start with a `CompressedStart` followed
    /// by zero or more `CompressedSegment`s.
    ///
    /// # Arguments
    /// * `start` - The starting point of the trajectory
    /// * `segments` - A slice of compressed trajectory segments
    /// * `start_addr` - The address in trajectory memory to upload to (0 by default)
    ///
    /// # Returns
    /// The number of bytes written
    pub async fn write_compressed(
        &self,
        start: &CompressedStart,
        segments: &[CompressedSegment],
        start_addr: usize,
    ) -> Result<usize> {
        let mut data = start.pack()?;
        for segment in segments {
            data.extend(segment.pack()?);
        }

        self.memory.write::<fn(usize, usize)>(start_addr, &data, None).await?;
        Ok(data.len())
    }

    /// Write a compressed trajectory with progress reporting
    ///
    /// # Arguments
    /// * `start` - The starting point of the trajectory
    /// * `segments` - A slice of compressed trajectory segments
    /// * `start_addr` - The address in trajectory memory to upload to (0 by default)
    /// * `progress_callback` - Called with (bytes_written, total_bytes)
    ///
    /// # Returns
    /// The number of bytes written
    pub async fn write_compressed_with_progress<F>(
        &self,
        start: &CompressedStart,
        segments: &[CompressedSegment],
        start_addr: usize,
        progress_callback: F,
    ) -> Result<usize>
    where
        F: FnMut(usize, usize),
    {
        let mut data = start.pack()?;
        for segment in segments {
            data.extend(segment.pack()?);
        }

        self.memory.write(start_addr, &data, Some(progress_callback)).await?;
        Ok(data.len())
    }

}