The Self-Balancing $45 Segway-like Scooter

A new Segway costs $6,000. I decided to make one for $45 in parts. Judging from the components I ended up using, one can tell. The motors and tires were salvaged from a broken electric wheelchair, the metal was scrap, the battery surplus from a garage sale, all the circuits custom-made. The system has three primary components: a controller circuit and its sensors (gyroscope and accelerometers), a dual H-bridge motor controller, and the mechanical assembly including the drive motors.

System Controller

The Tipper is controlled by a microcontroller that runs through a loop several hundred times per second reading in sensor data and then commanding two H-Bridge motor controllers. The objective is to always keep the handlebars close to upright. When this invariant is held true, if one leans forward the Tipper will accelerate forward to compensate and keep the handlebars upright. If one leans backward, the Tipper will turn the motors on in reverse to compensate in the opposite direction. This is precisely the desired operation, and it is how the Tipper is controlled. If the rider wants to go forward, just lean forward.

In order to do this, the controller needs to know the current tilt of the device. It uses accelerometer and gyroscope data to estimate the physical state of the system. Unfortunately, there is not a reliable tilt sensor that is resistant to offset from acceleration of the entire device, so the controller needs to estimate the tilt with the two sensors it has. The (simplified) intuition behind how this works is that the accelerometer gives a very good long-term estimate on tilt, but is rather poor for any single measurement, whereas the gyroscope is very effective for short-term measurements, but is inaccurate for the long-term.

The algorithm it uses to estimate tilt is essentially a combination of an accelerometer with a software "low pass filter" and a gyroscope with a software "high-pass filter." The theory is as follows: in the long run an accelerometer can read the component of gravity acting on it. When the device is completely horizontal, there is no gravity acting on the axis of measurement because gravity is therefore orthogonal to the Tipper. When straight up, the component of gravity is a full 9.8 m/s^2. A software low pass filter is used to partially filter out the "noise" caused by the sensor reading the forward and backward acceleration of the Tipper and misinterpreting it as gravity.

There is a high-pass filter on the gyroscope because it is prone to integration drift. The gyroscope is not a tilt sensor, it is a rate of tilt sensor. To get changes in tilt, the controller integrates the data coming from the gyroscope. Over time, small errors accumulate making the estimated position increasingly incorrect. To account for this drift, a software high pass filter biases recent data from the gyroscope. When weighted and then combined, they give a fairly accurate estimate on the Tipper's tilt.

With the current system state (the tilt) now known, and the desired state a constant (upright the Tipper at all times), the controller can now command the motor speed controllers. The speed controllers are variable, which means the controller must choose a desired velocity for the motors and send that request to the speed controllers. A PID (proportional, integral, derivative) control loop is used to pick the correct motor velocity in order to close the difference between the desired and current system state. The scale factors used in this control loop are very sensitive to changes. It took a great deal of time to fine tune the control loop to effectively stabilize the physical system.

Motor Controller

The Tipper uses a custom dual H-bridge motor controller to regulate voltage to the two drive motors. Since voltage sets speed on a DC motor, these are effectively speed controllers. The H-bridge uses mosfets to switch current on and off the motors at a very fast rate. By controlling the time (called the duty cycle) that the motors receive power in each switching cycle, the average voltage, and therefore speed, can be set. The following schematics show the integrated H-bridge and system controller circuit.

Click on schematic to see higher resolution version.

Board layout for the integrated system controller and H-bridge board.

Mechanical Assembly

The Tipper is built on an aluminum base. Bolted to this base are the two gearhead motors salvaged from an old electric wheelchair. To reinforce the base and motor structure, a thinner aluminum top plate is mounted above the gearheads. On top of the top plate sits a rechargeable 12v 17Ah lead-acid battery that powers the entire system. Strapped onto the battery is the control circuitry. The handlebars are made of standard steel piping. At the top a T-elbow is screwed on. A slit in the top plate gives the handlebars stability and the ability to pivot left and right. A bolt coupled to a potentiometer at the very bottom secures the handlebars to the Tipper.

The Tipper's bottom platform is made up of an aluminum base with gearhead wheelchair motors bolted on each side.

Steel pipe handlebars rise above the waterjet-cut top plate. A slit in the top plate allows the operator to move the handlebars left and right. A potentiometer reads this pivot and differentially powers the motors so that the Tipper steers in the desired direction. This picture was taken before any of the control and power electronics were installed.