9 Create a function to move the robot in a straight line

We want to create a function that moves the forward by a user-specified distance; for example:

moveForward(25)

should move the robot forward by 25 cm. Our function will use a constant motor speed of \(\pm 15\) units from the zero-speed position. The problem we need to solve is to find what linear velocity (in \(\text{cm}/s\)) corresponds to a motor speed of 15 units.

To write such a function, we will fix the speed at which the motors turn and then allow the robot to move forward for 1 second. Next we will measure the distance that the robot has travelled in 1 second which determines the average velocity of the robot. Using the average velocity of the robot and the desired distance to travel we can determine the correct amount of time to turn the motors so that the robot moves forward by the desired distance.

9.1 Start with an incorrect version of the function

In MATLAB, modify the function shown below (the function is already provided to you in the lab directory):

function moveForward(distance)
%MOVEFORWARD Move the differential drive forward
%   moveForward(distance) moves the robot forward by the
%   given distance in centimetres.
%
%   For safety reasons, the maximum value of distance is 50 cm.

% DO NOT REMOVE OR MODIFY THE FOLLOWING TWO if STATEMENTS
if distance < 0
    return
end
if distance > 50
    distance = 50;
end

% complete the next two lines 
zeroSpeed_motor0 = 
zeroSpeed_motor1 = 

% use motor velocities of 15
setServoPosition(0, zeroSpeed_motor0 - 15);
setServoPosition(1, zeroSpeed_motor1 + 15);

% let the motors run for t seconds
t = 1;
pause(t)

stopRobot()

To begin, set the values of zeroSpeed_motor0 and zeroSpeed_motor1 to the zero-speed values you found earlier.

Notice that this version of the function runs the motors for 1 second.

9.2 Important

Place the robot on the table so that the robot is pointing away from the edge of the table. Clear your workspace around the robot ensuring that there is nothing within 50 cm of the front of the robot. Be prepared to pick up the robot in case in moves towards the edge of the table or if it moves to collide with something.

9.3 Record how far the robot moves in 1 second

Mark the starting position of the axle of the robot using a removable arrow marker provided in the lab.

Call the function moveForward passing in any positive value for distance. The robot should move forward for 1 second (the length of the pause) and then stop. The robot should move in a straight line; if it does not then your zero speeds are probably incorrect. Adjust your zero speeds slightly to get the robot to move as straight as you can (it will likely be the case that you cannot get the robot to move in a perfectly straight line).

Once you manage to get the robot to move in a relatively straight line measure the distance in centimetres between the starting point of the robot axle and the stopping point of the robot axle after it moves for 1 second.

You now know how far the robot moves in 1 second. This lets you calculate the ground velocities \(v_l = v_r\) in centemetres per second corresponding to a motor velocity of 15 units. Use the ground velocity to determine how long you have to pause to move by a distance of distance. Modify the pause time t.

9.4 Test your function

Check that your function can make the robot move forward by 10, 20, 30, 40, and 50 centemetres. You should not expect the actual distance travelled to be very accurate.