Skip to content

Instantly share code, notes, and snippets.

@ven-k
Created July 27, 2023 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ven-k/2ae86a5e0d820a3b742420df1464e068 to your computer and use it in GitHub Desktop.
Save ven-k/2ae86a5e0d820a3b742420df1464e068 to your computer and use it in GitHub Desktop.
DC Motor with SpeedSensor
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Mechanical.Rotational
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit, OrdinaryDiffEq
using Plots
@mtkmodel DC_Motor begin
@components begin
voltage_step = Step(height = 10, start_time = 0)
ground = Ground()
source = Voltage()
R1 = Resistor(R = 0.5)
L1 = Inductor(L = 4.5e-3, i = 0.0)
emf = EMF(k = 0.5)
load_step = Step(height = -3, start_time = 3)
fixed = Fixed()
load = Torque(use_support = false)
inertia = Inertia(J = 0.02)
friction = Damper(d = 0.01)
speed_sensor = SpeedSensor()
end
# @icon "<icon for DC_Motor>"
@equations begin
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(voltage_step.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)
end
end
sys = structural_simplify(DC_Motor())
prob = ODEProblem(sys, Pair[], (0, 6.0))
sol = solve(prob, Rodas4())
plot(sol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment