Flying Robots

Programming for Robots: Software-in-the-Loop (SITL), Rust

Wolfgang Hönig

October 18, 2024

Software-in-the-loop (SITL or SIL)

Concept

Software-in-the-loop: Execute at least some part of the software that would run on the robot for simulation or validation.

Hardware-in-the-loop: Execute at least some part of the software on the robot for simulation or validation.

Typical Example: PX4

  • PX4 is a software stack for Pixhawk flight controllers
  • PX4 uses NuttX as embbedded real-time operating system (Posix)
  • The Posix standard allows to compile parts of the firmware on linux as executables
  • Used for simulation (PX4 has state estimators and controllers)

Details

Typical Example: Crazyflie

  • Crazyflie uses FreeRTOS, not Posix-compliant
  • Parts of the firmware can be compiled as library, with Python bindings
  • Testing/simulation can be done using Python

Useful principle outside SIL: High-speed implementation in C/C++ and ease-of-use through Python bindings is typical for modern projects (e.g., PyTorch, numpy).

Advantages / Disadvantages

  • Simulation-driven development
  • Much easier debugging / logging
  • Can test implementations that would be too inefficient on the real hardware
  • Code tends to be cleaner (HAL abstraction)
  • Timing (or other hardware-related) issues cannot be found
  • Workflow overhead (especially for mix of programming languages)

Programming Language Rust

History

  • 2006: Toy project at Mozilla research
  • 2009: Official work at Mozilla (10 engineers)
  • 2015: first stable release
  • 2021: Rust foundation formed (AWS, Huawei, Google, Microsoft, and Mozilla)

Current Usage (According to Github 2023)

Current Usage (According to Github 2023)

Current Usage (According to Stack Overflow 2024)

Source

Current Admiration (According to Stack Overflow 2024)

Source, Blue=Admired, Red=Desired

Use Cases

Professional Usage

  • Firefox Browser
  • Datacenter backend (Cisco, AWS)
  • Linux drivers (since 2023)
  • Rust Case Study (Fulton et al. 2021)

Academia

  • Why Scientists Are Turning to Rust (Perkel 2020)

Core Concepts

  • Syntax similar to C/C++
  • Compiled, strongly typed
  • No garbage collection
    • Compile-time checks for memory safety (Borrow-checker)
  • Integrated build system and package manager

Learning Resources

I used the book.

Comparison to Other Languages

  • Python
    • Compiled (instead of interpreted), strongly typed (rather than scripting language), harder to use, fewer packages/libraries
  • C
    • More features (some of which are not embedded friendly), memory safety guarantees
  • C++
    • better module system (integrated package manager), fewer features, fewer packages/libraries

Interoperability (e.g., calling Rust from C/C++ and vice versa) possible

Why Rust in this Course?

  • Robots are safety-critical and should use a (memory-)safe language.
  • US government does not recommend using C/C++ for new projects.(House 2024)
  • For SITL, we can use a single language (no prototype in Python)
  • Very difficult to peer review C++ code (memory issues hard to spot)
  • Students know Python well, but C(++) poorly
    • Experiment: can Rust replace the typical C++/Python combination in research?

Simulating Physics

A Typical Simulator

x = initial_state()
delta_t = 0.01 # some timestep
t = 0
while KeepRunning:
    y = computeObservation(x) # state estimation
    u = robotBehavior(y, ...) # control / planning
    x = integrateDynamics(x, u, delta_t) # dynamics
    t = t + delta_t
  • In the simplest case we have
    • computeObservation(x)=x
    • robotBehavior(...)=fixed value
  • integrateDynamics(x, u, delta_t) initial value problem (ODE solver!)

Euler Integration

\[ \mathbf{x}_{t+\Delta t} = \mathbf{x}_t + f(\mathbf{x}_t, \mathbf{u}_t) \cdot \Delta t \]

  • Simple
  • efficient
  • Inaccurate for large \(\Delta t\)

Classic Runge–Kutta Method (RK4)

\[ \begin{align} \mathbf{x}_{t+\Delta t} &= \mathbf{x}_t + \frac{\Delta t}{6} (k_1 + 2 k_2 + 2 k_3 + k_4)\\ k_1 &= f(\mathbf{x}_t, \mathbf{u}_t)\\ k_2 &= f(\mathbf{x}_t + \Delta t \frac{k_1}{2}, \mathbf{u}_t)\\ k_3 &= f(\mathbf{x}_t + \Delta t \frac{k_2}{2}, \mathbf{u}_t)\\ k_4 &= f(\mathbf{x}_t + \Delta t k_3, \mathbf{u}_t) \end{align} \]

More function evaluations of \(f\) needed

More accurate (allows larger \(\Delta t\))

Building a 1D Multirotor Simulator

Setting Up Rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup update
cargo new lecture1a
cd lecture1a
cargo build
cargo run

Physics

struct Multirotor1dState {
    z: f32,
    z_dot: f32,
}
struct Multirotor1dAction {
    f1: f32,
}
struct Multirotor1d {
    mass: f32,
    g: f32,
    dt: f32,
    x: Multirotor1dState,
}

impl Multirotor1d {
    fn step(&mut self, action: Multirotor1dAction) {
        let z_new = self.x.z + self.x.z_dot * self.dt;
        let z_dot_new = self.x.z_dot + ((action.f1 / self.mass) - self.g) * self.dt;

        self.x.z = z_new;
        self.x.z_dot = z_dot_new;
    }
}

Physics Demo

fn main() {
    let mut robot = Multirotor1d { 
        mass: 0.1, g: 9.81, dt: 0.1, 
        x: Multirotor1dState { z: 0.0, z_dot: 0.0 } };
    println!("{:?}", robot);
    for _ in 1..100 {
        robot.step(Multirotor1dAction { f1: 1.0 });
        println!("{:?}", robot.x);
    }
}

(Demo the code)

Visualization

Meshcat

  • Server runs in the browser (using three.js)
  • Client available for Python, C++ (as part of Drake), and Rust

(Small demo of meshcat Rust)

pip install meshcat
meshcat-server --open
cargo new lecture1b
cd lecture1b
cargo add meshcat

1D Multirotor Demo

(Demo the provided example code)

Conclusion

Learned Today

  1. Types and use cases of flying robots
  2. Dynamics of multirotors (1D and 2D)
  3. How to build a simple simulator in Rust

Assignment 0: Build a 2D Multirotor Simulator

  • Good way to learn Rust and see if you like the course.
  • Next week
    • Discussion/presentation of your solutions
    • Full 3D Multirotor dynamics and assignment 1

References

Fulton, Kelsey R., Anna Chan, Daniel Votipka, Michael Hicks, and Michelle L. Mazurek. 2021. “Benefits and Drawbacks of Adopting a Secure Programming Language: Rust as a Case Study.” In Seventeenth Symposium on Usable Privacy and Security, SOUPS 2021, August 8-10, 2021, edited by Sonia Chiasson, 597–616. USENIX Association. https://www.usenix.org/conference/soups2021/presentation/fulton.
House, The White. 2024. “Back to the Building Blocks: A Path Toward Secure and Measurable Software.” https://www.whitehouse.gov/wp-content/uploads/2024/02/Final-ONCD-Technical-Report.pdf.
Perkel, Jeffrey M. 2020. “Why Scientists Are Turning to Rust.” Nature 588 (7836): 185–86. https://doi.org/10.1038/d41586-020-03382-2.

Questions

?