Skip to content
Snippets Groups Projects
Commit 9d2d188b authored by Felix Agner's avatar Felix Agner
Browse files

added some electrical playtoys in the modeling_toolkit code

parent f31a1540
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` julia
using ModelingToolkit, Plots, DifferentialEquations
```
%% Cell type:code id: tags:
``` julia
@variables t
@connector function Pin(;name)
sts = @variables v(t)=1.0 i(t)=1.0 [connect = Flow]
ODESystem(Equation[], t, sts, []; name=name)
end
function Ground(;name)
@named g = Pin()
eqs = [g.v ~ 0]
compose(ODESystem(eqs, t, [], []; name=name), g)
end
function OnePort(;name)
@named p = Pin()
@named n = Pin()
sts = @variables v(t)=1.0 i(t)=1.0
eqs = [
v ~ p.v - n.v
0 ~ p.i + n.i
i ~ p.i
]
compose(ODESystem(eqs, t, sts, []; name=name), p, n)
end
function Resistor(;name, R = 1.0)
@named oneport = OnePort()
@unpack v, i = oneport
ps = @parameters R=R
eqs = [
v ~ i * R
]
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
function Capacitor(;name, C = 1.0)
@named oneport = OnePort()
@unpack v, i = oneport
ps = @parameters C=C
D = Differential(t)
eqs = [
D(v) ~ i / C
]
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
function ConstantVoltage(;name, V = 1.0)
@named oneport = OnePort()
@unpack v = oneport
ps = @parameters V=V
eqs = [
V ~ v
]
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
```
%% Output
ConstantVoltage (generic function with 1 method)
%% Cell type:code id: tags:
``` julia
R = 1.0
C = 1.0
V = 1.0
@named resistor = Resistor(R=R)
@named capacitor = Capacitor(C=C)
@named source = ConstantVoltage(V=V)
@named ground = Ground()
rc_eqs = [
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n)
connect(capacitor.n, ground.g)
]
@named _rc_model = ODESystem(rc_eqs, t)
@named rc_model = compose(_rc_model,
[resistor, capacitor, source, ground])
```
%% Output
Model rc_model with 17 equations
States (20):
resistor₊v(t) [defaults to 1.0]
resistor₊i(t) [defaults to 1.0]
resistor₊p₊v(t) [defaults to 1.0]
resistor₊p₊i(t) [defaults to 1.0]
resistor₊n₊v(t) [defaults to 1.0]
resistor₊n₊i(t) [defaults to 1.0]
Parameters (3):
resistor₊R [defaults to 1.0]
capacitor₊C [defaults to 1.0]
source₊V [defaults to 1.0]
%% Cell type:code id: tags:
``` julia
sys = structural_simplify(rc_model)
```
%% Output
\begin{align}
\frac{dcapacitor_{+}v(t)}{dt} =& \frac{\mathrm{capacitor_{+}i}\left( t \right)}{capacitor_{+}C}
\end{align}
Model rc_model with 1 equations
States (1):
capacitor₊v(t) [defaults to 1.0]
Parameters (3):
resistor₊R [defaults to 1.0]
capacitor₊C [defaults to 1.0]
source₊V [defaults to 1.0]
Incidence matrix:sparse([1, 1], [1, 2], Num[×, ×], 1, 2)
%% Cell type:code id: tags:
``` julia
u0 = [
capacitor.v => 0.0
]
```
%% Output
1-element Vector{Pair{Num, Float64}}:
capacitor₊v(t) => 0.0
%% Cell type:code id: tags:
``` julia
prob = ODAEProblem(sys, u0, (0, 10.0))
```
%% Output
ODEProblem with uType Vector{Float64} and tType Float64. In-place: true
timespan: (0.0, 10.0)
u0: 1-element Vector{Float64}:
0.0
%% Cell type:code id: tags:
``` julia
sol = solve(prob, Tsit5())
```
%% Output
retcode: Success
Interpolation: specialized 4th order "free" interpolation
t: 19-element Vector{Float64}:
0.0
9.999999999999999e-5
0.0010999999999999998
0.011099999999999997
0.076742091466188
0.2256219976613778
0.4455760022489606
0.7272505097568545
1.0899643857570624
1.5331652827982714
2.06972399829104
2.705750446504391
3.4562444530999095
4.3378403240538095
5.377903566781988
6.614654307931712
8.1073506878944
9.946895379599903
10.0
u: 19-element Vector{Vector{Float64}}:
[0.0]
[9.999500016666247e-5]
[0.001099395221772342]
[0.011038622307372232]
[0.0738713206981718]
[0.20198030129465663]
[0.3595447255651815]
[0.5167641452122546]
[0.6637713825376194]
[0.7841482271226826]
[0.873778404447212]
[0.9331779821204202]
[0.9684489616227437]
[0.9869310637824636]
[0.9953772824474669]
[0.9986536797747498]
[0.9996930416967437]
[0.9999471971800106]
[0.9999499280997356]
%% Cell type:code id: tags:
``` julia
plot(sol)
```
%% Output
%% Cell type:code id: tags:
``` julia
```
......
%% Cell type:code id:d47c9765-5d14-4e9d-a42e-1623da8bf5ae tags:
``` julia
using Modia3D
```
%% Output
... Info: ENV["DLR_VISUALIZATION"] is not defined.
Therefore, no online renderer is used in Modia3D.
%% Cell type:code id:964c5cea-1e33-4fa0-be73-68acff0fd5a2 tags:
``` julia
Pendulum = Model3D(
world = Object3D(feature=Scene()),
body = Object3D(feature=Solid(massProperties=MassProperties(mass=1.0))),
bodyFrame = Object3D(parent=:body, translation=[-0.5, 0.0, 0.0]),
rev = Revolute(obj1=:world, obj2=:bodyFrame)
)
```
%% Output
TypeError: in keyword argument parent, expected Union{Nothing, Object3D}, got a value of type Symbol
Stacktrace:
[1] top-level scope
@ In[2]:1
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
%% Cell type:code id:6584c04b-be0e-49f4-a7ac-d4ad06c404a5 tags:
``` julia
pendulum = @instantiateModel(Pendulum, unitless=true)
```
%% Cell type:code id:976e8115-66fb-4bea-a5cf-59dfeffc7224 tags:
``` julia
simulate!(pendulum, stopTime=3.0)
@usingModiaPlot # use the plot package defined by ENV["MODIA_PLOT"]
plot(pendulum, "rev.phi")
```
%% Cell type:markdown id: tags:
# Physical specification for the omnibot
%% Cell type:markdown id: tags:
The omnibot should consist of three major components:
* Chassi (traingle of plastic)
* 3 omni-wheels
* 3 servos that drive the wheels
%% Cell type:markdown id: tags:
# Wheel model
%% Cell type:markdown id: tags:
# Servo model
%% Cell type:markdown id: tags:
# Chassi model
%% Cell type:markdown id: tags:
# Everything combined
%% Cell type:code id: tags:
``` julia
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment