[FreeBSD Logo] [Apple Logo]
FreeBSD and MacPorts


General

Project Home
Consulting Services
C/Unix Programmer's Guide
FreeBSD and MacPorts
Which FreeBSD?
VEX Robotics under Unix
Creating Port-friendly Projects

Port Descriptions

APE
Roboctl
NBC/NXC

Links

FreeBSD Ports Project
MacPorts Project
Ports Source Code

Vex Robotics under Unix

This page is an information repository for programming Vex robots (and to some extent other PIC micro-controller based systems) under Unix. It's also a showcase for some popular virtualization tools used under Unix.

SDCC for Vex is here!


Caveat:
Using SDCC to program the Vex is unsupported and experimental. Improper programming may result in damage to your robot. I make no claims about the validity or safety of any information provided here.

SDCC (Small Device C Compiler) is an open source C compiler that supports several different micro-controllers. SDCC is available in both FreeBSD ports and MacPorts. See the SDCC WEB site for instructions on installing on other platforms. As of this writing (Dec 31, 2008), support for PIC MCUs is reported as "unfinished". Nevertheless, our initial results with SDCC on the Vex have been very successful.

Requirements

  1. You will need SDCC version 2.7.0 or later in order to successfully compile for the Vex's PIC 18F8520 MCU (use sdcc -mpic16 -p18f8520 file.c).
  2. To produce a hex file with the correct base address, you need to to use this linker script, which is a slight modification of the default, 18f8520.lkr.
  3. A great deal of firmware code is necessary to initialize and operate the Vex controller. The easiest way to get started is by using LibVexBot.

Vexctl

Programs can be uploaded to the Vex robot from *nix using the vexctl command, part of the roboctl project. Besides being a multi-platform program, vexctl also has the advantage of being much faster than the Windows IFI loader. Default firmware uploads on FreeBSD, Linux, and Mac consistently take about 5 seconds (2280 bytes/sec) vs. the 20-30 I've experienced with the Windows IFI loader.

Communicating with the controller requires a serial port and driver. Modern Macs do not have serial ports. The orange USB-Serial adapter in the Vex programming kit uses the PL-2303 chip set from Prolific. There are two drivers available for the Mac.

The commercial driver is available at this link. After installing the driver, plugging in the USB-serial adapter will create the device node /dev/cu.usbserial.

There is also a PL-2303 driver available at Sourceforge. It creates a device called /dev/cu.PL2303-0000101D. This driver is much slower, and doesn't seem to work with Parallels. (You may be unable to connect the USB device to Windows.)

I have also tried a Keyspan USB to serial adapter with vexctl and it worked very well. You may need a male-to-female serial extension cable (with screws on both ends) to work with adapters other than the Vex's. Standard male serial connectors have posts, while standard female connectors have screws. The Vex dongle doesn't follow this standard: It has a female serial connector and posts. This can be remedied with a serial extension cable that has screws on both ends.

The New LibVexBot C Library

LibVexBot is an opensource C library which makes it easy to program Vex robots using Microchip's MCC18 compiler. SDCC support is also in the works for a future version. Using LibVexBot is almost as easy as EasyC, without the limitations that hamper more advanced programmers. LibVexBot is FREE, and always will be. LibVexBot is released and distributed under the GNU Public License.

Why LibVexBot?

The Vex default code is full of cryptic references to PIC hardware resources whose names provide no clue as to what their purpose is in a Vex robot. In order to understand and modify the default code, the programmer has to locate these resources in the PIC spec sheet and/or header files. LibVexBot eliminates the need to know how PIC resources are wired to Vex ports.

For example, to enable interrupts on the Vex interrupt port 1, the default code uses the following code:

    INTCON3bits.INT2IE = 1;
    

In contrast, LibVexBot encapsulates the PIC-to-Vex port mappings, so you can program your Robot using simple, descriptive function calls that relate to the Vex controller as you see it:

    interrupt_enable(1);
    

LibVexBot supports all the common Vex sensors, using synchronous I/O ports or interrupts where appropriate. You can plug any sensor into any port: you need only provide the right port numbers when calling the API functions, e.g.

    #define BUMPER1_PORT                5
    #define LIGHT_SENSOR1_PORT          2
    #define INTERRUPT_PORT_FROM_SONAR   1
    #define IO_PORT_TO_SONAR            6
    
    io_get_digital(BUMPER1_PORT);
    io_get_analog(LIGHT_SENSOR1_PORT);
    
    sonar_init_async(IO_PORT_TO_SONAR,INTERRUPT_PORT_FROM_SONAR);
    
    sonar_emit_pulse_async(IO_PORT_TO_SONAR,INTERRUPT_PORT_FROM_SONAR);
    
    if ( sonar_data_available(INTERRUPT_PORT_FROM_SONAR) )
	sonar_distance = sonar_read_async(INTERRUPT_PORT_FROM_SONAR);
    
For more details, see the complete API documentation.

Beginning programmers can program the Vex with no knowledge of the PIC processor details. Since the library is 100% open source, more advanced programmers can learn more by studying the library code, and modifying it as they please.

The source code includes an sample program with a rewritten version of the VEX default user routine using only LibVexBot calls, and debug output for the remote control, a limit switch, bumper switch, light sensor, line-follower, shaft encoder, and ultrasonic range finder.

LibVexBot was developed using MCC18 under FreeBSD and Wine. MPLAB projects are also included with the source code for Windows users.

Terminal Emulators

In order to see debugging output from your Vex code, you'll need a terminal emulator that can receive output from the Vex serial port. Unix has a plethora of terminal emulators and serial communication tools dating back to the simple cu and tip commands, and pre-www geek toys like kermit and minicom. The nicest tool I've found for working with the Vex is cutecom. Another good option is PuTTY.

