Pages: [1] 2   Go Down
Author Topic: Arduino to MATLAB  (Read 5811 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I want to pass some accelerometer and gyro signals through an Arduino NG into MATLAB for analysis. Does anyone have an API for this? MATLAB does not currently support Arduino.

Thanks, any thoughts on this would be helpful.

Jamie
Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this something you could do by loading a properly-formatted text file into MATLAB?  I'm speaking with utter ignorance of what MATLAB allows you to do... I mean, I know it has something to do with math, but other than that... EEK!  smiley

Anyway, perhaps you could Serial.print() properly formatted text, capture that text with a serial terminal program, save to a file, then load that into MATLAB?
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 572
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should just be able to get MATLAB to open the serial port and listen to incoming data. We did this using other serial sensors back in the Uni days, but I don't have a working version of MATLAB anymore to test it with Arduino.
Logged

uruguay
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
last year I had to search the web quite a lot on this subject. I needed to analyze an analog signal coming from a couple of hall effect sensors. I used an arduino for the A/D convertion of both sensors. As I don´t have Matlab's "serial communication" toolbox (don't know If it's called like that, but I guess it is..) I decided to try with scilab (an open source Matlab-like application). There is a scilab function here that may help you:

http://www.gds-sw.de/goodies.htm

it's called serial.dll, just select your COM port, bit rate,  byte length, parity, etc, and you will be able to  reciebe the data. scilab uses almost the same commands as Matlab.
I know it's not exactly wat you wanted, but give it a try if you have no other options

P.S:  http://www.scilab.org/

good luck



Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was wondering if any progress was made on this issue. I was hoping to send some ADC values to MATLAB.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

same here, I'd be very interested in this!

Regards,

Till
Logged

New Zealand
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
arduino: far too much like electrical engineering.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

MATLAB has a serial library, unless you're using a mac. Info in the MATLAB docs http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f105659.html. If like me you're using a mac, you're pretty stuffed, as 2007R1 and 2008R1 broke the 3rd party mac serial library. Hope this helps.

Logged


-- Alec

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you very much! I'll see what I can do with this when I have time.
Matlab is one of the reasons I am running bootcamp on my mac...

regards,

Till
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had the same problem and managed to solve it using the following generic Matlab Code to read from the Serial Port:

Code:
%Basic Comm
s = serial('COM5');
set(s,'BaudRate',9600);
set(s,'DataBits',8);
set(s,'StopBits',1);
fopen(s);
fprintf(s,'*IDN?')
out = fscanf(s);
fclose(s)
delete(s)
clear s

On the other hand I did some code to read signals from the ADXL30 with the arduino.
I programmed the arduino so that it would send a serial stream as the following
X<accX> [space] Y <accY> [space] Z [accZ] [CR]
where <accX> is the value of the A/D comming from the accelerometer.
I enclose the MATLAB side,

Code:
%Graph Accelerometer Data




s = serial('COM5');
set(s,'BaudRate',9600);
set(s,'DataBits',8);
set(s,'StopBits',1);
fopen(s);

out ='a'
figure
hold on
xacc =[];
yacc =[];
zacc =[];
for i = 1 : 5000
    out = fscanf(s);
    if(sum(size(out))>0)
        out2 = reshape(out(1:(round(size(out,2)/5)-1)*5),[5 (round(size(out,2)/5)-1)]);
        out2 = out2';
        out2 = str2num(out2(1:end,2:end));
        if out(1) == sprintf('X')
            xacc = [xacc out2(1:3:end)'];
            plot(xacc);
            yacc = [yacc out2(2:3:end)'];
            plot(yacc,'r');
            zacc = [zacc out2(3:3:end)'];
            plot(zacc,'g');
        end
        drawnow
    end
end


fclose(s)
delete(s)
clear s


This code only reads the signals from the serial port, stores them in different variables (depending on the axis) and plots them.

The main problem with Matlab ,though, is that is not fast enough to process all the datam, you'll see (if you try the code) that the graphs are drawn pretty slow (I got a celeron but I do not think that's the problem.

If you are interested in the Arduino code for that, let me know.


Dolphin
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,

did you use the code? let me know if it helped!!!

I will be posting the arduino part here https://webfiles.uci.edu/cagell/news/news.htm.


Dolphin
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 140
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Reading serial and writing to serial is very easy in matlab, but it sometimes hang, serial port already in use and then u need to restart matlab.  


The matlab documentation is very good, just type doc serial.


Code:


s = serial('COM1','BAUD',4800);
fid = fopen(s);

%do whatever u want to fid here.....
fclose(s);

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm looking to use matlab to interact with a Diecimla board.  I think its on serial com port 3 on my machine, but i'm not quite sure where to go from here.

for a test I was thinking of maybe having some kind of random number generator uploaded onto the board, then send that as the output.

My goal here is to use matlab to control an input to the board via the usb.  But just a little help with my beginner problem would be enough for me to figure the rest out hopefully.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Never mind I'll find something else... This isn't going to work. [smiley=sad.gif]
Logged

Seattle, WA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks my socks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If I have time before school starts up again, I'll try to get Matlab talking with Firmata.  I would also like to see a Firmata library for Labview.  If anyone else has time, please start and post your progress in the playground!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello
Dolphin: I tried your code. The Arduino Side didnt work really, I hat to change printByte to Serial.print and came to the following piece of code:

Code:
int ledPin = 13;
int xaccPin = 0;
int yaccPin = 1;
int zaccPin = 2;
int value = 0;

void setup() {
  //beginSerial(9600); // Sets the baud rate to 9600
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
  pinMode(xaccPin, INPUT);
  pinMode(yaccPin, INPUT);

}

void readAcceleration(int axe){
  value = analogRead(axe);

  if (axe == xaccPin){
    Serial.print('X',BYTE);
  }
  if (axe == yaccPin){
    Serial.print('Y',BYTE);
  }
  if (axe == zaccPin){
    Serial.print('Z',BYTE);
  }

  /* printInteger(value); */
  Serial.print(value);
  Serial.print(' ',BYTE);
}

void loop() {
  readAcceleration(xaccPin); //reads and represents acceleration X
  readAcceleration(yaccPin); //reads and represents acceleration Y
  readAcceleration(zaccPin); //reads and represents acceleration Y
  digitalWrite(ledPin, HIGH);
  delay(1200);
  digitalWrite(ledPin, LOW);

}

The code for Matlab did something to the serial port: The Arduino stopped blinking as soon as I executed the code in Matlab.  And nothing was displayed in the graph. Whats the prob there?
Thanks!
Logged

Pages: [1] 2   Go Up
Jump to: