1개의 초음파센서 동작 속도 측정 방법?

조회 수: 2 (최근 30일)
유나 정
유나 정 2021년 5월 28일
답변: Shivani 2024년 5월 21일
function y = fcn(u)
if u >=0.5
y=1;
else
y=0;
end;
인 함수 블록에 입력되는
초음파센서가 입력된 함수의 출력=1 에서 출력=0이 될때까지의 시간을 측정할 수 있는 방법이 있나요?

답변 (1개)

Shivani
Shivani 2024년 5월 21일
Hello @유나 정,
Based on my understanding, you are looking to clock the total time taken for the output of the function fcn() ,’y’, to change from 1 to 0. I am assuming that there is an external function that is capturing the data from the ultrasonic sensor and storing it in the input variable ‘u. Since the code calling the function 'fcn()' is not shared in the question, I am assuming that the function is being called within a loop with unique u values at every iteration.
To calculate the total time taken for variable ‘y’ to move from 1 to 0, we can use the ‘tic’ and ‘toc’ function. Refer to the sample code attached below for an example of how to use these functions to determine the total execution time. Please note that I am assuming u to be populated by the function ‘readUltrasonicSensor().
prev_y = fcn(readUltrasonicSensor());
startTime = tic;
while true
u = readUltrasonicSensor();
y = fcn(u);
if prev_y == 1 && y == 0
elapsedTime = toc(startTime); % Measure time since start
fprintf('Time elapsed from start to output change (1 to 0): %f seconds\n', elapsedTime);
break;
end
prev_y = y;
pause(0.01);
end
Additionally, you can refer to the following documentation link for more information:
Hope this helps!

카테고리

Help CenterFile Exchange에서 블록 라이브러리에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!