Robot programming in 3D

A standing panel shows a robot control loop where Sense, Compare, Decide and Act blocks are linked in a feedback cycle.Drag to turn

Robotics programming is the writing of software that lets a machine read sensors, decide what to do and drive actuators such as motors. The program usually runs as a repeating loop that links the robot to the physical world.

What a robot program has to do

A robot is a machine with sensors, actuators and a controller. Sensors measure the world, for example distance, light or wheel rotation. Actuators produce motion, for example electric motors. The controller is a small computer that runs the program.

Unlike a program that only processes data, a robot program must deal with a physical world that is noisy and changes. Sensor readings are imperfect, and motors do not move exactly as commanded.

The sense, plan, act loop

Most robot programs repeat three steps. Sense means reading the sensors. Plan means choosing a response from those readings. Act means sending commands to the actuators. The loop runs many times per second, and the time between runs is called the control period.

A simple plan can be a rule such as: if the distance sensor reads under 20 cm, turn; otherwise drive forward. More advanced robots use state machines, which hold a current mode such as searching, approaching or stopping, and switch modes on events. Path planning often uses graph search algorithms.

Worked example: proportional control of speed

Feedback control compares a target with a measurement and corrects the difference, called the error. A proportional controller sets the command equal to a gain Kp times the error. Suppose a robot should drive at 1.0 m/s, the measured speed is 0.6 m/s, and Kp is 50 in motor power percent per m/s.

The error is 1.0 minus 0.6, which is 0.4 m/s. The command is 50 times 0.4, which is 20 percent power added. As the speed rises to 0.9 m/s, the error is 0.1 and the added power is 50 times 0.1, which is 5 percent. The correction shrinks as the target is approached. A pure P controller can leave a small steady error, which is why integral and derivative terms are often added in PID control.

Common misconceptions

One belief is that a robot does exactly what the code says. Wheels slip and sensors drift, so the program must use feedback rather than assume a command worked. Another belief is that robots need artificial intelligence. Many useful robots, such as simple line followers, run only fixed rules and control formulas.

A third error is treating time as free. If the loop is slow or its timing varies, control can become unstable. Many robots therefore use real-time scheduling for the control loop.

Where robot programming appears

Robot programming is used in factory arms, warehouse vehicles, drones, surgical assistance tools and robot vacuum cleaners. Hobby and teaching robots often use microcontrollers programmed in C or C++ or Python. Larger systems often use a framework such as ROS, which splits the software into nodes that exchange messages.

Key terms

Sensor
A device that measures a physical quantity such as distance, light or rotation and gives the program a reading.
Actuator
A component, such as a motor, that turns a command into physical motion.
Control loop
A routine that repeatedly senses, decides and acts at a fixed rate.
Feedback
Using a measured result to adjust the next command.
Error
The target value minus the measured value.
PID controller
A controller whose command combines proportional, integral and derivative terms of the error.
State machine
A model in which a program is in one named mode at a time and changes mode on defined events.

Common questions

Which language is used to program robots?

C and C++ are common for real-time control and microcontrollers. Python is common for teaching and high-level logic. The choice depends on speed needs and the hardware.

What is the difference between open-loop and closed-loop control?

Open-loop control sends commands without checking the result, such as running a motor for two seconds. Closed-loop control measures the result and corrects it, as in the proportional example.

Why do robots need sensors if the program knows the commands?

Commands do not guarantee outcomes. Friction, slip, load and obstacles change what happens, so sensors let the program detect and correct the difference.

Learn this with a tutor

Sidechair's AI tutor will teach this topic in 3D. Join the waitlist for early access.

We'll only email you about Sidechair. Read our privacy policy.