Introduction
This tutorial demonstrates how to use the Direct Command features of National Instrument’s LabVIEW NXT Toolkit to construct an application that can be used to control the electrical motors found in Lego Mindstorms NXT kit via a graphical interface.
Overview and Assumptions:
The tutorial is broken into two parts. This first part details how to layout the controls needed on the Front Panel to connect the application to a NXT Device. It also provides instructions on wiring these controls along with the rest of the application’s “base.” The second part of the tutorial explains how to add and wire the controls needed to operate a motor.
Before beginning, please note that this tutorial assumes the following:
- 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/).
- The reader has an understanding of LabVIEW and is familiar with the NXT Toolkit.
- The reader has successfully completed the Bluetooth Motor Control via Distance tutorial and has a functioning copy of BTM Send.vi and BTM Receive.vi.
Part 1: Connection Controls and Base Wiring
 |
Step 1-1: | Start by opening up a Blank VI and saving it as GUI Motor Control.vi. Add the following controls to the Front Panel, labeling them as specified. |
| Control | Label | String Control Square LED Ok Button Ok Button Stop Button | Device Name Connected Connect Reset NXT Disconnect |
|
Step 1-2: | Open up the Properties window for one of the buttons and click the Appearance tab. Make sure the settings there match those shown in the picture below and retype the button’s label in the text box under Off text. Click Ok and then repeat this procedure for the other two buttons. |
|
Step 1-3: | Next change the mechanical action for the Connect and Disconnect buttons to Latch When Released. For the Reset NXT button, change the mechanical action to Switch Until Released. |
Step 1-4: | Resize the controls as desired and switch to the Block Diagram. |
 |
Step 1-5: | Start work on the Block Diagram by adding a While Loop and wiring a False Constant to its conditional terminal. Next add a Flat Sequence Structure with three frames inside the loop. |
Step 1-6: | Add another While Loop to the inside of the first frame and wire its conditional terminal to the Connect object. |
Step 1-7: | In order to use NXT Direct Command VIs, a connection must be established between to application and a NXT Device. Accomplishing this requires the Find NXT and Create NXTObject VIs (both available under NXT Direct Commands>>Connection). Add both of these to the inside the second frame. |
Step 1-8: | Locate the Device Name object and wire it to the NXT Name terminal of the Find NXT VI. After that, wire the value 60 to the Bluetooth Search Timeout terminal which is also on the Find NXT VI. |
Step 1-9: | Right-click the Find NXT’s Connection Type terminal and create a constant. Switch to the operating tool change the value of the constant to Bluetooth. |
Step 1-10: | Wire the VISA Resource String output terminal on the Find NXT VI to the input terminal of the same name on the Create NXTObject VI. Also wire the Find NXT VI’s Error Out terminal to the Create NXTObject VI’s Error In terminal. |
Step 1-11: | While still working inside the second frame, add a Case Structure and wire its selector terminal to the Error Out terminal on the Create NXTObject VI. This should give the Case Structure two cases: Error and No Error. Add a One Button Dialog object to the Error case and wire the string Could not connect to NXT Device. to its Message terminal. |
|
Step 1-12: | Switch over to the No Error case and add a While Loop to the inside of it, wiring its conditional terminal to the Disconnect object. Then locate the Connect object and wire a True constant to it, making sure that both the constant and the object are inside the loop. |
 |
Step 1-13: | The Create NXTObject VI is very important when using NXT Direct Commands. Once a connection has been established with an NXT Device, this VI creates a reference to that Device which is then passed to other Direct Command VIs via a wire chain using the NXTObject and NXTObject DUP terminals. Similarly, a wire chain of Error In and Error out terminals is used to pass information about any problems that may arise with the Device or the connection to it. With this in mind, add two pairs of Shift Registers to the While Loop. Using the registers on the left side of the loop, wire one to the NXTObject terminal on the Create NXTObject VI and the other to its Error Out terminal via the Case Structure’s selector terminal. |
Step 1-14: | Next add the Start Program VI (available under NXT Direct Commands>>Program Execution) to the inside of the loop. This particular VI will start the BTM Receive program residing on the NXT Device controlled by the application (Refer to the Bluetooth Motor Control via Distance tutorial for more details). Wire its NXTObject and Error In terminals to the appropriate Shift Register. Then wire the string BTM Receive to the VI’s Filename terminal. |
Step 1-15: | Now add an Event Case Structure. Making sure that the Timeout event case is displayed, wire the value 100 to the Timeout terminal in the upper right-hand corner of the structure. |
Step 1-16: | Wire the Start Program VI’s NXTObject DUP and Error Out terminal to their corresponding Shift Registers on the right side of the While Loop. When wiring each terminal, be sure to click once inside the Event Case Structure so that the wiring passes through it. A visible indication of this will be square wire tunnels on each side of the structure. |
|
Step 1-17: | These next few steps will demonstrate how to wire the Reset NXT button. This button can be used to terminate the BTM Receive program running on NXT Device in the event it becomes unresponsive. The program will then be restarted during the next iteration of the While Loop. Bring up the Edit Events window by right-clicking on the Event Case Structure and clicking Add Event Case. |
|
Step 1-18: | Scroll through the box under Event Sources until the Controls element is visible. Expand the list by clicking on the plus sign to the left of it and continue scrolling through the list of controls. Select NXT Reset and look at the list of events now being displayed in the box to the right. Scroll through and select the Mouse Up event, then click Ok. |
Step 1-19: | In the new event case, add the Stop Program VI (available under NXT Direct Commands>>Program Execution). Wire its NXTObject and Error In terminals to the appropriate wire tunnels on the left side of the Event Case Structure. Do the same thing with its NXTObject DUP and Error out terminals and the wire tunnels on the right side. |
Step 1-20: | Add another Stop Program VI to the outside of the While Loop but inside the No Error/Error Case Structure. Wire its NXTObject and Error In terminals to the appropriate Shift Registers on the right side of the While Loop. This will terminate the BTM Receive program before the application disconnects from the NXT Block. |
Step 1-21: | Add the Destroy NXTObject VI (available under NXT Direct Commands>>Connection) to the inside of the Case Structure, again wiring the NXTObject and Error In terminals to the corresponding output terminals on the Stop Program VI. This will delete the reference to the NXT Device that was used throughout the application. |
Step 1-22: | Now add the Simple Error Handler VI (available under Programming>>Dialog & User Interface) and wire the Error Out terminal of the Destroy NXTObject VI to its Error In terminal. |
Step 1-23: | Finally, add a Local Variable to the third frame that references the Connected object and wire a False Constant to it, then save. |
This concludes the layout of the connection controls and base wiring for the application. To test the application so far, make sure that the NXT Device it will be connecting to is on and then run the application. In the text control, type the name of the NXT Device (the name appears at the top of the Device’s display when it is turned on) and click Connect. Once connected, the LED will light up. Click Disconnect and watch as the LED goes dim again once the connection is terminated.
The next part will detail how add and wire the controls for the motor.
Part 2: Motor Controls
 |
