སྒྱུ་རྩལ
མཛེས་སྡུག་དང་བདེ་ཐང
བཟོ་རིག
རིག་གནས་དང་ལོ་རྒྱུས
དགའ་སྟོན
ཁོར་ཡུག
ཟས་དང་བཏུང་རྫས
ཕྱིར་འཕྲུལ་རིག
ཚན་རིག
རྩེད་འགྲན
རིག་རྩལ
གྱོན་རུང
Self-Balancing Robot on Two Stepper Wheels (after Kamen, 1997)
Emma

བཟོས་མཁན

Emma

4. སྤྱི་ཟླ་བཅུ་གཉིས་པ 2026SE
༠
༠
༠
༠
༠

Self-Balancing Robot on Two Stepper Wheels (after Kamen, 1997)

A robot that stands on two wheels and keeps itself upright, the way Dean Kamen's 1997 patent balances a person: a tilt sensor measures how far it leans and the wheels drive under the fall. Printed frame and wheels, two NEMA 17 steppers, an ESP32 and an MPU-6050. A notebook works out the four control gains from your own measurements. The firmware compiles; we have not built this robot yet, so treat the gains as a starting point and tune.
བར་མ
About 12 hours of printing; one day to build and tune

ལམ་སྟོན

1

What a balancing robot does

A two-wheel robot balancing with the same MPU-6050 tilt sensor (Tinker Twins, Arduino and DC motors). Ours drives steppers from an ESP32 with Kamen's four-term law.
2

Kamen's balance law, 1997

US 5,701,965 (FIG. 6): the wheel torque is set to T = K1·θ + K2·θ' + K3·X + K4·X', where θ is the lean, X the distance from a reference point on the floor and the dashes mean rate of change. The gains "are dependent upon the physical parameters of the system". With two wheels each motor is worked out separately, and tracking both wheels stops the machine turning by itself. Leaning forward makes it drive forward. Steppers take a speed, not a torque, so our firmware uses the same four terms to set the wheel acceleration.
གོམ་པ་ 2 - Image 1
3

Work out the gains

Jupyter ཚང་དེབ་མངོན་གསལ་འབད་དོ་…

ལག་ཆས་དགོས་མཁོ:

འཇལ་ཐགའཇལ་ཐག
ཨང་རྟགས་ཚད་འཇལ་ཆས་ ཨིནཅི 6ཨང་རྟགས་ཚད་འཇལ་ཆས་ ཨིནཅི 6
4

Print the motor frame

Plate on the bed, walls up, no supports (the motor holes are teardrops). 0.2 mm layers, 4 walls, 30 % infill.
བཟོ་བཀོད་ཀྱི་ཡིག་སྣོདSTLCC0 - Free

གོམ་པ་འདིའི་རྫས་རིགས:

PETG སྐུད་པ་PETG སྐུད་པ་82 g

ལག་ཆས་དགོས་མཁོ:

FDM 3D པར་སྐྲུན་འཕྲུལ་ཆས།FDM 3D པར་སྐྲུན་འཕྲུལ་ཆས།
5

Print the decks

Print two: one for the electronics, one for the battery. Flat, 0.2 mm layers, 3 walls, 20 % infill.
བཟོ་བཀོད་ཀྱི་ཡིག་སྣོདSTLCC0 - Free

གོམ་པ་འདིའི་རྫས་རིགས:

PETG སྐུད་པ་PETG སྐུད་པ་106 g

ལག་ཆས་དགོས་མཁོ:

FDM 3D པར་སྐྲུན་འཕྲུལ་ཆས།FDM 3D པར་སྐྲུན་འཕྲུལ་ཆས།
6

Print the wheels

Print two, hub up (the flat face on the bed). 0.2 mm layers, 4 walls, 40 % infill so the hub holds the set screw.
བཟོ་བཀོད་ཀྱི་ཡིག་སྣོདSTLCC0 - Free

གོམ་པ་འདིའི་རྫས་རིགས:

PETG སྐུད་པ་PETG སྐུད་པ་62 g

ལག་ཆས་དགོས་མཁོ:

FDM 3D པར་སྐྲུན་འཕྲུལ་ཆས།FDM 3D པར་སྐྲུན་འཕྲུལ་ཆས།
7

Mount the motors

Pass each motor's cable up through the slot in the plate. Push the motor's round boss into a wall from the inside, shaft out, and fix it with four M3 x 6 screws from the outside (counterbored). Turn both motors so the flat on each shaft points up.

