Bluetooth Serial Connection Matlab Gui
Posted By admin On 02.01.20Hi guys, Here's the solution: HC-05 is actually a bluetooth SPP device, so we cannot use traditional serial function in matlab, because its only support 'real' COM port. However, matlab give us a similar lib named 'bluetooth': You can apply ccde as below to access the HC-05: 1.
First make sure you havn't change the device name of you HC-05, default is 'HC-05', you can check it at your bluetooth Properities menu at Control Panel. Code%creat a bluetooth object%HC-05 channel default is 1 b = Bluetooth('HC-05',1); fopen(b);%write and read function fwrite(b,BluetoothWrite,'uchar'); BluetoothRead=fgets(b);%close and clear fclose(b); clear(b); 3. Try and enjoy Best, LU LI.
Table of Contents. Here is a library of classes for interfacing with mbed from MATLAB. This might be useful for things like:. Using MATLAB to aquire and process data from sensors connected to mbed. Using MATLAB to control actuators connected to your mbed. Implementing hardware in the loop programs in MATLAB, where the sensors and actuators are connected to mbed but MATLAB carries out the calculations and control.
This page gives examples of two methods of interfacing between MATLAB and mbed. MATLAB RPC This is a set of classes which allows you to control the mbeds inputs and outputs directly from MATLAB.
For many applications you may not need to write any mbed code. Serial Comunication These examples show how you can send and receive information over serial in MATLAB and so transfer information between a program running on mbed and MATLAB. Mymbed = mbed.SerialRPC('COM5', 9600); myled = mbed.DigitalOut(mymbed, mbed.LED1); To avoid a clash with the namespace name it is recommended that you do not use 'mbed' as a variable/object name.
A test connection, more robust argument checking and help infomation have also been added. Thanks goto for these improvements. In this example we control the mbed using the RPC. This makes it possible to remotely create objects and then execute their methods over any transport mechanism by sending the commands as a string. For methods which return a value the mbed returns that value as a string. Here are a set of MATLAB classes which expose a lot of the mbed API.
Classes for MATLAB RPC: This has been tested and confimed to work using:. R2008a and R2008b (32-bit Windows), with a minor warning. R2009a and later (both 32 and 64 bit Windows) Using the mbed MATLAB classes To use these classes download the above zip and extract the files to somewhere on your computer.
Then add the location of these files to the MATLAB path, the files contain a function 'addmbedtopath.m' which will do this for you. The aim in developing these classes has been to mirror the mbed API as closely as possible. This means that they follow the same object oriented approach as when you program on mbed, but with one key difference.
In MATLAB you must first create an mbed object and then when you create interface objects pass them that mbed object as this effectively passes information about how the mbed is connected and how to communicate with it. This method will allow you to communicate with multiple mbeds from MATLAB. HelloWorld import mbed. mymbed = SerialRPC('COM5', 9600) myled = DigitalOut(mymbed, LED1); for i = 1:1:10 myled.write(1); pause(0.5); myled.write(0); pause(0.5); end mymbed.delete; clear; The RPC library is contained within the 'mbed' namespace, this means that you must first either import the mbed package (using 'import mbed.' ) or prefix all classes with the namespace (eg 'mbed.SerialRPC(.)'). More examples of this format can be found in the source files. The first step when programming for mbed in MATLAB is to create your mbed object.
You do this by creating an mbed object from the class for the relevant transport mechanism. This can be Serial or HTTP.
%Calling methods from MATLAB%to set a DigitalOut dout1.write(1); dout3.write(0);%to set PwmOut pwm1.write(0.5); pwm2.period(1); pwm2.write(0.5);%to read an AnalogIn x = ain1.read%to read and write to a serial port device.puts('data to send out of p13 and p14'); if device.readable 1 Data = device.gets; end Note that the serial class only supports gets, getc, puts, putc, baud and readable/writeable. As the MATLAB classes actually create objects on the mbed it can become important to reset your mbed before running your MATLAB code as this will prevent there being too many objects or conflicting objects created on the same pin. You can of course do this by pressing the button or alternatively by using the reset function in the mbedSerial class. Mymbed = mbed.TestRPC; When you have finished testing your code, all you need to do is change this line of code to the type of transport mechanism you want to use. Communicating with Custom Code Most projects require not only simply Digital or Analog I/O but also make use of some of the many libraries for the mbed. These don't support RPC and nor does the MATLAB library provided here include support from them. Instead a mechanism which allows you to directly access variables on mbed or call custom functions is provided by the.
The MATLAB library provided here includes support for these objects. Function accell TIMEOUT = 5;%time to wait for data before aborting XPOINTS = 50;%number of points along x axis try%create serial object to represent connection to mbed mbed = serial('COM4'. 'BaudRate', 9600. 'Parity', 'none'. 'DataBits', 8. Mbed = serial('COM4'.
'BaudRate', 9600. 'Parity', 'none'. 'DataBits', 8. 'StopBits', 1); fopen(mbed); values = fscanf(mbed, '%f,%f,%f'); fclose(mbed); There is also an fprintf function which can be used to write data back to mbed over the same serial connection using the same syntax, though I've not played with this yet. Range Finder Demo MATLAB can be very useful for processing and manipulating data, particuarly when you have a lot of data points. This example shows how you can use the library above to work with both standard IO and custom code to allow use of many other interfaces such as I2C or SPI sensors This demo uses a mounted on a servo pan tilt mount to record lots of data and then uses MATLAB to process and display this data. This could be mounted on the front of a robot and used by the robot to find its way around its surroundings.
Matlab has complete control of the servos and the range finder in real time. The servos that control the direction of the Range Finder can be controlled directly from MATLAB as they just use PWMOut (though an external power supply needs to be used). The Ranger Finder is read using a RPCFunction object which returns a string consisting of just the measured distance in cm.
The actual read is done using the library that has already been written for the SRF08. The MATLAB script controls the servos, reads from the range finder and plots the points on a graph. The Matlab script ignores points which are out of range. A PTM button is also included on the circuit to demonstrate reading from the a DigitalIn and so that the range finder can be stopped during a scan.
This image shows the results for a singal sweep, there were too boxes infront of the scanner and you can clearly see where the gap is between them. The range finder gives good results but as it has a conical beam shape it tends to see edges as very blured, in this case it extends the edges of the boxes slightly. However the great advatage of having loaded the data from matlab is that it would now be possible to use Matlab to process the data to get a better idea of what objects are where.
Matlab Gui Tutorial
The points are held in a matirx and so it would be relativly easy to filter or manipulate them in a number ways so that your device could interpret what was in front of it. As the RangeFinder is mounted on a pan tilt mount the scan above can be repeated at different angles and could then be used to build up a 3D image of the surroundings and the extra data used to refine the accuracy. This is the matlab script used: Coming soon - video of the range finder in action.
Rs232 Serial Connection
Here is a slightly simplified version of the Matlab script which highlights the key parts of the program.