Category Archives: Embedded Systems

Mindstorms EV3 Revisited

When I noticed that there was a new version of ev3dev available (ev3dev-jessie-ev3-generic-2017-02-11) I decided it was a good opportunity to re-do my system setup post.

This effort also included creating scripts that perform as many of the setup steps as possible.

This post updates and mostly replaces my previous ev3dev blog post.

The first steps are to download and install the latest version of ev3dev from the ev3dev website, and get the EV3 connected to the local wifi network.

Then check out the git repo containing the setup scripts here:  https://bitbucket.org/don/ev3-setup/overview

Run the Mac setup script  setupMac.sh.

Then ssh into the EV3 and run the setup script  setupEV3.sh.

You may want to edit that script to remove the Textmate ‘rmate’ setup step, although it’s harmless if you don’t use Textmate.

What do the setup scripts do?

setupMac.sh

  1. Copies the host RSA key to the EV3.
  2. Copies the setup files to the EV3.
  3. Copies a local ‘Scripts’ folder to the EV3. (If you don’t have one of these, comment out this line.)

setupEV3.sh

  1. Sets up the RSA key. This allows you to ssh to the EV3 without needing a password each time.
  2. Installs the Python FTP library.
  3. Installs a simple FTP server.
  4. Makes any Python scripts copied from the (optional) Scripts folder.

 

After running both scripts, reboot the EV3 and when it finishes booting you should be able to access the FTP server as well as ssh into the EV3 without needing to enter the password.

Advertisements

Lego Mindstorms EV3 and ev3dev

So recently my Lego infatuation led, as it inevitably would, to Lego Mindstorms.

This post discusses a lot of the setup I’ve done to get the EV3 controller brick set up for development using a “normal” programming language instead of the visual one provided by Lego. That does look to be a nice way for a lot of people, especially kids, to learn to program robots. However, as a guy who has spent a very large number of years programming, visual languages like that are a bit confining. So imagine how happy I was to see that the ev3dev project has stepped in! (More about that later in this post)

So now I have a pretty functional development environment set up – time to work on some actual robotics!

Topics covered:

  1. Remote Editing
  2. RSA Key for SSH
  3. FTP Server
  4. Python 3

While some of these such as remote editing are Mac-specific, a lot of the content here is the same regardless of your host computer.

About Ev3dev

Ev3dev is an awesome replacement OS for the EV3 brick, available here: ev3edev.

I won’t duplicate a lot of detail that’s on their website, but in general, it provides an alternate OS that lets you write Mindstorms programs in a variety of languages. The one I’m using is Python. The entire project is really slick, and an impressive accomplishment.

The ev3dev build I’m using is Jessie 2015-12-30.

All of the steps here assume that it is installed and running, and a network connection has been established along with being SSHed into the brick. (All of that is covered on the ev3dev website.)

Disclaimer: I have not gone back to a clean install of ev3dev and run though the steps in this post, so while I believe I captured everything, it is certainly possible I missed something.


Enable remote editing with TextMate

This obviously assumes that you have the latest TextMate running on your Mac. Did I mention that this post is going to be Mac-centric in places?

On EV3:

$ sudo gem install rmate

On Mac:

Add this to your  ~/.ssh/config file:

Host *
RemoteForward 52698 localhost:52698

Or, when starting SSH, you can do this and not muck with the SSH config file:

$ ssh -R 52698:localhost:52698 robot@ev3dev.local


 Setup RSA key for SSH

By setting up the RSA key for my host computer, I can skip needing to enter a password every time I log in via SSH.

On the Mac:

$ scp ~/.ssh/id_rsa.pub robot@ev3dev.local:/tmp

This assumes that an RSA key has been previously generated! You can generate a key using the ssh-keygen command, which will create the private and public keys in (typically) the ~/.ssh directory. The file without the extension is the private key, and the one with the .pub extension is the public key.

This public key is the one transfered to the system you want to access, and append to its authorized_keys file.

On the EV3:

$ mkdir ~/.ssh
$ cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys

 


FTP Server

Even though I can edit files via rmate and sip them back and forth from the remote shell, it’s nice to be able to move batches of files etc. via FTP. And with something like the Transmit application as an FTP client it’s trivial to do a sync and essentially back up my EV3 home directory to my Mac.

I spent a stupid amount of time trying to get vsftpd installed and running (installation went smoothly, but running always failed with an error no matter what I tried. And I tried  a lot of things!)

