Robotics
How a robot can tell it's stuck, with an encoder
A robot can tell it's stuck when its motors are on but its wheel encoder has stopped ticking. A turning wheel ticks every so often; a wheel jammed against a wall, a chair leg or a rug doesn't. Note the time of every tick, and if too long passes without one while the motors are driving, the robot is stalled: back off, then try again.
Two timestamps
lastTick is the time of the latest tick, refreshed on every tick edge. since notes when the reverse began:
if (now == LOW && last == HIGH) lastTick = millis(); // a tick
last = now;
if (!reversing && millis() - lastTick > 500) { // stuck
reversing = true;
since = millis();
}
if (reversing && millis() - since >= 1000) { // backed off
reversing = false;
lastTick = millis(); // start the stuck timer fresh
}
digitalWrite(REVERSE, reversing ? HIGH : LOW);
analogWrite(9, 255); analogWrite(10, 255);
While reversing, the wheels stay at full with the REVERSE line set, so they turn backwards and the robot backs off the wall. Resetting lastTick at the end matters: otherwise it's still the old time, and the robot is declared stuck again on the very next pass.
Pick the stuck time
If ticks come every 100 ms when the wheel turns, 500 ms, five normal gaps, is a good start. Too short, 100 ms, and an ordinary gap between ticks looks like a stall, so the robot backs away before it has gone anywhere. Too long, 2000 ms, and it shoves the wall for two seconds before noticing.
Live experiment
Pick a stuck time
The rover drives at a wall. Every tick refreshes lastTick; motors on with no tick for the stuck time means stuck, so it reverses for a second and tries again. 100 ms is shorter than the gap between ticks, so it backs off before it has gone anywhere; 500 ms trips only at the wall; 2000 ms grinds for two seconds first.
“Stuck!” is lesson 4 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.
On the timeline
Ticks until 1 s, then the wall. At 1.5 s the stuck rule fires and the robot reverses until 2.5 s, then drives forward again. If the wall is still there, no ticks come, and at 3 s it's stuck again. Each cycle backs it about 20 cm off.
"If nothing has happened for a while, act" is called a watchdog. Processors have watchdog timers that restart a chip whose program has stopped checking in, and garage doors reverse when they stop moving against something for the same reason.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Stuck!” is lesson 4 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Stuck: a moving wheel would have ticked by now. Ticks come every 100 ms when the wheel turns. Six hundred without one, with the motors on, means the wheel isn't turning.
- Why not “Turning”? A turning robot's wheels still roll and tick. No ticks at all means no rolling.
- Why not “Fine”? A turning wheel would tick every 100 ms. Six hundred without one, with the motors on, means it isn't turning.
- Why not “Stopped on purpose”? The motors are on, so it isn't meant to be stopped. Its wheel just isn't moving.
Show the answer
Answer: So the stuck timer starts fresh instead of firing again at once. lastTick is still the old time, well over 500 ms ago. Without the reset the rover is declared stuck again on the very next pass.
- Why not “To stop the motors”? lastTick is just a time. The wheels are set elsewhere, from the reversing flag.
- Why not “It's optional”? Without it, lastTick is still the old time, well over 500 ms ago, so the robot is declared stuck again at once.
- Why not “To count a tick”? No tick arrived. Setting lastTick only restarts the stuck timer.
Show the answer
Answer: Full, with REVERSE HIGH turning them backwards, for a second. REVERSE HIGH runs the wheels backwards, so wheels full with REVERSE on backs the rover off the wall for a second. With the wheels off it would sit there for a second, then push the same wall again.
- Why not “Off, with REVERSE on”? With the wheels off it would sit still for a second, then push the same wall again.
- Why not “Pivoting”? The reverse backs straight off. Turning away is a separate step you could add after it.
- Why not “Full forwards”? Full forwards is what got it stuck. REVERSE HIGH turns the wheels backwards.
“Stuck!” is lesson 4 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.
Questions people ask
How else can a robot detect a stall?
By the motor current: a stalled DC motor draws far more than a free-running one, so a current sensor and a threshold catch it. Bumpers catch the walls they touch, and an accelerometer can notice that the robot isn't moving. Encoders are the most direct: no ticks, no turning.
What stuck time should I use?
A few times the slowest normal gap between ticks. If ticks come every 100 ms at cruising speed, 500 ms works. At low speeds, and when starting from a standstill, the gaps are longer, so ignore the check while ramping up or scale it with the speed.
Why does my robot keep reversing?
Usually lastTick isn't reset when the reverse ends, so it's still the old time and the robot is declared stuck again on the next pass. Or the stuck time is shorter than a normal gap between ticks, so it trips while driving normally.
Related topics
- How a wheel encoder measures distanceCount the notches as the wheel turns, and you know how far the robot went.
- How to write Arduino robot code without delay()Swap the waits for a glance at the clock, and the robot hears its bumper mid-leg.
- How to program a robot as a state machineOne state at a time, each with its own job and its own ways out.
- How to make a robot bump and turn with a bumper switchA switch on the nose, LOW on a bump, and a four-step escape: stop, pause, pivot, go.