Loginskip to content

May 21st, 2007

NXT Radar - Part III

In this post I’ll describe how I programmed the NXT Radar using LabView. I’ve decided to use LabView, the core engine of NXT-G, to demonstrate how to use a more ‘mature’ graphical programming language for NXT. The NI LabView toolkit for Mindstorms NXT (download free in NI website) allows, besides making new NXT-G blocks (which I’ve covered some advanced issues of in previous posts), to compile programs for the NXT. The compiler used is the same as in NXT-G, yet one has more access to low-level implementation of the firmware commands and typically less overhead. In writing the code I decided to adopt a unconventional approach - let the NXT stream data to the PC as the Bluetooth master. To do this, one simply connect to the PC from the Bluetooth menu on the NXT. The current fantom driver, however, does not support this mode. The fantom driver is the interface layer between the Bluetooth serial port and all other programs. Allmost all NXT fly-by-wire programming tools (for LabView, .NET, C# etc.) use fantom to communicate with the NXT. To overcome the fantom limitations, I used direct serial port commands in LabView, accessible in the VISA subVI menu.


(zoom)

The most important subVI I use is my ‘Get BT Message.vi’ whose diagram is shown above. This VI checks if there are at least two bytes waiting in the serial port buffer (each BT packet starts with 2 bytes packet size). If not - it simply returns with a false value in ‘Valid?’. If true, it converts the read 2 byte string into unsigned integer (U16) with ‘Unflatten from String’. An important issue here is the use of little-endian, as opposed to the ‘Unflatten from String’ default which is big-endian. The VI then waits for the whole packet to arrive, and decompose it into a mailbox, a size and the actual message (sent as a null-terminated string).


(zoom)

The VI shown above is compiled and run on the NXT. One must use the USB cable to download this when the NXT is configured as the BT master. This VI simply rotates the radar sensor, and sends the value of the Sharp IR sensor and the rotation sensor as a single BT message.


(zoom)

Last, but not least, is the main LabView PC-side program. This VI initialize the BT serial port for getting the incoming data stream. One crucial potential problem is the ‘Termination Character’ usually used in serial communication. Typically when the line-feed char (0×0A or ASCII code 10) is found in the incoming serial buffer, any read buffer command will get the data truncated at that point. This is very bad when binary data is transmited over the serial port, as is the case in this program. Fortunatly, the ‘VISA Configure Serial Port’ VI has an optional terminal which can disable this. After the port is opened, the VI iterates the main loop. There it waits till a valid BT command is found (or front panel Stop button pressed), decodes the two sent data pieces from the NXT code shown before, and creates a radar-like polar display using the ‘Polar Plot’ VI (which exists only on newer LabView professional/full versions).

The above programs, allthough clearly tailored for the NXT Radar, can be modified to allow any kind of direct data streaming from the NXT to the PC. In the near future I’ll post on a simple off-brick data logging tool using this technique.

Guy Ziv
NXTasy.org