-deleted-
11 years, 1 month ago.

regarding matlab interfacing for mbed

Hi,can anyone tell me how to interface matlab with mbed nxp lpc 1768 to see the sensor data in the form of graph??and also please tell me where can i find lpy530al gyrosensor header file.

can anyone help please

posted by -deleted- 12 Apr 2013

2 Answers

-deleted-
poster
11 years, 1 month ago.

please help anyone...

11 years, 1 month ago.

There is no LPY530AL code on the mbed site, so you will at the very least have to port it from somewhere else (I expect there to be arduino code for example), or write it yourself.

And for matlab interfacing you can just use the serial connection. Send data with mbed to matlab, let matlab read the data and make a graph.

thanks Erik for the reply.i installed serial connection,but i need an example to see how it is working.can you give me link of examples interfacing matlab with mbed..

posted by -deleted- 16 Apr 2013

This is a simple example I once made for matlab. The mbed here has to send the data as floats, with newlines in between them. So on mbed something like:

printf("%f\n", yourdata);

The corresponding matlab code:

%fclose(instrfind)  
data=0;
h=plot(data);
set(h,'YDataSource','data')
s1 = serial('COM11');
fopen(s1);
while(1)
    a=fgetl(s1);
    a=sscanf(a,'%f');
    data(end+1:end+1)=a(1);
    if length(data)>400
        data=data(end-400:end);
    end
    refreshdata(h);
    drawnow; pause(.001)
end

The first line closes all open comports. It has to be commented out the first time, after that you need it. Since this is very simply program you can only close it with ctrl+c, and that won't close the comport, so that needs to be done in the beginning of the program in the next run.

After that it opens a new plot, and serial connection (here on COM11, of course replace the one your mbed has). Then in the while loop it reads a serial line, gets the float value from it, and adds that to the end of the data that is to be plotted. If max length is reached it removes data from the start, and then it refreshes the graph + small pause to give it time to actually plot the data. This means you cannot send data too fast from the mbed.

posted by Erik - 16 Apr 2013

thanks erik for the example..but sorry for asking basic questions as i am new to this..can you please tel how to add mbed_MATLAB_lib.zip path tp matlab and also do we have to write program in both online compiler of mbed and in matlab??

posted by -deleted- 17 Apr 2013

You will require a program on your mbed that reads the sensor data and sends it to matlab. And on matlab you need that code to actually plot the graph.

And a zip is a bit weird as matlab path. But you can just paste that program in a new .m file, and then click on the run button in the matlab editor. If it is in the wrong path it will then ask you if it should change its path.

posted by Erik - 17 Apr 2013

ya when i extract that zip,i found add_mbed_to_path.m file ,when i run it i didnot get any errors then i tried flashinf led program in matlab i got errors..so do i have to write program for mbed also to flash led?

posted by -deleted- 17 Apr 2013

I have no idea what add_mbed_to_path is for kind of file. I guess it is related to RPC, I never used RPC, that is something different.

posted by Erik - 17 Apr 2013

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;

tell what else do i have to add for this program?

posted by -deleted- 17 Apr 2013

I have no experience with RPC, but the RPC page has an example program for RPC via serial: http://mbed.org/cookbook/Interfacing-Using-RPC

posted by Erik - 17 Apr 2013

oh fine..thanks a lot erik for the reply. i will try once again.if u find solution for this please let me know

posted by -deleted- 17 Apr 2013

in the above program you wrote just one line for mbed program. can you elaborate it

posted by -deleted- 17 Apr 2013

The matlab program expects floats being send over a serial connection, with after every float a newline character. That mbed command will send the value of 'yourdata' in the correct format. As an example you could use something like (minus typos that might be in it):

#include "mbed.h"

int main() {
    float val = 0.0;
    while(1) {
        val = val + 0.01;
        printf("%f\n", val);
        if (val>1)
            val = 0;
        wait_ms(10);
    }
}

You can watch in teraterm (or similar program) the output also, it just counts from 0 to 1. On matlab it will give you a sawtooth wave if you use it with the code I posted earlier (don't forget you have to uncomment the first matlab line after the first time).

If this is useful for you depends on your requirements. I use similar stuff to check sensor values in realtime in a handy graphical form.

posted by Erik - 17 Apr 2013

hi erik, can you explain when to use %fclose(instrfind) line in matlab.. first time when i execute the code i got the sawtooth.after that when i try once again ,it's not coming.please help

posted by -deleted- 19 Apr 2013

Warning: Unexpected Warning: A timeout occurred before the Terminator was reached. Attempted to access a(1); index out of bounds because numel(a)=0.

Error in trial (line 10) data(end+1:end+1)=a(1);

this is the error i got when i uncommented first line and run it...and also even in teraterm,it could not open mbed port.please reply

posted by -deleted- 19 Apr 2013

Restarting matlab should probably work then. The problem is the com port isn't properly closed with this very simple program. (And remove the mbed and plug it in again).

posted by Erik - 19 Apr 2013

can anyone give me sample matlab program for analog input values taken from mbed.and to plot graph versus time in matlab??

posted by -deleted- 26 Apr 2013