rust is for robots!
TRANSCRIPT
RUST IS FOR ROBOTSBuilding an autonomous vehicle with
Rust and the Raspberry Pi
Andy Grove - Boulder/Denver Rust Meetup - 9/21/16
PLEASE ASK QUESTIONS!
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.
G-FORCE(ironic name)
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?
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
COMPONENTS
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
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 !
RASPBERRY PI 3
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
• Hobby projects are a great excuse for learning more Rust!
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
SERIAL IO
PARSING GPS DATA
• GPS data uses NMEA format
• Simple comma-separated format
• Example: $GPGLL,4916.45,N,12311.12,W,225444,A
SPI• SPI devices are represented as files too!
• Example: /dev/spidev0.0• Use spidev crate to interact
I2C• I2C devices are represented as… yes,
files!
• Example: /dev/i2c-1• Use i2cdev crate
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
CALLING C CODEUSING FFI
OPENCV
AUGMENTING VIDEO
DEMOSPARKFUN AVC PRACTICE
https://www.youtube.com/watch?v=IaBiMtyEZOs
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!
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)
RESOURCES
WEB SITES• For inspiration, tutorials, blogs, parts, visit these sites:
• sparkfun.com
• adafruit.com
• instructables.com
• hackaday.com
• make.com
• tindie.com
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
QUESTIONS?• Source code
• https://github.com/andygrove/rust-avc
• Contact
• Twitter: @andygrove73
• Email: [email protected]
• Blog: http://theotherandygrove.com