Skip to content

Commit

Permalink
Merge pull request #148 from tkoolen/constraint-bias
Browse files Browse the repository at this point in the history
Add constraint_bias
  • Loading branch information
tkoolen authored Jan 31, 2017
2 parents fe47724 + b89abd6 commit aa5c651
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/RigidBodyDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export
Twist,
GeometricJacobian,
Wrench,
WrenchMatrix,
Momentum,
MomentumMatrix,
SpatialAcceleration,
Expand Down Expand Up @@ -94,6 +95,7 @@ export
motion_subspace,
constraint_wrench_subspace,
bias_acceleration,
constraint_bias!,
spatial_inertia,
crb_inertia,
setdirty!,
Expand Down
8 changes: 8 additions & 0 deletions src/joint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ function bias_acceleration{M, X}(joint::Joint{M}, q::AbstractVector{X}, v::Abstr
@rtti_dispatch (QuaternionFloating{M}, Revolute{M}, Prismatic{M}, Fixed{M}) _bias_acceleration(joint.jointType, joint.frameAfter, joint.frameBefore, q, v)
end

function constraint_bias!{M}(joint::Joint{M}, bias::AbstractVector, jointTwist::Twist)
@framecheck jointTwist.body joint.frameAfter
@framecheck jointTwist.base joint.frameBefore
@framecheck jointTwist.frame joint.frameAfter
@boundscheck length(bias) == 6 - num_velocities(joint) || error("wrong size")
@rtti_dispatch (QuaternionFloating{M}, Revolute{M}, Prismatic{M}, Fixed{M}) _constraint_bias!(joint.jointType, bias, jointTwist)
end

function configuration_derivative_to_velocity!{M}(joint::Joint{M}, v::AbstractVector, q::AbstractVector, q̇::AbstractVector)::Void
@boundscheck check_num_velocities(joint, v)
@boundscheck check_num_positions(joint, q)
Expand Down
11 changes: 11 additions & 0 deletions src/joint_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ function _bias_acceleration{T<:Number, X<:Number}(
zero(SpatialAcceleration{S}, frameAfter, frameBefore, frameAfter)
end

function _constraint_bias!(jt::QuaternionFloating, bias::AbstractVector, jointTwist::Twist)
bias[:] = 0
nothing
end

function _configuration_derivative_to_velocity!(jt::QuaternionFloating, v::AbstractVector, q::AbstractVector, q̇::AbstractVector)
quat = rotation(jt, q)
@inbounds quatdot = SVector(q̇[1], q̇[2], q̇[3], q̇[4])
Expand Down Expand Up @@ -202,6 +207,11 @@ function _bias_acceleration{T<:Number, X<:Number}(
zero(SpatialAcceleration{promote_type(T, X)}, frameAfter, frameBefore, frameAfter)
end

function _constraint_bias!(jt::OneDegreeOfFreedomFixedAxis, bias::AbstractVector, jointTwist::Twist)
bias[:] = 0
nothing
end

function _configuration_derivative_to_velocity!(::OneDegreeOfFreedomFixedAxis, v::AbstractVector, q::AbstractVector, q̇::AbstractVector)
copy!(v, q̇)
nothing
Expand Down Expand Up @@ -351,6 +361,7 @@ function _bias_acceleration{T<:Number, X<:Number}(
zero(SpatialAcceleration{promote_type(T, X)}, frameAfter, frameBefore, frameAfter)
end

_constraint_bias!(jt::Fixed, bias::AbstractVector, jointTwist::Twist) = (bias[:] = 0; nothing)
_configuration_derivative_to_velocity!(::Fixed, v::AbstractVector, q::AbstractVector, q̇::AbstractVector) = nothing
_velocity_to_configuration_derivative!(::Fixed, q̇::AbstractVector, q::AbstractVector, v::AbstractVector) = nothing
_joint_torque!(jt::Fixed, τ::AbstractVector, q::AbstractVector, joint_wrench::Wrench) = nothing
20 changes: 19 additions & 1 deletion test/test_mechanism_algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,29 @@
for joint in joints(mechanism)
qjoint = configuration(x, joint)
S = motion_subspace(joint, qjoint)
T = constraint_wrench_subspace(joint, qjoint)::RigidBodyDynamics.WrenchSubspace{Float64}
T = constraint_wrench_subspace(joint, qjoint)::RigidBodyDynamics.WrenchSubspace{Float64} # TODO
@test isapprox(T.angular' * S.angular + T.linear' * S.linear, zeros(6 - num_velocities(joint), num_velocities(joint)); atol = 1e-14)
end
end

@testset "constraint_bias" begin
for joint in joints(mechanism)
qjoint = configuration(x, joint)
vjoint = velocity(x, joint)
q̇joint = similar(qjoint)
velocity_to_configuration_derivative!(joint, q̇joint, qjoint, vjoint)
qjointAutodiff = create_autodiff(qjoint, q̇joint)
TAutodiff = constraint_wrench_subspace(joint, qjointAutodiff)#::RigidBodyDynamics.WrenchSubspace{eltype(qjointAutodiff)} # TODO
angular = map(x -> ForwardDiff.partials(x, 1), TAutodiff.angular)
linear = map(x -> ForwardDiff.partials(x, 1), TAutodiff.linear)
= WrenchMatrix(joint.frameAfter, angular, linear)
jointTwist = transform(x, relative_twist(x, joint.frameAfter, joint.frameBefore), joint.frameAfter)
bias = fill(NaN, 6 - num_velocities(joint))
constraint_bias!(joint, bias, jointTwist)
@test isapprox(Ṫ.angular' * jointTwist.angular +.linear' * jointTwist.linear, bias; atol = 1e-14)
end
end

@testset "relative_acceleration" begin
for body in bodies(mechanism)
for base in bodies(mechanism)
Expand Down

0 comments on commit aa5c651

Please sign in to comment.