1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
import pytest
import math
import numpy as np
import dartpy as dart
def read_world(uri: dart.common.Uri):
options = dart.utils.SdfParser.Options()
options.mDefaultRootJointType = dart.utils.SdfParser.RootJointType.FIXED
world = dart.utils.SdfParser.readWorld(uri, options)
for i in range(world.getNumSkeletons()):
skel = world.getSkeleton(i)
for j in range(skel.getNumJoints()):
joint = skel.getJoint(j)
joint.setLimitEnforcement(True)
return world
def test_static():
# Load world
world = read_world("dart://sample/sdf/test/force_torque_test.world")
assert world is not None
assert world.getNumSkeletons() == 1
# Check if the world is correctly loaded
model_1 = world.getSkeleton(0)
assert model_1 is not None
assert model_1.getName() == "model_1"
assert model_1.getNumBodyNodes() == 2
assert model_1.getNumJoints() == 2
world.setGravity([0, 0, -50])
# Simulate 1 step
world.step()
t = world.getTime()
# Get time step size
dt = world.getTimeStep()
assert dt > 0
assert t == pytest.approx(dt)
# Get joint and get force torque
link_1 = model_1.getBodyNode("link_1")
assert link_1 is not None
link_2 = model_1.getBodyNode("link_2")
assert link_2 is not None
joint_01 = model_1.getJoint("joint_01")
assert joint_01 is not None
joint_12 = model_1.getJoint("joint_12")
assert joint_12 is not None
tf = dart.math.Isometry3()
# Run 10 steps
for _ in range(10):
world.step()
#----------------------
# Test joint_01 wrench
#----------------------
# Reference adjustment for the difference of the joint frame conventions
# between Gazebo and DART
tf.set_identity()
tf.set_translation(joint_01.getTransformFromParentBodyNode().translation())
parent_frame01 = dart.dynamics.SimpleFrame(dart.dynamics.Frame.World(), "parent_frame01", tf)
tf.set_identity()
tf.set_translation(joint_01.getTransformFromChildBodyNode().translation())
child_frame01 = dart.dynamics.SimpleFrame(link_1, "child_frame01", tf)
parent_f01 = joint_01.getWrenchToParentBodyNode(parent_frame01)
assert (parent_f01 == [0, 0, 0, 0, 0, 1000]).all()
child_f01 = joint_01.getWrenchToChildBodyNode(child_frame01)
assert (child_f01 == -parent_f01).all()
#----------------------
# Test joint_12 wrench
#----------------------
# Reference adjustment for the difference of the joint frame conventions
# between Gazebo and DART
tf.set_identity()
tf.set_translation(joint_12.getTransformFromParentBodyNode().translation())
parent_frame12 = dart.dynamics.SimpleFrame(link_1, "parent_frame12", tf)
tf.set_identity()
tf.set_translation(joint_12.getTransformFromChildBodyNode().translation())
child_frame12 = dart.dynamics.SimpleFrame(link_2, "child_frame12", tf)
parent_f12 = joint_12.getWrenchToParentBodyNode(parent_frame12)
assert (parent_f12 == [0, 0, 0, 0, 0, 500]).all()
child_f12 = joint_12.getWrenchToChildBodyNode(child_frame12)
assert (child_f12 == -parent_f12).all()
def test_force_torque_at_joint_limits():
# Load world
world = read_world("dart://sample/sdf/test/force_torque_test.world")
assert world is not None
assert world.getNumSkeletons() == 1
# Check if the world is correctly loaded
model_1 = world.getSkeleton(0)
assert model_1 is not None
assert model_1.getName() == "model_1"
assert model_1.getNumBodyNodes() == 2
assert model_1.getNumJoints() == 2
world.setGravity([0, 0, -50])
# Simulate 1 step
world.step()
t = world.getTime()
# Get time step size
dt = world.getTimeStep()
assert dt > 0
assert t == pytest.approx(dt)
# Get joint and get force torque
link_1 = model_1.getBodyNode("link_1")
assert link_1 is not None
link_2 = model_1.getBodyNode("link_2")
assert link_2 is not None
joint_01 = model_1.getJoint("joint_01")
assert joint_01 is not None
joint_12 = model_1.getJoint("joint_12")
assert joint_12 is not None
# Change gravity so that the top link topples over, then remeasure
world.setGravity([-30, 10, -50])
# Wait for dynamics to be stabilized
for i in range(2000):
world.step()
tf = dart.math.Isometry3()
# Run 5 steps
for _ in range(5):
world.step()
#----------------------
# Test joint_01 wrench
#----------------------
# Reference adjustment for the difference of the joint frame conventions
# between Gazebo and DART
tf.set_identity()
tf.set_translation(joint_01.getTransformFromParentBodyNode().translation())
parent_frame01 = dart.dynamics.SimpleFrame(dart.dynamics.Frame.World(), "parent_frame01", tf)
tf.set_identity()
tf.set_translation(joint_01.getTransformFromChildBodyNode().translation())
child_frame01 = dart.dynamics.SimpleFrame(link_1, "child_frame01", tf)
parent_f01 = joint_01.getWrenchToParentBodyNode(parent_frame01)
assert np.isclose(parent_f01, [750, 0, -450, 600, -200, 1000], rtol=0.1, atol=4.5).all()
child_f01 = joint_01.getWrenchToChildBodyNode(child_frame01)
assert np.isclose(child_f01, [-750, -450, 0, -600, 1000, 200], rtol=0.1, atol=4.5).all()
#----------------------
# Test joint_12 wrench
#----------------------
# Reference adjustment for the difference of the joint frame conventions
# between Gazebo and DART
tf.set_identity()
tf.set_translation(joint_12.getTransformFromParentBodyNode().translation())
parent_frame12 = dart.dynamics.SimpleFrame(link_1, "parent_frame12", tf)
tf.set_identity()
tf.set_translation(joint_12.getTransformFromChildBodyNode().translation())
child_frame12 = dart.dynamics.SimpleFrame(link_2, "child_frame12", tf)
parent_f12 = joint_12.getWrenchToParentBodyNode(parent_frame12)
assert np.isclose(parent_f12, [250, 150, 0, 300, -500, -100], rtol=0.1, atol=0.1).all()
child_f12 = joint_12.getWrenchToChildBodyNode(child_frame12)
assert np.isclose(child_f12, [-250, -150, 0, -300, 500, 100], rtol=0.1, atol=0.1).all()
def test_force_torque_at_joint_limits_with_external_forces():
# Load world
world = read_world("dart://sample/sdf/test/force_torque_test2.world")
assert world is not None
assert world.getNumSkeletons() == 1
# Check if the world is correctly loaded
model_1 = world.getSkeleton(0)
assert model_1 is not None
assert model_1.getName() == "boxes"
assert model_1.getNumBodyNodes() == 3
assert model_1.getNumJoints() == 3
assert model_1.getNumDofs() == 2
# The first joint is fixed joint
assert model_1.getRootJoint().getType() == dart.dynamics.WeldJoint.getStaticType()
world.setGravity([0, 0, -50])
# Simulate 1 step
world.step()
t = world.getTime()
# Get time step size
dt = world.getTimeStep()
assert dt > 0
assert t == pytest.approx(dt)
# Get joint and get force torque
link_1 = model_1.getBodyNode("link1")
assert link_1 is not None
link_2 = model_1.getBodyNode("link2")
assert link_2 is not None
link_3 = model_1.getBodyNode("link3")
assert link_3 is not None
joint_12 = model_1.getJoint("joint1")
assert joint_12 is not None
joint_23 = model_1.getJoint("joint2")
assert joint_23 is not None
tf = dart.math.Isometry3()
# Run 45005 steps
kp1 = 5e+4
kp2 = 1e+4
target1 = 0
target2 = -0.25 * math.pi
steps = 4500
for _ in range(steps):
# PD control
j1_state = joint_12.getPosition(0)
j2_state = joint_23.getPosition(0)
p1_error = target1 - j1_state
p2_error = target2 - j2_state
effort1 = kp1 * p1_error
effort2 = kp2 * p2_error
joint_12.setForce(0, effort1)
joint_23.setForce(0, effort2)
world.step()
assert joint_12.getPosition(0) == pytest.approx(target1, abs=1e-1)
assert joint_23.getPosition(0) == pytest.approx(target2, abs=1e-1)
tol = 2
#----------------------
# Test joint_12 wrench
#----------------------
# Reference adjustment for the difference of the joint frame conventions
# between Gazebo and DART
tf.set_identity()
tf.set_translation(joint_12.getTransformFromParentBodyNode().translation())
parent_frame01 = dart.dynamics.SimpleFrame(link_1, "parent_frame01", tf)
tf.set_identity()
tf.set_translation(joint_12.getTransformFromChildBodyNode().translation())
child_frame01 = dart.dynamics.SimpleFrame(link_2, "child_frame01", tf)
parent_f01 = joint_12.getWrenchToParentBodyNode(parent_frame01)
assert np.isclose(parent_f01, [25, -175, 0, 0, 0, 300], rtol=0.01, atol=tol).all()
child_f01 = joint_12.getWrenchToChildBodyNode(child_frame01)
assert np.isclose(child_f01, [-25, 175, 0, 0, 0, -300], rtol=0.01, atol=tol).all()
#----------------------
# Test joint_23 wrench
#----------------------
# Reference adjustment for the difference of the joint frame conventions
# between Gazebo and DART
tf.set_identity()
tf.set_translation(joint_23.getTransformFromParentBodyNode().translation())
parent_frame12 = dart.dynamics.SimpleFrame(link_2, "parent_frame12", tf)
tf.set_identity()
tf.set_translation(joint_23.getTransformFromChildBodyNode().translation())
child_frame12 = dart.dynamics.SimpleFrame(link_3, "child_frame12", tf)
parent_f12 = joint_23.getWrenchToParentBodyNode(parent_frame12)
assert np.isclose(parent_f12, [25, 0, 0, 0, 0, 50], rtol=0.01, atol=tol).all()
child_f12 = joint_23.getWrenchToChildBodyNode(child_frame12)
assert np.isclose(child_f12, [-17.678, 0, 17.679, -35.355, 0, -35.355], rtol=0.01, atol=tol).all()
if __name__ == "__main__":
pytest.main()
|