MATLAB from Mathworks is a widely known and capable scripting language which can be great for controlling hardware. The Formula AllCode is no exception and in this blog we will look at how we interface MATLAB to the Formula AllCode hardware.
On loading MATLAB I can poll the Bluetooth functionality by entering this command and hitting enter.
This lists all of my paired Bluetooth devices in the command window.
I can assign the Bluetooth device object to a global variable by entering the following code. Note that this command takes a fair old while to execute so I haven’t included it in the example scripts. Simply remember to execute this line of code before you call anything else to ensure the FA_BT variable is populated correctly.
>>FA_BT = Bluetooth(‘FA_XXXX’, 1);
The example script “example0SetupBTVAR” will do this for you but will need to be edited to insert your specific Bluetooth FA device name.
To view the contents of the variable are valid you can write the following into the command window.
Which should produce something like this in the command window.
The Bluetooth connection can then be established by using the following command.
And the connection can also be closed using this command.
So we can open and close the Bluetooth connection to the Formula AllCode lets start to add some sub routines to allow us to control the various aspects of the robot.
To start I created a new script called LEDTest, below is the contents of the file.
This allows me to connect to the robot, write several values to the LEDs and then close the data connection. The LEDWrite is a separate pre-written script file which can be downloaded towards the end of the post. The value 85 should light up every other LED on the robot and the value 170 should light up the remaining LEDs.
For reference here are the contents of the LEDWrite script. Note that the global FA_BT variable is used in the script as a handle to the robot communications channel.
In the next example we use the movement scripts to drive the robot in a square 100mm x 100mm. We use a loop to cut down on the code required and make the robot drive the four walls of the square.
Here is a more useful example to read the line sensors and control the motors so that the robot will follow a dark line on a light surface. Note that I have commented out a few lines of code to allow the robot to perform better once we know what values the robot is returning.
Here is another useful example to turn the Formula AllCode robot into a proximity sensor. If you go too close to it then it will beep and the time will be logged in the MATLAB command window. This time the program is terminated by pressing SW1 on the robot.
The examples and scripts shown in this blog are available to download from the link below.
6,543 total views, 4 views today