1020: Introduction to Programming Winter 2014

Assignment 8

Due: Monday, March 24 at noon.

This assignment is to be done in pairs. Each pair of students should submit only one solution, but both names must be listed in the code.

In this assignment you will create some C++ functions to drive the robot following a track consisting of (relatively) straight black line segments joined at (approximately) right angles, possibly including 'T' intersections, so as to solve the maze. We have provided the following files:

To complete this assignment, download the above files as for previous assignments, and save them in the project directory. You must then create assign8.cpp and in it give definitions for the function described below. You must include appropriate commenting for your file and for each function.

The function you are to implement is as follows:

  1. int solveMazeBest(char route[], int maxLen)
    Image of maze A function to cause the 3π to drive along a track, as pictured to the right, following the black line segments (using followSegment) keeping track of the route until the target is found. At each intersection the algorithm must decide what to do next and note the decision by adding a character to route, as follows:
    • 'L' for each left turn
    • 'R' for each right turn
    • 'S' for going striaght through an intersection.
    • 'A' for turning around (dead end) Note: the final value of route should not contain any 'A' decisions..
    It counts the intersections found, which should not exceed maxLen, and returns that number. This differs from solveMaze in Assignment 7 only in that it 'trims' the route, using trimRoute, as it procedes so that the final value of route will not include any trips down dead end branches of the maze. Once it reaches the target it should stop.
  2. int trimRoute(char route[], int len)
    Remove dead end branches from the end of the route. If the last but one element of route is 'A' (indicating a dead end) then the last three elements can be replaced by one turn that is equivalent to the total turn of the last three steps. For example 'LAL' is 360 degrees, which is the same as 'S', 'LAR' is 540 degrees, which is the same as 180, or 'A'. Returns the new length of route.
  3. bool followRoute(char route[], int len)
    A function to cause the 3π to drive along a track, as pictured above, following the black line segments. At each intersection the decision as to which direction to go is made by referring to the corresponding element of route. It returns true if and only if it ends with the 3π on the target.
  4. bool isTarget()
    This and the next three functions are exactly as in Assignment 7, so you can just copy your solutions from there. They are included here because you should use it to implement the above functions. Returns true iff the 3π is on the target, which is a black block about 7cm x 5cm such that followSegment will leave the 3π with all five sensors on the block. Hint: You might need to use read_line directly to implment this. You can see how it is used in readSensorsBranches
  5. void followSegment(bool& left, bool& right, bool& straight)
    A function to cause the 3π to drive along a straight track following the black line until it reaches an intersection (branch on left or right) or dead end and report the options available at this point. The function should adjust the position of the 3π so that, if there is a branch on the left or right and the 3π turns 90 degrees in the correct direction then it will be correctly positioned to follow the next segment. It should set left, right and straight to indicate what options are available at this point.
  6. void turnRight()
    Cause the 3π to turn on the spot 90 degrees to the right (clockwise).
  7. void turnLeft()
    Cause the 3π to turn on the spot 90 degrees to counter clockwise.

While you are welcome to change the code in assign8_3pi.cpp, there are some aspects of it that are important for correct functioning. In particular, lineCalibration must be used before the sensors will function correctly. To run the test program, after you have written your functions, compiled it and downloaded it to the 3π, follow the following steps:
  1. Set the 3π down so that the sensors are approximately centered over the line at the start of the course.
  2. Press B to start the calibration. After a short pause the 3π will turn left about a half turn, then turn right twice as much, and then left again so that it ends up where it started. During this motion the line sensors must pass over the black line and on to clear white space so that they are calibrated.
  3. You may want to adjust the position of the 3π at this point so that the number displayed is about 2000.
  4. Press B to solve the maze.

After you have tested your program and are confident it is right, submit assign8.cpp using websubmit. Do not submit your testing code.


Last modified $LastChangedDate: 2014-03-14 21:45:32 -0230 (Fri, 14 Mar 2014) $