Simulink

Communicating with LEGO® MINDSTORMS® NXT™ Hardware

This example shows you how to tune the parameters and monitor the signals of an algorithm running on LEGO MINDSTORMS NXT hardware.

Introduction

Simulink Support Package for LEGO NXT MINDSTORMS Hardware enables you to monitor and tune algorithms running on LEGO NXT MINDSTORMS hardware from the same Simulink models from which you developed the algorithms.

In this example you will learn how to tune and monitor the algorithm in real time as it is executing. When you are developing algorithms, it is often necessary to determine appropriate values of critical algorithm parameters in iterative fashion. For example, an algorithm that detects hand clapping may use a threshold to determine hand clapping in the presence of ambient noise. If the threshold value is set too low, the algorithm may confuse any sound for hand clapping. If the threshold value is set too high, the algorithm may not be able to detect any sound at all. In such cases, the right threshold value may be obtained by trying different values until the desired algorithm performance is reached. This iterative process is called parameter tuning.

Simulink's External mode feature enables you to accelerate the process of parameter tuning by letting you change certain parameter values while the model is running on target hardware, without stopping the model. When you change parameter values from within Simulink, the modified parameter values are communicated to the target hardware immediately. The effects of the parameters tuning activity may be monitored by viewing algorithm signals on scopes or displays in Simulink.

This example introduces the Simulink External mode feature by showing you how to:

  • Configure communications between Simulink and LEGO MINDSTORMS NXT hardware

  • Tune parameters of an algorithm from the same Simulink model that is running on the LEGO MINDSTORMS NXT hardware

  • Use Simulink scopes to monitor results from an algorithm running on LEGO MINDSTORMS NXT hardware

Prerequisites

We recommend completing Getting Started with LEGO MINDSTORMS NXT HardwareGetting Started with LEGO MINDSTORMS NXT Hardware example.

Required Hardware

To run this example you will need the following hardware:

  • LEGO MINDSTORMS NXT Intelligent Brick

  • USB cable

  • LEGO MINDSTORMS NXT-compatible Bluetooth® dongle

Task 1 - Set up the Bluetooth Connection

In this task, you will set up a Bluetooth connection between your computer and LEGO MINDSTORMS NXT hardware.

1. Connect the Bluetooth dongle to your computer. Windows® finds the driver automatically.

2. Turn LEGO MINDSTORMS NXT Intelligent Brick on. Using the menu on the LCD, select Bluetooth > On/Off > On.

3. On your Windows 7 computer, open Control Panel > Devices and Printers, and select Add a Bluetooth device.

4. In the Add a device dialog box, select your LEGO MINDSTORMS NXT hardware, and click the Next button.

5. Press the orange button on the LEGO MINDSTORMS NXT Intelligent Brick to accept the default passkey, 1234.

6. On your computer, enter 1234 as the pairing code.

Note that other versions of Windows may have different procedures for adding a Bluetooth device. For more information, search the product help for "Troubleshooting Bluetooth".

Task 2 - Run the Model in External Mode

1. Connect a USB cable from your computer to the LEGO MINDSTORMS NXT Intelligent Brick.

2. Open the Communicating with LEGO MINDSTORMS NXT HardwareCommunicating with LEGO MINDSTORMS NXT Hardware model.

3. In the model, make sure that the Simulation mode on the toolbar is set to External.

4. In the model, click the Run button on the toolbar.

Task 3 - Communicate with the Model

Notice the subsystem called Detect Clapping in the model. As it detects hand clapping, this subsystem toggles the value of a signal that controls which song is played on the LEGO MINDSTORMS NXT speaker. To detect the hand clapping, subsystem compares the sound level measured by the LEGO MINDSTORMS NXT Sound Sensor block with a preset threshold. In this task, you will find the threshold value that enables the algorithm to detect the hand clapping and toggle the choice of the song played.

1. Open the Scope block. The scope displays the signal that selects which song is played on LEGO MINDSTORMS NXT speaker. If the signal has the value of 0, the Song2 is played. Otherwise, the Song1 is played.

2. Clap your hands and listen to the song played while looking at the scope block. You will not be able to make the song change. This indicates that the selected threshold is probably too high.

3. Double-click the Threshold block in the model, change its value, and click OK or Apply.

4. Clap your hands again and listen to the song played while looking at the scope block. If there is no change, repeat the Steps 3 and 4 until you find the right value of threshold.

5. Click Stop button in the Simulink model.

Other Things to Try

Tune other parameters and monitor other signals in the model. For example:

  • Tune the volume of the song (Hint: see Speaker block options).

  • Monitor the value of the select signal directly on LEGO MINDSTORMS NXT Intelligent Brick.

Summary

This example showed a workflow for tuning and monitoring an algorithm running on LEGO MINDSTORMS NXT hardware. In this example you learned:

  • How to set up and establish a Bluetooth connection between a computer and LEGO MINDSTORMS NXT hardware.

  • How to tune an algorithm parameter on LEGO MINDSTORMS NXT hardware using the External mode feature.