གོམ་པ་འདིའི་རྫས་རིགས:

གོམ་པ་འཕྲུལ་འཁོར།གོམ་པ་འཕྲུལ་འཁོར།2 དུམ་བུ།
སོ་ཀེཊ་མགོ་ཡོད་པའི་སྒྲོག་གཟེར།སོ་ཀེཊ་མགོ་ཡོད་པའི་སྒྲོག་གཟེར།8 piece (M3 x 6)

ལག་ཆས་དགོས་མཁོ:

ཟུར་དྲུག་ལྡེ་མིག་ཆ་ཚན།ཟུར་དྲུག་ལྡེ་མིག་ཆ་ཚན།
8

Fit the wheels

Stretch a 75 x 3.5 mm O-ring into each wheel's groove as the tyre. Push an M3 nut into the slot in the hub, slide the wheel onto the shaft with 3 mm to spare from the wall, and tighten an M3 set screw onto the flat.

གོམ་པ་འདིའི་རྫས་རིགས:

ཨོ་སོར་གདུབ།ཨོ་སོར་གདུབ།2 piece (75 x 3.5 mm)
ཟུར་དྲུག་གི་ནཊ།ཟུར་དྲུག་གི་ནཊ།2 piece (M3)
ཁ་གཞོང་གི་འཛིན་གཟེར།ཁ་གཞོང་གི་འཛིན་གཟེར།2 piece (M3 x 6)

ལག་ཆས་དགོས་མཁོ:

ཟུར་དྲུག་ལྡེ་མིག་ཆ་ཚན།ཟུར་དྲུག་ལྡེ་མིག་ཆ་ཚན།
ཨང་རྟགས་ཚད་འཇལ་ཆས་ ཨིནཅི 6ཨང་རྟགས་ཚད་འཇལ་ཆས་ ཨིནཅི 6
9

Build the stack

Cut four 200 mm lengths of M4 threaded rod. Fix each in a corner hole of the frame plate with a nut and washer on both sides. Run nuts down to 74 mm above the plate, drop on the electronics deck, then add washers and nuts on top. Do the same for the battery deck at 149 mm. Raising the battery deck slows the fall and makes balancing easier.

གོམ་པ་འདིའི་རྫས་རིགས:

ཊི་རུའི་དབྱུག་ཏོ།ཊི་རུའི་དབྱུག་ཏོ།4 piece (M4 x 200 mm)
ཟུར་དྲུག་གི་ནཊ།ཟུར་དྲུག་གི་ནཊ།24 piece (M4)
ལེབ་ཏོག་ཏོའི་ཝཱ་ཤར།ལེབ་ཏོག་ཏོའི་ཝཱ་ཤར།24 piece (M4)

ལག་ཆས་དགོས་མཁོ:

འཇལ་ཐགའཇལ་ཐག
10

Wire the controller board

On a 70 x 90 mm perfboard, solder headers for the ESP32, the MPU-6050 and two A4988 drivers. ESP32 GPIO 25/26 to the right driver's STEP/DIR, GPIO 27/14 to the left's, GPIO 13 to both EN. MPU-6050 SDA to 21, SCL to 22, VCC to 3V3. Each driver: VDD to 3V3, MS1 and MS2 to 3V3, MS3 to GND (1/8 step), RESET to SLEEP, motor coils to 1A/1B and 2A/2B. Battery + through the 3 A fuse and switch to both VMOT, with a 100 µF 35 V capacitor across each VMOT and GND; the buck converter set to 5.0 V feeds the ESP32's 5V pin. All grounds together. Mount the MPU-6050 flat with its X axis pointing forward. Tie the board to the deck.

གོམ་པ་འདིའི་རྫས་རིགས:

