3. In ENGR 1016, you learn how to program a small robot to perform various tasks. One typical task is to have your robot follow another around a track. A sensor detects the distance between the two robots. If the following robot gets too close to the lead robot, then it must slow down or stop to avoid a collision. Write a MATLAB function called SpeedControl that takes the input from the sensor (Distance) and outputs the percentage of full speed (PerSpeed) for the robot to travel based on the following: If Distance is >= 12 inches, Perspeed = 100 (full speed) If Distance is >= 9 inches and < 12 inches, PerSpeed = 75 If Distance is >= 6 inches and < 9 inches, PerSpeed = 50 If Distance is >= 3 inches and < 6 inches, PerSpeed = 25 If Distance is < 3 inches, PerSpeed = 0 (complete stop) Test your function for various Distances, and sketch a flow chart of your function.
Matlab code
============================================================================================
function [Perspeed] = SpeedControl()
distance=input('Enter distance: ');
if(distance>=12)
Perspeed = 100;
elseif(distance>=9)
Perspeed = 75;
elseif(distance>=6)
Perspeed = 50;
elseif(distance>=3)
Perspeed = 25;
else
Perspeed = 0;
end
fprintf('PerSpeed is: %d\n',Perspeed);
end
============================================================================================
Output
============================================================================================
FLOW CHART
Get Answers For Free
Most questions answered within 1 hours.