Charles Masenas
April 14, 2013

Lego NXT Segway Design

Click here to edit subtitle

 Segway Design Using a Gyro Tilt Sensor

Two wheeled vehicles, such as the Segway, belong to an inherently unstable class of systems known as the inverted pendulum.  Using a gyro sensor to sense the rate of change of tilt with time, you might try solving this problem using the classical method of placing the roots of the closed loop transfer function using a PID (proportional, integral, differential) controller.  The problem with this approach is it works only for very restrictive conditions.  If the initial conditions of the system are not ideal or if the sensor has an offset, then this solution has real problems.  That is, the vehicle will remain upright but the velocity will be uncontrolled (until the vehicle crashes).  So, the classical method stabilizes the output (tilt angle) but it may allow internal states of the system to be unbounded rendering the solution impractical.


Using state-space analysis the problem of unbounded internal states can be avoided.  There is also a third method of analysis (my preferred) that can stabilize all internal states of the system, namely using software to solve the system of differential equations.  In my opinion, Mathematica is preferable to Matlab and MathCad for this.  Mathematica does not require the system of differential equations to be entered as first order equations.  Also, Mathematica allows easy tuning of the graphically displayed system outputs using slider widgets to control the model parameters.


Practical solutions for the inverted pendulum problem all involve a controller with positive feedback.  Some solutions introduce positive feedback through an accelerometer.  Some solutions (the Segway) introduce positive feedback through wheel encoders.  There are write-ups that explain the positive feedback loop in terms of complementary filtering of the gyro output to improve the signal quality.  The complementary filtering theory falls apart when one realizes that even with a perfect sensor output, PID control alone is insufficient to stabilize all states of the system.


What I have found is that an unstable controller (positive feedback within the controller itself) with no additional gyro signal filtering is a great solution.  This solution is the simplest in terms of hardware (no need for an accelerometer or wheel encoders).  For all its simplicity,  the unstable controller solution is not widely used.



Downloads:


Slideshow

Mathematica notebook for the slideshow (requires Mathematica)

Ryo Watanabe's solution

NXC code for the NXTWay (same controller as the Segway)

NXC code for the AnotherWay (using my controller)

Kent Lundberg's rationale for the need for positive feedback in the controller


The NXC code can be compiled using the free Brixcc programming environment for windows.  The following option must be set:  Edit > Preferences > Compiler > NBC/NXC > Optimization Level > 1.


When either the NXTWay or AnotherWay program is started on the NXT, the NXT must be placed motionless on a surface so that the gyro semsor may be calibrated.  This takes a few seconds after which the NXT will beep.  The NXT must then be held upright and the center button pushed again after which it will begin balancing in 2 seconds.