So, since my goal is just to have simple FTP access,  on to plan B – use Python! I grabbed a very nice FTP package for Python here: https://pypi.python.org/pypi/pyftpdlib/

The following shows the steps I went through (you may want to update the wget line based on the current version of pyftplib). In a long-ago effort to install this library on a Beaglebone, the installer failed, so the steps below include just manually moving the library into place – didn’t want to invest a lot of time debugging that issue since manually moving it seemed to work just fine.

Install the FTP package

$ wget https://pypi.python.org/packages/source/p/pyftpdlib/pyftpdlib-1.5.0.tar.gz
$ gunzip pyftp*
$ tar -xf pyftp*.tar
$ sudo mv pyftpdlib /usr/lib/python2.7

Create the FTP Server Python program

I added a super-simple FTP server program to my home directory ~/robot.

#!/usr/bin/env python
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

authorizer = UnixAuthorizer(rejected_users=["root"], require_valid_shell=True)

handler = FTPHandler
handler.authorizer = authorizer

handler.abstracted_fs = UnixFilesystem

address = ('', 21)
ftpd = FTPServer(address, handler)

ftpd.serve_forever()

This will allow you to log in using any account other than root. By default, the username of robot and the password maker.

Set up to start the FTP server at bootup

Add this script as /etc/init.d/ftpserver

#!/bin/sh
python /home/robot/ftpserver.py

Then make it executable and let the init system know to run it at boot up:

$ sudo chmod +x /etc/init.d/ftpserver
$ sudo update-rc.d ftpserver defaults

Python 3

Set up to use Python3 (this may not be needed with a future release of ev3dev?)

Note that Python 3 is pre-installed on the ev3dev distribution but some of the supporting libraries are not, which is what makes these steps necessary.

On EV3:

$ sudo apt-get update
$ sudo apt-get install python3-pil
$ sudo python3 -m easy_install python-ev3dev

Note that “apt-get update” may take a while…

Lua Web Server

One of my large and ever-unfinished projects is a Home Automation system. This is designed as multiple processes where most of them are Lua script engines along with several C++ processes as well that handle low-level interfacing and infrastructure for the system.

This is all running on a RaspberryPi.

As part of this effort, I wrote a simple Lua web server to allow access to the system from a browser or a custom iOS app. Since this Lua web server might be useful to others, I am making it available as a separate component.

It is quite simple, but for serving JSON and HTML, and responding to requests (JSON or otherwise) in a low-demand system like personal home automation, it should be just fine.

Note that this depends on the Lua socket library being present.

Lua Web Server repository on Bitbucket

1802 Emulator

In a previous post I talked about the 1802 Assembler I was playing with when I revisited an ancient 1802-based FIG-Forth implementation.

Since it was a pain to get that code running on real hardware, I also threw together an emulator. This was done in Objective-C for the Mac.

Note that the core emulation code is written in ‘C’ however, so it should be quite portable.

It is fairly quick, although I have not tried to quantify it’s equivalent speed.

It will build under recent Xcode versions such as Xcode 6 or Xcode 7.

Listing files can be loaded and executed. There is application-specific code to treat some of the IO ports as serial input/output to a simple terminal facility.

This is in no way a polished final product, but it is made available on the off chance that someone else might find the code useful or interesting.

https://bitbucket.org/don/cosmac-1802-emulator

Cosmac 1802 Assembler in Python

So back in the day, one of my Cosmac 1802 projects was getting FIG-Forth up and running on the 1802.

I laboriously converted the printed listings to a digital assembler source file. This file was then assembled with a cross-assembler that was written in BASIC.
As part of that effort I also modified the Forth system to run from EPROM.

Since I wanted to get Forth running on the 1802 Membership Card (see earlier blog post), I needed the ability to assemble that source, and do some updates etc.

In the same way it’s been a long time since I’ve had a computer with a parallel port, it’s been a long time since I’ve had a computer with a BASIC interpreter installed.

The obvious next step was to write an assembler in a more modern language. Again Python was my tool of choice.

This is a fairly simple assembler, just sufficient to assemble my ancient FIG-Forth listings. It is not a macro-assembler and I’m sure has other limitations relative to a “real” 1802 assembler.

The source is available here: Cosmac 1802 Assembler

This plays nicely with my 1802 Membership Card Loader. The loader can invoke the assembler automatically when given a source file, allowing you to give one single simple command line that assembles the source and downloads it to the Membership Card.

1802 Membership Card Loader

