Skip to content

Commit 361c4cd

Browse files
committed
Renamed panda mechanisms, split tests into runtests_manual.jl and setup_test_mechanisms.jl to make running tests independently simpler. Fixed projecting joint torque for revolute joints.
1 parent e805bd6 commit 361c4cd

15 files changed

+293
-196
lines changed

RSONs/rsons/franka_panda/pandaDrill.rson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"rson_version": "0.1.0",
33
"type": "mechanism",
4-
"name": "panda",
4+
"name": "panda_drill",
55
"frames": [
66
"root_frame",
77
"L1_frame",

RSONs/rsons/franka_panda/pandaDummyInstrument.rson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"rson_version": "0.1.0",
33
"type": "mechanism",
4-
"name": "panda",
4+
"name": "panda_dummy_instrument",
55
"frames": [
66
"root_frame",
77
"L1_frame",

RSONs/rsons/franka_panda/pandaSurgical.rson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"rson_version": "0.1.0",
33
"type": "mechanism",
4-
"name": "panda",
4+
"name": "panda_surgical",
55
"frames": [
66
"root_frame",
77
"L1_frame",

RSONs/rsons/franka_panda/pandaSurgicalStryker.rson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"rson_version": "0.1.0",
33
"type": "mechanism",
4-
"name": "panda",
4+
"name": "panda_surgical_stryker",
55
"frames": [
66
"root_frame",
77
"L1_frame",

RSONs/rsons/franka_panda/pandaSurgicalV2.rson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"rson_version": "0.1.0",
33
"type": "mechanism",
4-
"name": "panda",
4+
"name": "panda_surgical_v2",
55
"frames": [
66
"root_frame",
77
"L1_frame",

RSONs/rsons/franka_panda/pandaVeterinarian.rson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"rson_version": "0.1.0",
33
"type": "mechanism",
4-
"name": "panda",
4+
"name": "panda_veterinarian",
55
"frames": [
66
"root_frame",
77
"L1_frame",

examples/rne_visualization.jl

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,87 @@ using
55
LinearAlgebra,
66
StaticArrays,
77
VMRobotControl
8-
9-
X = SVector{3, Float64}(1.0, 0.0, 0.0)
10-
Y = SVector{3, Float64}(0.0, 1.0, 0.0)
11-
Z = SVector{3, Float64}(0.0, 0.0, 1.0)
12-
13-
T1 = zero(Transform{Float64})
14-
J1 = Revolute(Y, T1)
15-
16-
T2 = Transform(Z)
17-
J2 = Revolute(Y, T2)
18-
19-
T3 = Transform(Z)
20-
J3 = Rigid(T3)
21-
22-
mech = Mechanism{Float64}("2Link")
23-
cart_frame = add_frame!(mech, "Cart")
24-
L2_frame = add_frame!(mech, "L2")
25-
EE_frame = add_frame!(mech, "EE")
26-
27-
add_joint!(mech, J1; parent="root_frame", child=cart_frame, id="J1")
28-
add_joint!(mech, J2; parent=cart_frame, child=L2_frame, id="J2")
29-
add_joint!(mech, J3, parent=L2_frame, child=EE_frame; id="J3")
30-
31-
# Then, we add a coordinate to the mechanism to represent the tip of the pendulum, and a
32-
# coordinate to represent the position of the cart. These are used to add point masses to
33-
# the mechanism.
34-
add_coordinate!(mech, FrameOrigin(EE_frame); id="tip_pos")
35-
add_coordinate!(mech, FrameOrigin(cart_frame); id="cart_pos")
36-
add_component!(mech, PointMass(1.0, "cart_pos"); id="cart_mass")
37-
add_component!(mech, PointMass(1.0, "tip_pos"); id="pendulum_mass")
38-
8+
# begin
9+
# X = SVector{3, Float64}(1.0, 0.0, 0.0)
10+
# Y = SVector{3, Float64}(0.0, 1.0, 0.0)
11+
# Z = SVector{3, Float64}(0.0, 0.0, 1.0)
12+
13+
# T1 = zero(Transform{Float64})
14+
# J1 = Revolute(Y, T1)
15+
16+
# T2 = Transform(Z)
17+
# J2 = Revolute(Y, T2)
18+
19+
# T3 = Transform(Z)
20+
# J3 = Rigid(T3)
21+
22+
# mech = Mechanism{Float64}("2Link")
23+
# cart_frame = add_frame!(mech, "Cart")
24+
# L2_frame = add_frame!(mech, "L2")
25+
# EE_frame = add_frame!(mech, "EE")
26+
27+
# add_joint!(mech, J1; parent="root_frame", child=cart_frame, id="J1")
28+
# add_joint!(mech, J2; parent=cart_frame, child=L2_frame, id="J2")
29+
# add_joint!(mech, J3, parent=L2_frame, child=EE_frame; id="J3")
30+
31+
# # Then, we add a coordinate to the mechanism to represent the tip of the pendulum, and a
32+
# # coordinate to represent the position of the cart. These are used to add point masses to
33+
# # the mechanism.
34+
# add_coordinate!(mech, FrameOrigin(EE_frame); id="tip_pos")
35+
# add_coordinate!(mech, FrameOrigin(cart_frame); id="cart_pos")
36+
# add_component!(mech, PointMass(1.0, "cart_pos"); id="cart_mass")
37+
# add_component!(mech, PointMass(1.0, "tip_pos"); id="pendulum_mass")
38+
# m = compile(mech)
39+
# end
40+
3941
# We compile the mechanism, and setup an ODE problem to simulate the dynamics of the rail robot.
40-
m = compile(mech)
42+
m = compiled_mechanisms[end]
4143
cache = Observable(new_inverse_dynamics_cache(m))
44+
# @testset test_inverse_dynamics(m)
4245

46+
bundle = cache[]
4347

4448
begin
49+
50+
4551
t = 0.0
4652
q = [0.0, π/2]
47-
= zero_q̇(m)
48-
= zero_q̈(m)
49-
g = SVector{3, Float64}(0.0, 0.0, -9.81)
50-
inverse_dynamics!(cache[], t, q, q̇, q̈, g)
53+
q = zero_q(m) + randn(7)
54+
= zero_q̇(m) + randn(7)
55+
g = SVector{3, Float64}(0.0, 0.0, -10) #+ rand(SVector{3, Float64})
56+
57+
# rng = MersenneTwister(1234)
58+
# t = 0.0
59+
# q = rand!(rng, zero_q(Float64, m))
60+
# q̇ = rand!(rng, zero_q̇(Float64, m))
61+
# g = randn(rng, SVector{3, Float64})
62+
63+
u = zero_u(m)
64+
65+
dcache = new_dynamics_cache(m)
66+
dynamics!(dcache, t, q, q̇, g, u)
67+
= get_q̈(dcache)
68+
69+
VMRobotControl._inverse_dynamics_set_inputs!(bundle, t, q, q̇, q̈, g)
70+
VMRobotControl._inverse_dynamics_forward_pass!(bundle)
71+
VMRobotControl._inverse_dynamics_zero!(bundle)
72+
VMRobotControl._inverse_dynamics_backward_pass_a!(bundle)
73+
VMRobotControl._inverse_dynamics_backward_pass_b!(bundle)
74+
VMRobotControl._inverse_dynamics_backward_pass_c!(bundle)
75+
VMRobotControl._inverse_dynamics_backward_pass_d!(bundle)
76+
77+
# q̈_out = dynamics!(dcache, t, q, q̇, g, get_u(bundle))
78+
79+
@test get_u(cache[]) u atol=1e-7 rtol=1e-7
80+
# @test q̈ ≈ q̈_out atol=1e-7 rtol=1e-7
81+
end
5182

83+
84+
begin
5285
fig = Figure()
5386
display(fig)
5487
ls = LScene(fig[1, 1]; show_axis=false)
55-
robotsketch!(ls, cache; linewidth=3)
88+
robotsketch!(ls, cache; linewidth=3, scale=0.05)
5689

5790
frameIDs = get_compiled_frameID.((cache[],), frames(cache[]))
5891
tfs = map(cache) do cache
@@ -65,14 +98,18 @@ fs = map(cache) do cache
6598
fs = map(id -> VMRobotControl.get_frame_force(cache, id), frameIDs)
6699
end
67100
f_scale = map(fs) do fs
68-
0.1/norm(maximum(fs))
101+
0.2/maximum(norm(fs))
69102
end
70103
τs = map(cache) do cache
71104
map(id -> VMRobotControl.get_frame_torque(cache, id), frameIDs)
72105
end
106+
τ_scale = map(τs) do τs
107+
0.4/maximum(norm(τs))
108+
end
109+
73110

74-
arrows!(ls, positions, fs; lengthscale=f_scale, arrowsize=0.1, color=:red, )
75-
arrows!(ls, positions, τs; lengthscale=f_scale, arrowsize=0.1, color=:red, )
111+
arrows!(ls, positions, fs; lengthscale=f_scale, arrowsize=map(f->*(f, 50), f_scale), color=:red, )
112+
arrows!(ls, positions, τs; lengthscale=τ_scale, arrowsize=0.04, color=:blue, )
76113

77114

78115
end

src/components/components.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,8 @@ Add opspace force `f` to the cache for coordinate `c`.
359359
@inline function _add_opspace_force!(bundle::CacheBundle, c::C) where C<:ComponentData
360360
@assert hasfield(C, :coord) "Component '$c' does not have a field 'coord'. If this is a valid component, please implement `_add_opspace_force!` for it."
361361
f::SVector = opspace_force(bundle, c)
362-
coord_id = c.coord
363-
coord = bundle[coord_id]
364-
f_cache_view(bundle, coord) .-= f # f is negated, due to the definition of bias forces. e.g. If a spring opspace force is Kz
365-
nothing
362+
f_view = f_cache_view(bundle, bundle[c.coord])
363+
f_view .-= f # f is negated, due to the definition of bias forces. e.g. If a spring opspace force is Kz
366364
end
367365

368366
function _opspace_force(cache::CacheBundle, c::ComponentData)

src/coordinates/coordinate_implementations.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,9 @@ function __propagate_opspace_force!(cache::CacheBundle, mc::CMC{<:FrameAngularVe
493493
c = mc.coord_data
494494
f_view = f_cache_view(cache, mc)
495495
@assert length(f_view) == 3
496-
f = f_cache_view(cache, mc)[SVector(1, 2, 3)] # Get the force applied to the coord as an SVector
496+
f = f_cache_view(cache, mc)[SVector(1, 2, 3)] # Get the applied torque as an SVector
497497
fID = c.frameID
498-
get_frame_torques(cache)[fID] += f
498+
get_frame_torques(cache)[fID] += f # Apply torque to the frame
499499
nothing
500500
end
501501

src/interface.jl

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -481,69 +481,94 @@ end
481481
===============================================================================#
482482
function inverse_dynamics! end
483483

484-
function _inverse_dynamics!(bundle::MechRNEBundle)
485-
# Forward pass
484+
function _inverse_dynamics!(bundle::MechRNEBundle)
485+
_inverse_dynamics_forward_pass!(bundle)
486+
_inverse_dynamics_zero!(bundle)
487+
_inverse_dynamics_backward_pass_a!(bundle)
488+
_inverse_dynamics_backward_pass_b!(bundle)
489+
_inverse_dynamics_backward_pass_c!(bundle)
490+
_inverse_dynamics_backward_pass_d!(bundle)
491+
nothing
492+
end
493+
494+
function _inverse_dynamics_set_inputs!(bundle, t, q, q̇, q̈, gravity)!
495+
t_cache, q_cache, q̇_cache, q̈_cache, g_cache, u_cache, = get_t(bundle), get_q(bundle), get_q̇(bundle), get_q̈(bundle), get_gravity(bundle), get_u(bundle)
496+
497+
t_cache[] = t
498+
copyto!(q_cache, q)
499+
copyto!(q̇_cache, q̇)
500+
copyto!(q̈_cache, q̈)
501+
g_cache[] = gravity
502+
end
503+
504+
function _inverse_dynamics_forward_pass!(bundle)
486505
_acceleration_kinematics!(bundle)
487506
m, cache = bundle.mechanism, bundle.cache
488507
compute_in_coordinate_order(coordinates(m)) do coord
489508
__configuration!(bundle, coord)
490509
__velocity!(bundle, coord)
491510
__acceleration!(bundle, coord)
492511
end
493-
494-
#
495-
u = get_u(bundle)
496-
frame_forces = get_frame_forces(bundle)
497-
frame_torques = get_frame_torques(bundle)
512+
nothing
513+
end
498514

515+
function _inverse_dynamics_zero!(bundle)
516+
u = get_u(bundle)
517+
frame_forces, frame_torques = get_frame_forces(bundle), get_frame_torques(bundle)
499518
# Zero all coordinate/frame forces and torques.
500519
fill!(bundle.cache.coord_cache.f, zero(eltype(bundle.cache.coord_cache.f)))
501520
fill!(frame_forces, zero(eltype(frame_forces)))
502521
fill!(frame_torques, zero(eltype(frame_torques)))
503522
fill!(u, zero(eltype(u)))
523+
nothing
524+
end
504525

505-
# Backward pass
506-
foreach(get_force_components(m)) do component # add forces for each component to coordinates
526+
function _inverse_dynamics_backward_pass_a!(bundle)
527+
# Add forces for each component to coordinates
528+
foreach(get_force_components(bundle.mechanism)) do component
507529
_add_opspace_force!(bundle, component)
508-
# @show f_cache_view(bundle, m[component.coord])
509530
end
510-
# @show frame_forces
511-
# println("Backward pass part 1")
512-
compute_in_reverse_coordinate_order(coordinates(m)) do coord # propagate forces through coordinates
531+
# Now the force for each coordinate with a component *directly* attached to it is set
532+
nothing
533+
end
534+
535+
function _inverse_dynamics_backward_pass_b!(bundle)
536+
# Propagate forces through coordinates.
537+
compute_in_reverse_coordinate_order(coordinates(bundle.mechanism)) do coord
513538
__propagate_opspace_force!(bundle, coord)
514-
# @show frame_forces
515539
end
516-
# println("Backward pass part 2")
517-
for (parent, child) in Iterators.reverse(m.rbtree.walk) # propagate forces through frames/joints
540+
# Now, *frame* forces/torques contain the force/torque applied to each frame by the components
541+
nothing
542+
end
543+
544+
function _inverse_dynamics_backward_pass_c!(bundle)
545+
frame_forces, frame_torques = get_frame_forces(bundle), get_frame_torques(bundle)
546+
# propagate forces through frames/joints
547+
for (parent, child) in Iterators.reverse(bundle.mechanism.rbtree.walk) # TODO check this order is correct... might not work
518548
frame_forces[parent] += frame_forces[child]
519-
δ = origin(get_transform(cache, CompiledFrameID(parent))) - origin(get_transform(cache, CompiledFrameID(child)))
520-
frame_torques[parent] += frame_torques[child] - cross(δ, frame_forces[child])
521-
# @show frame_forces
549+
δ = origin(get_transform(bundle, CompiledFrameID(child))) - origin(get_transform(bundle, CompiledFrameID(parent)))
550+
frame_torques[parent] += frame_torques[child] + cross(δ, frame_forces[child])
522551
end
552+
end
553+
554+
function _inverse_dynamics_backward_pass_d!(bundle)
555+
u = get_u(bundle)
556+
frame_forces, frame_torques = get_frame_forces(bundle), get_frame_torques(bundle)
523557
# Project forces to joints
524-
foreach(joints(m)) do joint
558+
foreach(joints(bundle.mechanism)) do joint
525559
length(q_idxs(joint)) == 0 && return nothing
526560
p, c = joint.parentFrameID, joint.childFrameID
527-
tf_p, tf_c = get_transform(cache, p), get_transform(cache, c)
561+
tf_p, tf_c = get_transform(bundle, p), get_transform(bundle, c)
528562
f_p, τ_p, f_c, τ_c = frame_forces[p], frame_torques[p], frame_forces[c], frame_torques[c]
529563
u[q_idxs(joint)] .= _project_forces_to_jointspace(joint.jointData, tf_p, tf_c, f_p, f_c, τ_p, τ_c)
530564
nothing
531565
end
532-
nothing
533566
end
534567

535568
function inverse_dynamics!(bundle::MechRNEBundle, t, q, q̇, q̈, gravity)
536-
t_cache, q_cache, q̇_cache, q̈_cache, g_cache, u_cache, =
537-
get_t(bundle), get_q(bundle), get_q̇(bundle), get_q̈(bundle), get_gravity(bundle), get_u(bundle)
538-
539-
t_cache[] = t
540-
copyto!(q_cache, q)
541-
copyto!(q̇_cache, q̇)
542-
copyto!(q̈_cache, q̈)
543-
g_cache[] = gravity
569+
_inverse_dynamics_set_inputs!(bundle, t, q, q̇, q̈, gravity)
544570
_inverse_dynamics!(bundle)
545-
546-
u_cache
571+
get_u(bundle)
547572
end
548573

549574

0 commit comments

Comments
 (0)