getting real time values from distance sensor arduino

4 ビュー (過去 30 日間)
Robin Johannes Terstappen
Robin Johannes Terstappen 2017 年 12 月 6 日
コメント済み: Jayanta Deb 2018 年 2 月 15 日
For a Bachelor Project at the TU delft we're making a model in simulink in which the DC-motors of a ship are modelled in such a way that they respond to the distance the ship has to a ship in front of it. We wrote a code in Matlab which receives distance values from an arduino UNO using the serial and sscanf commands. However this gives us the distances from a defined time frame which is not real-time. Can anybody tell us how we should change the code to get a continuous flow of real-time distances from the sensor which we can then link to simulink?
The code looks as following:
if true
clear all
clc
delete(instrfindall);
arduino=serial('COM3');
set(arduino,'BaudRate',9600);
fopen(arduino);
fgetl(arduino); %flush out any partial line in the buffer
for i=1:L
thisline = fgetl(arduino); %Read line from file, discard newline character.
if strcmpi(thisline, 'Out of Range')
fprintf('Out of Range reported at point #%d\n', i) %format and display data
y(i) = nan;
else
thisdist = sscanf(thisline, '%f');%Read data from matlab variable thisline
if isempty(thisdist)
fprintf('Unexpected response at point #%d; line was: "%s"\n', i, thisline);
y(i) = nan;
else
if length(thisdist) > 1
fprintf('Multiple distances reported for point #%d, using first; line was "%s"\n', i, thisline);
end
y(i) = thisdist(1);
end
end
pause(0.05);
end% code
end
fclose(arduino);
disp('making plot..')
plot(x,y);
  1 件のコメント
Jayanta Deb
Jayanta Deb 2018 年 2 月 15 日
Hi, I dont know if the problem was solved. In my opinion, if I understood it correct, You need to get a continuous flow from a distance sensor without running the matlab script manually everytime? So I think you need to put it in a while loop and continue the whole program.
for x = round((2-1).*rand(1) + 1) while (x<3) . . . end end
* further you need to modify the code in such a way that the sensor gets prepare for once so as to you don't have to make the arduino get ready each time and you get closer to real time more efficiently. Try to write the "get ready" matlab code to run before the loop begins.
Regards, J

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by