So I built Lee Hart’s slick little 1802 Membership Card and its Front Panel card. Next I wanted to load some programs onto it! There was that nice DB-25 connector all ready to connect to my PC’s parallel port. However… I’ve got a Mac, and haven’t seen a computer parallel port in perhaps a decade.

My solution to this problem was to create my own interface hardware based on an Arduino. I considered using a Spark Core to allow doing things wirelessly via WiFi, but decided that for this purpose the Arduino was something more people were likely to have, and it was just a somewhat simpler solution.

The final design uses an Arduino, a 16-line I2C port expander chip, and some software. The software consists of the Arduino program and a Python program that runs on my Mac. Since it’s Python, it should also run on a Windows system, a Linux box, etc.
You can also forego the Python side of things and directly control the Arduino loader from a terminal, or from your own software if you prefer.

Why go to all this trouble? Just a fun little project!

It’s been a good mental exercise and a nice trip down memory lane.

Here is the Git repository with the source code and documentation

1802 Loader Documentation

Coming up next – an 1802 Emulator

Embedded Development – The Hobby Edition

Ok, so here are pictures of embedded controllers and such. Just a snapshot of my hobby embedded environment.

Not shown is my workbench, which gets set up and taken down on the kitchen table as needed. (apartment living!)
This includes soldering station, digital oscilloscope, and tools.

The table top near the desk:

Spark Core, RaspBerryPi, Arduino and so forth. Plus an old-time iPod!

The RaspberryPi runs the home automation system, and is connected to an XBee used for communication with various peripherals. The Arduino is one of those, in development. The Spark Core is my development core, with others already in use around the house.

IMG 6078

Drawer #1:

Arduino stuff, XBee modules, and various shields and parts from Adafruit.

IMG 6079

Drawer #2:

RaspberryPi and Spark Core. Power supplies, and a fairly ancient Radio Shack multimeter.

IMG 6080

Cross-Compiling for BeagleBone Using a Linux VM on the Mac

BeagleBone Cross Compiling using OSX on the Mac (using Linux)

Although it may seem more complicated, I decided the best way to cross-compile from my Mac would be to do it from a Linux system.

I would of course love to have my BeagleBone cross-development environment running directly on OSX, but that’s an effort for another day – my goal here was to be able to cross-compile for the BeagleBone.

Yes, in theory this should be possible on a Mac since it’s Unix-based, but it appears to me that may be more pain. More pain than just setting up an Ubuntu system using VMWare Fusion anyway. This is quite easy these days – both VMWare and Parallels make it almost effortless. There are many many resources on how to do this, so I’m not going to cover that here.

With Ubuntu 12.04 running happily in a VMWare VM on my MacBook Pro, away I went.

So to just cross-compile, it doesn’t appear that you really need the OpenEmbedded and Angstrom kernel stuff, so I went with just the Angstrom pre-built toolchain available at:
http://www.angstrom-distribution.org/toolchains/

I used angstrom-2011.03-i686-linux-armv7a-linux-gnueabi-toolchain.tar.bz2
This is the 32-bit version, which matches my Ubuntu VM setup. There is a 64-bit version there as well, if you are running a 64-bit version of Linux on your host system.

Step-by-Step

On the Cross-development Host

Extract the pre-built angstrom toolchain:

    $ cd /
    $ sudo tar -xf <path to the angstrom toolchain>

Which creates the new directories /usr/local/angstrom and /var/lib/opkg.

Run the environment setup script, which puts the new tools in the path as well as sets up several other environment variables:

    $ . /usr/local/angstrom/arm/environment-setup

Create a simple “Hello, world” program using whatever editor you prefer. I named mine “hello.c”.

    #include <stdio.h>

    int main()
    {
        printf( "Hello, worldn" );
    }

And finally, do a test build.

    $ arm-angstrom-linux-gnueabi-gcc hello.c -o hello

