The Simulink Support Package for LEGO MINDSTORMS Hardware enables you to monitor and tune algorithms running on LEGO 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.
The External mode feature in Simulink 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 parameter 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 hardware
- Tune the parameters of an algorithm from the same Simulink model that is running on the LEGO MINDSTORMS hardware
- Use Simulink scopes to monitor the results from an algorithm running on LEGO MINDSTORMS hardware
We recommend completing the Getting Started with LEGO MINDSTORMS Hardware example.
To run this example, you will need the following hardware:
- LEGO MINDSTORMS Intelligent Brick
- USB cable
- LEGO MINDSTORMS –compatible Bluetooth dongle
In this task, you will set up a Bluetooth connection between your computer and the LEGO MINDSTORMS hardware.
1. Connect the Bluetooth dongle to your computer. Windows finds the driver automatically.
2. Turn on the LEGO MINDSTORMS Intelligent Brick. 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 hardware, and click the Next button.
5. Press the orange button on the LEGO MINDSTORMS 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.”
1. Connect the USB cable from your computer to the LEGO MINDSTORMS Intelligent Brick.
2. Open the ‘Communicating with LEGO MINDSTORMS 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.
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 speaker. To detect the hand clapping, subsystem compares the sound level measured by the LEGO MINDSTORMS 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 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 Steps 3 and 4 until you find the right value of threshold.
5. Click the Stop button in the Simulink model.
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 Intelligent Brick.
This example showed a workflow for tuning and monitoring an algorithm running on LEGO MINDSTORMS hardware. In this example you learned how to:
- Set up and establish a Bluetooth connection between a computer and LEGO MINDSTORMS hardware.
- Tune an algorithm parameter on LEGO MINDSTORMS hardware using the External mode feature.
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. Please see mathworks.com/trademarks for a list of other trademarks owned by The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective owners.