Programmable Electronics · Final project

Balancing Robot Simulator

A virtual version of the robot from the six classes: an MPU6050 that reads like the real one, the same complementary filter, PID, dead zone, tilt cutoff and outer loop, and a body that falls the way a real one does.

Balancing Standing 0.0 s t = 0.0 s

Try this

    The robot, from the side

    Forward is to the right
    0.0°
    Hold ← → to drive (f · b · s also work) · space pauses

    MPU6050 registers (raw)

    ax 0x3B
    ay 0x3D
    az 0x3F
    gx 0x43
    gy 0x45
    gz 0x47

    16384 counts = 1 g, 131 counts = 1 °/s. The robot tips around the X axis, so ay and az carry the tilt and gx the turning rate.

    What the code computes

    accAngle°
    gyroRate°/s
    angle (fused)°
    true tilt° (sim only)
    u → PWM
    STBY
    encoder counts330 per wheel turn
    wheelSpeedmm/s
    positionmm from the tape

    Teleplot, last 10 seconds

    Click a name to hide or show it

    Knobs and switches

    Inside loop(), every 5 ms

    Live values in the comments
    
        

    Why the accelerometer gets fooled

    An accelerometer measures gravity plus every other push it feels. When the wheels speed up under the robot, that push looks exactly like a lean the other way. Tap the table or slide the robot in Class 1 to see it. On a balancing robot the wheels are always speeding up and slowing down, so the filter has to lean on the gyro. In Class 6, compare α = 0.98 and 0.995 with the same push.

    Why it wanders

    To keep rolling, a motor needs a steady PWM, and with PID alone a steady PWM needs a steady lean, which makes it roll faster. The I term holds that PWM without the lean. The outer loop in Class 5 watches the encoders and tips the target angle back, so the robot finds its own balance point, even with a book on top.

    What this model leaves out

    The numbers are assumptions, not your robot: JGB37-520-type motors (about 315 rpm, 0.75 N·m stall at 12 V), 80 mm wheels, a 1.1 kg body with the power bank on top, an IMU mounted 1° off level. It skips gear backlash, wheel slip, I²C timing and a sagging battery. Use it to see why things happen, then tune the real robot with the knobs.