Friday Night Robotics – Visualizing Data w/Matlab
I recently purchased Matlab since it seems to be pretty popular on this campus, and I’m taking a part of a class on it. You didn’t really have to purchase Matlab though, since you could obtain it through different ways, or use a lab computer. My reasoning was that I wouldn’t want to be stealing from an industry that is computer science related, that’s kind of FAIL, LOL! =) Plus, I wouldn’t want people to steal my software!
This software is really cool! You can graph data extremely easily!!!
I did a quick search for Arduino and Matlab, and saw that there were some working results!
I used my Sanguino with the IR sensor to send data to Matlab.
I found some very useful starting code from here . I actually didn’t modify it too much as I was eager to see it run!
-
/*
-
*
-
* Take IR sensor data from Sanguino and put it into Matlab!
-
*
-
* Original code comments:
-
* Accelerometer Sensor
-
* ——————–
-
*
-
* Reads an 3-D accelerometer
-
* attached to three digital inputs and
-
* sends their values over the serial port;
-
* There exists an opposite code to read
-
* the values from MATLAB.
-
*
-
* Modified by CARLOS AGELL
-
* (www.carlosagell.tk)
-
*
-
* Original code by
-
* http://www.0j0.org
-
* copyleft 2005 K3 – Malmo University – Sweden
-
* @author: Marcos Yarza
-
* @hardware: Marcos Yarza
-
* @project: SMEE – Experiential Vehicles
-
* @sponsor: Experiments in Art and Technology Sweden, 1:1 Scale
-
*/
-
-
#include <Wire.h>
-
#include "WProgram.h"
-
void readAcceleration(int axe);
-
-
int ledPin = 13;
-
int xaccPin = 0;
-
int yaccPin = 1;
-
int zaccPin = 2;
-
int value = 0;
-
-
-
void setup() {
-
Serial.begin(9600); // Sets the baud rate to 9600
-
pinMode(ledPin, OUTPUT);
-
pinMode(xaccPin, INPUT);
-
pinMode(yaccPin, INPUT);
-
}
-
-
-
/* (void) readAccelerometer
-
* procedure to read the sensor, calculate
-
* acceleration and represent the value
-
*/
-
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);
-
}
-
-
Serial.print(value, DEC);
-
Serial.print(‘ ‘, BYTE);
-
-
}
-
-
void loop() {
-
readAcceleration(xaccPin); //reads and represents acceleration X
-
digitalWrite(ledPin, HIGH);
-
delay(1200);
-
digitalWrite(ledPin, LOW);
-
}
The matlab code was also from this site, but I commented out a few lines so that there was only one graph line.
-
%Graph Accelerometer Data
-
-
s = serial(‘/dev/tty.usbserial-FTE4Z66C’);
-
set(s,‘BaudRate’,9600);
-
set(s,‘DataBits’,8);
-
set(s,‘StopBits’,1);
-
fopen(s);
-
-
out =‘a’
-
figure
-
hold on % on keeps all the current plot data, off resets the axes before drawing
-
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 = str2double(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
It works okay, but it’s really slow. Even though the data is being sent every 1.2 seconds, it takes Matlab 10 seconds to plot it! Also, it kept saying this in the command window:

The graph looked like this in the end:

It was pretty exciting to see it being plotted!!
I think what is making it slow is that the data is being stored in an array/matrix, and then the lines are calculated, and then they are plotted.
What I want to do is just have a 3D graph of the IR sensor data over time. It doesn’t have to be a complete history, maybe just the past 10 values. I would also want the delay to be minimal, 100ms at most!
I have no idea how to do this yet, but I’m guessing playing around with that weird out2 variable would be a start. Rewriting the code the way I want it would also be a good place to start, because then I can understand everything that’s going on
Oh yeah, and the day I received Matlab was the day the newer version came out that supports 64 bit for Macs.
but at least I have the second-latest version of the software.
There are also these ‘add-ons’ for Matlab called toolboxes. There are gajillions out there for HMMs, Bayes logic, AI, Robotics (Kinematics), Probability, Neural Nets, and sooo much more. I wonder if there is a toolbox for determining the proper ratio of icing to a cupcake though? That would be extremely useful, I should probably make that
The benefit this has over using Processing to visualize data is that it’s very more robust and mathematical. However, if one wanted to make an art display, Processing would win hands down.

January 10th, 2010 at 12:37 PM
Hey,
I am glad somebody used the code I wrote to transfer Arduino data to Matlab
Thanks!!
Cagell
January 18th, 2010 at 8:50 AM
Hey dude!
I wrote some other code that is even better. Check it out!
February 20th, 2010 at 2:04 AM
hmm… I’m tempted to abandon my plans of making a fancy graph app with processing and use matlab instead. looks ten times easier if you ask me.
By the way, is the a good reason for the 1.2s delay?