# MV-7004: Inverted Pendulum Control Using MotionSolve and MATLAB

In this tutorial, you will learn how to use MotionView and MotionSolve to design a control system that stabilizes an inverted pendulum.

The goal of this tutorial is to design a regulator using the pole placement method. The inverted pendulum MDL model file is supplied.

- Check the stability of the open loop system.
- Export linearized system matrices A,B,C, and D using MotionSolve linear analysis.
- Design a controller using MATLAB.
- Implement a controller in MotionView.
- Check the stability of a closed loop system using MotionSolve linear analysis.
- Add disturbance forces to the model and run simulation using MotionSolve.

You want to find a full-state feedback control law to achieve the goal. The control input is a force applied to the slider along the global X-axis. Plant output is the pendulum angle of rotation about the global Y-axis.

Start by loading the file inv_pendu.mdl, located in the mbd_modeling\motionsolve folder, into MotionView. Upon examination of the model topology, you will notice that everything needed for this exercise is already included in the model. However, depending on which task you are performing, you will need to activate or deactivate certain entities.

## Determine the Stability of the Open Loop Model

Compute the eigenvalues to determine the stability of the Inverted pendulum.

## Obtain a Linearized Model

where $$A$$, $$B$$, $$C$$, and $$D$$ are the state matrices, $$x$$ is the state vector, $$u$$ is the input vector, and $$y$$ is the output vector. The A,B,C,and D matrices depend on the choice of states, inputs, and outputs. The states are chosen automatically by MotionSolve and the chosen states are reported in one of the output files. You only need to define the inputs and outputs.

## Design a Control System in MATLAB

A detailed discussion of control system design is beyond the scope of this document. However, the steps to design a regulator using pole placement [1] to stabilize the inverted pendulum are described briefly. For details, refer to the standard controls text and the MATLAB documentation.

It can be easily verified using MATLAB that the system is completely state controllable [1, 2].
We employ a full-state feedback control law $$u=-k\ast x$$, where $$u$$ is the control input, $$k$$ is the gain vector, and $$x$$ is the state vector. Then, assuming the desired pole
locations are stored in vector $$P$$, you may use the pole placement method to compute $$k$$. For desired poles at [-20 -20 -20 -20] (rad/s), the
acker function in MATLAB yields ```
k=1e3[-2.4186 -0.0163 -0.070
-0.0033]
```

.

## Implement the Control Force in

``-1e3*(-2.4186*AY({b_pendu.cm.idstring})-0.0163*DX({b_pendu.cm.idstring}),-0.070*WY({b_pendu.cm.idstring})-0.0033*VX({b_pendu.cm.idstring}))``

Notice that it is simply the dot product between the gain vector (k) and the state vector (x) elements. This solver variable is used to define a force named Control Force - CL.

Activate the force Control Force - CL if it is deactivated.

## Check the Stability of a Closed Loop System

## Add Disturbance Force and Running the Simulation

References

*Feedback Control of Dynamic Systems*, G. G. Franklin, J. D. Powell, and A. Emami-Naeini, Third Edition, Addison Wesley.