# Python Dynamics Simulations : Part 1 — Setting Up Simulations

From some years now, it has become popular to teach university subjects with the help of Matlab and Simulink. It is justified as it is a plug and play environment which allows to launch all kind of simulations with minor efforts and to focus in the actual content under study. Nice. The problem it originates is the dependence of this property software. Many people cannot simulate dynamics (e.g. a simple DC motor) *without using Matlab*.

I worked in a project for which engineers were struggling to embed Simulink in a test-bed system as they were not able to simulate the digital twin otherwise… That does not make sense.

For this reason I have planned to write some stories, a tutorial, to show how I deal with the dynamics simulations using Python, NumPy and SciPy (mainly) which can be executed even with a Raspberry Pi. Here you have the complete index of the tutorial.

In this **Part 1** I will show some easy ways to create system “blocks” and how to interconnect them for the simulation.

Note:This postis the most basic in terms of code. Itis introductory, written for people who is not comfortable dealing with OOP. If it is too easy for you, I encourage you to take a look of the index and to jump to the stuff you want.

# 1. Steps

The following is my workflow recommendation, it is not the only way to do it so feel free to make suggestions.

- Draw a
**block diagram**. As we do not have a visual framework to setup the system blocks, it is easier if you draw them first with reference to all the signals. - Write, at least,
**one function**for**each block**. The code is more readable when there is a function for the DC motor model and another for its controller. This gets more important as systems become more complex. - Write an
**interconnection function**.

- The functions must begin with the extraction of the previous simulation step arguments, i.e. for time interval k, the variable x_{k} will be equal to x_{k+1} from the previous step.

- With the help of the block diagram and (important!) suitable nomenclature, write outputs variables as inputs for the subsequent blocks.

- Return the values which will be the updating arguments for the following interval and also all the data you may want to store or plot later. - Create a
**initial values list**with the same length of the interconnection function return list, one value per variable. - Create
**time vector**and (if necessary)**reference values**for the controller. - Do not forget to
**plot your results**or to store them after simulating.

**2. Non-Linear Motor Control**

To show it in a simple way, we will see the non-linear control (**input-output linearization** method) of a DC motor as an example of those steps and to show what can we achieve simulating dynamics with Python. If you only want to see how to implement your equations, **you can skip the maths** stuff.

## 2.1 The Mathematics

Suppose the motor dynamics following the expression [1]:

Where ** tau** is the torque,

**(empirically determined) weights the inertia due to the current torque,**

*a***(empirically determined too) weights the effort through the current torque and the input to modify the change rate of the torque itself, and**

*k***is the control input to the motor, the voltage. Then, choosing the control law (how**

*u***is calculated) as follows:**

*u*Leads the output torque to:

Thus the term which depends of tau gets compensated by the first control term, linearizing the relation input-output and making the error to exponentially tend to zero.

## 2.2 Code Implementation

- Blocks diagram:

**Note**. The simulation step block only serves for us to clearly see how outputs are propagated in time, but it is not something you have to create.

2. Functions for each module: Here the controller and the motor model.

3. Interconnection function: This function also must have a special arguments in order to work as ODE Integrator expects to, i.e. integrated signals (states), time steps and external arguments like the references for the controller.

4. Initial values, time vector and ODE simulation: In this example, the ODE integration is made for every step, loading the corresponding references.

**Note:** It may be easier to run the simulation once instead of taking every step separately in a for loop, but this way you can add logic and other stuff without breaking the integrator. Feel free to share any considerations here!

# 3. Results

The resulting controller for this simple system makes perfect results, so **I added some modelling error** to the controller embedded model, with respect to the real motor model, in order to evaluate how it works with some **uncertainties**. For values: a = 1, k = 1, a_model_error = 0.1 k_model_error = 0.3, the results are something like this (in red, real output, dashed the reference):

Pretty good even with injected errors in the modelling of 10% and 30% respectively for each model parameter.

That’s all ! Here you can find the **complete code** **on Github** (also with the plots functions). To learn how to do these simulations, I read this Marat Kopytjuk story and also this web owned by John D. Hedengren. They have good material, so take a look of it.

I hope you enjoyed this tutorial and, if you have any thoughts, recommendations or petitions for future tutorials on Dynamic Simulations with Python, I would really appreciate your comments!

Now you can check the **second part** of this tutorial to learn how to test C/C++ controllers using what you know of Python Dynamics Simulations.

# 4. References

[1] Jean-Jacques E. Slotine, Weiping Li (1988)* ‘*Feedback Linearization’ in *Applied Nonliner Control*.