Cutecom under FreeBSD (click to enlarge)

[Cutecom

Cutecom is a simple, easy-to-use graphical terminal emulator for serial communications, based on the QT user interface library. It is specifically designed with hardware developers in mind, so it isn't cluttered with lots of stuff you won't need, and includes some nice features that you won't find in other tools (like hex output).

Piklab

Piklab is a KDE-based integrated development environment (IDE) for PIC micro-controllers. It can interface with SDCC as well as commercial PIC compilers. Piklab is available as an official FreeBSD port. An experimental MacPort is available as part of the collection on this site (installed by update_jb_ports).
Piklab running under PC-BSD (Click to enlarge)

[Piklab

Piklab MacPort (Click to enlarge)

[Piklab

Piklab includes a communications interface, but at the time if this writing, it cannot communicate with the Vex. Hence, if you program in Piklab, you will need to use vexctl from the command line to upload your programs.

MCC18 under Wine

One option for running Windows programs (on Intel Unix systems only) is the Wine API. Unlike hardware emulators and virtual machines, which emulate PC hardware in order to run a real Windows (or other) OS, Wine emulates the Windows API (application program interface) so that you can run Windows programs directly under Unix. (Intel based systems only.) This is a much more ambitious endeavor than emulating hardware and letting a real Windows system provide the API, but it eliminates the need for a Windows license and the space required for a Windows installation.

Wine allows us to run the MCC18 compiler from the Unix command line. I've written a set of simple wrappers to make the MCC commands (mcc18, mpasm, mplib, mplink) look like native Unix commands (i.e. you don't need to prefix every command with "wine"). They're available in my FreeBSD ports and MacPorts collections as lang/mcc18_wrapper. For other systems, just download the distfile, unpack it, and install the scripts somewhere in your PATH.

EasyC under Wine

My early attempts at running MPLAB and EasyC under wine met with mixed success, but wine 0.9.59 runs EasyC 2.9.08 almost flawlessly. I still haven't had much luck with MPLAB (student edition v6.61) under Wine, so I run it under a virtual machine (see below).

EasyC under Wine (Click to enlarge)

[EasyC

To install EasyC under Wine: (Contributed by Ivan "Rambius" Ivanov)

  1. Install Wine (e.g. from emulators/wine port on FreeBSD, or PBI on PC-BSD)
  2. Get EasyC (7-day trial available at Intelitek download site)
  3. wineconsole easyC_V2_for_Vex_2908.exe
  4. wineconsole "easyC V2 for Vex.exe"
You may need to specify the full pathname of the .exe file, unless you configure wine to find it automatically.

Virtualization

Some other options to program the Vex include running MPLAB, ROBOTC, or EasyC under a commercial virtual machine such as Parallels, VirtualBox, or the open source QEMU project.

VirtualBox is probably worthy of some special attention. It's very fast (on my MacBook), has a nice QT based interface and a feature set comparable to Parallels, and it's free and open source. It currently runs on Linux, Mac, Solaris, and Windows hosts, and will likely be available for other platforms in the near future.

You may be able to use the Windows IFI loader under the guest Windows OS with some finagling, or you can drop the .hex files onto the Unix filesystem from Windows and use vexctl from the command line to upload them.

Windows IFI Loader under Parallels

You must have a Mac serial port driver installed in order to use the USB-serial adapter under Parallels. (See discussion of PL-2303 drivers above.) Otherwise, Parallels won't be able to find the device and offer it to the guest OS through the Devices menu.

Commercial IFI Loader running under Parallels (Click to enlarge)

[IFI

QEMU

QEMU is an open source emulator and virtual machine that runs a wide variety of guest operating systems on a wide variety of hosts. It has some bottlenecks compared with other systems like Parallels and VirtualBox, but the QEMU 0.9.1 under FreeBSD with standard acceleration is plenty fast on my AMD64 system for running EasyC and MPLAB.

I install the QEMU FreeBSD port with the kqemu acceleration module enabled, and launch with:

    qemu -m 256 -localtime -usb -usbdevice tablet winxp.qcow2
    

QEMU run this way has been flawless for me. It does require a little patience, since it takes a few minutes to settle down after booting Windows, and Microsoft Update is rather slow (yes, even slower than on a real machine). You can make make it fairly painless by running Windows for a while every day so that Microsoft Update can do it's thing while you're not using Windows. If you only boot up Windows when you need to use it, you'll be competing with Windows Update all the time.

You can set up and run QEMU guests from the command line (which I prefer), or use a GUI interface like QEMU Launcher, qtemu or AQEMU, all of which are available as FreeBSD ports.

MPLAB under QEMU (click to enlarge)

[MPLAB

Standard kqemu acceleration currently only virtualizes the user code (i.e. non-privileged instructions run by programs). Adding

    -kernel-kqemu 
enables full acceleration, which will cause qemu to virtualize kernel-mode instructions as well. This makes a big difference in speed in some cases, but I've seen some instability when using it, which probably explains why it's not the default mode yet.

I recommend the qcow2 format and a generous virtual disk size (at least 10G for Windows XP). The qcow2 format limits the size of the file containing your virtual disk to the blocks actually used, so allocating a bigger virtual disk won't cost you any more disk space. Changing the size after Windows runs out of room is a real pain, however, so there's good reason not to allocate too small. My virtual disk (winxp.qcow2) has a size of 30G (this is what Windows sees), but currently only takes up 3.6G on my FreeBSD filesystem.

FreeBSD, Linux, and many other Unix systems support sparse files, so using the raw disk format with QEMU won't actually take up more disk blocks than the qcow2 image, initially. However, some common Unix tools used to copy files (e.g. scp) don't preserve sparse files, so I tend to avoid them. I was not able to measure any difference in speed between a Windows XP system on a qcow2 image and the same system on a raw image.