LEGO Segway: Controlling an Inverted Pendulum

Introduction

Controlling inverted pendulums in different forms is common in engineering. They can be found in everything from rockets during liftoff, to skyscrapers, to Segways. Build a control system for a Segway constructed from LEGO Mindstorms NXT components.

Explore the available scenarios by evaluating each cell in order. Evaluation is done by selecting a cell and pressing Shift+Enter.

In order to get the full experience of this example, you need the following:

Wolfram SystemModeler; a free trial download is available at www.wolfram.com/system-modeler

The model that came in the same bundle as this notebook

For more information about Wolfram SystemModeler, visit www.wolfram.com/system-modeler.

Initialize

Define the model names:

mmodel="EducationExamples.ElectricalEngineering.LEGOSegway.Components.LEGOSegwayBase";
mmodelWithControl="EducationExamples.ElectricalEngineering.LEGOSegway.LQControl";
mmodelWithNoControl="EducationExamples.ElectricalEngineering.LEGOSegway.NoControl";

Control Design

Design a controller to keep the LEGO Segway upright, based on a linearized representation of the model. Save the resulting control parameters to the model to simulate and verify the behavior.

Get the linear state space model representation of the Segway. Linearize it around the upright position by using the initial values from the model:

ss=SystemModelLinearize [mmodel,"InitialValues",Method->"NumericDerivative"]

Retrieve a list of all state names:

Part[ss,-1,2,3]

Keep only the state variables you are interested in:

sys=SystemsModelExtract[
ss,
All,
All,
Flatten[
FirstPosition[Part[ss,-1,2,3],#]&/@{
"wheelSet.der_theta1",
"wheelSet.der_theta2",
"revolute1.phi",
"revolute1.w",
"RightMotor.inductor.i",
"LeftMotor.inductor.i"
}]
]

Define weight variables for the control design. Use a large penalty for deviations in the angle of the Segway, revolute1.phi:

q=(({{.1, 0, 0, 0, 0, 0}, {0, .1, 0, 0, 0, 0}, {0, 0, 50000, 0, 0, 0}, {0, 0, 0, 10, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 1}}));
r=(({{10, 0}, {0, 10}}));

Compute the gains for the LQ controller:

L=LQRegulatorGains[sys,{q,r}]

Verify that the eigenvalues of the system are in the left side of the plane (negative real parts):

clsys=SystemsModelStateFeedbackConnect[sys,L];
Eigenvalues[Normal[clsys][[1]]]//ColumnForm

Save the computed values of the LQ controller to the model with a controller:

newControl=SystemModel[mmodelWithControl,<|"ParameterValues"-> {"L"->L}|>]

3D Animation

With the control parameters from the previous section saved in the model, you can get a 3D animation in SystemModeler Simulation Center by following the steps below.

Open the model with the designed controller in the SystemModeler Simulation Center. Click the Simulate button () and then the Animate button () to get a 3D animation of the robot. The green arrows represent forces hitting the top of the Segway to test the robustness of the controller:

SystemModeler[newControl,"Animation"]

Compare with the model without a controller:

SystemModeler[mmodelWithNoControl,"Animation"]

Terms and Conditions of Use

This domain example is an informational resource made freely available by Wolfram Research.

Use of This Example

• You may not use this example for any purpose that is unlawful or dangerous.
• You assume total responsibility and risk for your use of this example.
• You may not present this example with any alteration to its trade dress, trademarks, or design.

License

All content in this bundle is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. By accessing the content or using it in any way, you accept and agree to be bound by the terms of this license. If you do not agree to these Terms of Use, you may not use this content. Wolfram Research reserves the right to change, modify, add to, or remove portions of these Terms of Use at any time without notice. Please refer back to www.wolfram.com for the latest Terms of Use.

A summary of the licensing terms can be found at:
http://creativecommons.org/licenses/by-nc-sa/3.0

The full legal code can be found at: 
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode