-
-
Notifications
You must be signed in to change notification settings - Fork 45
Adds permanent magnet DC machine #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# DC Motor with PI-controller | ||
In this example a PI-controller is setup for speed control of a DC-motor. First the needed packages | ||
are imported and the parameters of the model defined. | ||
|
||
```@example dc_motor_pi | ||
using ModelingToolkit | ||
using ModelingToolkitStandardLibrary.Electrical | ||
using ModelingToolkitStandardLibrary.Mechanical.Rotational | ||
using ModelingToolkitStandardLibrary.Blocks | ||
using OrdinaryDiffEq | ||
using Plots | ||
|
||
@parameters t | ||
|
||
R = 0.5 # [Ohm] | ||
L = 4.5e-3 # [H] | ||
k = 0.5 # [N.m/A] | ||
J = 0.02 # [kg.m²] | ||
f = 0.01 # [N.m.s/rad] | ||
tau_L_step = -3 # [N.m] | ||
nothing # hide | ||
``` | ||
|
||
The actual model can now be composed. | ||
|
||
```@example dc_motor_pi | ||
@named ground = Ground() | ||
@named source = Voltage() | ||
@named ref = Blocks.Step(height = 1, start_time = 0) | ||
@named pi_controller = Blocks.LimPI(k = 1.1, T = 0.035, u_max = 0.6, Ta = 0.035) | ||
@named feedback = Blocks.Feedback() | ||
@named R1 = Resistor(R = R) | ||
@named L1 = Inductor(L = L) | ||
@named emf = EMF(k = k) | ||
@named fixed = Fixed() | ||
@named load = Torque(use_support = false) | ||
@named load_step = Blocks.Step(height = tau_L_step, start_time = 3) | ||
@named inertia = Inertia(J = J) | ||
@named friction = Damper(d = f) | ||
@named speed_sensor = SpeedSensor() | ||
|
||
connections = [connect(fixed.flange, emf.support, friction.flange_b) | ||
connect(emf.flange, friction.flange_a, inertia.flange_a) | ||
connect(inertia.flange_b, load.flange) | ||
connect(inertia.flange_b, speed_sensor.flange) | ||
connect(load_step.output, load.tau) | ||
connect(ref.output, feedback.input1) | ||
connect(speed_sensor.w, feedback.input2) | ||
connect(feedback.output, pi_controller.err_input) | ||
connect(pi_controller.ctr_output, source.V) | ||
connect(source.p, R1.p) | ||
connect(R1.n, L1.p) | ||
connect(L1.n, emf.p) | ||
connect(emf.n, source.n, ground.g)] | ||
|
||
@named model = ODESystem(connections, t, | ||
systems = [ | ||
ground, | ||
ref, | ||
pi_controller, | ||
feedback, | ||
source, | ||
R1, | ||
L1, | ||
emf, | ||
fixed, | ||
load, | ||
load_step, | ||
inertia, | ||
friction, | ||
speed_sensor, | ||
]) | ||
nothing # hide | ||
``` | ||
|
||
Now the model can be simulated. Typical rotational mechanical systems are described via `DAE` | ||
(differential algebraic equations), however in this case ModelingToolkit can simplify the model enough | ||
so that it can be represented as a system of `ODEs` (ordinary differential equations). | ||
|
||
```@example dc_motor_pi | ||
sys = structural_simplify(model) | ||
prob = ODEProblem(sys, [], (0, 6.0)) | ||
sol = solve(prob, Rodas4()) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using prob = ODAEProblem(sys, [], (0, 6.0))
sol = solve(prob, Rodas4()) will result in ERROR: UndefVarError: pi_controller₊err_input₊u(t) not defined
Stacktrace:
[1] macro expansion
@ .julia\dev\ModelingToolkit\src\structural_transformation\codegen.jl:199 [inlined]
[2] macro expansion
@ .julia\packages\SymbolicUtils\qulQp\src\code.jl:351 [inlined]
[3] macro expansion
@ C:\Users\kaisv.TUGRAZ\.julia\packages\RuntimeGeneratedFunctions\KrkGo\src\RuntimeGeneratedFunctions.jl:129 [inlined]
[4] macro expansion
@ .\none:0 [inlined]
[5] generated_callfunc
@ .\none:0 [inlined]
[6] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#14840964602010972036"), Symbol("##arg#18305951688076727758"), :t), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0x8b4a2f6c, 0xa9522116, 0x134b5081, 0x0a8c04c2, 0x33f2532d)})(::Vector{Float64}, ::Vector{Float64}, ::Float64)
@ RuntimeGeneratedFunctions .julia\packages\RuntimeGeneratedFunctions\KrkGo\src\RuntimeGeneratedFunctions.jl:117 |
||
p1 = Plots.plot(sol.t, sol[inertia.w], ylabel = "Angular Vel. in rad/s", | ||
label = "Measurement", title = "DC Motor with Speed Controller") | ||
Plots.plot!(sol.t, sol[ref.output.u], label = "Reference") | ||
p2 = Plots.plot(sol.t, sol[load.tau.u], ylabel = "Disturbance in Nm", label = "") | ||
Plots.plot(p1, p2, layout = (2, 1)) | ||
nothing # hide | ||
``` | ||
|
||
 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
""" | ||
Torque(;name) | ||
Torque(; name, use_support=false) | ||
|
||
Input signal acting as external torque on a flange | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to all
Blocks.RealInputs
being marked asinput=true
even the simplified system has a whopping 19 states: