Robot Control Basics: Conditional Branches and Loops

How a robot runs a sense-decide-act loop, and how to set stop, slow-down and time-limit conditions, using run-time estimates and tests in the Webots simulator.

Author
AuthorAuthor

AI Engineer · 36+ years in IT · Japanese, based in Manila for 13+ years

Robot Control Basics: Conditional Branches and Loops

Summary

  • A robot repeats a "sense, decide, act" loop at short intervals. The conditional branches inside this loop choose whether to stop or slow down.
  • Before you run the robot, predict the result from the relationship between speed, time and distance. Then you can check the gap between the prediction and the actual motion in numbers.
  • Decide the condition for reaching the target, the point where slowing down starts, and the time limit. In my test in Webots, a robot simulator, slowing down before the target cut the overshoot (how far the robot goes past the target) from a maximum of 2.24 mm to 0.32 mm.

When you bring in a delivery robot or a serving robot, one natural question is how precisely it can stop in front of a shelf or a table. When you think about the stopping point, what matters is how often the robot checks its position and how it moves based on what it finds. Below, I go from the basics of conditional branches and loops to the steps for setting stop and slow-down conditions, with results from a simulation.

Robot Control Is a Loop of "Sense, Decide, Act"

A robot's control program has a loop that repeats the same set of steps. One pass through the loop does three things:

  1. Sense: Use sensors to check the current position or the distance traveled
  2. Decide: Based on the sensor values, choose whether to keep going, slow down or stop
  3. Act: Send a command to the motors based on that decision

This repetition is the control loop. Here, I call the time one pass through the loop takes a step.

How to Write a Control Loop in Webots

According to the official Webots guide, the control code is usually placed inside a for or while loop. Inside it, the wb_robot_step function is called at a regular interval.

In the guide's example, the value 32 is passed to this function. It means advance the time inside the simulation by 32 milliseconds (ms), then return control to the controller (the program that runs the robot). The real computing time depends on how complex the world (the simulation environment) is. The guide says it can take 1 ms, or it can take a minute.

The step interval also affects how sensors are read and how motors move.

  • Sensor values are updated when wb_robot_step is called. Even if you make the sensor's update interval shorter, the controller reads the values only once per step.
  • Commands to the motors take effect the next time wb_robot_step is called. The guide says it helps to think of the robot's motion as small movements that add up step by step.

The robot checks its position and sends commands to the motors once per step. It keeps moving between checks, so it can pass the target position.

Using Conditional Branches to Decide How to Move

The "decide" part of the loop is handled by conditional branches. They switch the robot's behavior in the form "if this is true, do this; otherwise, do that."

For example, a delivery robot or a serving robot might use conditional branches like these:

  • If it has reached the target position, stop
  • If it has not reached the target after the time limit, stop
  • If it has reached the slow-down point before the target, lower its speed
  • If none of these apply, keep going at the current speed

The conditions are checked from the top, in order, at every step. If you put the stop conditions first, stopping takes priority even when the time limit runs out while the robot is slowing down. At a work site, rules such as "stop in front of the shelf," "stop if it has not arrived after 60 seconds" and "slow down 1 m before the table" turn directly into conditional branches.

Keep Conditional Branches Simple Enough to Explain on Site

The more conditional branches you add, the harder it becomes to trace which condition is acting in a given situation.

I once built too many conditional branches using AI and Python. At first, only I understood how the system worked. In the end, even I could not follow the logic. When trouble came up, no one else on the team could deal with it either.

The same is true for robots. If the staff on site cannot explain why the robot stopped, it takes longer to respond to breakdowns and accidents. It is important to keep the rules for where the robot slows down and what makes it stop simple enough to explain on site.

When to End the Loop and How Many Times It Runs

When you design a loop, think about when the process ends and how many times it repeats.

The first point is the condition for ending the loop. If arrival is the only stop condition, the robot keeps running when a broken sensor fails to detect arrival. So, in case arrival cannot be detected, you also set a time limit (timeout).

The second point is estimating the number of repetitions and the amount of processing per pass. Even in systems other than robots, small tasks repeated many times can add up to a heavy load or a large amount of data transfer.

