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!