Drexel University's Logo

Clayton McNeil

Bluetooth Motor Control via Distance


Home
About Me
Tutorials
Media Archive
Resources

Introduction

This tutorial will introduce a way to accurately control the rotation of motors in Lego Mindstorms NXT models from within applications designed using the Direct Command features of National Instrument’s LabVIEW NXT Toolkit.

NXT Direct Commands make use of the Fantom driver, which allows applications running on a computer to control NXT models via Bluetooth or USB. This freedom from having to download programs to the NXT Device and run them there allows for development of GUIs that further take advantage of LabVIEW’s capabilities. Unfortunately, unlike the “Motor Distance” VI included in the NXT Library (the NXT Library is comprised of VIs for use in programs that will be run on the NXT Device), the Direct Commands portion of the NXT Toolkit lacks an analog for controlling motor rotations.

Overview and Assumptions:

While attempts have been made to control motor rotation on the application side alone, the methods utilized can be complicated to implement and, due to latency issues related to the use of Bluetooth, unreliable. To simplify things and improve accuracy, this tutorial demonstrates an approach which uses a program continuously running on the NXT Device to manage the motors while a special subVI that communicates with it provides control to the application running on the computer.

Before beginning, please note that this tutorial assumes the following:
  1. The reader has access to a copy of National Instrument’s LabVIEW with the NXT Toolkit add-on installed (available at http://www.ni.com/academic/mindstorms/).
  2. The reader has an understanding of LabVIEW and is familiar with the creation of subVIs.

Part 1: NXT Receive Program


Completed Block Diagram for BTM Receive.vi
Step 1-1:
Start by opening up a Blank VI and saving it as BTM Receive.vi. Then switch to the block diagram.
Step 1-2:
In order for the subVI (detailed later) to communicate with this program, it must be ready to receive and act upon Bluetooth messages sent to it. To accomplish this, add the Read Message VI (available under NXTToolkit>>NXT Library>>BT Messages) and a Case Structure. With the Case Structure set to True, wire its selector terminal to the Message Received terminal on the Read Message VI.
Step 1-3:
The message sent by the subVI will be a specially formatted string containing instructions for controlling a specified motor. To process this string, add four String Subset objects labeled Port, Direction, Power, and Degrees (these objects will be referred by their labels in the rest of the tutorial) to by inside of the Case Structure. Wire each these objects’ String terminals to the Text Out terminal of the Read Message VI.
Step 1-4:
Add the Motor Distance VI (available under NXTToolkit>>NXT Library>>Output). This will be what actually turns the motor.
Step 1-5:
The first character of the message string specifies the port, which represents the motor connected to it. To extract this information, use the Port object. Wire the value 0 to its Offset terminal and the value of 1 to its Length terminal.
Step 1-6:
Add another Case Structure to the diagram and wire its selector terminal to the Port object’s Substring terminal. The Case Structure should contain the following cases:
Case:Value-'A':0,'B':1,'C':2
Wire the wire tunnel on the right of the Case Structure to the Output terminal of the Motor Distance VI.
Step 1-7:
The next character in the message string indicates whether the motor is to move in the forward or reverse direction. Wire the value 1 to both the Offset and Length terminals of the Direction object.
Step 1-8:
Again, add another Case Structure and wire its selector terminal to the Direction object’s Substring terminal. The Case Structure should contain the following cases:
Case:Value-'F':True,'R':False
Wire the wire tunnel on the right of the Case Structure to the Direction terminal of the Motor Distance VI.
Step 1-9:
Characters three through five specify the power level at which the motor should be ran, for example 010 represents 10%. Wire the values 2 and 3 to the Offset and Length terminals, respectively, of the Power object. Add a Decimal String To Number object and wire its String terminal to the Power object’s Substring terminal, then wire its Number terminal to the Power terminal of the Motor Distance VI.
Step 1-10:
Since the rest of the string indicates how many Degrees the motor should turn, only the Offset terminal of the Degrees object needs to be wired – this time to the value of 5. Add and wire a Decimal String To Number object as in the previous step, this time connecting its String terminal to the Degree’s Substring terminal and its Number terminal to the Distance in Degrees's terminal of the Motor Distance VI.
Step 1-11:
As an optional step, add the Play Tone VI (available under NXTToolkit>>NXT Library>>Sound). This will cause the NXT Device to a play a sound whenever it receives a message – very useful when debugging connection issues.
Step 1-12:
Finally, place everything inside a While Loop and wire a False Constant to its conditional terminal.

Part 2: NXT Receive Program


Completed Block Diagram for BTM Send.vi
Step 2-1:
Open up a Blank VI and save it as BTM Send.vi.
Step 2-2:
Add the following controls to the Front Panel, labeling them as specified.
ControlLabel
Radio Buttons
Vertical Toggle
(Numeric) Knob
Numeric Control
Ports
Direction (F/R)
Power
Degrees
Step 2-3:
Ensure the Radio Buttons control has three radio buttons, then label them as Port A, Port B, and Port C.
Step 2-4:
In the Properties window for the Power knob select the Scale tab and under Scale Range specify a minimum value of 0 and a maximum value of 100.
Scale tab in properties window for the Power knob
Step 2-5:
Next select the Format and Precision tab. In the drop-down menu select Scale, and then select Floating point from the list box on the left. Under Digits, specify 0 and then under Precision Type select Digits of precision from the drop-down menu. Then click Ok.
Format and Precision tab in properties window for the Power knob
Step 2-6:
Follow the same instructions from Step 2-5 for the Degrees indicator except select Value from the drop-down menu instead of Scale.
Step 2-7:
Nearly all NXT Direct Command VIs possess the same four special terminals. Since the subVI presently being constructed will also be a NXT Direct Command VI, it is important that these be added. The easiest way to obtain the controls and indicators which represent these terminals is to take them from one of the supplied NXT Direct Command VIs. Start by switching to the Block Diagram. Add one of the NXT Direct Command VIs, such as the Play Sound File VI (available under NXT Direct Commands>>Sound and then double-click it to open a new LabVIEW window displaying its Front Panel.
Step 2-8:
Depending on which VI was opened, the resulting Front Panel will show many different controls and indicators; however, there should be four sets labeled NXT Object, error in (no error), NXT Object dup, and error out. These are the only ones of value presently. Select each set and then copy and paste them into the BTM Send.vi Front Panel.
Front Panel for PlaySoundFile.vi
Step 2-9:
Once all four sets of controls/indicators have been transferred, close all LabVIEW windows associated with the NXT Direct Command VI used to obtain them. If prompted to save the file, choose Don’t Save.
Step 2-10:
Return to BTM Send.vi. With its Front Panel complete, switch over to the Block Diagram.

Figure 2-3: Completed Block Diagram for BTM Send.vi
Step 2-11:
Before beginning, locate the NXT Direct Command VI added earlier (the one which was double-clicked) and delete it. Then add a Concatenate Strings object to the Block Diagram and make sure it has four input terminals (Strings 0 through String 3). If it does not, add additional inputs by right-clicking on the left side of the object and then clicking Add Input.
Step 2-12:
Add a Case Structure and wire its selector terminal to the object labeled Ports. The Case Structure should contain the following cases:
Case:Value-Port A:'A',Port B:'B',Port C:'C'
Wire the wire tunnel on the right of the Case Structure to the String 0 terminal of the Concatenate Strings object.
Step 2-13:
Add another Case Structure and wire its selector terminal to the object labeled Direction (F/R). The Case Structure should contain the following cases:
Case:Value-True:'F',False:'R'
Wire the wire tunnel on the right of the Case Structure to the String 1 terminal of the Concatenate Strings object.
Step 2-14:
Add two Number To Decimal String objects, wiring one to the Power object and the other to the Degrees object via the Number terminal. Then wire the value 3 to both of their the Width terminals.
Step 2-15:
Locate the Number To Decimal String object connected to the Power object and wire its Decimal Integer String terminal to the String 2 terminal of the Concatenate Strings object. Then wire the same terminal of the other Number To Decimal String object to the String 3 terminal.
Step 2-16:
Add the Message Write VI, (available under NXT Direct Commands>>BT Messages) and Wire its Message terminal to the Concatenated String terminal of the Concatenate Strings object.
Step 2-17:
Locate the objects labeled NXT Object, error in (no error), NXT Object dup, and error out and wire each to its corresponding terminal on the Message Write VI (the terminals and objects have the same names).
Step 2-18:
Finally, switch back to the Front Panel and wire each control/indicator (or set) to an appropriate terminal on the Connector Pane. Edit the subVI logo if desired and save.

Implementation Notes

In order to utilize the subVI created above in a LabVIEW application, the program created in Part 1 must first be downloaded to the NXT Device. Furthermore, this program must also be running on the Device in conjunction with the LabVIEW application running on the computer. This can be accomplished by either manually starting the program, or by using other NXT Direct Command VIs, such as the Start Program VI, within the application. It should be noted that since neither the program nor the subVI contains any sort of data validation or error trapping as it relates to the message string sent or received; it is up to the implementer to ensure that all values passed to the subVI are valid. Other than that, the subVI works just like any other NXT Direct Command VI.


Home | About Me | Tutorials | Media Archive | Resources