I once skipped this estimate in a program I built to check articles. Each check loaded the full text of every article, about 9 MB per run, and it ran 892 times in 24 hours. The data transfer went past the 5 GB limit of the service I used and reached 26.90 GB, and I received a notice that usage would be restricted. I changed the program to load only updated articles. As a result, the amount loaded per run dropped from 8,672 KB to 45 KB.

A robot's control loop also runs many times at short intervals. Multiply the amount of processing per pass by the number of repetitions, and you can estimate how much processing the whole job needs.

Predicting Speed, Time and Distance with Formulas

Before running the robot, calculate how far it will go and how many seconds it will take to arrive. If the speed is constant, you can use these two formulas:

  • Distance = speed × time
  • Time = distance ÷ speed

If you keep the speed the same and run longer, the distance grows in proportion to the time. (I explain proportion in functions and graphs for robot motion.) If you keep the distance the same and raise the speed, the time gets shorter in inverse proportion to the speed.

Speed (m/s)Time (s)Distance (m)
0.5105
1.01010
0.52010
1.02020
0.25205
Try it: speed and acceleration
Speed
Speed
0.5 m/s
2 m ÷ 4 s

The slope of this line is the speed.

Acceleration
Acceleration
0.5 m/s²
(2 m/s − 0 m/s) ÷ 4 s

The slope of this line is the acceleration.

Calculating for a Warehouse Delivery Robot

Picture a warehouse at a Japanese company in the Philippines. A delivery robot carries parts to a shelf 10 m away at 0.5 m per second. (This is a hypothetical example.)

  • At 0.5 m per second: 10 ÷ 0.5 = 20 seconds
  • At 1.0 m per second: 10 ÷ 1.0 = 10 seconds

Double the speed, and the time is cut in half. With a prediction like this, you can check in numbers how far the actual run differs from what you expected.

Try it: change one condition at a time

Move the sliders, then press the button. Try changing only one slider, then both.

Judging the Gap Between Prediction and Result in %

With robots and with other systems, I estimate the result with a formula first, then check the difference from the measured value as a percentage. By my standard, a gap within 5% passes. If it is over 5%, I look for something I missed in the assumptions of the calculation or in the program. I cover how to make predictions, keep conditions the same and record results in Robot Experiment Basics.

Setting the Stop Condition, Slow-Down Point and Time Limit

Once you have estimated the run time, decide three things: the condition for reaching the target, the point where slowing down starts, and the time limit.

The Stop Condition and Overshoot

A robot checks its position once per step. If it reaches the target between two checks, it has already passed the target by the time of the next check. Here, I call this gap the overshoot.

Take a serving robot at a restaurant. It travels 10 m from the kitchen to a table at 0.5 m per second and checks every 0.1 seconds whether it has reached the table.

  • The distance it travels from one check to the next is 0.5 × 0.1 = 0.05 m (5 cm)
  • So the overshoot, the distance it travels past the target before the next check, is about 5 cm at most

Slowing Down Before the Target

To make the overshoot smaller, lower the speed before the target. If the same serving robot slows to 0.1 m per second from 1 m before the table, the numbers look like this:

  • The distance it travels in 0.1 seconds after slowing down is 0.1 × 0.1 = 0.01 m (1 cm)
  • The time to arrive is (10 − 1) ÷ 0.5 + 1 ÷ 0.1 = 28 seconds
  • Compared with 20 seconds without slowing down, the trip takes 8 seconds longer

Slowing down keeps the stopping position closer to the target, but it takes longer to arrive. Choose the slow-down point and the slower speed to fit the job. For example, put accuracy of the stopping position first when the robot carries plates of food, and put travel time first when it brings back empty trays.

Setting the Time Limit (Timeout)

In case arrival cannot be detected, add a condition that sends a stop command when the time limit is reached.

Say a robot that carries parts in a factory travels 15 m at 0.5 m per second. The expected time to arrive is 15 ÷ 0.5 = 30 seconds. If you set the time limit to twice that, 60 seconds, then even if the sensor cannot detect arrival, the robot can be told to stop when the limit is reached, as long as the control loop is running.

