# Lab 4

This assigment will give you the chance to practice using loops.

## 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 explore the use of looping flow control via while loops. While doing this, you will:

• practice using compound lab statements and
• continue practicing the declaration, definition and calling of functions.

In this week’s lab, you will start to use the IR sensors on your 3$\pi$ robot to make the robot navigate more reliably. In previous weeks, we have used dead reckoning: we turned the power on, waited some time and then turned the power off, hoping the robot ended up where we wanted it to. If we measured well, the robot might travel the correct distance (for instance), but we had no way to correct any drifting off-course.

This week, we will use the IR sensors on the underside of the 3$\pi$ robot in order to follow a line reliably, whether that line is straight or curved. An example of a “course” with several curved lines, one after the other, is shown to the right.

Your top-level goal for this lab is to make your robot follow a curved line until it ends.

## Preparation

1. Find the contract for the readLineLocation() function in engi1020.h. Summarize its purpose and the meaning of its return value.

2. What values, when returned from readLineLocation(), would indicate that the 3$\pi$ robot is not on the line at all?

3. Given your answers to the previous questions, draw a flowchart for an algorithm that could be used to keep a 3$\pi$ robot “on course” when following a line. Use only the readLineLocation() and setMotorPower() functions.

4. Translate your flowchart from the previous question into pseudocode.

## Procedure

### Initial setup

1. Import the lab 4 template file (lab4.zip) just as you did in previous labs.

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

### Complete onLine()

The lab4 template contains a stub (incomplete) implementation of the onLine() function. This function should return true if the line location passed to it represents a valid sensor reading and false otherwise. Complete this function using the values you identified in your prelab.

### Complete followLineToEnd()

The lab4 template also contains a stub implementation of followLineToEnd. This stub calls readLineLocation to determine where the line is with respect to the robot, then ignores this information. You must complete this implementation based on your prelab. As with the stub implementation you have been given, the robot should beep whever it reaches the end of a line segment.