Skip to content

Commit 41a0547

Browse files
committed
pass vars and pars in Body
1 parent d4f219a commit 41a0547

2 files changed

Lines changed: 60 additions & 85 deletions

File tree

src/components.jl

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -374,65 +374,43 @@ This component has a single frame, `frame_a`. To represent bodies with more than
374374
# @warn "Make the body have state variables by using isroot=true rather than state=true"
375375
isroot = true
376376
end
377-
@variables r_0(t)[1:3]=r_0 [
378-
state_priority = state_priority+isroot,
379-
description = "Position vector from origin of world frame to origin of frame_a",
380-
]
381-
@variables v_0(t)[1:3]=v_0 [
382-
state_priority = state_priority+isroot,
383-
description = "Absolute velocity of frame_a, resolved in world frame (= D(r_0))",
384-
]
385-
@variables a_0(t)[1:3] [
386-
description = "Absolute acceleration of frame_a resolved in world frame (= D(v_0))",
387-
]
388-
@variables g_0(t)[1:3] [ description = "gravity acceleration"]
389-
@variables w_a(t)[1:3]=w_a [
390-
state_priority = isroot ? quat ? state_priority : -1 : 0,
391-
description = "Absolute angular velocity of frame_a resolved in frame_a",
392-
]
393-
@variables z_a(t)[1:3] [
394-
description = "Absolute angular acceleration of frame_a resolved in frame_a",
395-
]
396-
# 6*3 potential variables + Frame: 2*3 flow + 3 potential + 3 residual = 24 equations + 2*3 flow
397-
@parameters m=m [description = "mass"]
398-
@parameters r_cm[1:3]=r_cm [
399-
description = "Vector from frame_a to center of mass, resolved in frame_a",
400-
]
401-
@parameters radius=radius [
402-
description = "Radius of the body in animations",
403-
]
404-
@parameters cylinder_radius=cylinder_radius [
405-
description = "Radius of the cylinder from frame to COM in animations",
406-
]
407-
@parameters color[1:4] = color [description = "Color of the body in animations (RGBA)"]
408-
@parameters length_fraction=length_fraction, [description = "Fraction of the length of the body that is the cylinder from frame to COM in animations"]
409-
@parameters render = render [description = "Render the component in animations"]
410-
# @parameters I[1:3, 1:3]=I [description="inertia tensor"]
411377

412378
if sparse_I
413379
Isparsity = sparse(.!isequal.(0, [I_11 I_21 I_31; I_21 I_22 I_32; I_31 I_32 I_33]))
414380
end
415381

416-
@parameters I_11=I_11 [description = "Element (1,1) of inertia tensor"]
417-
@parameters I_22=I_22 [description = "Element (2,2) of inertia tensor"]
418-
@parameters I_33=I_33 [description = "Element (3,3) of inertia tensor"]
419-
@parameters I_21=I_21 [description = "Element (2,1) of inertia tensor"]
420-
@parameters I_31=I_31 [description = "Element (3,1) of inertia tensor"]
421-
@parameters I_32=I_32 [description = "Element (3,2) of inertia tensor"]
382+
vars = @variables begin
383+
r_0(t)[1:3]=r_0, [state_priority = state_priority+isroot, description = "Position vector from origin of world frame to origin of frame_a"]
384+
v_0(t)[1:3]=v_0, [state_priority = state_priority+isroot, description = "Absolute velocity of frame_a, resolved in world frame (= D(r_0))"]
385+
a_0(t)[1:3], [description = "Absolute acceleration of frame_a resolved in world frame (= D(v_0))"]
386+
g_0(t)[1:3], [description = "gravity acceleration"]
387+
w_a(t)[1:3]=w_a, [state_priority = isroot ? quat ? state_priority : -1 : 0, description = "Absolute angular velocity of frame_a resolved in frame_a"]
388+
z_a(t)[1:3], [description = "Absolute angular acceleration of frame_a resolved in frame_a"]
389+
end
390+
391+
pars = @parameters begin
392+
m=m, [description = "mass"]
393+
r_cm[1:3]=r_cm, [description = "Vector from frame_a to center of mass, resolved in frame_a"]
394+
I_11=I_11, [description = "Element (1,1) of inertia tensor"]
395+
I_22=I_22, [description = "Element (2,2) of inertia tensor"]
396+
I_33=I_33, [description = "Element (3,3) of inertia tensor"]
397+
I_21=I_21, [description = "Element (2,1) of inertia tensor"]
398+
I_31=I_31, [description = "Element (3,1) of inertia tensor"]
399+
I_32=I_32, [description = "Element (3,2) of inertia tensor"]
400+
radius=radius, [description = "Radius of the body in animations"]
401+
cylinder_radius=cylinder_radius, [description = "Radius of the cylinder from frame to COM in animations"]
402+
color[1:4] = color, [description = "Color of the body in animations (RGBA)"]
403+
length_fraction=length_fraction, [description = "Fraction of the length of the body that is the cylinder from frame to COM in animations"]
404+
render = render, [description = "Render the component in animations"]
405+
end
422406

