Skip to content

Class 5 · Know where it is

In Class 4 the car learned where a tag is, measured from the car. Today you flip that around. If you know where each tag is on the track, then seeing one tag tells the car where it is: its position (x, y) and which way it's pointing. With that, it can go to a place you name instead of just to the tag in front of it.

By the end of class

The car's position shows live on a top-down map of the track, and on command it drives to tag 3, then to tag 7.

New parts today

Part What it's for
Foam board walls for the full track, about 2 × 2 m The track
8–10 printed tags on foam board Landmarks at measured spots
Tape measure, masking tape Measuring and marking the track

Where Claude Code fits today

Claude Code may help with the tag pose → car pose math (Mission 2) and the live plot. The tag map (you measure it) and the decisions (Mission 3, the state machine that drives to a tag) are yours.

Build it

  1. Lay out the track on the floor: corridors one car-width plus about 10 cm wide, walls about 15 cm tall, with at least two turns and one junction. Sketch it on paper first, to scale.
  2. Choose a corner as the origin (0, 0). The x axis runs along one wall, the y axis along the other, both in meters. Angles are measured counterclockwise from the +x direction, in degrees, the same way T 90 turns the car left. Mark the origin and the axes on the floor with masking tape.
  3. Tape tags to the walls at junctions and corners, with each tag's center at camera height (about 8 cm above the floor). Use each ID only once.
  4. For each tag, measure:
    • x and y of the tag's center, from the origin, to the nearest centimeter
    • which way the tag faces (the direction you'd walk to get away from it): 0° faces +x, 90° faces +y, 180° faces −x, 270° faces −y

Mission 1 · The tag map

Put your measurements in a Python file the other programs import:

# tag_map.py: measured on the floor track. Meters and degrees.
# id: (x, y, facing)
TAGS = {
    3: (0.45, 2.00, 270),
    7: (2.00, 1.10, 180),
    # ...
}

The numbers above are made up. Use yours.

Then get a top-down plot of the track: walls as lines, each tag as a short line facing its direction, with its ID next to it. Use matplotlib (pip install matplotlib in your venv). Claude Code may help with the plot.

Done when

The plot, held next to a photo of the track taken from above (stand on a chair), shows every tag in the right place facing the right way.

Tool: from 'where is the tag' to 'where am I'

For each tag it sees, the detector gives the tag's position and rotation measured from the camera. The map gives the tag's position and direction measured from the origin. Put the two together and you can work backward to the camera, and so to the car:

  • The car's heading is the tag's facing direction, minus how much the tag looks turned from the camera's point of view.
  • The car's position is the tag's position, minus the "car → tag" arrow, rotated by the car's heading.

A small example of the same idea, not on the car: you're in a field and your friend, who is at (10, 5), tells you "I'm 3 m straight ahead of you, and you're facing east (0°)". Then you're at (10 − 3, 5) = (7, 5). If you were facing north (90°), you'd be at (10, 5 − 3) = (10, 2). The rotation in the second bullet is what turns "straight ahead" into a direction on the map.

Why does this work? The math, for the curious

Let the tag be at (X, Y), facing direction θ on the map. In the car's frame, the camera sees the tag a distance z ahead and x to the right, and the tag's face is turned by an angle β (β = 180° when the tag faces the camera squarely; it comes from the pose_R rotation matrix). Then the car's heading is ψ = θ − β, and the car is at

x_car = X − z·cos ψ − x·sin ψ
y_car = Y − z·sin ψ + x·cos ψ

Check it with the friend example: θ doesn't matter there, ψ = 0°, x = 0, z = 3 gives (X − 3, Y).

With two tags in view, you get two estimates. Averaging them (or trusting the closer tag more) gives a better answer.

Mission 2 · Where am I?

Write a function, car_pose(detections), that turns the tags in view into the car's (x, y, heading). This is the part Claude Code may help with; test it yourself. Draw the car on the live plot as an arrow, updated every time it sees a tag. When no tag is in view, keep the last position and change the arrow's color, so you can see it's an old guess.

Done when

You place the car by hand at three spots you measure with the tape measure. Each time, the plot shows it within 10 cm and 10° of where it really is.

Mission 3 · Go to tag 3, then tag 7

Extend your Class 4 state machine so python navigate.py 3 7 drives to tag 3, stops 20 cm in front of it, then drives to tag 7. These are your decisions. Some ideas to start from:

  • Write the route on your paper map as a few waypoints (corridor centers at turns), then make the car aim at the next waypoint from its pose, turn, and drive in corridor mode (C) until the pose says it's there.
  • Corridor mode keeps the car off the walls between tag sightings. The tags tell it where it is when it reaches a junction.
  • Once the target tag is in view, switch to your Class 4 APPROACH and ARRIVED states.
  • Add a LOST state: if no tag is seen for a while, stop and spin slowly until one is.

Draw your states and arrows on paper first and keep the drawing in your build log.

Done when

From a start spot Dr. Alvear picks, the car stops 15 to 25 cm in front of tag 3, then in front of tag 7, without touching a wall. Two runs out of three. Take a video with the live plot in view.

If you finish early

  • Draw the car's path on the plot as a trail of dots.
  • Dead reckoning: between tag sightings, move the arrow on the plot using the commands you sent (speed × time, angle of each T). How far off is it by the time the next tag shows up?
  • python navigate.py 7 3 5: any list of tags, in order.

Sign-off with Dr. Alvear

  • Tag map measured; plot matches the real track
  • Car pose on the plot within 10 cm and 10° at three spots
  • State diagram for navigation in the build log
  • Goes to tag 3, then tag 7
  • Build log updated (photo + two sentences)