A time limit that is either too short or too long causes problems. Too short, and it stops even normal runs. Too long, and the response to a problem is delayed. If you estimate the normal run time first, it is easier to judge how much extra time to allow.

Results from Testing in Webots

I tested these ideas in Webots, a robot simulator. The conditions were:

  • Software: Webots R2025a
  • Robot: e-puck (wheel radius 0.02 m). According to the official documentation, the maximum angular velocity of the wheels (how fast they turn, in radians per second) is 6.28 rad/s
  • Step: 32 ms. basicTimeStep in WorldInfo is the setting that decides the length of one simulation step, and its default value is 32 ms
  • Target: Travel 0.2 m and stop
  • Wheel angular velocity: Three settings, 1.0, 2.0 and 4.0 rad/s, with three trials for each
  • Conditional branches at each step: Estimate the distance traveled from the encoders (sensors that measure how far the wheels turn). Stop when the robot reaches the target or when the 30.0-second time limit passes. In the trials with slowing down, lower the speed before the target
  • Measurement method: Measure the stopping position with the Supervisor, a type of Webots controller, and move the robot back to the starting point before the next trial. To get the position, I used wb_supervisor_node_get_position, which returns the position of a node (an object in the simulation) in world coordinates, the coordinate system of the whole simulation (see the Webots simulation tutorial for details)

The wheel radius is 0.02 m. If you calculate the robot's speed (m/s) as "0.02 × wheel angular velocity (rad/s)," the time to arrive without slowing down is:

  • 1.0 rad/s: 0.2 ÷ (0.02 × 1.0) = 10 seconds
  • 2.0 rad/s: 0.2 ÷ (0.02 × 2.0) = 5 seconds
  • 4.0 rad/s: 0.2 ÷ (0.02 × 4.0) = 2.5 seconds

The distance traveled in one step (0.032 seconds) is 0.64 mm, 1.28 mm and 2.56 mm, respectively.

Test 1: Running Without Slowing Down

Wheel angular velocityExpected timeMeasured timeDifference from expectedDistance past the targetDistance per step
1.0 rad/s10 s10.016 s (313 steps)0.160%0.32 mm0.64 mm
2.0 rad/s5 s5.024 s (157 steps)0.480%0.96 mm1.28 mm
4.0 rad/s2.5 s2.528 s (79 steps)1.120%2.24 mm2.56 mm

The stopping positions were 0.20032 m, 0.20096 m and 0.20224 m, in that order. At every angular velocity, the three results matched, and in every trial the robot stopped after detecting that it had reached the target.

Webots console showing three runs at each wheel angular velocity without slowing down At every angular velocity, the three measurements matched, and the robot stopped as soon as it judged that it had reached the target.

Graph of wheel angular velocity against time to reach the target and distance past the target, without slowing down Left: expected and measured time to arrive. Right: distance past the target.

The results without slowing down show two things:

  • The time gap was within 5% at every speed
  • The overshoot was smaller than the distance per step under every condition, and it grew as the wheel angular velocity went up. The 2.24 mm at 4.0 rad/s is 7 times the 0.32 mm at 1.0 rad/s

Test 2: Slowing Down 0.02 m Before the Target

This time, I added a condition that lowers the wheel angular velocity to 1.0 rad/s from 0.02 m before the target. I estimated the time to arrive by splitting the run into the part before slowing down and the part after.

  • 4.0 rad/s: (0.2 − 0.02) ÷ (0.02 × 4.0) + 0.02 ÷ (0.02 × 1.0) = 3.25 seconds
  • 2.0 rad/s: (0.2 − 0.02) ÷ (0.02 × 2.0) + 0.02 ÷ (0.02 × 1.0) = 5.5 seconds
Wheel angular velocityExpected timeMeasured timeDifference from expectedDistance past the target
1.0 rad/s10 s10.016 s0.160%0.32 mm
2.0 rad/s5.5 s5.504 s (172 steps)0.073%0.32 mm
4.0 rad/s3.25 s3.200 s (100 steps)−1.538%0.32 mm

Here too, the three results matched under each condition, and in every trial the robot stopped after detecting that it had reached the target. I explain how to change only one condition and compare the results in robot experiment graphs.

