Sign In / Join Cloud 360
Engineering • Innovation • Learning
Robotics & Automation Intermediate 24 min

Mobile Robot Differential Odometry with 4X Hardware Quadrature Decoding

Track 2D robot position (x, y, theta) in Cartesian space using dual optical wheel encoders, hardware timer quadrature modes, and Runge-Kutta kinematics integration.

2. Prerequisites & Background Theory:

Differential robot kinematics, 4X quadrature edge detection, hardware timer encoder modes, and floating-point math.

STEP 1

Goal & Engineering Objectives

Compute continuous real-time Cartesian pose (x, y, theta) of an autonomous mobile robot across floor navigation courses with less than 2% cumulative drift.

STEP 2

Governing Mathematical Formulation & Physics

Forward Kinematics of Differential Drive Robot: $$\Delta d_L = \frac{2\pi r \cdot \Delta N_L}{PPR_{4X}}, \quad \Delta d_R = \frac{2\pi r \cdot \Delta N_R}{PPR_{4X}}$$ Linear Center Displacement & Heading Increment: $$\Delta d = \frac{\Delta d_R + \Delta d_L}{2}, \quad \Delta\theta = \frac{\Delta d_R - \Delta d_L}{B}$$ 2nd-Order Runge-Kutta Pose Integration: $$x_{k} = x_{k-1} + \Delta d \cos\left(\theta_{k-1} + \frac{\Delta\theta}{2}\right)$$ $$y_{k} = y_{k-1} + \Delta d \sin\left(\theta_{k-1} + \frac{\Delta\theta}{2}\right)$$ $$\theta_k = \theta_{k-1} + \Delta\theta$$ Where r is wheel radius and B is track width (wheel-to-wheel center separation).
STEP 3

System Architecture & Block Flow Diagram

[Left Wheel Optical Encoder]  --(Chan A, B)--> [STM32 TIM2 32-Bit Quadrature Mode]
[Right Wheel Optical Encoder] --(Chan A, B)--> [STM32 TIM3 16-Bit Quadrature Mode]
                                                        |
                                         (Periodic Timer Interrupt @ 50 Hz)
                                                        v
                                        [Delta Tick Calculation]
                                        [Runge-Kutta Pose Integration]
                                                        v
                                        [Cartesian Pose (x, y, theta)]
STEP 4

Required Hardware Bill of Materials (BOM)

Hardware Component Qty Engineering Specification
Optical Quadrature Wheel Encoders (600 PPR) 2 Push-pull AB channels, 2400 counts/rev in 4X
STM32F401 Development Board 1 Hardware 32-bit timers with quadrature decoder blocks
Precision Aluminum Robot Wheels with O-Rings 2 Radius r = 32.5 mm ± 0.05 mm
Chassis Baseplate 1 Fixed track wheelbase B = 145.0 mm
STEP 5

Wiring, Pinout & Schematics Mapping

• Left Encoder Chan A -> STM32 PA0 (TIM2_CH1) • Left Encoder Chan B -> STM32 PA1 (TIM2_CH2) • Right Encoder Chan A -> STM32 PA6 (TIM3_CH1) • Right Encoder Chan B -> STM32 PA7 (TIM3_CH2) • Connect encoder VCC pins to +5V and GND to common system ground • Shield braid of encoder cable grounded at chassis end only.
STEP 6

Firmware Implementation & Algorithms

// C++: Differential Drive Robot Odometry Integrator
#include <cmath>

struct RobotPose {
    double x;     // Meters
    double y;     // Meters
    double theta; // Radians
};

class OdometryTracker {
public:
    OdometryTracker(double wheel_radius, double track_width, int ppr_4x)
        : r(wheel_radius), b(track_width), cpr(ppr_4x) {
        pose = {0.0, 0.0, 0.0};
        prev_left_ticks = 0;
        prev_right_ticks = 0;
    }

