How do I set up a USB communication with an NXT brick running nxtOSEK?

July 21, 2012 in LEGO, NXT Brick, Python, Tutorials

Introduction

In this tutorial we will implement a simple application to study how to establish a communication between a Mac (running Snow Leopard, but everything should apply to newer versions of the OS) and a NXT brick running nxtOSEK. The software module running on the brick is written in C, whereas the one running on the Mac is written in Python. Note that we will not rely on the Fantom driver traditionally used for these types of tasks (a superfluous, anachronistic, closed source, 32 bit only driver… come on LEGO!).

Our applications will echo the messages written on a terminal to the NXT display and will receive a notification from the brick that states the string was received successfully.

It is my hope to utilize this communication approach in a system where high priority tasks are managed on a brick using a real time OS (such as nxtOSEK on the NXT brick), whereas lower priority, more computationally intensive tasks are performed on a remote platform (in this case the Mac, but possibly a single board computer like the Raspberry Pi or the BeagleBoard which can be hosted on the LEGO robot). Stay tuned…

Finally note that these notes were heavily inspired by the sample examples contained in the nxtOSEK package (in particular usbtest) and by the nxt-python package (which unfortunately requires the Fantom driver and does not natively interact with nxtOSEK). I gratefully acknowledge all the people that contributed to such projects.

Preliminaries

This is a list of the things you may want to read or set up before continuing:

  • An NXT  brick with nxtOSEK installed and the ARM toolchain (see this post to learn how to have both things up and running)
  • The PyUSB python package
  • A superficial knowledge regarding how the USB communication works (just take a step beyond the plug & pray approach we are used to). I find the discussion USB in a NutShell extremely good (no need to read everything, just the sections referring to the USB features mentioned in the tutorial)

USBLoopBack

This is the module that will run on the NXT brick under nxtOSEK. The code can be dowloaded from GitHub. The repository should be checked out inside the folder ${LEGO}/nxtOSEK/samples_c/ (inside such folder, simply issue the command git clone https://github.com/zuliani/USBLoopBack.git). To build the executable run make all and then download the file to the brick using the command ./nexttool /COM=usb -download=${LEGO}/nxtOSEK/samples_c/USBLoopBack/USBLoopBack.rxe (recall that the nexttool command is in ${LEGO}/nxtOSEK/bin/)

The code is based on a modified version of the sample usbtest and it should be pretty self-explanatory. It will listen to string sent via USB and print them on the brick’s display. If everything went well, it will send back an acknowledgment notification to the sender.

USBLoopBackTester

This is the python script that runs on the Mac and that will send the string entered from the console to the NXT brick. The code can also be found on GitHub and you should clone the script wherever you want using git clone https://github.com/zuliani/USBLoopBackTester.git. The code is rich of comments and it should be pretty straightforward to understand how it works.

Testing the Communication

Make sure the nxt Brick is connected to the Mac via USB and launch the USBLoopBack that you uploaded before. You should see this message on the display:

The message displayed on the brick when USBLoopBack is launched.

Now, following the instructions, we launch the python script: python USBLoopBackTester.py. The script will notify us that it is looking for a brick, saying Seeking for the first NXT block. Once the brick is found it will invite us to enter the string to be trasmitted to the brick. If the communication went fine and the brick sent back the acknowledgment string, the script will issue the message Acknowledgment string received from the brick. The screenshot of my terminal looks like:

The messages on the terminal returned by USBLoopBackTester and the strings sent to the brick.

At the same time, you should see the messages echoed on the brick’s display:

The messages entered from the Mac terminal are displayed on the brick.

Note that the number in square brackets indicates the number of bytes transmitted back by the brick to the python script as an acknowledgment string.