Skip to content
Programming Robots

Steering and Movement


Learning Objectives

  • You can set a simulated car’s steering angle and drive for a specified duration.
  • You can distinguish steering angle from the car’s heading.
  • You can explain how a retained setting affects later method calls.
  • You can convert a straight-line distance to a driving duration.
  • You can execute a route stored as a list of movement records.

The grid robot’s turn_left() completed a quarter-turn in place. Our next model is a car. Its front wheels can be steered to an angle, and that setting affects the path it follows when driven.

Steering Angle and Heading

The car begins at (0, 0), facing east. Its heading describes the direction its body faces: zero degrees is east, and 90 degrees is north. Its steering angle describes the front wheels relative to the car’s body. Zero steering means straight ahead; positive angles steer left and negative angles steer right.

The supplied car model has two movement methods:

MethodOperation
car.steer(angle)Set the front-wheel angle, in degrees, between -30 and 30.
car.drive_for(seconds)Drive forward at 0.5 metres per second for the given simulation duration, then stop.

The simulator calls program(car), supplying the car object. Run main.py below to open the preview. Press Right arrow once to inspect the steering command, then again to complete the drive. Space plays or pauses the movement, and R resets playback. The white arrow shows the body’s heading; the orange front wheels show steering.

No files opened Select a file to edit

Immediately after steer(20), the steering value is 20 and the heading is still zero. The wheels have turned, but the car has not driven. During drive_for(2), it follows a left-curving path. It travels one metre along that path because its speed is 0.5 metres per second.

state() returns a record containing x, y, heading, steering, and elapsed. Coordinates use metres, angles use degrees, and elapsed simulation time uses seconds. Reading the record does not move the car.

The numeric display shows the current state and the program’s printed output. Playback inspects recorded states; after editing program, Run main.py again to calculate a new movement from (0, 0).

The supplied program prints steering and heading before and after steer(20), then the position after driving. The output appears alongside the preview. To inspect another change, print the relevant fields, such as print(car.state()["steering"], car.state()["heading"]). Short readings are easier to compare than whole state dictionaries.

Retained Steering Settings

Extend the example with a straight segment:

def program(car):
car.steer(20)
car.drive_for(2)
car.steer(0)
car.drive_for(1)

The first drive ends with the car facing a new direction. Setting steering to zero straightens its wheels; it does not restore an eastward heading. The final drive continues straight from the new position and heading.

The steering setting remains active until changed. Removing car.steer(0) would make the final segment continue the curve instead. The call to drive_for stops the car at its end but retains the steering value.

In the simulator, press 3 for the curve followed by a straight segment. Step through steer(0) and the final drive. The heading at the start of that straight segment is the heading reached at the end of the curve.

The diagram below separates the steering setting from the movement that uses it. Both the setting and the duration contribute to the resulting position and heading.

Setting steering and performing a movement are separate operations.

Retained Steering

0 / 5 points

Two cars start at the same position, facing east with steering zero. Positive steering angles turn the front wheels left. Each drive_for call drives for the stated simulation duration, then stops.

Program A:

def program(car):
car.steer(15)
car.drive_for(2)
car.steer(0)
car.drive_for(1)

Program B:

def program(car):
car.steer(15)
car.drive_for(2)
car.drive_for(1)

The programs follow the same path for the first two seconds. How do their final one-second movements differ?

Practise a few small changes to steering, durations, and state readings before combining them in helpers.

Comparing Angles and Durations

From the same initial state, compare steer(10) followed by a two-second drive with steer(20) followed by a two-second drive. The larger steering angle gives a tighter curve in this model. Now keep the angle at 20 and compare one and two seconds of driving. The longer drive travels farther around the curve and changes the heading further.

A 20-degree steering setting does not mean that the car will change its heading by 20 degrees. The change also depends on how far it drives. The supplied model calculates the path; the program chooses the steering settings and durations.

Press 4 in the simulator to compare the two steering angles, or 5 to compare the two durations. Each comparison starts both cars at the same position and heading. The overlaid paths and numeric readouts show how the changed angle or duration affects the movement.

Driving a Distance

The fixed speed also lets us choose a distance. Duration is distance divided by speed: at 0.5 metres per second, a distance of 2 metres takes 2 / 0.5, or 4 seconds. This is distance along the path. Straightening the wheels first makes that path a straight line from the car’s current position and heading.

In the next exercise, write a helper that converts the requested distance to a duration. The preview calls it after a curve, so check that the final segment is straight without turning the car back east.

Driving a Straight Distance

0 / 20 points

Write drive_straight_distance(car, metres) in main.py. Straighten the supplied car’s steering, then drive the requested distance at its fixed speed of 0.5 metres per second. For example, 1.5 metres takes 3 seconds.

Continue from the car’s current position and heading. metres is a nonnegative number. Return None.

Run the supplied preview to inspect the path; it calls your helper after a curve.

Curved and Straight Driving Segments

As with the grid robot’s movement helpers, a function can organize several calls:

def curve_then_straight(car, angle, curve_seconds, straight_seconds):
car.steer(angle)
car.drive_for(curve_seconds)
car.steer(0)
car.drive_for(straight_seconds)
def program(car):
curve_then_straight(car, -15, 2, 1)

The negative angle selects a right curve. The caller supplies both durations, so the helper can produce different movements without changing its body. Its calls still act on the supplied car and return no new car object.

The next helper combines two curves. Its arguments describe the bend instead of fixing a particular angle and duration inside the function.

S-Bend

0 / 20 points

Write s_bend(car, angle, seconds) in main.py. Use the supplied car to make these calls in order:

  1. Set steering to angle.
  2. Drive for seconds.
  3. Set steering to -angle.
  4. Drive for the same seconds.
  5. Restore steering to zero.

Use car.steer and car.drive_for. The caller supplies a valid angle and a nonnegative duration. Keep all five calls, including for zero values, and return None.

Run the supplied preview to inspect the two curves.

Run the exercise’s main.py and press 1 to see your function’s path. Change the arguments in the supplied program(car) call to compare different angles and durations, then run main.py again.

Routes as Records

A longer route can be data rather than a longer function body:

segments = [
{"angle": -10, "seconds": 1},
{"angle": 0, "seconds": 2},
{"angle": 15, "seconds": 0.5},
]
for segment in segments:
print(segment["angle"], segment["seconds"])

The loop visits the records in list order. The first describes a right curve, the second a straight segment, and the third a left curve. Use those values to run a route in the following exercise. Leave steering straight afterward so a later drive does not unexpectedly keep curving.

Following a Driving Route

0 / 25 points

Write follow_route(car, segments) in main.py. Each record in segments has an "angle" and "seconds". In list order, set that steering angle and drive for that duration. After the route, leave steering at zero.

For example, [{"angle": 15, "seconds": 1}, {"angle": 0, "seconds": 2}] describes a one-second left curve followed by two seconds straight ahead. An empty route only straightens the steering.

Use the supplied car and keep the records unchanged. Angles and durations are valid for the simulator. Return None. Run the supplied preview to inspect your route.

Check Your Understanding

  1. What changes when steer(20) is called while the car is stationary, and what stays unchanged?
  2. Why does steer(0) after a curve fail to restore the car’s original heading?
  3. From the same starting state and steering setting, how would doubling the driving duration affect the distance traveled?
  4. segments contains a right curve followed by a straight segment. How should swapping those records change the order of commands in follow_route?