Raspberry Pi Projects: LEGO Brick Alternate Language

By Mike Cook, Jonathan Evans, Brock Craft

Need a new Raspberry Pi project? You can run quite a few third-party languages on the LEGO brick, but ev3dev puts a cut-down version of Linux on the brick. You don’t need to change anything on the brick because ev3dev runs off a micro SD card inserted into the brick. You can easily revert your LEGO brick back to the original state by simply unplugging the SD card.

The bulk of the installation work involves creating the SD card with the Linux distribution on it. It’s sort of like creating the SD card for the Raspberry Pi itself. At ev3dev.org, you can find instructions on creating the SD card under the Mac, PC, or Linux operating system.

After you create the SD card, you can plug the brick into the Raspberry Pi. You have to connect the brick directly into one of the Raspberry Pi’s USB sockets (as opposed to a USB hub) in order for it to work.

You communicate with the LEGO brick from the Raspberry Pi by using a Secure Shell (SSH) session. Open up a command-line window and set up a session. This is what you’ll see:

ssh root@192.168.2.3
root@192.168.2.3’s password:
             _____     _
   _____   _|___ /  __| | _____   __
  / _   / / |_  / _` |/ _   / /
 |  __/ V / ___) | (_| |  __/ V /
  ___| _/ |____/ __,_|___| _/
Debian GNU/Linux jessie on LEGO MINDSTORMS EV3!
The programs included with the Debian GNU/Linux 
system are free software;
the exact distribution terms for each program 
are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, 
to the extent permitted by applicable law.
Last login: Wed Nov  5 11:45:33 2014 from 192.168.2.1
root@ev3dev:~#

To Linux, everything is a file, and the LEGO motors are no exception. You talk to them by writing to a file, and you get back information by listing a file. Because this is Linux, the normal command-line prompts still work and you’ll find it useful to use cd to change directory and ls to see what files are at the current directory level.

To use this system from within Python, you have to manage an SSH session from within a program. Most people recommend using Pexpect, but you may have more success with Paramiko. Install it using the following:

pip install paramiko -–upgrade

The following code shows you how to open, run, and close an SSH session from Python.

'''
ssh Sesson for EV3dev
'''
import paramiko
import sys
import math, time, os
# Create an SSH client
ssh = paramiko.SSHClient()
shell = " ; debug = True
def main():
   print"Wait for brick to be contacted"
   login()
   print "Now talking to brick"
   send_command("lsn") # Linux list directory
   ssh.close()
   print "Now shutting down"
   sys.exit()
# functions for accessing EV3dev
def login():
    global shell
    ssh.load_system_host_keys()
    ssh.load_host_keys(os.path.expanduser(
                      "~/.ssh/known_hosts"))
    # Make sure that we add the remote server’s 
    # SSH key automatically
    ssh.set_missing_host_key_policy(
                     paramiko.AutoAddPolicy())
    paramiko.util.log_to_file("ev3brick.log")
    # Connect to the EV3 brick 
    # change password to your own
    ssh.connect("10.42.0.3", username="root",
        password="legobrick9")
    # Create a raw shell
    shell = ssh.invoke_shell()
    wait_for_prompt()
    send_command("cd /sys/class/tacho-motorn") 
    #makes commands shorter
def wait_for_prompt():
    # Create a new receive buffer
    receive_buffer = "
    while not "#" in receive_buffer:
        # Get the receive buffer
        receive_buffer += shell.recv(1024)
    if debug :
        print receive_buffer
def send_command(command):
    shell.send(command)
    receive_buffer = "
    while not "#" in receive_buffer:
        receive_buffer += shell.recv(1024)
    if debug or "-bash" in receive_buffer :
        print receive_buffer
if __name__ == '__main__':
    main()