Atlas Robotics
Robot ControlBasic

Velocity control: from /cmd_vel to motor PWM

Atlasbot · 17/8/2026 · 11 min read

Velocity control: from /cmd_vel to motor PWM
Motor speed control loop using PID and encoder feedback

The ros2 topic used the command ros2 topic pub /cmd_vel geometry_msgs/msg/Twist to send the velocity command. But sending a command is just desire — the motor driver has to find a way to achieve that exact speed in reality, regardless of whether the load is heavy/light, the floor is rough/slippery, or the battery is running low. This is exactly the velocity control problem.

Key concepts

The simplest way — without using feedback at all — is to map the desired speed directly to a fixed PWM value, based on theoretical motor parameters:

PWM = desired_speed / maximum_speed × 255

Problem: 50% PWM on a flat floor gives X speed, but the same 50% PWM on an inclined floor or when the robot is carrying more load gives X speed lower than X. This is called control open-loop (open loop) — there is no way to automatically detect and correct bias.

How it works

True Velocity control requires a feedback loop: read the actual speed from the encoder, compare it with the desired speed, use PID to continuously adjust the PWM to eliminate errors:

loop (fixed frequency, e.g. 100Hz):
    toc_do_thuc_te = doc_encoder()
    error = toc_do_mong_muon − toc_do_thuc_te
    pwm = pid.compute(error, dt)
    set_pwm(pwm)

In short: Open-loop is only true when all conditions are ideal and constant. Closed-loop (using encoder + PID) is the only way to ensure that the actual speed closely follows the desired speed when operating conditions continuously change.

Two levels of velocity control in a ROS2 robot:

High layer (ROS2, on MPU): /cmd_vel (v, ω) → inverse kinematics → v_L, v_R desired
Low level (MCU): PID velocity control loop for each wheel, running 100Hz-1kHz

Technical details

Formulas

The steady-state error for velocity control on AMR is usually calculated as a percentage of target speed, not a fixed absolute value:

Error (%) = |Actual speed − Target speed| /Target speed × 100%

For example: target 0.5 m/s, after PID stabilized, measured 0.48 m/s
Error = |0.48 − 0.5| / 0.5 × 100% = 4%

Typical acceptance threshold for AMR: less than 2-5% at steady state

Parameters

In the ROS2 ecosystem, not everyone writes their own velocity control loop from scratch — package ros2_control provides a standardized architecture for this:

Ingredients ros2_controlRole
diff_drive_controllerGet /cmd_vel, calculate inverse kinematics to calculate the speed of each wheel
Hardware InterfaceAbstract layer that communicates with the real motor driver (read encoder, write commands)
Controller ManagerManage the lifecycle and running frequency of controllers

Practical applications

Most modern AMR projects using ROS2 do not rewrite the entire /cmd_vel → PID → PWM pipeline from scratch — ros2_control and diff_drive_controller (or equivalent for Mecanum) have already standardized this architecture, just need to write the correct "Hardware Interface" connecting to the robot's specific motor driver (read real encoder, write real PWM/current value). This approach clearly separates the standard control logic (reusable across many different robots) from the specific hardware drivers (which vary depending on the robot), significantly reducing the amount of code that needs to be written and debugged compared to implementing it entirely from scratch.

Practical example

$ ros2 topic pub /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.5}}" --once
$ ros2 topic echo /wheel_velocities
Output over time (simulation, robot climbs slightly halfway):
t=0.0s: target=0.50, actual=0.00 (just received order)
t=0.5s: target=0.50, actual=0.47 (accelerating, PID adjusting)
t=1.0s: target=0.50, actual=0.49 (nearly stable, error ~2%)
t=1.5s: target=0.50, actual=0.41 (start climbing, slow down)
t=2.0s: target=0.50, actual=0.49 (PID has compensated, speed is stable again)

This data series correctly illustrates the closed-loop behavior mentioned in the Principle of Operation section — the actual speed drops as soon as it encounters a slope (t=1.5s), but the PID automatically detects the deviation and compensates within half a second, without knowing in advance what the slope is.

Common mistakes

  • Mistaken unit between /cmd_vel (m/s, rad/s) and raw PWM — forgot to convert the correct coefficient between the ROS2 standard physical unit and the driver's internal 0-255/0-1000 PWM value, causing the robot to run at the wrong speed even though the message sent was the correct value.
  • Do not handle timeout when /cmd_vel stops publishing — if the high-level control node (Nav2, teleop) crashes or loses connection, the robot must stop itself if it does not receive a new command within a certain period of time — lack of this mechanism is a serious security flaw, the robot can continue to run following the last command indefinitely.
  • Missing maximum speed clamp — straight from /cmd_vel without checking the safety limit of the robot, an incorrect command (due to a bug in the upper layer or corrupted data) can command a speed far beyond the safe capabilities of the hardware.
  • Encoder feedback latency over the network makes the PID unstable — if the velocity control loop runs on a different machine than where the encoder is read (over the network instead of locally on the MCU), network latency added to the feedback loop can cause fluctuations — this is why the speed PID loop should always run locally on the MCU closest to the motor, not over the network.

Conclusion

Velocity control turns the /cmd_vel command from "desired" into reliable actual speed, regardless of load or terrain changes — thanks to a closed-loop feedback loop using encoder + PID, and in practice is often standardized via ros2_control instead of writing it yourself from scratch. The next article in the column is about Acceleration Limits — why hitting the target speed right away isn't necessarily what a robot should do.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

Velocity control: from /cmd_vel to motor PWM — Atlas Robotics