This collection of Python files implement the popular RK4 numerical integration method and a few example differential equation applications of electrical and electromechanical systems.
The top-level file simulation.py
simulates the system using the RK4 method. For the sake of learning, no library functions are used---all equations, solvers, and simulation steps are hand-coded directly in these files.
For a more structured and complete simulation framework written in Python for electromechanical systems, check out the open-source
motulator
project from Aalto University in Finland.
What is provided in this repo?
Common to all examples:
./simulation.py
---top-level which calls the RK4 ODE solver./solver_rk4.py
---implements the step function which approximates the next solution point
Example-specific files:
./exampleX/diff_eq.py
---the differential equation for the example application./exampleX/applied_input.py
---generates the applied input profile purely as a function of time
Run python3 simulation.py
to run the simulation.
On the console, the state information will be printed.
For example, in a scalar system (1 input, 1 output):
# time (sec), applied input vector, y state vector
# Step size is 0.01 sec:
0.00 [1.0] [0.000000000000000]
0.01 [1.0] [0.242798353909465]
0.02 [1.0] [0.367694626496638]
0.03 [1.0] [0.431941680296624]
0.04 [1.0] [0.464990576284271]
0.05 [1.0] [0.481991037183267]
0.06 [1.0] [0.490736130238306]
# ...
Per Wikipedia, any explicit differential equation of order
where
Numerical integration methods (like RK4) can be generally applied to systems of this form.
If we write our system equations in this form, their solution can be approximated using the solver. Notice that the function
See the example code:
./e01_rl_circuit/...
Let's model a resistive-inductive (RL) circuit:
The governing equations which relate current
Isolate the derivative as:
Or, in general form:
We can solve for the steady-state operating point by setting our differential equation to 0, meaning the state is not changing.
Assuming a constant voltage applied
This result matches our intuition: in steady-state, the inductive part of the RL circuit vanishes and the system is simply governed by Ohm's law:
Parameter | Symbol | Value | Units |
---|---|---|---|
Resistance | 2 | ||
Inductance | 0.04 | H |
With the example code provided, in the circuit:
- Initial current value at
$t=0$ is 0 A - 1 V voltage is applied at
$t=0$ .
The circuit time constant
The resistance is set such that steady-state current with
See the example code:
./e02_dc_motor/...
To a user, the DC motor can be thought of a black-box system. Consider the motor pictured below with no load attached to it. The user starts by applying a DC voltage across its two terminals. Current then flows into the motor from the source. The motor will accelerate and reach a steady-state speed. These three items (voltage, current and speed) are all interrelated. Understanding the relationship is possible by examining the differential equations which govern the system.
DC motors can be modeled as a system with two parts: electrical and mechanical. Governing equations can be written for each system individually, and then coupling equations relate states in each system together.
The electrical system of a DC motor looks like the RL circuit above, but also includes a back-EMF term
The mechanical system assumes a torque source
Notice the duality compared to the electrical system: both
When the motor spins, the non-zero speed
When current
Note that these motor constants,
Combining the above relations give the governing differential equations of the DC motor:
Isolating the derivatives:
Or, in general form:
We can solve for the steady-state operating point by setting our differential equation to 0, meaning the states are not changing.
Assume a constant voltage applied
Then, solve for the current and speed in steady-state:
After solving the system of equations:
Now, let's examine these results under an extreme case:
Consider the mechanical damping
- The steady-state speed becomes
$\Omega_{ss} = V_{ss} / k_\mathrm{e}$ . - The steady-state current becomes
$I_{ss} = 0$ .
This operating point is called the theoretical no-load speed and current: at this idealized no-load, the motor will accelerate up to the voltage limit (i.e., applied voltage). At this point, the motor's back-EMF matches the applied voltage, so no current flows. Since no current flows, no torque is created, so the speed is in steady-state. In real systems, the damping
The following specifications and parameters model the popular 775 size DC motor (12 V, not geared version), which is what was pictured above. Note that, depending on the vendor, these parameters might vary. However, these values provide a good baseline representative model of the motor.
Specification | Symbol | Value | Units |
---|---|---|---|
Rated Voltage | 12 | V | |
No-Load Speed | 12000 | RPM | |
No-Load Current | 1.2 | A | |
Stall Current | 42.4 | A |
Based on the above specs, combined with assumptions on the electrical and mechanical time constants, the full model parameters can be derived.
The armature winding resistance
Based on the other motor specifications (no-load current and speed, stall current), the mechanical damping constant
where
Now, all the steady-state parameters are known. The armature winding inductance
However, we can estimate the
Based on the above motor specifications, equations, and assumptions, the following table lists the full model parameters:
Parameter | Symbol | Value | Units |
---|---|---|---|
Resistance | 2.83e-01 | ||
Inductance | 1.42e-03 | ||
Inertia | 2.66e-06 | ||
Damping | 8.86e-06 | ||
Back-EMF constant | 9.28e-03 | ||
Torque constant | 9.28e-03 |
TODO: in progress...
TODO: in progress...
TODO: in progress...