[{"data":1,"prerenderedAt":174},["ShallowReactive",2],{"example-page-balancer":3},{"id":4,"info":5,"parts":6,"wired":24,"refs":25,"nets":26,"moreNets":168,"code":169},"balancer","A board standing on two wheels with the battery 15 cm above the axle: without control it falls over. The ESP32 reads the tilt from the MPU-6050 200 times a second (gyroscope and accelerometer combined), a PD controller turns it into a motor command, and the L298N drives both gearmotors with PWM. The wheels roll on the floor with real friction; the motors' torque comes from their current, their speed goes back into the circuit. In the World, push the robot with the mouse: it drives under its centre of mass and catches itself. Change the gains KP, KD and KV in the sketch, or raise the battery (standoff in the inspector) and see how the robot reacts.",[7,10,13,15,18,20,22],[8,9],"capacitor",3,[11,12],"gearmotor",2,[14,12],"resistor",[16,17],"battery",1,[19,17],"esp32_devkitc",[21,17],"l298n_module",[23,17],"mpu6050_qfn",false,{"A1":19,"BT1":16,"C1":8,"C2":8,"C3":8,"U2":23,"U3":21,"R1":14,"R2":14,"ML":11,"MR":11},[27,53,68,75,82,87,93,99,105,111,117,123,130,136,143,149,154,161],{"name":28,"pins":29},"0",[30,33,36,39,41,43,45,47,49,51],[31,32],"A1","GND",[34,35],"BT1","−",[37,38],"C1","2",[40,38],"C2",[42,38],"C3",[44,32],"U2",[44,46],"AD0",[44,48],"CLKIN",[44,50],"FSYNC",[52,32],"U3",{"name":54,"pins":55},"V3P3",[56,58,60,62,64,66],[31,57],"3V3",[37,59],"1",[61,59],"R1",[63,59],"R2",[44,65],"VDD",[44,67],"VLOGIC",{"name":69,"pins":70},"V5",[71,73],[31,72],"5V",[52,74],"+5V",{"name":76,"pins":77},"VBAT",[78,80],[34,79],"+",[52,81],"+12V",{"name":83,"pins":84},"CPOUT",[85,86],[42,59],[44,83],{"name":88,"pins":89},"ENA",[90,92],[31,91],"GPIO25",[52,88],{"name":94,"pins":95},"ENB",[96,98],[31,97],"GPIO14",[52,94],{"name":100,"pins":101},"IN1",[102,104],[31,103],"GPIO26",[52,100],{"name":106,"pins":107},"IN2",[108,110],[31,109],"GPIO27",[52,106],{"name":112,"pins":113},"IN3",[114,116],[31,115],"GPIO32",[52,112],{"name":118,"pins":119},"IN4",[120,122],[31,121],"GPIO33",[52,118],{"name":124,"pins":125},"ML_A",[126,128],[127,79],"ML",[52,129],"OUT1",{"name":131,"pins":132},"ML_B",[133,134],[127,35],[52,135],"OUT2",{"name":137,"pins":138},"MR_A",[139,141],[140,35],"MR",[52,142],"OUT3",{"name":144,"pins":145},"MR_B",[146,147],[140,79],[52,148],"OUT4",{"name":150,"pins":151},"REGOUT",[152,153],[40,59],[44,150],{"name":155,"pins":156},"SCL",[157,159,160],[31,158],"GPIO22",[61,38],[44,155],{"name":162,"pins":163},"SDA",[164,166,167],[31,165],"GPIO21",[63,38],[44,162],0,{"file":170,"lang":171,"text":172,"lines":173,"more":24},"sketch.ino","cpp","\u002F\u002F Self-balancing robot: an ESP32 keeps an inverted pendulum on two wheels upright.\n\u002F\u002F The MPU-6050 measures the tilt (pitch): the gyroscope integrated for the fast part, the\n\u002F\u002F accelerometer for the slow part (complementary filter). A PD controller turns the tilt into a\n\u002F\u002F motor command; the L298N drives both gearmotors with PWM on ENA \u002F ENB. Without wheel encoders\n\u002F\u002F the speed is estimated from the command (the motors follow their voltage): the robot leans\n\u002F\u002F back against it, so it does not run away. Tilted beyond 45° it has fallen: the motors stop\n\u002F\u002F until it stands upright again.\n\u002F\u002F\n\u002F\u002F Tuning: KP pushes back against the tilt, KD damps the swing (gyro rate), KV keeps it in place.\n\u002F\u002F Raise KP until it oscillates, then KD; a heavier or taller robot needs more of both.\n#include \u003CWire.h>\n\n\u002F\u002F pins\nconstexpr int I2C_SDA = 21, I2C_SCL = 22;\nconstexpr int ENA = 25, IN1 = 26, IN2 = 27;     \u002F\u002F left motor\nconstexpr int ENB = 14, IN3 = 32, IN4 = 33;     \u002F\u002F right motor\nconstexpr uint8_t MPU = 0x68;\n\n\u002F\u002F controller\nfloat KP = 110, KD = 2;                         \u002F\u002F command per degree, per degree\u002Fs\nfloat KV = 8;                                   \u002F\u002F lean against the speed: degrees per unit of the estimated speed\nconstexpr float SPEED_TAU = 0.5;                \u002F\u002F the command, low-pass filtered (s): the speed estimate\nconstexpr float FALLEN = 45;                    \u002F\u002F beyond this: fallen, motors off\nconstexpr float ALPHA = 0.995;                  \u002F\u002F complementary filter: share of the gyroscope (driving accelerates the sensor too)\nconstexpr int PERIOD_MS = 5;                    \u002F\u002F 200 Hz\nconstexpr int PWM_BITS = 10, PWM_MAX = (1 \u003C\u003C PWM_BITS) - 1;\nconstexpr int DIR = 1;                          \u002F\u002F +1 \u002F −1: which way \"forward\" turns the wheels\nconstexpr float MIN_DUTY = 0.12;                \u002F\u002F below this duty the gearbox does not turn: start from here\n\nfloat angle = 0, speed = 0, lastU = 0;\nuint32_t last = 0;\n\nint16_t read16(uint8_t reg) {\n  Wire.beginTransmission(MPU); Wire.write(reg); Wire.endTransmission(false);\n  Wire.requestFrom(MPU, (uint8_t)2);\n  return (int16_t)((Wire.read() \u003C\u003C 8) | Wire.read());\n}\n\n\u002F\u002F a signed command -1..1 for one motor: direction pins and PWM duty\nvoid motor(int en, int a, int b, float u) {\n  u = constrain(u * DIR, -1.0f, 1.0f);\n  digitalWrite(a, u > 0 ? HIGH : LOW);\n  digitalWrite(b, u > 0 ? LOW : HIGH);\n  float duty = fabsf(u) \u003C 0.005f ? 0 : MIN_DUTY + (1 - MIN_DUTY) * fabsf(u);\n  ledcWrite(en, (int)(duty * PWM_MAX));\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);\n  ledcAttach(ENA, 1000, PWM_BITS);\n  ledcAttach(ENB, 1000, PWM_BITS);\n  motor(ENA, IN1, IN2, 0); motor(ENB, IN3, IN4, 0);\n  Wire.begin(I2C_SDA, I2C_SCL, 400000);\n  Wire.beginTransmission(MPU); Wire.write(0x6B); Wire.write(0); Wire.endTransmission();   \u002F\u002F wake up\n  \u002F\u002F start from the accelerometer's tilt\n  angle = atan2f(-read16(0x3B), read16(0x3F)) * 57.2958f;\n  last = micros();\n  Serial.println(\"balancer ready\");\n}\n\nvoid loop() {\n  \u002F\u002F every 5 ms: the task sleeps in between (FreeRTOS)\n  static TickType_t wake = xTaskGetTickCount();\n  vTaskDelayUntil(&wake, pdMS_TO_TICKS(PERIOD_MS));\n  uint32_t now = micros();\n  float dt = (now - last) * 1e-6f;\n  last = now;\n  float ax = read16(0x3B), az = read16(0x3F), gy = read16(0x45) \u002F 131.0f;   \u002F\u002F raw, raw, deg\u002Fs\n  angle = ALPHA * (angle + gy * dt) + (1 - ALPHA) * atan2f(-ax, az) * 57.2958f;\n  speed += (lastU - speed) * dt \u002F SPEED_TAU;\n  float err = angle + KV * speed;\n  if (fabsf(angle) > FALLEN) {\n    speed = lastU = 0;\n    motor(ENA, IN1, IN2, 0); motor(ENB, IN3, IN4, 0);\n    return;\n  }\n  float u = (KP * err + KD * gy) \u002F PWM_MAX;\n  lastU = constrain(u, -1.0f, 1.0f);\n  motor(ENA, IN1, IN2, u);\n  motor(ENB, IN3, IN4, u);\n  static uint32_t lastPrint = 0;\n  if (now - lastPrint > 500000) { lastPrint = now; Serial.printf(\"tilt %.1f°  motors %+.0f%%\\n\", angle, lastU * 100); }\n}",84,1796304409674]