Selbstbalancierender Roboter (ESP32, MPU-6050, zwei Getriebemotoren)
Ein inverses Pendel auf zwei Rädern hält sich selbst aufrecht. Schubs es in der Welt an.
- MPU-6050
- L298N
- Gearmotors

So funktioniert es
Eine Platine steht auf zwei Rädern, der Akku 15 cm über der Achse: ohne Regelung kippt sie um. Der ESP32 liest 200-mal pro Sekunde die Neigung vom MPU-6050 (Gyroskop und Beschleunigungssensor kombiniert), ein PD-Regler macht daraus einen Motorbefehl, und der L298N treibt beide Getriebemotoren per PWM. Die Räder rollen mit echter Reibung auf dem Boden; das Drehmoment der Motoren kommt aus ihrem Strom, ihre Drehzahl wirkt zurück auf die Schaltung. Schubs den Roboter in der Welt mit der Maus an: er fährt unter seinen Schwerpunkt und fängt sich. Ändere die Verstärkungen KP, KD und KV im Sketch oder setz den Akku höher (Abstand im Inspektor) und schau, wie der Roboter reagiert.
Verdrahtung
Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.
| Netz | Pins |
|---|---|
| GND | A1 GNDBT1 −C1 2C2 2C3 2U2 GNDU2 AD0U2 CLKINU2 FSYNCU3 GND |
| V3P3 | A1 3V3C1 1R1 1R2 1U2 VDDU2 VLOGIC |
| V5 | A1 5VU3 +5V |
| VBAT | BT1 +U3 +12V |
| CPOUT | C3 1U2 CPOUT |
| ENA | A1 GPIO25U3 ENA |
| ENB | A1 GPIO14U3 ENB |
| IN1 | A1 GPIO26U3 IN1 |
| IN2 | A1 GPIO27U3 IN2 |
| IN3 | A1 GPIO32U3 IN3 |
| IN4 | A1 GPIO33U3 IN4 |
| ML_A | ML +U3 OUT1 |
| ML_B | ML −U3 OUT2 |
| MR_A | MR −U3 OUT3 |
| MR_B | MR +U3 OUT4 |
| REGOUT | C2 1U2 REGOUT |
| SCL | A1 GPIO22R1 2U2 SCL |
| SDA | A1 GPIO21R2 2U2 SDA |
Bauteile
Firmware
sketch.ino · 84 Zeilen
// Self-balancing robot: an ESP32 keeps an inverted pendulum on two wheels upright.
// The MPU-6050 measures the tilt (pitch): the gyroscope integrated for the fast part, the
// accelerometer for the slow part (complementary filter). A PD controller turns the tilt into a
// motor command; the L298N drives both gearmotors with PWM on ENA / ENB. Without wheel encoders
// the speed is estimated from the command (the motors follow their voltage): the robot leans
// back against it, so it does not run away. Tilted beyond 45° it has fallen: the motors stop
// until it stands upright again.
//
// Tuning: KP pushes back against the tilt, KD damps the swing (gyro rate), KV keeps it in place.
// Raise KP until it oscillates, then KD; a heavier or taller robot needs more of both.
#include <Wire.h>
// pins
constexpr int I2C_SDA = 21, I2C_SCL = 22;
constexpr int ENA = 25, IN1 = 26, IN2 = 27; // left motor
constexpr int ENB = 14, IN3 = 32, IN4 = 33; // right motor
constexpr uint8_t MPU = 0x68;
// controller
float KP = 110, KD = 2; // command per degree, per degree/s
float KV = 8; // lean against the speed: degrees per unit of the estimated speed
constexpr float SPEED_TAU = 0.5; // the command, low-pass filtered (s): the speed estimate
constexpr float FALLEN = 45; // beyond this: fallen, motors off
constexpr float ALPHA = 0.995; // complementary filter: share of the gyroscope (driving accelerates the sensor too)
constexpr int PERIOD_MS = 5; // 200 Hz
constexpr int PWM_BITS = 10, PWM_MAX = (1 << PWM_BITS) - 1;
constexpr int DIR = 1; // +1 / −1: which way "forward" turns the wheels
constexpr float MIN_DUTY = 0.12; // below this duty the gearbox does not turn: start from here
float angle = 0, speed = 0, lastU = 0;
uint32_t last = 0;
int16_t read16(uint8_t reg) {
Wire.beginTransmission(MPU); Wire.write(reg); Wire.endTransmission(false);
Wire.requestFrom(MPU, (uint8_t)2);
return (int16_t)((Wire.read() << 8) | Wire.read());
}
// a signed command -1..1 for one motor: direction pins and PWM duty
void motor(int en, int a, int b, float u) {
u = constrain(u * DIR, -1.0f, 1.0f);
digitalWrite(a, u > 0 ? HIGH : LOW);
digitalWrite(b, u > 0 ? LOW : HIGH);
float duty = fabsf(u) < 0.005f ? 0 : MIN_DUTY + (1 - MIN_DUTY) * fabsf(u);
ledcWrite(en, (int)(duty * PWM_MAX));
}
void setup() {
Serial.begin(115200);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
ledcAttach(ENA, 1000, PWM_BITS);
ledcAttach(ENB, 1000, PWM_BITS);
motor(ENA, IN1, IN2, 0); motor(ENB, IN3, IN4, 0);
Wire.begin(I2C_SDA, I2C_SCL, 400000);
Wire.beginTransmission(MPU); Wire.write(0x6B); Wire.write(0); Wire.endTransmission(); // wake up
// start from the accelerometer's tilt
angle = atan2f(-read16(0x3B), read16(0x3F)) * 57.2958f;
last = micros();
Serial.println("balancer ready");
}
void loop() {
// every 5 ms: the task sleeps in between (FreeRTOS)
static TickType_t wake = xTaskGetTickCount();
vTaskDelayUntil(&wake, pdMS_TO_TICKS(PERIOD_MS));
uint32_t now = micros();
float dt = (now - last) * 1e-6f;
last = now;
float ax = read16(0x3B), az = read16(0x3F), gy = read16(0x45) / 131.0f; // raw, raw, deg/s
angle = ALPHA * (angle + gy * dt) + (1 - ALPHA) * atan2f(-ax, az) * 57.2958f;
speed += (lastU - speed) * dt / SPEED_TAU;
float err = angle + KV * speed;
if (fabsf(angle) > FALLEN) {
speed = lastU = 0;
motor(ENA, IN1, IN2, 0); motor(ENB, IN3, IN4, 0);
return;
}
float u = (KP * err + KD * gy) / PWM_MAX;
lastU = constrain(u, -1.0f, 1.0f);
motor(ENA, IN1, IN2, u);
motor(ENB, IN3, IN4, u);
static uint32_t lastPrint = 0;
if (now - lastPrint > 500000) { lastPrint = now; Serial.printf("tilt %.1f° motors %+.0f%%\n", angle, lastU * 100); }
}Im Simulator öffnen