Skip to content

Commit 6bef511

Browse files
baggepinnenYingboMa
authored andcommitted
add use_arrays kwarg
1 parent 2d6a55a commit 6bef511

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/fancy_joints.jl

+23-13
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ end
377377

378378

379379
@component function RevoluteWithLengthConstraint(; name, n = Float64[0, 0, 1], axisflange = false,
380-
positive_branch = true, radius = 0.05, length = radius, color = [0.5019608f0,0.0f0,0.5019608f0,1.0f0], state_priority = 1.0, phi_offset=0, phi_guess=0, length_constraint=1)
380+
positive_branch = true, radius = 0.05, length = radius, color = [0.5019608f0,0.0f0,0.5019608f0,1.0f0], state_priority = 1.0, phi_offset=0, phi_guess=0, length_constraint=1, use_arrays = false)
381381
systems = @named begin
382382
frame_a = Frame()
383383
frame_b = Frame()
@@ -401,11 +401,11 @@ end
401401
description = "Driving torque in direction of axis of rotation",
402402
]
403403
@variables phi(t) [
404-
# state_priority = state_priority,
404+
state_priority = 1,
405405
description = "Relative rotation angle from frame_a to frame_b",
406406
]
407407
@variables angle(t) [
408-
# state_priority = -1,
408+
state_priority = -1,
409409
description = "= phi + phi_offset (relative rotation angle between frame_a and frame_b)",
410410
]
411411
@variables r_a(t)[1:3], [description = "Position vector from frame_a to frame_a side of length constraint, resolved in frame_a of revolute joint"]
@@ -443,9 +443,11 @@ end
443443
0 .~ collect(frame_a.f .+ resolve1(Rrel, frame_b.f))
444444
0 .~ collect(frame_a.tau .+ resolve1(Rrel, frame_b.tau))
445445

446-
# angle ~ compute_angle2(length_constraint, e_array, r_a_array, r_b_array, positive_branch)[1]
447-
# angle ~ Symbolics.term(compute_angle2, length_constraint, e_array, r_a_array, r_b_array, positive_branch, type=Real)
448-
angle ~ compute_angle(length_constraint, e, r_a, r_b, positive_branch)
446+
if use_arrays
447+
angle ~ compute_angle2(length_constraint, e_array, r_a_array, r_b_array, positive_branch)
448+
else
449+
angle ~ _compute_angle2(length_constraint, e, r_a, r_b, positive_branch)
450+
end
449451
]
450452

451453
sys = ODESystem(eqs, t; name=:nothing, systems)#, parameter_dependencies = [positive_branch => select_branch(length_constraint, e, phi_offset + phi_guess, r_a, r_b)]) # JuliaSimCompiler ignores parameter dependencies, the user has to provide it instead
@@ -502,6 +504,7 @@ The rest of this joint aggregation is defined by the following parameters:
502504
phi_offset = 0,
503505
phi_guess = 0,
504506
positive_branch,
507+
use_arrays = false,
505508
)
506509
systems = @named begin
507510
frame_a = Frame()
@@ -550,6 +553,7 @@ The rest of this joint aggregation is defined by the following parameters:
550553
phi_offset,
551554
phi_guess,
552555
positive_branch,
556+
use_arrays,
553557
)
554558
push!(more_systems, revolute)
555559

