rust is for robots!

28
RUST IS FOR ROBOTS Building an autonomous vehicle with Rust and the Raspberry Pi Andy Grove - Boulder/Denver Rust Meetup - 9/21/16

Upload: andy-grove

Post on 13-Apr-2017

769 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Rust is for Robots!

RUST IS FOR ROBOTSBuilding an autonomous vehicle with

Rust and the Raspberry Pi

Andy Grove - Boulder/Denver Rust Meetup - 9/21/16

Page 2: Rust is for Robots!

PLEASE ASK QUESTIONS!

Page 3: Rust is for Robots!

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.

Page 4: Rust is for Robots!

G-FORCE(ironic name)

Page 5: Rust is for Robots!

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?

Page 6: Rust is for Robots!

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

Page 7: Rust is for Robots!

COMPONENTS

Page 8: Rust is for Robots!

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

Page 9: Rust is for Robots!

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 !

Page 10: Rust is for Robots!

RASPBERRY PI 3

Page 11: Rust is for Robots!

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!

Page 12: Rust is for Robots!

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

Page 13: Rust is for Robots!

SERIAL IO

Page 14: Rust is for Robots!

PARSING GPS DATA

• GPS data uses NMEA format

• Simple comma-separated format

• Example: $GPGLL,4916.45,N,12311.12,W,225444,A

Page 15: Rust is for Robots!

SPI• SPI devices are represented as files too!

• Example: /dev/spidev0.0• Use spidev crate to interact

Page 16: Rust is for Robots!

I2C• I2C devices are represented as… yes,

files!

• Example: /dev/i2c-1• Use i2cdev crate

Page 17: Rust is for Robots!

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

Page 18: Rust is for Robots!

CALLING C CODEUSING FFI

Page 19: Rust is for Robots!

OPENCV

Page 20: Rust is for Robots!

AUGMENTING VIDEO

Page 21: Rust is for Robots!

DEMOSPARKFUN AVC PRACTICE

Page 22: Rust is for Robots!

https://www.youtube.com/watch?v=IaBiMtyEZOs

Page 23: Rust is for Robots!

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!

Page 24: Rust is for Robots!

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)

Page 25: Rust is for Robots!

RESOURCES

Page 26: Rust is for Robots!

WEB SITES• For inspiration, tutorials, blogs, parts, visit these sites:

• sparkfun.com

• adafruit.com

• instructables.com

• hackaday.com

• make.com

• tindie.com

Page 27: Rust is for Robots!

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

Page 28: Rust is for Robots!

QUESTIONS?• Source code

• https://github.com/andygrove/rust-avc

• Contact

• Twitter: @andygrove73

• Email: [email protected]

• Blog: http://theotherandygrove.com