Raspberry PI with Erlang

Just recently got around to using the Raspberry PI I bought almost a year ago and have a bit of interest in using Erlang with it. A friend got me into Erlang a few years ago and my primary interest in it was how two VMs could easily talk to one another and in general from the point of view of the code you are writing it just works.

You don’t really care if the message you just sent is on the node your module is on or on a remote node — it’s mostly transperent. I really dig that. I haven’t written a large amount of code in Erlang, but the more I write – the more I like it; still quite new to it though.

The idea is to use something like the raspberry pi running erlang as a subsystem in robot or some other electronic projects. Erlang would be the communication hub between other robots, and a server. Currently toying with the idea of creating something like robocode, but with rc tanks (I say rc more so you picture what I’m talking about, they would really be autonomous not RC).

Today I setup the PI up again; for the second time. I’m running Raspbian (won’t go into the configuration) and have a cheap usb wifi dongle plugged in that works really well. I did set it up a few weeks ago running the arch distro, which I is the distro (well not the arm version) I use for my laptop — but hosed it messing with configuration (like any good programmer/hacker). Just figured I would go with raspbian this time around as it seems to be more popular.

On top of that I installed erlang-mini (checkout the raspbian tab and scroll down a bit) which is smaller version targeted for this purpose, after which I subsequently compiled Erlang ALE. This provides a set of modules backed by c libs that interface with the GPIO header on the raspberry pi. All of this was fairly simple to do, and then I wired up a simple led to the 4th pin on the GPIO header for the obligatory hello world of electronics projects and set out to make that damn thing blink.

Fortunately the git hub page for erlang ale has some good examples along with enough info to get me started so looking at the page I so that I could do

make shell

in the root of the ale directory to bring up an erlang shell ready to go and that’s just what I did. I followed their directions modifying them for the pin I used and ended up with something like

%% spin up a process, and link to it that 
%% manages pin 4, and set pin 4 as an output pin
gpio_sup:start_link([{4, output}]).

%% pull pin 4 high
gpio:write(4, 1).

%% pull pin 4 low (I was typing this in the shell,
%% and obviously pausing to see the result in between)
gpio:write(4, 0).

and I’ll be damned if the led didn’t come on when I pulled the pin high and go off when I pulled the pin low. I then proceeded to repeat this several times because it was cool. No other reason, I was just amazed at how simple all this was and to be able to just do this in a shell interactively. Used to typically writing C code compiling and burning then trying it, then repeat (and yes I’m sure there setups out there that let you do this interactively etc…).

So I was like ok, now I’d like to make it go blinky blinky, to make it a true hello world program. At his point I also remembered the new named funs in Erlang R17, so I proceeded to type this into the shell

F = fun Blink(0) ->
   gpio:write(4, 0),
   timer:sleep(1000),
   Blink(1);
Blink(1) ->
   write(4, 1),
   timer:sleep(1000),
   Blink(0)
end.

%%now set it in motion
F(0).

And sure enough it blinked, and that’s where I left off. I do have a handy dandy servo laying on my work bench (AKA, dinner table) so my next goal will be trying the PWM module.

Advertisements
This entry was posted in Embedded, Erlang, Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s