Webots console showing three runs at each wheel angular velocity, slowing down from 0.02 m before the target With the slow-down condition added, the distance past the target was the same under all three conditions.

Graph of wheel angular velocity against time to reach the target and distance past the target, slowing down from 0.02 m before the target The higher the wheel angular velocity before slowing down, the more the slow-down added to the time to arrive.

  • The overshoot was 0.32 mm under all three conditions. In every case, the wheel angular velocity was 1.0 rad/s just before the target
  • At 4.0 rad/s, the time went from 2.528 seconds to 3.200 seconds, an increase of 0.672 seconds. At 2.0 rad/s, it went from 5.024 seconds to 5.504 seconds, an increase of 0.48 seconds

Only the run that started at 4.0 rad/s arrived faster than predicted. This is likely because the robot also checks once per step whether it has reached the slow-down point. The robot kept moving until the next check, so it probably started slowing down a little past the point assumed in the calculation, and the fast section became longer.

As in the serving robot example, I confirmed that slowing down before the target makes the overshoot smaller and makes the trip longer.

What to Check When Your Company Brings In Robots

When you are considering robots, asking the maker or developer questions like these will show you how they think about control:

  • How many seconds is one step of the control loop? How far can the robot move in that time, at most?
  • Does the robot slow down before the target? If it does, how many seconds does that add to each trip?
  • If a sensor breaks, under what condition does the robot stop? If there is a time limit, how many seconds is it, and on what basis did you set it?

If you know the stop and slow-down conditions in numbers, it is easier to find the cause when the robot stops somewhere you did not expect.

FAQ

Q: Which comes first, conditional branches or loops?

A: Start with the loop that repeats "sense, decide, act." Then place conditional branches in the "decide" part to set what the robot does at each step, such as stopping when it arrives and slowing down when it gets close to the target.

Q: Can the overshoot be brought down to zero?

A: Because the robot checks its position at intervals, it is hard to remove overshoot completely with this method. Slowing down before the target reduces how far the robot moves between checks. In the Webots test, the overshoot went from a maximum of 2.24 mm down to 0.32 mm.

Q: How many seconds should the time limit be?

A: Estimate the normal run time with distance ÷ speed, then add a margin. In the factory example above, the expected time was 30 seconds and the time limit was set to 60 seconds. If you set it too short, the robot may stop partway even when it is running normally.

Q: What changes when the robot runs faster?

A: If the check interval stays the same, a faster robot covers more distance per step and passes the target more easily. In the Webots test, raising the wheel angular velocity from 1.0 rad/s to 4.0 rad/s cut the time from about 10 seconds to about 2.5 seconds. At the same time, the overshoot grew from 0.32 mm to 2.24 mm.

Conclusion

A robot repeats "sense, decide, act" and uses conditional branches to decide when to stop or slow down. To design the stopping position, the basic approach is to estimate the run time from speed and distance, then use that estimate to decide three things: the condition for reaching the target, the point where slowing down starts, and the time limit. If you also estimate the amount of processing per pass and the number of repetitions, and keep the conditions simple enough to explain on site, it is easier to find the cause when something goes wrong.

PH AI Works is based in Manila, Philippines, and helps small and mid-sized businesses and Japanese companies with AI adoption, business automation and web development. If you are thinking about bringing robots or automation into a warehouse, factory or restaurant, book a free consultation.

References

About the author

Author
Author

Founder / AI Engineer (36+ years in IT)

  • ●From Tokyo · based in Manila for 13+ years
  • ●36+ years in IT (development, SEO, AI)
  • ●Google Certified AI Professional
  • ●IBM Certified Generative AI Engineer
  • ●AI chatbots, RAG & AI agent development
  • ●Physical AI Engineer

A Japanese AI engineer with 36+ years in IT and 13+ years on the ground in the Philippines. I write from hands-on experience to help Japanese companies adopt AI that actually delivers results — chatbots, workflow automation, AI agents, and AI-driven marketing. Feel free to reach out in Japanese or English.

Your Competitors Are Already Using AI!

Is your business keeping up?