Post on 18-Jul-2015
transcript
Embedded Erlang, Nerves, and SumoBots
Frank HunlethTwitter: @fhunleth
Erlang Factory 2015
Agenda
● Embedded Erlang and the problem to be solved● Nerves Project overview● Getting started with the base image ● Communicating with hardware● Putting everything together
Road to Embedded Erlang (ca. 2012)
● Searching for a better way to build embedded devices– Embedded for me was 32-bit micro w/ RTOS or Linux and
C/C++
● Erlang/OTP provided the high level functionality that I had been writing already
● Others had used it in embedded– Ericsson's AXD301 switch
– Various hits on Google
– Open-source projects for accessing hardware on GitHub
The Problems
● Erlang installations were huge– Required desktop Linux install
– Megaco, CORBA??
– Clearly others had fixed this, but where?
● Open-source libraries for accessing hardware were not well-maintained– Erlang/ALE helped, but now suffers
– How many erlang-serial forks?
Nerves Project
● What would a small to mid-sized embedded system look like if as much as possible was done or controlled by Erlang?
● Practice– Embedded Linux w/ glibc for POSIX APIs
– Cross-compiled to only need libraries and apps required for the device (Build systems pull in a lot)
– Calling into programs for network setup, etc is ok to avoid rebuilding everything
Nerves Themes
● Erlang VM, of course● Minimalist● Buildroot● Custom init process, erlinit● OTP releases● Full system updates
Nerves Work Flow
OTP applicationsBEAMs and
cross-compiled binaries
Erlang/OTP release
Base Firmware Image
SDCard imageand
firmware update package
mix,rebar, erlang.mk
relx
Nerves scripts
nerves-sdk customizations to
Buildroot
Updated filesystem on target
(Development)
relsync
Cross-compiling
● Building your program on a different machine (or VM) that runs it
● Why?– Development cycle on the target is painful
– Target is slow or doesn't have enough memory
– Networking, video and other interfaces taken over by the program
● Sometimes cross-compiled development is painful– Structure code so that it can be developed and tested on host
– Debug hardware interface code on target with a full Linux install
Demo
● Building the base Nerves image– Clone https://github.com/nerves-project/nerves-sdk
– make nerves_rpi_defconfig
– make
– make burn-complete
● Booting● Bringing up Ethernet and sending messages
10 Seconds on Building the Frame
● Original plans from http://sumobotkit.com/– One of the least expensive robotics “kits” out there
– Intended for use with Node.js (search for NodeBots)
– Lasercut 5 mm hardwood plywood
– 3D printed ball holder
● Modified plans at https://github.com/fhunleth/elixirbot
Controlling Hardware from Erlang
● Erlang/ALE and Elixir/ALE– GPIO, I2C, SPI for Linux platforms
– Erlang/ALE has PWM support for Raspberry Pi
● Erlang-serial– UART for Linux platforms
– Many forks - see https://github.com/knewter/erlang-serial
Servos
● Motors that turn to a precise angle based on an input signal● Continuous-turn servos are servos with the feedback
mechanism removed– Really just motors in a convenient package
– Stop, Clockwise, Counter-clockwise
● Signal is sent on the white wire– Pulse sent every 20 ms
– Duration of pulse controls angle or direction
– 1.5 ms is neutral
Servo Control Strategies
● Option 1– Gpio.write(myservo, 1)
– :timer.sleep(1.5)
– Gpio.write(myservo, 0)
– :timer.sleep(20 - 1.5)
● Issues– Pulse durations must be consistent (within microseconds) or the
servo is unstable
– Much better implemented in C (called software PWM)
– Even C has trouble on Linux due to context switches, etc.
Servo Control Strategies
● Option 2:– Use hardware PWM on Raspberry Pi
● Issues– Original Pi's only exposed one hardware PWM
– Standard RPi way doesn't use generic PWM kernel driver
Servo Control Strategies
● Option 3: Use a microcontroller– Microcontrollers excel at hard real-time!
– Communicate via standard I2C bus
– Arduinos have made programming and using microcontrollers very accessible
– ATtiny85 is $1.67 from Digikey - doesn't need any extra parts
– Can even buy preprogrammed PWM drivers
● Issues– Microcontrollers are programmed in C
Simplified ElixirBot Connections
4 AA Batteries
5 V
I2C, 3.3 V
5 V Regulator
ATtiny85
Servos
Battery levelControl signal
Control signal
I2C Interface to the ATTiny85
● I2C bus address 0x4● Protocol
– First byte in a write sets the ATTiny85's active register
– Subsequent read or write accesses active register
– Active register increments after operation
● Write <<2, Right:16/little>> to set the right servo's pulse duration
Left duration (μs) LSB
Left duration (μs) MSB
Right duration (μs) LSB
Right duration (μs) MSB
Battery volts (mV) LSB
Battery volts (mV) MSB
Registers
0
1
2
3
4
5
Debug
Debug
6
7
Demo
● Create a new Elixir project that uses Nerves● Pull in Elixir/ALE● Communicate via I2C to the ATTiny85● Move the robot
The Rest of the Pieces
● Web-based control– Slightly modified Cowboy websockets example
● Video– MotionJPEG streamed via Cowboy
– Image tag in HTML running on client
● Wi-Fi● https://github.com/fhunleth/elixirbot
Going Farther
● Try out the Nerves Project for yourself– All open source and hosted on GitHub
– http://nerves-project.org
● Supported hardware– Raspberry Pi, Beaglebone Black, various embedded x86
– Ports to hardware supported by Buildroot generally easy
● Keep in touch– Twitter: @fhunleth
– Email: fhunleth@troodon-software.com
Embedded Erlang, Nerves, and SumoBots
Frank HunlethTwitter: @fhunleth
Erlang Factory 2015