    void update(int64_t cur_left_ticks, int64_t cur_right_ticks) {
        int64_t delta_l = cur_left_ticks - prev_left_ticks;
        int64_t delta_r = cur_right_ticks - prev_right_ticks;
        prev_left_ticks = cur_left_ticks;
        prev_right_ticks = cur_right_ticks;

        double dist_l = (2.0 * M_PI * r * delta_l) / cpr;
        double dist_r = (2.0 * M_PI * r * delta_r) / cpr;

        double delta_dist  = (dist_r + dist_l) / 2.0;
        double delta_theta = (dist_r - dist_l) / b;

        // Runge-Kutta 2nd Order integration
        double mid_theta = pose.theta + (delta_theta / 2.0);
        pose.x += delta_dist * cos(mid_theta);
        pose.y += delta_dist * sin(mid_theta);
        pose.theta += delta_theta;

        // Normalize theta to [-PI, +PI]
        while (pose.theta > M_PI)  pose.theta -= 2.0 * M_PI;
        while (pose.theta < -M_PI) pose.theta += 2.0 * M_PI;
    }

    RobotPose getPose() const { return pose; }

private:
    double r, b;
    int cpr;
    int64_t prev_left_ticks, prev_right_ticks;
    RobotPose pose;
};
STEP 7

Bench Testing & Waveform Verification

1. Push robot forward by hand exactly 1000 mm along a tape measure: Verify calculated x reads 1.000m ± 0.015m. 2. Spin robot in place exactly 10 complete 360° revolutions: Verify calculated theta returns to 0.00 radians ± 0.10 rad. 3. Complete a 1-meter square path: Verify robot reports final position (0, 0) within 25 mm.
STEP 8

Troubleshooting & Common Failure Modes

• Problem: Robot reports moving backwards when driven forward. Solution: Invert encoder count polarity in hardware timer register (CCER CC1P bit). • Problem: Systematic heading drift turns straight paths into arcs. Solution: Calibrate effective wheel diameters rL and rR. Even a 0.2 mm tire wear difference creates a continuous turning bias. • Problem: Position jumps wildly during rapid acceleration. Solution: Wheel slip. Optical encoders measure wheel rotation, not ground motion. Reduce peak motor acceleration limits.
STEP 9

Extensions & Undergraduate Research Challenges

1. Fuse wheel odometry with 6-DOF IMU yaw gyroscope using an Extended Kalman Filter (EKF) to eliminate tire slip errors. 2. Publish odometry transform frames (odom -> base_link) over ROS2 micro-ROS to an autonomous navigation stack.
All Engineering Guides
Closed-Loop Discrete PID Control & Inverted Pendulum Balancing
Control Systems & Robotics • 25 min
CAN-Bus 2.0B Telemetry Node with Industrial Sensor Fusion
Embedded Systems & Automotive • 20 min
Precision Dual-Axis Stepper Motion Controller with S-Curve Acceleration
Mechatronics & Motion Control • 22 min
IoT Environmental Monitoring Station with OLED & Secure Telemetry
Internet of Things (IoT) • 18 min
Closed-Loop DC Motor Velocity Control with Optical Quadrature Encoder
Power Electronics & Motion • 20 min
Astable 555 Timer & 74HC595 Shift Register Digital Sequencer
Discrete Electronics & Digital Logic • 16 min
Field-Oriented Control (FOC) & Space Vector Modulation for BLDC Motors
Power Electronics & Motors • 28 min
High-Efficiency Synchronous Buck DC-DC Converter Design
Power Electronics & Energy • 24 min
Discrete Kalman Filter IMU Sensor Fusion for Tilt & Orientation
Control Systems & Robotics • 26 min
Industrial Modbus RTU Slave Node over Galvanically Isolated RS-485
Industrial Automation • 22 min
4th-Order Sallen-Key Active Butterworth Filter for Sensor Anti-Aliasing
Electronics Fundamentals • 22 min
Mobile Robot Differential Odometry with 4X Hardware Quadrature Decoding
Robotics & Automation • 24 min
Discrete N-Channel MOSFET H-Bridge with Bootstrap Gate Drive
Electronics & Motor Control • 25 min
High-Speed SPI NOR Flash Black-Box Flight Data Logger
Embedded Systems & IoT • 22 min
MPU-6050 Digital Motion Processor (DMP) Quaternion Spatial Tracking
Sensors & Embedded Algorithms • 24 min
Deterministic Multitasking Architecture with FreeRTOS Kernel
Embedded Systems & Software • 25 min
Ask LUNA About This Lab

Ask LUNA for circuit calculations, state-space equations, tuning parameters, or component substitutes.

Hardware Catalog

Inspect verified electrical specs, voltage rails, and pinouts across all 10 component categories.

Browse Components →