This document discusses a project to build an autonomous vehicle using Rust and a Raspberry Pi. The objectives were to navigate to GPS waypoints, avoid obstacles using ultrasonic sensors, and record video. Rust was chosen for its performance, safety, and existing crates for hardware interfaces. Challenges included slow compilation on the Pi and lack of sensor libraries in Rust. The vehicle struggled detecting obstacles at angles and had compass calibration issues. Overall it was a learning experience to use Rust on embedded systems.
3. ABOUT ME
• Co-Founder & Chief Architect @ AgilData
• Started in C++, then 20 years Java, now Scala & Rust
• AgilData is building its next product in Rust
• Also offering Rust consultancy and project kick-start
services
• I enjoy being a “Maker” in my spare time
• Digital electronics, robotics, 3D printing, woodworking,
etc, etc.
5. WHY THIS PROJECT?
• I was inspired by the last Rust meetup and I agreed
to give a talk at this one
• Sparkfun Autonomous Vehicle Competition (AVC)
was coming up
• This was a great opportunity for me to learn about
using Rust on the Raspberry Pi and get to share that
knowledge
• And four weeks was plenty of time, right?
6. OBJECTIVES
• Navigate to pre-determined waypoints (GPS co-ordinates)
• Using GPS and compass to determine location and heading
• Change speed of left/right motors to control movement
• Avoid obstacles
• Using ultra-sonic sensors to detect distance to objects
• Record instrumented video
• Essential for debugging
8. DIGITAL IO INTERFACES
• GPIO - General Purpose IO
• Turning pins on and off, or reading high/low inputs from pins
• SPI - Serial Peripheral Interface
• Four wire interface (MISO, MOSI, SCLK + dedicated SS per slave)
• I2C - Inter-Integrated Circuit (pronounced I-squared-C)
• Two wire interface (SDA - Serial Data & SCL - Serial Clock)
• Each device has unique 7-bit address
• Serial
• Familiar to most of use - same as file or network i/o
9. WHY RASPBERRY PI?
• Great support for Linux (Raspbian)
• Designed for hacking hardware
• Hardware support for GPIO, SPI, and I2C
• Support for webcams and video processing (with GPU
acceleration)
• USB makes it easy to connect to serial devices
• Built-in WiFi
• All this for $35 !
11. WHY RUST?• Better performance than Java or scripting languages
(assumption)
• Safe language - nobody wants their robot to fail with a
segmentation fault or null pointer exception
• Crates already exist for interfacing with GPIO, SPI, I2C and
Serial
• Rust is natively supported on the Pi using rustup.rs
• Easy to call existing C libraries such as OpenCV with zero
overhead
12. SERIAL IO
• Linux presents serial IO devices as files e.g.
/dev/ttyUSB0
• Use udev rules to map to convenient names e.g.
/dev/gps
• USB-Serial adapter required to connect to serial devices
• serial-rs crate makes it easy to configure baud rate,
parity, etc
• Use std::io::Read and std::io::Write to interact with
device
14. PARSING GPS DATA
• GPS data uses NMEA format
• Simple comma-separated format
• Example: $GPGLL,4916.45,N,12311.12,W,225444,A
15. SPI
• SPI devices are represented as files too!
• Example: /dev/spidev0.0
• Use spidev crate to interact
16. I2C
• I2C devices are represented as… yes, files!
• Example: /dev/i2c-1
• Use i2cdev crate
17. GPIO
• GPIO devices are represented as files too, but the sysfs_gpio crate abstracts this
• GPIO is the simplest interface - simply read or write 1 or 0 to an individual pin
• Raspberry Pi does not support analog IO directly
• Suitable for:
• Interacting with digital circuits
• Blinking LEDs
• Not suitable for:
• Driving high current devices like motors
23. WHAT WENT WRONG?
• Ultrasonic sensors aren’t fantastic at detecting hay bales,
especially at an oblique angle
• LIDAR would have been better, but also more $$
• Magnetometer (compass) was not correctly calibrated and
I also hadn’t accounted for magnetic declination (8
degrees!)
• Four weeks wasn’t quite long enough for this project
• But I learned a lot!
24. RUST/PI CHALLENGES
• Compilation is SLOW on the Pi
• Splitting project into multiple crates helps a lot
• Incremental compilation will be here soon
• Compilation on the Mac natively is not possible since the libraries are
Linux-specific
• Cross-compiling is a pain to set up, but probably worth the investment
• Docker can help
• Severe lack of sample code or existing libraries in Rust for any sensors,
so you have to write your own (using Arduino libraries as a reference)
26. WEB SITES
• For inspiration, tutorials, blogs, parts, visit these sites:
• sparkfun.com
• adafruit.com
• instructables.com
• hackaday.com
• make.com
• tindie.com
27. MAKER COMMUNITY
• Local maker spaces
• Solid State Depot (Boulder)
• The Gizmo Dojo (Broomfield)
• Tinkermill (Longmont)
• Mini Maker Faires
• NoCo Mini Maker Faire (Loveland) - October 8/9
• Denver Mini Maker Faire - Summer 2017