AMR odometry: a practical pipeline from encoder pulses to /odom
Atlasbot · 22/8/2026 · 11 min read
Article Encoder in the Hardware AMR category learned how to read pulses from sensors. Lessons Odometry in Localization and kinematics Differential Drive learned the dead reckoning principle and the position integration formula. This article does not repeat those parts — it assembles them into a complete firmware pipeline, which actually runs on the MCU of an AMR, with 3 different frequencies operating in parallel.
Key concepts
The complete odometry pipeline consists of 4 serial steps, each running at a different hardware/frequency layer:
- Impulse Count (ISR) — hardware interrupt, highest frequency, counter increment only, no calculation
- Convert Δticks to distance/velocity — run periodically (50-100Hz), using wheel geometry parameters
- Calculate
v,ωfrom the speed of each wheel — apply the Differential or Mecanum formula learned in the previous 2 lessons - Integrate into
x, y, θand publish — lower frequency (20-50Hz), exactly required ROS2 period
How it works
The first three steps run on the MCU, independent of ROS2 — this is why odometry is still "available from the first second" as stated in Odometry in Localization, independent of the startup latency of the higher software layer:
1. ISR (hardware interrupt, each time the encoder pulse edge appears):
tick_L++ or tick_L-- (depending on direction, determined via A/B channel — learned in Encoder lesson)
2. Each cycle Δt (eg: 20ms, 50Hz):
Δtick_L = current_tic_L − previous_tic_L
left_gear_distance = Δtick_L × distance_per_tick
v_L = left_wheel_distance / Δt
3. Apply the learned Differential/Mecanum formula:
v, ω (or vx, vy, ω) from v_L, v_R (or v1..v4)
4. Integrate (frequency can be lower than steps 2-3, eg: 30Hz):
θ += ω × Δt; x += v×cos(θ)×Δt; y += v×sin(θ)×Δt
→ packaged into message nav_msgs/Odometry, published to topic /odom
Technical details
Formulas
"distance per tick" conversion constant — the link connecting the encoder's raw data to the odometry formula, calculated once when designing the hardware:
distance_per_tick = (π × Wheel_diameter) / (PPR × Gear_ratio × 4)
(×4 factor applies when using quadrature encoder to count both rising/falling edges of both channels —
learned in Encoder lesson)
For example: wheel diameter 6.5cm, original encoder 11 PPR, gear ratio 1:34
distance_per_tick = (π × 6.5) / (11 × 34 × 4) ≈ 0.0136 cm/tick
Parameters
| Steps in the pipeline | Typical frequency | Where to run? |
|---|---|---|
| Pulse Count (ISR) | Several kHz (hardware interrupt) | MCU, hard interrupt |
| Calculate v, ω + PID | 50-100Hz | MCU, main loop |
| Publish /odom | 20-50Hz | MCU → ROS2, or directly on the MPU if the driver runs there |
Practical applications
Message nav_msgs/Odometry published to topic /odom is a direct input to package robot_localization (mentioned in the IMU article in AMR) — where EKF merges this data with the IMU to compensate for wheel slippage, and then is an input to AMCL/Nav2 at the Navigation layer. One detail that's easy to miss: this message needs to have a reasonable covariance (uncertainty) field, not just the default — EKF uses this value to decide "what percentage of odometry to believe" relative to other data sources when merging.
Practical example
import math
DIST_PER_TICK = 0.0136 # cm/tick, calculated under Formulas
L = 35 # wheel separation (cm)
dt = 0.02 # 50Hz
x, y, theta = 0.0, 0.0, 0.0
tick_L_prev, tick_R_prev = 0, 0
def update_odometry(tick_L, tick_R, x, y, theta):
global tick_L_prev, tick_R_prev
d_L = (tick_L - tick_L_prev) * DIST_PER_TICK
d_R = (tick_R - tick_R_prev) * DIST_PER_TICK
tick_L_prev, tick_R_prev = tick_L, tick_R
v = (d_L + d_R) / 2 / dt
omega = (d_R - d_L) / L / dt
theta += omega * dt
x += v * math.cos(theta) * dt
y += v * math.sin(theta) * dt
return x, y, theta
# Simulate 1 cycle: right wheel turns more than left (gentle left turn)
x, y, theta = update_odometry(tick_L=50, tick_R=70, x=x, y=y, theta=theta)
print(f"x={x:.4f} y={y:.4f} theta={math.degrees(theta):.2f} do")
Output:
x=0.0136 y=0.0000 theta=0.65 do
After exactly 1 cycle of 20ms, the robot has moved and rotated a very small angle — this pipeline repeats 50 times per second, each time accumulating a very small step, exactly the nature of dead reckoning learned in the lesson Odometry in Localization.
Common mistakes
- Forgot the ×4 quadrature factor when calculating distance_per_tick — as warned in the Encoder article, mistakenly using the original PPR instead of the effective PPR causes the entire pipeline to deviate dozens of times even though the logic code is completely structurally correct.
- Integer overflow of the tick counter — if you use a small integer type (eg 16-bit) for the counter to run continuously for hours, the counter overflows to 0 causing a large false jump in Δtick at the exact moment of overflow.
- Read tick_L and tick_R not at the same time — if the code reads
tick_Land then does something else before readingtick_R, the delay between the two reads (even though very small) skews the pair of values used to calculatev,ωin the same cycle — so read both almost simultaneously (disable temporary interrupt on read, or atomic read). - Publish /odom with default covariance does not reflect reality — as stated in the Application section, EKF needs a reasonable covariance value to balance reliability between data sources, the default value of the sample code should not be left alone.
Conclusion
Odometry in an AMR is not actually a one-time formula — it is a 4-step pipeline that runs continuously at 3 different frequencies, from hardware interrupts at kHz to ROS2 publishes at a few tens of Hz. Understanding this pipeline correctly makes debugging much more effective than just understanding simple math formulas — most actual odometry errors are in the raw data conversion stage (ticks, geometric constants, reading frequencies), not in the integral formula. This is also the last article of the AMR Drive System section — the next AMR Software Architecture section puts this entire pipeline in the right place in the 5 software layers introduced in the AMR Architecture article.
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.