བུ་ག་ཡོད་པའི་པང་ལེབ / དཔེ་གཟུགས་པང་ལེབ།བུ་ག་ཡོད་པའི་པང་ལེབ / དཔེ་གཟུགས་པང་ལེབ།1 དུམ་བུ།
ཟུར་ཐིག་མཐུད་ཁྲམ།ཟུར་ཐིག་མཐུད་ཁྲམ།1 strip
ESP32 གོང་འཕེལ་པང་ལེབ།ESP32 གོང་འཕེལ་པང་ལེབ།1 དུམ་བུ།
འགུལ་བསྐྱོད་འཇལ་ནིའི་སྡེ་ཚན་ (མཐའ་ཟུར་ ༦)འགུལ་བསྐྱོད་འཇལ་ནིའི་སྡེ་ཚན་ (མཐའ་ཟུར་ ༦)1 དུམ་བུ།
གོམ་པ་འཕྲུལ་འཁོར་བསྐུལ་བྱེད།གོམ་པ་འཕྲུལ་འཁོར་བསྐུལ་བྱེད།2 དུམ་བུ།
གློག་ཞུན་ ཀེ་པེ་སི་ཊར།གློག་ཞུན་ ཀེ་པེ་སི་ཊར།2 piece (100 µF 35 V)
DC-DC གློག་ཤུགས་ཕབ་ཆསDC-DC གློག་ཤུགས་ཕབ་ཆས1 དུམ་བུ།
DC ཕིའུསི་འཛིན་ཆསDC ཕིའུསི་འཛིན་ཆས1 དུམ་བུ།
ཞུ་ཐིག།ཞུ་ཐིག།1 piece (3 A)
འཕར་སྒུལ་སྒོ་ལྕགས།འཕར་སྒུལ་སྒོ་ལྕགས།1 དུམ་བུ།
མཐུད་སྦྱོར་གློག་ཐགམཐུད་སྦྱོར་གློག་ཐག2 m
ཚ་བས་འཁུམ་པའི་སྦུ་གུ།ཚ་བས་འཁུམ་པའི་སྦུ་གུ།10 cm
ཞ་སྦྱར་རྫས་ཀྱི་སྐུད་པཞ་སྦྱར་རྫས་ཀྱི་སྐུད་པ2 m
གློག་ཐག་གི་སྡོམ་ཐག་གློག་ཐག་གི་སྡོམ་ཐག་8 དུམ་བུ།

ལག་ཆས་དགོས་མཁོ:

ཞ་བཏབ་ནིའི་ལྕགས།ཞ་བཏབ་ནིའི་ལྕགས།
ལྕགས་སྐུད་ཀྱི་ཤུབས་བཤུས་ཆས།ལྕགས་སྐུད་ཀྱི་ཤུབས་བཤུས་ཆས།
མཱལ་ཊི་མི་ཊར།མཱལ་ཊི་མི་ཊར།
11

Fit the battery

Use four protected 18650 cells (built-in protection circuit). Tie the 4-cell holder, wired in series (14.8 V), to the top deck. Charge the cells one at a time out of the holder, and never leave them charging unattended.

གོམ་པ་འདིའི་རྫས་རིགས:

18650 ལི་ཐི་ཡམ་ནང་ཆས18650 ལི་ཐི་ཡམ་ནང་ཆས4 དུམ་བུ།
18650 གློག་སྟེགས་འཛིན་ཆས (ནང་གསེས་ ༤)18650 གློག་སྟེགས་འཛིན་ཆས (ནང་གསེས་ ༤)1 དུམ་བུ།
གློག་ཐག་གི་སྡོམ་ཐག་གློག་ཐག་གི་སྡོམ་ཐག་4 དུམ་བུ།

ལག་ཆས་དགོས་མཁོ:

LiPo གསོག་འཇུག་པང་ལེབ།LiPo གསོག་འཇུག་པང་ལེབ།
12

Set the driver current

Motors unplugged, power on. Read the sense resistors on each A4988 (R100 = 0.1 Ω). The datasheet gives I = Vref / (8 x Rs), so Vref = I x 8 x Rs: for 1.0 A with 0.1 Ω set 0.80 V between the trimmer and GND. Stay below your motor's rated current. Set the buck converter to 5.0 V before connecting the ESP32.

ལག་ཆས་དགོས་མཁོ:

མཱལ་ཊི་མི་ཊར།མཱལ་ཊི་མི་ཊར།
དག་ཞིབ་ཀྱི་སི་ཀུརུ་ཌརཱའི་བར་ཆ་ཚང་དག་ཞིབ་ཀྱི་སི་ཀུརུ་ཌརཱའི་བར་ཆ་ཚང་
13

Upload the firmware

ESP32 board package 3.x, board "ESP32 Dev Module". Paste your K1-K4 from the notebook at the top.
balancing_robot.inocpp
// Two-wheel balancing robot: ESP32 + MPU-6050 + two A4988 stepper drivers.
// Control law after Kamen et al., US 5,701,965 (1997): T = K1*theta + K2*theta' + K3*X + K4*X'.
// Ours: the steppers take a SPEED, so the law sets the wheel ACCELERATION instead of a torque.
// Gains: from the notebook step, for YOUR measured robot. Start with these, then tune.
#include <Wire.h>
#include "soc/gpio_reg.h"   // direct register writes: safe and fast inside the interrupt