This should result in an executable file named hello. (This is a Csource file, so I used gcc, but you can also change the gcc at the end of the command to g++ to compile C++ source code.

On the BeagleBone

If you transfer the executable binary output file hello to your BeagleBone (e.g. via FTP), you should be able to run it and see the text “Hello, world” printed out in the BeagleBone terminal.

    $ ./hello
    Hello, world

Resources

This guide is based on bits of information gleaned from a variety of sources, including:

Linux To Go
electrons on radio
Trey Weaver’s Blog
gpio.kaltpost.de

Thanks to everyone who has blogged about their experiences with the BeagleBone!

Writing a BeagleBone SD Card Image From the Mac Revisited

Since I last dealt with writing an SD card image to update the BeagleBone software, some things have changed. So, here’s an updated guide to the process.

Download the Image

The latest images are available here: http://beagleboard.org/latest-images

The one I downloaded was named: Angstrom-Cloud9-IDE-GNOME-eglibc-ipk-v2012.05-beaglebone-2012.11.22.img.xz

Uncompress the Image

The latest BeagleBone images are not compressed in a format that tar or unzip can deal with. Fortunately there is a free application The Unarchiver that can. It’s available here: The Unrchiver.

Run this application on the .xz image file to get a (much larger) file that ends in .img. This is the file you’ll write to the SD card.

Unmount the SD Card

To write the image to the SD card, it first needs to be unmounted.

    $ diskutil unmount /volumes/YourCardNameHere

Find the Device Name

This step is critical.

Using the wrong device name can destroy the data on your computer’s hard drive, so be very very sure to get the device name correct in the following steps!

This can be found in a couple of ways. You can use the “Disk Utility” application, or the command line diskutil or df commands.

Diskutil

From the command line:

    $ diskutil list

df

    $ df

This may show you the partition, something like disk7s2. You want to entire SD card, not any partitions so drop the s2 part.

Write the Image

Then, write the image to your SD card. Note that the diskXXX should be the actual disk device assigned when the SD card is connected, and Angstrom-XXX should be the name of the actual card image you downloaded and extracted previously.

Again be sure to use the right device name for the SD card in this step!

Don’t be surprised if this takes a while – on my system it took about 37 minutes, and there are no “in progress” indications of any sort, so be patient! (note that this time is more a function of how big the image is – ~3.4G – and the speed of your micro-SD card then how fast your computer is)

    $  dd if=Angstrom-XXX.img of=/dev/diskXXX bs=4096

If you aren’t logged in as root, you may need to use sudo, in which case the command is:

    $  sudo dd if=Angstrom-XXX.img of=/dev/diskXXX bs=4096

BeagleBone FTP Server

BeagleBone FTP Server

The BeagleBone has multiple text editors available onboard, including vi and vim. I can stumble around (badly) in vi. Using Vim is a little prettier, but not much – I’m really much more of a modal-editor guy. On the Mac, I use BBEdit when I’m not using XCode for iPhone and iPad development.

So my goal is to be able to easily edit on my Mac. BBEdit has a very slick ability to edit files via FTP, so that’s my next step.

Oddly, the BeagleBone doesn’t have an FTP server available in the default distribution. (or if it does, I couldn’t find it)

One skill I know I need to brush up on is acquiring, building, and installing packages in Linux. Since I wanted a quick-and-dirty FTP server running without too much fuss, I naturally looked to Python.

Python is a terrific “scripting” language, and my go-to tool for a lot of tasks.

There is a very nice FTP Server library available: pyftpdlib.

I grabbed this using wget, then did the unzip/untar dance:

    gunzip pyftp*
    tar -xf pyftp*.tar

Installation was simple. The setup python program failed, so I just manually moved the pyftpdlib directory into /usr/lib/python2.7/.

Last but not least, I whipped up a small Python program by modifying the quickstart and demo examples just a bit:

    # FTP server

    from pyftpdlib import ftpserver
    authorizer = ftpserver.DummyAuthorizer()
    authorizer.add_user("root", "12345", "/home/root", perm="elradfmw")
    handler = ftpserver.FTPHandler
    handler.authorizer = authorizer
    address = ("", 21)
    ftpd = ftpserver.FTPServer(address, handler)
    ftpd.serve_forever()

Take that code, stuff it into a file with a .py extension (e.g. ftpserver.py), and invoke it:

    python ftpserver.py

And there you go – FTP access to the home directory of user root.

Now, is this what you’d use to server files to the world at large? Maybe – it looks like a very complete implementation, although I’m no FTP expert.
But it’s perfect for my local development purposes.

The only thing that would be nice is to have it auto-start. That’s pretty easy to do as well, at least in the simple case.

The directory /etc/init.d contains scripts that are executed upon system startup. We place a very simple shell script there, which I called ftpserver:

    #!/bin/sh
    python /home/root/ftpserver.py

This will run the Python FTP server program (which in this case is located in the root account’s home directory – it could be located elsewhere of course).
Once you create this script, don’t forget to make it executable by doing chmod +x ftpserver.

Although this works, our script should really be more compliant and let us stop and restart the server. I plan to address that soon!