@@ -734,27 +738,33 @@ function compute_angle(L::Real, e::AbstractVector, r_a::AbstractVector, r_b::Abs
734738
atan(ksin1, kcos1)
735739
end
736740

737-
function compute_angle2(L::Real, e::AbstractVector, r_a::AbstractVector, r_b::AbstractVector, positive_branch)
741+
function _compute_angle2(L::Real, e::AbstractVector, r_a::AbstractVector, r_b::AbstractVector, positive_branch)
742+
@show e,r_a
738743
e_r_a = e'r_a
739744
e_r_b = e'r_b
740745
A = -2*(r_b'r_a - e_r_b'e_r_a)
741746
B = 2*r_b'cross(e, r_a)
742747
C = r_a'r_a + r_b'r_b - L^2 - 2*e_r_b'e_r_a
743748
k1 = A^2 + B^2
744749
k1a = k1 - C^2
745-
k1a > 1e-10 || error("Singular position of loop (either no or two analytic solutions; the mechanism has lost one-degree-of freedom in this position). Try to use another joint-assembly component. In most cases it is best to let joints outside of the JointXXX component be revolute and _not_ prismatic joints. If this also leads to singular positions, it could be that this kinematic loop cannot be solved analytically. In this case you have to build up the loop with basic joints (_no_ aggregation JointXXX components) and rely on dynamic state selection, i.e., during simulation the states will be dynamically selected in such a way that in no position a degree of freedom is lost.")
750+
#k1a > 1e-10 || error("Singular position of loop (either no or two analytic solutions; the mechanism has lost one-degree-of freedom in this position). Try to use another joint-assembly component. In most cases it is best to let joints outside of the JointXXX component be revolute and _not_ prismatic joints. If this also leads to singular positions, it could be that this kinematic loop cannot be solved analytically. In this case you have to build up the loop with basic joints (_no_ aggregation JointXXX components) and rely on dynamic state selection, i.e., during simulation the states will be dynamically selected in such a way that in no position a degree of freedom is lost.")
746751
k1b = max(k1a, 1.0e-12)
747752
k2 = sqrt(k1b)
748753
kcos1 = -A*C + B*k2*ifelse(positive_branch == true, 1, -1)
749754
ksin1 = -B*C + A*k2*ifelse(positive_branch == true, -1, 1)
750-
[atan(ksin1, kcos1)]
755+
atan(ksin1, kcos1)
751756
end
752757

753-
@register_array_symbolic compute_angle2(L::Real, e::AbstractVector, r_a::AbstractVector, r_b::AbstractVector, positive_branch::Bool) begin
754-
size = (1, )
755-
eltype = Float64
758+
function compute_angle2(L::Real, e::AbstractVector, r_a::AbstractVector, r_b::AbstractVector, positive_branch::Bool)
759+
_compute_angle2(L, e, r_a, r_b, positive_branch)
756760
end
757761

762+
@register_symbolic compute_angle2(L::Real, e::AbstractVector, r_a::AbstractVector, r_b::AbstractVector, positive_branch::Bool)
763+
#@register_array_symbolic compute_angle2(L::Real, e::AbstractVector, r_a::AbstractVector, r_b::AbstractVector, positive_branch::Bool) begin
764+
# size = (1, )
765+
# eltype = Float64
766+
#end
767+
758768
# @register_symbolic compute_angle(L::Real, e::AbstractVector, r_a::AbstractVector, r_b::AbstractVector, positive_branch::Bool)::Real
759769

760770
# @register_symbolic compute_angle(L::Num, e1::Num, e1::Num, e2::Num, r_a1::Num, r_a2::Num, r_a3::Num, r_b1::Num, r_b2::Num, r_b3::Num, positive_branch)
@@ -764,4 +774,4 @@ end
764774
# r_a = SA[r_a1, r_a2, r_a3]
765775
# r_b = SA[r_b1, r_b2, r_b3]
766776
# compute_angle(L, e, r_a, r_b, positive_branch)
767-
# end
777+
# end

test/test_JointUSR_RRR.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ W(args...; kwargs...) = Multibody.world
1616
@mtkmodel TestUSR begin
1717
@components begin
1818
world = W()
19-
j1 = JointUSR(positive_branch=true)
19+
j1 = JointUSR(positive_branch=true, use_arrays=false)
2020
fixed = FixedTranslation(r=[1,0,0])
2121
b1 = Body(isroot=false, neg_w=true)
2222
p1 = Prismatic(state_priority=100)
@@ -32,7 +32,7 @@ end
3232
@named model = TestUSR()
3333
model = complete(model)
3434
ssys = structural_simplify(IRSystem(model))
35-
prob = ODEProblem(ssys, [model.b1.a_0[1] => 0, D(D(model.p1.s)) => 0], (0.0, 1.0))
35+
prob = ODEProblem(ssys, [], (0.0, 1.0))
3636
sol = solve(prob, FBDF(autodiff=true))
3737

3838

0 commit comments

Comments
 (0)