423407
I = [I_11 I_21 I_31; I_21 I_22 I_32; I_31 I_32 I_33]
424408
if sparse_I
425409
I = I.*Isparsity
426410
end
427411

428-
# r_0, v_0, a_0, g_0, w_a, z_a, r_cm = collect.((r_0, v_0, a_0, g_0, w_a, z_a, r_cm))
429-
430-
# DRa = D(Ra)
431-
432-
dvs = [r_0;v_0;a_0;g_0;w_a;z_a;]
433-
434412
eqs = if isroot # isRoot
435-
413+
436414
if quat
437415
@named frame_a = Frame(varw=false)
438416
Ra = ori(frame_a, false)
@@ -443,7 +421,7 @@ This component has a single frame, `frame_a`. To represent bodies with more than
443421
@variables phi(t)[1:3]=phi0 [state_priority = 10, description = "Euler angles"]
444422
@variables phid(t)[1:3]=phid0 [state_priority = 10]
445423
@variables phidd(t)[1:3] [state_priority = 0]
446-
# phi, phid, phidd = collect.((phi, phid, phidd))
424+
append!(vars, [phi; phid; phidd])
447425
ar = axes_rotations(sequence, phi, phid)
448426
Equation[
449427
phid .~ D.(phi)
@@ -478,10 +456,7 @@ This component has a single frame, `frame_a`. To represent bodies with more than
478456
end
479457
(frame_a.tau ~ I * z_a + cross(w_a, collect(I * w_a)) + cross(r_cm, frame_a.f))]
480458

481-
# pars = [m;r_cm;radius;I_11;I_22;I_33;I_21;I_31;I_32;color]
482-
483-
sys = System(eqs, t; name=:nothing, metadata = Dict(IsRoot => isroot), systems = [frame_a])
484-
add_params(sys, [radius; cylinder_radius; color; length_fraction; render]; name)
459+
System(eqs, t, vars, pars; name, metadata = Dict(IsRoot => isroot), systems = [frame_a])
485460
end
486461

487462

test/test_robot.jl

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ u = m.axis2.controller.PI.ctr_output.u
235235
ssys = multibody(oneaxis)
236236