const float K1 = -3.966, K2 = -7.516, K3 = -60.234, K4 = -6.496;  // x, v, theta, omega (notebook)
const float WHEEL_R = 0.043;        // m
const float STEPS_PER_REV = 1600;   // 200-step motor at 1/8 microstep
const float DT = 0.005;             // s, 200 Hz loop
const float A_MAX = 15.0, V_MAX = 1.5;
const float START_DEG = 5.0, STOP_DEG = 35.0;
const int   PITCH_SIGN = 1;         // flip to -1 if the angle reads negative when the robot leans FORWARD
const int   LEFT_SIGN = -1, RIGHT_SIGN = 1;   // the motors face opposite ways

const int STEP_R = 25, DIR_R = 26, STEP_L = 27, DIR_L = 14, EN_PIN = 13;   // EN low = motors on
const int MPU = 0x68;

hw_timer_t *timer = nullptr;
volatile uint32_t incR = 0, incL = 0, accR = 0, accL = 0;
volatile bool highR = false, highL = false;

void ARDUINO_ISR_ATTR onTimer() {     // 40 kHz: one tick high, at least one tick low -> max 20 000 steps/s
  if (highR) { REG_WRITE(GPIO_OUT_W1TC_REG, BIT(STEP_R)); highR = false; }
  else { uint32_t o = accR; accR += incR; if (accR < o) { REG_WRITE(GPIO_OUT_W1TS_REG, BIT(STEP_R)); highR = true; } }
  if (highL) { REG_WRITE(GPIO_OUT_W1TC_REG, BIT(STEP_L)); highL = false; }
  else { uint32_t o = accL; accL += incL; if (accL < o) { REG_WRITE(GPIO_OUT_W1TS_REG, BIT(STEP_L)); highL = true; } }
}

void setWheel(int dirPin, volatile uint32_t &inc, float stepsPerSec) {
  digitalWrite(dirPin, stepsPerSec >= 0 ? HIGH : LOW);
  float s = fabsf(stepsPerSec); if (s > 19000) s = 19000;
  inc = (uint32_t)(s * (4294967296.0 / 40000.0));
}

void mpuWrite(uint8_t reg, uint8_t val) { Wire.beginTransmission(MPU); Wire.write(reg); Wire.write(val); Wire.endTransmission(); }
bool mpuRead(int16_t &ax, int16_t &az, int16_t &gy) {
  Wire.beginTransmission(MPU); Wire.write(0x3B); if (Wire.endTransmission(false) != 0) return false;
  if (Wire.requestFrom(MPU, 14) != 14) return false;
  uint8_t b[14]; for (int i = 0; i < 14; i++) b[i] = Wire.read();
  ax = (b[0] << 8) | b[1]; az = (b[4] << 8) | b[5]; gy = (b[10] << 8) | b[11];
  return true;
}

float theta = 0, gyBias = 0, x = 0, v = 0;
bool running = false;
uint32_t next = 0;

void setup() {
  Serial.begin(115200);
  pinMode(STEP_R, OUTPUT); pinMode(DIR_R, OUTPUT); pinMode(STEP_L, OUTPUT); pinMode(DIR_L, OUTPUT);
  pinMode(EN_PIN, OUTPUT); digitalWrite(EN_PIN, HIGH);          // motors off until upright
  Wire.begin(21, 22); Wire.setClock(400000);
  mpuWrite(0x6B, 0x00);   // wake
  mpuWrite(0x1A, 0x03);   // digital low-pass ~44 Hz
  mpuWrite(0x1B, 0x08);   // gyro +/-500 deg/s = 65.5 LSB per deg/s
  mpuWrite(0x1C, 0x00);   // accel +/-2 g = 16384 LSB per g
  delay(100);
  long sum = 0; int n = 0; int16_t ax, az, gy;                  // keep the robot STILL for 1 s: gyro zero
  for (int i = 0; i < 200; i++) { if (mpuRead(ax, az, gy)) { sum += gy; n++; } delay(5); }
  gyBias = n ? (float)sum / n : 0;
  if (mpuRead(ax, az, gy)) theta = PITCH_SIGN * atan2f(-(float)ax, (float)az);
  timer = timerBegin(1000000);
  timerAttachInterrupt(timer, &onTimer);
  timerAlarm(timer, 25, true, 0);
  next = micros();
}

