Lab 2

purpose | preparation | procedure

This lab will give you the chance to practice using variables, as well as simple expressions, in your functions.

In-lab presentation

The slides that Ms Hogan, the lab instructor, delivered in the lab are visible in the frame to the right. If they are difficult to see, however, you can also download the slides.

Purpose and Outcomes

In the lab, you will implement functions that perform simple motor control. While doing this, you will:

  • practice translating mathematical expressions into C++,
  • practice declaring and initializing variables and
  • continue practicing the declaration, definition and calling of functions.


Read the Wikipedia article on duty cycle and then read the entire lab procedure before answering the following questions (before coming to the lab):

  1. What is the equation relating a constant speed, distance and time?

  2. Given a robot travelling at a fixed but unknown speed over floor tiles that are one foot square, design a simple experiment to measure the robot’s speed.

  3. The 3$\pi$ robot’s wheel speed is controlled using a duty cycle value between 0 (0% duty cycle) and 255 (100% duty cycle). If you needed to measure the robot’s speed (as in the question above) as a function of this duty cycle value, which values (in the range 0–255) would you choose for your measurements and why?


1. Initial setup

  1. Import the lab 2 template file ( just as you did in previous labs.

  2. Add a stub (incomplete) implementation of the driveForwards function. For now, this should be an empty function definition (i.e., no statements in the function body).

  3. Ensure that the code compiles and can be downloaded to your 3pi robot, even though it doesn’t yet make the robot move.

2. Speed experiment

Using the experiment that you designed in the prelab, measure the speed of your robot.

Start with the following code for driveForwards:

void driveForwards(double distance)
	int DUTY = 40;
	double time = 1000;

	setMotorPower(DUTY, DUTY);
	setMotorPower(0, 0);

Compile, download and run it.

  1. Varying the duty cycle applied to the 3pi robot’s wheels, measure the distance the robot travels for a varying duty cycle over a time of your choice (1000 ms is likely to be a convenient time for low duty cycles) and plot the robot’s speed vs duty cycle. Use any convenient units. Describe the shape of the curve you find: is it linear? Quadratic? Exponential?

  2. Draw a line of best fit through your plotted points. What is the equation that describes this line, in terms of the variables $D$ (the duty cycle value in the range 0–255) and $v$ (speed, in whatever units you chose)? Solve for $D$. Solve for $v$.

3. Working code

  1. Fix the variable FEET_PER_METRE.
  2. Using the data gathered in the experiment above, write a complete implementation for the driveForwards function.
  3. Test your implementation by making the robot attempt to drive one foot, two feet and five feet. How far does it actually travel in each case? Show distances in a table of values.

4. Conclusion

Describe what you observed in this lab. Are there any interesting aspects of the robot’s behaviour that do not fit the code as you wrote it?

Submit your work:

Once you have finished writing up your lab activity, submit your log book to the TAs in the lab. If you need extra time to finish the lab, you can submit your log book in the Engineering One Help Centre.