237237
op = Dict([
238-
# oneaxis.axis.flange.phi => 0
238+
oneaxis.axis.motor.Rd4.i => 0
239239
# D(oneaxis.axis.flange.phi) => 0
240240
# D(D(oneaxis.axis.flange.phi)) => 0
241241
# D(D(oneaxis.load.phi)) => 0
@@ -263,7 +263,7 @@ u = m.axis2.controller.PI.ctr_output.u
263263
# ], (0.0, 5.0))
264264

265265

266-
prob = ODEProblem(ssys, collect(op), (0.0, 3),)
266+
prob = ODEProblem(ssys, op, (0.0, 3),)
267267
sol = solve(prob, Tsit5());
268268
if doplot()
269269
plot(sol, layout=length(unknowns(ssys)), size=(1900, 1200))
@@ -348,34 +348,34 @@ end
348348

349349

350350

351-
@testset "subs constants" begin
352-
@info "Testing subs constants"
353-
@named robot = Robot6DOF()
354-
robot = complete(robot)
355-
ssys = multibody(robot)
356-
ssys = Multibody.subs_constants(robot; ssys)
357-
prob = ODEProblem(ssys, [
358-
robot.mechanics.r1.phi => deg2rad(-60)
359-
robot.mechanics.r2.phi => deg2rad(20)
360-
robot.mechanics.r3.phi => deg2rad(90)
361-
robot.mechanics.r4.phi => deg2rad(0)
362-
robot.mechanics.r5.phi => deg2rad(-110)
363-
robot.mechanics.r6.phi => deg2rad(0)
364-
365-
robot.axis1.motor.Jmotor.phi => deg2rad(-60) * (-105) # Multiply by gear ratio
366-
robot.axis2.motor.Jmotor.phi => deg2rad(20) * (210)
367-
robot.axis3.motor.Jmotor.phi => deg2rad(90) * (60)
368-
], (0.0, 2.0))
369-
sol2 = solve(prob, Rodas5P(autodiff=false))
370-
371-
@test SciMLBase.successful_retcode(sol2)
372-
373-
tv = 0:0.1:2
374-
# control_error = sol2(tv, idxs=robot.pathPlanning.controlBus.axisControlBus1.angle_ref-robot.pathPlanning.controlBus.axisControlBus1.angle)
375-
# @test maximum(abs, control_error) < 0.002
376-
377-
# using BenchmarkTools
378-
# @btime solve(prob, Rodas5P(autodiff=false));
379-
# 152.225 ms (2272926 allocations: 40.08 MiB)
380-
# 114.113 ms (1833534 allocations: 33.38 MiB) # sub 0, 1
381-
end
351+
# @testset "subs constants" begin
352+
# @info "Testing subs constants"
353+
# @named robot = Robot6DOF()
354+
# robot = complete(robot)
355+
# ssys = multibody(robot)
356+
# ssys = Multibody.subs_constants(robot; ssys)
357+
# prob = ODEProblem(ssys, [
358+
# robot.mechanics.r1.phi => deg2rad(-60)
359+
# robot.mechanics.r2.phi => deg2rad(20)
360+
# robot.mechanics.r3.phi => deg2rad(90)
361+
# robot.mechanics.r4.phi => deg2rad(0)
362+
# robot.mechanics.r5.phi => deg2rad(-110)
363+
# robot.mechanics.r6.phi => deg2rad(0)
364+
365+
# robot.axis1.motor.Jmotor.phi => deg2rad(-60) * (-105) # Multiply by gear ratio
366+
# robot.axis2.motor.Jmotor.phi => deg2rad(20) * (210)
367+
# robot.axis3.motor.Jmotor.phi => deg2rad(90) * (60)
368+
# ], (0.0, 2.0))
369+
# sol2 = solve(prob, Rodas5P(autodiff=false))
370+
371+
# @test SciMLBase.successful_retcode(sol2)
372+
373+
# tv = 0:0.1:2
374+
# # control_error = sol2(tv, idxs=robot.pathPlanning.controlBus.axisControlBus1.angle_ref-robot.pathPlanning.controlBus.axisControlBus1.angle)
375+
# # @test maximum(abs, control_error) < 0.002
376+
377+
# # using BenchmarkTools
378+
# # @btime solve(prob, Rodas5P(autodiff=false));
379+
# # 152.225 ms (2272926 allocations: 40.08 MiB)
380+
# # 114.113 ms (1833534 allocations: 33.38 MiB) # sub 0, 1
381+
# end

0 commit comments

Comments
 (0)