21
Embedded Erlang, Nerves, and SumoBots Frank Hunleth Twitter: @fhunleth Erlang Factory 2015

Embedded Erlang, Nerves, and SumoBots

Embed Size (px)

Citation preview

Page 1: Embedded Erlang, Nerves, and SumoBots

Embedded Erlang, Nerves, and SumoBots

Frank HunlethTwitter: @fhunleth

Erlang Factory 2015

Page 2: Embedded Erlang, Nerves, and SumoBots

Agenda

● Embedded Erlang and the problem to be solved● Nerves Project overview● Getting started with the base image ● Communicating with hardware● Putting everything together

Page 3: Embedded Erlang, Nerves, and SumoBots

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

Page 4: Embedded Erlang, Nerves, and SumoBots

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?

Page 5: Embedded Erlang, Nerves, and SumoBots

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

Page 6: Embedded Erlang, Nerves, and SumoBots

Nerves Themes

● Erlang VM, of course● Minimalist● Buildroot● Custom init process, erlinit● OTP releases● Full system updates

Page 7: Embedded Erlang, Nerves, and SumoBots

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

Page 8: Embedded Erlang, Nerves, and SumoBots

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

Page 9: Embedded Erlang, Nerves, and SumoBots

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

Page 10: Embedded Erlang, Nerves, and SumoBots

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

Page 11: Embedded Erlang, Nerves, and SumoBots

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

Page 12: Embedded Erlang, Nerves, and SumoBots

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

Page 13: Embedded Erlang, Nerves, and SumoBots

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.

Page 14: Embedded Erlang, Nerves, and SumoBots

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

Page 15: Embedded Erlang, Nerves, and SumoBots

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

Page 16: Embedded Erlang, Nerves, and SumoBots

Simplified ElixirBot Connections

4 AA Batteries

5 V

I2C, 3.3 V

5 V Regulator

ATtiny85

Servos

Battery levelControl signal

Control signal

Page 17: Embedded Erlang, Nerves, and SumoBots

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

Page 18: Embedded Erlang, Nerves, and SumoBots

Demo

● Create a new Elixir project that uses Nerves● Pull in Elixir/ALE● Communicate via I2C to the ATTiny85● Move the robot

Page 19: Embedded Erlang, Nerves, and SumoBots

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

Page 20: Embedded Erlang, Nerves, and SumoBots

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: [email protected]

Page 21: Embedded Erlang, Nerves, and SumoBots

Embedded Erlang, Nerves, and SumoBots

Frank HunlethTwitter: @fhunleth

Erlang Factory 2015