void loop() {
  if ((int32_t)(micros() - next) < 0) return;
  next += (uint32_t)(DT * 1e6);
  int16_t ax, az, gy;
  if (!mpuRead(ax, az, gy)) return;
  float omega = PITCH_SIGN * ((gy - gyBias) / 65.5f) * (PI / 180.0f);          // rad/s
  float thAcc = PITCH_SIGN * atan2f(-(float)ax, (float)az);                    // rad
  theta = 0.98f * (theta + omega * DT) + 0.02f * thAcc;                        // complementary filter
  float deg = theta * 180.0f / PI;

  if (!running && fabsf(deg) < START_DEG) { running = true; x = 0; v = 0; digitalWrite(EN_PIN, LOW); }
  if (running && fabsf(deg) > STOP_DEG) { running = false; v = 0; incR = incL = 0; digitalWrite(EN_PIN, HIGH); }

  if (running) {
    float u = -(K1 * x + K2 * v + K3 * theta + K4 * omega);
    u = constrain(u, -A_MAX, A_MAX);
    v = constrain(v + u * DT, -V_MAX, V_MAX);
    x += v * DT;
    // the motor turns the wheel RELATIVE TO THE BODY: wheel rate = v / r - body pitch rate
    float sps = (v / WHEEL_R - omega) * STEPS_PER_REV / (2 * PI);
    setWheel(DIR_R, incR, RIGHT_SIGN * sps);
    setWheel(DIR_L, incL, LEFT_SIGN * sps);
  }
  static int k = 0;
  if (++k >= 20) { k = 0; Serial.printf("lean %.2f deg  x %.3f m  v %.3f m/s  %s\n", deg, x, v, running ? "ON" : "off"); }
}

ལག་ཆས་དགོས་མཁོ:

Arduino IDE ཡོད་པའི་གློག་རིགArduino IDE ཡོད་པའི་གློག་རིག
USB Data CableUSB Data Cable
14

Check, stand it up, tune

Lay the robot still while it starts (it zeroes the gyro). Open the serial monitor at 115200: lean it forward and the angle must go positive, or set PITCH_SIGN = -1. Hold it upright: below 5° the motors switch on and the wheels must drive under the lean; if they drive away from it, swap one motor's coil pair. Over 35° it switches off. If it shakes, lower K3 and K4 by 20 %; if it drifts across the floor, raise K1 and K2. Raise the top deck if it is too quick.

ལག་ཆས་དགོས་མཁོ:

Arduino IDE ཡོད་པའི་གློག་རིགArduino IDE ཡོད་པའི་གློག་རིག
USB Data CableUSB Data Cable

རྫས་རིགས

24

ལག་ཆས་དགོས་མཁོ

11

བཟོ་བཀོད་ཡིག་སྣོད།

3

དཔེ་རིས་འདི་གིས་བཟོ་མི

CC0 སྤྱི་དབང

དཔེ་རིས་འདི་CC0 འོག་བཀྲམས་ཡོད། ཁྱེད་རང་གིས་ཆོག་མཆན་མ་བཞེས་པར་ཕབ་ལེན་དང་བཟོ་བཅོས། བགོ་བཤའ། དགོས་མཁོ་གང་ལའང་བཀོལ་སྤྱོད་བྱས་ཆོག

བཟོ་མཁན་ལ་རྒྱབ་སྐྱོར་བྱེད་པའི་ཆེད་ཁོང་ཚོའི་དཔེ་རིས་བརྒྱུད་ཐོན་སྐྱེད་ཉོ། བཟོ་མཁན་གྱིས བཟོ་མཁན་གྱི་ཁེ་ཕོགས ཚོང་པས་གཏན་འཁེལ་བྱས་པ། ཡང་ན་དཔེ་རིས་འདིའི་པར་གསར་བཟོས་ཏེ་ཁྱེད་རང་གི་དཔེ་རིས་ནང་མཐུད་སྦྲེལ་བྱས་ཏེ་ཡོང་སྒོ་བགོ་བཤའ་བྱེད།

གྲོས་བསྡུར

(0)

ནང་འཛུལ གྲོས་བསྡུར་ནང་མཉམ་ཞུགས་ཆེད

བསམ་ཚུལ་ཚུ་ཐོབ་བཞིན...