DEVLOG – How to Move Straight in a Line?
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:
- Start a timer (T).
- Let the robot move X cm at 0°.
- Compute
speed = X ÷ T.
The track has two tasks:
- Task 1: Move from point A to B straight (short distance, no external load).
- 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 distancedand speedv.
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
-
Initialize a position vector
(Rx, Ry)at(0, 0). -
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
Ais the distance moved in this tick (computed from wheel rotation, orA = v × t). -
Calculate the straight‑line displacement:
R = sqrt(Rx² + Ry²) -
Stop the robot when
R ≥ X, whereXis the desired straight‑line distance.
While this is theoretically correct, it didn’t work out due to the robot’s limitations. Back to PID! 😊