DEVLOG – How to Move Straight in a Line?

Published: (December 13, 2025 at 07:36 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

Problem Statement

To cope with any problem, we first need to tailor it into a problem statement that is explicit and understandable, so here’s ours:

“How can I make a SPIKE Prime robot move X distance in a straight line, while pulling an external load?”

Center of Mass

It may seem obvious: “Just let it move 0 degrees!” you might say. However, the center of mass (the point at which the total mass of the object is concentrated) is not always in the geometric center. Only regular bodies have their center of mass coinciding with the geometric center; for all other objects, these points do not coincide.

Therefore, commanding the robot to move at angle 0° will let it tilt over time.

Proposal #1: MOVE(0 – YAW)

My initial idea was to let the robot move (0 – yaw) degrees, i.e., ensuring that whenever the robot detects any tilting (by its internal gyroscope), it readjusts accordingly.

Because the robot programming environment does not support “MOVE UNTIL X DISTANCE” inside a loop, I had to rely on time. The idea:

  1. Start a timer (T).
  2. Let the robot move X cm at 0°.
  3. Compute speed = X ÷ T.

The track has two tasks:

  1. Task 1: Move from point A to B straight (short distance, no external load).
  2. Task 2: Move from point B to C with a load.
  • For task 1, the robot can move in a straight line due to the short distance and lack of load, so we can calculate its speed.
  • For task 2, we run a loop for the MOVE(0 – YAW) method. To get the required time to travel distance X, we use time = d / v, where we already know distance d and speed v.

Proposal #2: Displacement‑Based Movement

The first proposal relied on time, but because the robot tilts while moving, the total distance traveled increases and time becomes unreliable.

Instead of relying on time, we track the actual straight‑line displacement of the robot from its starting point—i.e., the “as‑the‑crow‑flies” distance, not the total path length.

How it works

  1. Initialize a position vector (Rx, Ry) at (0, 0).

  2. At each small time interval (tick):

    • Measure how far the robot moved since the last tick (via wheel encoders).
    • Read the current yaw angle from the gyroscope.
    • Update the vector position along the direction of motion:
    Rx = Rx + A × cos(yaw)
    Ry = Ry + A × sin(yaw)

    where A is the distance moved in this tick (computed from wheel rotation, or A = v × t).

  3. Calculate the straight‑line displacement:

    R = sqrt(Rx² + Ry²)
  4. Stop the robot when R ≥ X, where X is the desired straight‑line distance.

While this is theoretically correct, it didn’t work out due to the robot’s limitations. Back to PID! 😊

Back to Blog

Related posts

Read more »