Step 2-1: | Add the following controls to the Front Panel, labeling them as specified. |
| Control | Label | (Numeric) Knob Horizontal Toggle Press Button Horizontal Pointer Slide | DTR-A FR-A RT-A PWR-A |
|
| Note: The A at the end of each label indicates the port to which the motor that it will control should be connected. |
Step 2-2: | For each of the controls just added, bring up its Properties window. In the Appearance tab, uncheck Visible under Label; then check it under Caption. In the text box, also below Caption, type the caption that corresponds to each control. |
| Control | Label | (Numeric) Knob Horizontal Toggle Press Button Horizontal Pointer Slide | Degrees To Rotate Forward/Reverse Rotate Power |
|
Step 2-3: | Once each of the captions has been set, bring up the Properties window for the Knob. Under the Appearance tab again, check Show Digital Display(s) at the bottom. Next select the Scale tab. Under Scale Range, set the minimum to 0 and the maximum to 90 (or higher if more degrees of rotation are desired). |
|
Step 2-4: | After that, 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.
|
|
Step 2-5: | Next bring up the Properties window for the Horizontal Pointer Slide. Under Scale Range, set the minimum to 0 and the maximum to 100. As before, select the Format and Precision tab and then select Scale from the drop-down menu. Also as before, select Floating point from the list box on the left, specify under Digits 0 and then select Digits of precision from the drop-down menu under Precision Type. Click Ok. |
Step 2-6: | To finish the additions to the Front Panel, change the mechanical action for the Press Button to Switch Until Released. Now move over to the Block Diagram. |
 |
Step 2-7: | Find the Event Case Structure we added earlier in Part 1 and bring up its Edit Events window by adding another event case. In the window, scroll through the box under Event Sources until the Controls element is visible. Expand the list by clicking on the plus sign to the left of it and continue scrolling through the list of controls. Select RT-A and look at the list of events now being displayed in the box to the right. Scroll through and select the Mouse Up event. Then click Ok. |
|
Step 2-8: | Inside the new event case, add the BTM Send subVI created in the Bluetooth Motor Control via Distance tutorial. Wire its NXTObject, Error In, NXTObject DUP and Error Out terminals to the appropriate wire tunnels on the outside of the Event Case Structure. |
Step 2-9: | Locate the FR-A, PWR-A, and DTR-A objects and move them inside of the event case. Wire the FR-A object to a Not operator and then the Not Operator to the Direction (F/R) terminal on the BTM Send VI. Next wire the PWR-A object to the VI’s Power terminal and the DTR-A object to its Degrees terminal. |
Step 2-10: | Now right-click on BTM Send’s Port terminal and create a constant. Using the operating tool, change the value of the constant Port A and then save. |
Final Notes
Before running the application, first make sure the motor to be controlled is connected to Port A on the NXT Device. Also make sure that the Device is powered on and has BTM Receive download onto it. Once the application has been started, connect to the NXT Device as detailed at the end of Part 1. While still connected, adjust the Power slider to 50 and the Degrees To Rotate knob to 45. Press Rotate and watch as the motor rotates forty-five degrees. To go in reverse, simply switch the toggle and click Rotate again. When finished, click Disconnect.
For the purposes of simplifying things, only one set of motor controls were wired in this tutorial. However, by repeating Part 2 for ports B and C, this application can very easily be expanded to control up to three motors from the same interface. Such an application is demostrated in the 3 DOF Lego Minstorm Arm (LabVIEW Controlled) video available in the Media Archive section of this website.