This example illustrates communication with a Lego Mindstorms NXT brick using text commands sent over the Bluetooth Serial Port Profile.
Before setting up a connection in MATLAB, the NXT brick has to be paired with your computer. Once this is done, the device will be visible in MATLAB.
To get a list of all the available Bluetooth devices use the instrhwinfo
command. Get the "friendly" names of the Bluetooth devices available using the RemoteNames
field from the output of instrhwinfo
.
bluetoothDevices = instrhwinfo('Bluetooth')
remoteNames = bluetoothDevices.RemoteNames
bluetoothDevices = RemoteNames: {'NXT'} RemoteIDs: {'btspp://00165310E7C4'} BluecoveVersion: 'BlueCove-2.1.1-SNAPSHOT' JarFileVersion: 'Version 3.2' remoteNames = 'NXT'
Get the details of the NXT brick.
deviceInfo = instrhwinfo('Bluetooth','NXT')
deviceInfo = RemoteName: 'NXT' RemoteID: 'btspp://00165310E7C4' ObjectConstructorName: {'Bluetooth('NXT', 1);'} Channels: {'1'}
The device's RemoteName
is NXT
and its Channel
is 1
. Construct a bluetooth object called bt
as shown by the ObjectConstructorName
in the previous step.
bt = Bluetooth('NXT', 1);
Now connect to the device.
fopen(bt);
The Lego Mindstorms data sheet defines a protocol for communicating with NXT brick. It specifies that to get the device information, the command should have following format:
Bytes 1:2 - Length of command
Byte 3 - Type of command
Byte 4 - Additional information to execute the command
Using this information, raw data packet is formed which is sent to the NXT brick.
Bytes 1:2 - Length of command = 2
Byte 3 - Type of command = 1 (since it is a system command with reply from device)
Byte 4 - 0x9B (This value is defined by the Lego Mindstorms NXT communication protocol)
This translates the raw data packet to be: [2 0 1 155]
. Write this data to the brick.
fwrite(bt,[2 0 1 155]);
The device then gives a response containing device information. The response has following format:
Bytes 1:2 - Length of response
Byte 3 - Type of command issued
Byte 4 - Data sent to device
Byte 5 - Command status
Bytes 6:20 - Name of device (14 characters + null terminator)
Bytes 21:27 - Bluetooth address
Byte 28 - LSB of Bluetooth signal strength
Byte 29 - Not defined
Byte 30 - Not defined
Byte 31 - MSB of Bluetooth signal strength
Byte 32 - LSB of user flash
Byte 33 - Not defined
Byte 34 - Not defined
Byte 35 - MSB of user flash
Now read the response from the brick to get its information
btResponse = fread(bt,32)'
btResponse = Columns 1 through 13 33 0 2 155 0 78 88 84 0 0 0 0 0 Columns 14 through 26 0 0 0 0 0 0 0 0 22 83 16 231 196 Columns 27 through 32 0 0 0 0 0 132
In this example, the response from the NXT brick is:
Bytes 1:2 - Length of response = 33
Byte 3 - Type of command issued = 2
Byte 4 - Data sent to device = 0x9B
Byte 5 - Command status = 0 (command was successful)
Bytes 6:20 - Name of device (14 characters + null terminator)
Bytes 21:27 - Bluetooth address
Byte 28 - LSB of Bluetooth signal strength = 0
Byte 29 - Not defined = 0
Byte 30 - Not defined = 0
Byte 31 - MSB of Bluetooth signal strength = 0
Byte 32 - LSB of user flash = 0
Byte 33 - Not defined = 0
Byte 34 - Not defined = 0
Byte 35 - MSB of user flash = 132
Let us now read only the name of the brick from its response. We know that bytes 6 to 20 correspond to the brick's name.
btName = char(btResponse(6:20))
btName = NXT
Clear the buffer of the Bluetooth object
flushinput(bt);
Let us now play a tone at frequency 1500 Hz for 1000 ms on the brick. The Lego Mindstorms NXT direct commands define a command to play a tone on the brick as follows:
Bytes 1:2 - Length of command
Byte 3 - Type of command
Byte 4 - Message id
Bytes 5:6 - Frequency of the tone in Hz
Bytes 7:8 - Duration of the tone in ms
In this example, the raw data packet is formed as follows:
Byte 1:2 - Length of command = 6
Byte 3 - Type of command = 0 (since it is a direct command with reply from device)
Byte 4 - Message id = 3
Bytes 5:6 - Frequency of the tone is 1500Hz, which translates to uint8[220 5]
Bytes 7:8 - Duration of the tone is 1000 ms, which translates to uint8[232 3]
Hence the command to be written to the brick is [6 0 0 3 220 5 232 3]
.
fwrite(bt,[6 0 0 3 220 5 232 3]);
In addition to the audio tone played on the NXT brick, the device also gives a 5 byte response. Let us now read the response from the brick. The response has the following format:
Byte 1:2 - Length of response
Byte 3 - Type of command issued
Byte 4 - Message id
Byte 5 - Status byte
btResponse = fread(bt,5)'
btResponse = 3 0 2 3 0
In this example, the response from the NXT brick is:
Byte 1:2 - Length of response = 3
Byte 3 - Type of command issued = 2
Byte 4 - Message id = 3
Byte 5 - Status byte = 0 (command was successful)
fclose(bt);
delete(bt);
clear('bt');
You can get more information on communicating with Lego Mindstorms NXT using high level commands here.