data acquisition from arduino to matlab

5 次查看(过去 30 天)
hello and good day to all the experts and visitors of matlab,
i am a mechanical student and now im facing problem trying to interface matlab and arduino..
my project is to connect and run a two-link robot arm..by attaching a potentiometer at each of the motor shaft, i am required to obtain the data from the potentiometer to know the rotation of my motor shaft..but i need to use arduino to get the data then transfer the data to matlab and plot the graph..
now i have successfully acquire the data from arduino to matlab by manually rotating the potentiometer myself and not by the motor.. my stepper motors operate by using MATLAB GUI so since i have to operate my motor to move the potentiometer i cant open another file at the same time to acquire the data from potentiometer.. the problem is how do i acquire the data from arduino to matlab by operating my stepper motor at the same time?..i wish to acquire the real result of my potentiometer while attaching to my moving stepper motor simultaneously..how do i do that?..do i need to combine both the files together?? thanks again for your information^^
the code i put in my matlab is:
% --- Executes on button press in data_pushbutton1.
function data_pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to data_pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Try-catch is to prevent Matlab from crashing when the program is finished
try
% Initialize serial port
s = serial('COM7');
%set(s, ‘ Terminator’, ‘LF’); % Default terminator is \n
set(s,'BaudRate', 9600);
set(s,'DataBits', 8);
set(s,'StopBits', 1);
fopen(s);
s.ReadAsyncMode = 'continuous';
% Various variables
numberOfDatas = 200;
data = zeros(1, numberOfDatas);
i = 1;
% Main graph figure
figure(1);
hold on;
title('Incomming Data from External Device');
xlabel('Data Number');
ylabel('Analog Voltage (0-1023)');
% Start asynchronous reading
readasync(s);
while(i<=numberOfDatas)
% Get the data from the serial object
data(i) = fscanf(s, '%d');
% Plot the data
figure(1);
plot(i, data(i), 'm*');
% Ensure there are always 10 tick marks on the graph
if(i>10)
xlim([i-10 i]);
set(gca,'xtick',[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
end
% Draw and flush
drawnow;
%Increment the counter
i=i+1;
end
% Give the external device some time…
pause(3);
return;
catch
% Some of these crash the program – it depends. The serial port is left
% open, which is not good.
stopasync(s);
fclose(s); % bad
%delete(s);
%clear s;
fprintf(1, 'Sorry, you"re going to have to close out of Matlab to close the serial port\n');
return
end

回答(2 个)

Rolfe Dlugy-Hegwer
Rolfe Dlugy-Hegwer 2012-6-14
If you are running the R2012a version of MATLAB/Simulink, you can install support for Arduino Mega 2560, and then use External mode to send data back to MATLAB/Simulink. External mode is not available for Arduino Uno. To install support for Arduino Mega 2560, enter targetinstaller in the MATLAB Command Window. This action opens Target Installer.In Target Installer, follow the instructions for installing support for Arduino. After you have completed this process, attach scopes to the Analog Input or Digital input block outputs. When you run your model on Arduino, External mode sends data to the scopes in the Simulink model on your host computer.

kalpana
kalpana 2015-2-19
I want to plot the real time data in MATLAB window of any sensor connected to the Arduino board, Please help to make connection between Arduino and MATLAB.

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by