DEV Community

Cover image for Why Rust for Robots?
Adam Rodnitzky for Tangram Vision

Posted on • Originally published at tangramvision.com

Why Rust for Robots?

Robotics largely runs on C++. Or at least it does for now. Here at Tangram Vision, we believe that there is better language for robotics, and that language is Rust.

C++ for Robotics

For robotic platforms where the intention is to deploy commercially and at scale, C++ has emerged as the standard over the last few decades, and there are a few key reasons why. For one, the level of ubiquity has become a self-reinforcing mechanism, with an ecosystem of libraries, tools and engineers that almost exclusively work in C++. In fact, ROS (the main client library of the most popular framework used by most roboticists) is primarily written in C++. Similarly, the popular OpenCV computer vision library is also written in and accessed with C++.

However, ubiquity of tools and engineers alone doesn't explain the popularity of C++ for robotics. There is a more fundamental reason: most robotics platforms must meet acceptable performance thresholds under known resource constraints. C++ is well suited for these embedded applications because of how "close to the metal" the language can get.

This "close to the metal" advantage also makes C++ a potentially huge hazard. Testing, architecture, and memory management for C++ isn't consistent across well-used dependencies; yet, these dependencies might capture and manipulate essential low-level resources. This means it becomes very easy to inadvertently build in a critical bug without realizing it... that is, until you're well into production. That critical bug won't be discovered until the product has been deployed in the real world, where an edge case scenario that slipped past QA triggers a memory leak, or system crash, or [your favorite disaster scenario here].

Venn Diagram of C++ and Robotics

Unless you resemble this Venn diagram, there's a good chance that you'll inadvertently program in some level of trouble as you build out your robotics codebase in C++.

This is where Rust starts to make sense.

Enter Rust for Robotics

Rust is a relatively new language for robotics, but there's a rapidly growing set of projects and libraries that provide key frameworks for robotics development. Why the change?

For starters, the biggest benefit of building with Rust is memory safety and management. You have to try very hard to create a memory leak or race condition in Rust. Common gotchas like null pointers and data races are blocked altogether and won't compile. Likewise, the memory management approach in Rust is to use the stack to keep track of the program, and then use pointer references aimed at heaps where larger data structures are contained.

To access a data structure, ownership has to be established, thereby preventing multiple variables from accessing or modifying data structures simultaneously. Efficient? Yes. Safe? Also, yes. The best part: developers maintain that "close to the metal" access that they would normally go to C++ for. This makes Rust a highly-efficient, extremely safe language that also allows low-level access, something well-suited to the world of robotics where resource constraints and code safety are critical.

Rust is an obvious choice for robotics, but the transition to platforms written in Rust will take time. Rust itself is just over a decade old, whereas C++ has been around for nearly four decades. That's a lot of inertia to work against, but the Rust community is moving fast.

Resources for Rust in Robotics

There's a small but growing community of companies and developers (Tangram Vision included!) that are taking some of robotics' most commonly used libraries and tools and making them Rust compatible, as well as developing new tools to ease the development path for creating a Rust-programmed robot.

Here are a few of our favorites that cover some of the critical areas of robotics development. We've chosen to highlight resources that are still actively maintained within the last year.

Frameworks

  • OpenRR: An open-source Rust robotics platform

ROS

  • rosrust: A pure Rust implementation of the ROS client library
  • ros2-rust: Bindings, a code generator and code examples for ROS2
  • rustros_tf: A Rust port of the ROS tf library for keeping track of three dimensional transforms
  • Optimization Engine: Embedded optimization for robots and autonomous systems

Computer Vision

  • realsense-rust: High-level bindings for using Intel RealSense depth cameras (disclosure: Tangram Vision maintains this library!)
  • opencv-ros-camera: An OpenCV-compatible geometric model of a camera
  • adskalman: Kalman filter smoothing
  • cam-geom: Geometric models of cameras
  • bayes_estimate: A Bayesian estimation library

Collision Detection

Controls

  • stepper: A universal stepper motor driver and controller interface for Rust

Simulation

  • nphysics: A 2D and 3D physics engine that can be used for robot simulation

Mathematics

  • Nalgebra: It's linear algebra...for Rust
  • petgraph: Graph data structure library, compatible with Rust

If there are other Rust resources that you've used for a robotics product that worked well, let us know! We plan on keeping this list curated and updated over time.

Should you build your Robot in Rust?

We think the answer will increasingly be yes. The fundamental benefits of the language already make it a great fit for the needs of roboticists and robots. The growing set of libraries and resources make it easier than ever to get started with foundations — and that includes the perception sensor tools and APIs that we are building here at Tangram Vision. Finally, more and more engineers are adopting Rust as a language of choice, particularly for embedded scenarios like robots. So, yes, the future is robots. And it will also be robots, built with Rust.

Top comments (0)