Skip to content

Commit 603d283

Browse files
committed
Reverted set_joint_position(s) to having disable_dynamics flag.
1 parent 1cea714 commit 603d283

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

examples/example_panda_ik.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
input('Press key to run solve_ik_via_sampling...')
3333
new_joint_pos = agent.solve_ik_via_sampling([x, y, z - 0.4], quaternion=q)[0]
3434

35-
agent.set_joint_positions(new_joint_pos)
35+
# Because the arm is in Forxe/Torque mode, we need to temporarily disable
36+
# dynamics in order to instantaneously move joints.
37+
agent.set_joint_positions(new_joint_pos, disable_dynamics=True)
3638
input('Press any key to finish...')
3739

3840
pr.stop()

pyrep/backend/utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import io
33
import sys
44
from contextlib import contextmanager
5+
from threading import Lock
56
from typing import List, Tuple
67
import pyrep
78
from pyrep.backend import sim
@@ -16,6 +17,8 @@
1617
from pyrep.objects.camera import Camera
1718
from pyrep.objects.octree import Octree
1819

20+
step_lock = Lock()
21+
1922

2023
def to_type(handle: int) -> Object:
2124
"""Converts an object handle to the correct sub-type.

pyrep/objects/joint.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Tuple, List, Union
2-
from pyrep.backend import sim
2+
from pyrep.backend import sim, utils
33
from pyrep.const import JointType, JointMode
44
from pyrep.objects.object import Object, object_type_to_class
55
from pyrep.const import ObjectType
@@ -14,7 +14,6 @@ class Joint(Object):
1414

1515
def __init__(self, name_or_handle: Union[str, int]):
1616
super().__init__(name_or_handle)
17-
self._config_tree = self.get_configuration_tree()
1817

1918
def _get_requested_type(self) -> ObjectType:
2019
return ObjectType.JOINT
@@ -37,16 +36,41 @@ def get_joint_position(self) -> float:
3736
"""
3837
return sim.simGetJointPosition(self._handle)
3938

40-
def set_joint_position(self, position: float) -> None:
39+
def set_joint_position(self, position: float,
40+
disable_dynamics: bool = False) -> None:
4141
"""Sets the intrinsic position of the joint.
4242
43+
:param disable_dynamics: If True, then the position can be set even
44+
when the joint mode is in Force mode. It will disable dynamics,
45+
move the joint, and then re-enable dynamics.
46+
4347
:param positions: A list of positions of the joints (angular or linear
4448
values depending on the joint type).
4549
"""
46-
sim.simSetConfigurationTree(self._config_tree)
50+
if not disable_dynamics:
51+
sim.simSetJointPosition(self._handle, position)
52+
return
53+
54+
is_model = self.is_model()
55+
if not is_model:
56+
self.set_model(True)
57+
58+
prior = sim.simGetModelProperty(self.get_handle())
59+
p = prior | sim.sim_modelproperty_not_dynamic
60+
# Disable the dynamics
61+
sim.simSetModelProperty(self._handle, p)
62+
with utils.step_lock:
63+
sim.simExtStep(True) # Have to step for changes to take effect
64+
4765
sim.simSetJointPosition(self._handle, position)
4866
self.set_joint_target_position(position)
4967

68+
# Re-enable the dynamics
69+
sim.simSetModelProperty(self._handle, prior)
70+
self.set_model(is_model)
71+
with utils.step_lock:
72+
sim.simExtStep(True) # Have to step for changes to take effect
73+
5074
def get_joint_target_position(self) -> float:
5175
"""Retrieves the target position of a joint.
5276

pyrep/pyrep.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import sys
1212
import time
1313
import threading
14-
from threading import Lock
1514
from typing import Tuple, List
1615
import warnings
1716

@@ -57,7 +56,6 @@ def __init__(self):
5756

5857
self._ui_thread = None
5958
self._responsive_ui_thread = None
60-
self._step_lock = Lock()
6159

6260
self._init_thread_id = None
6361
self._shutting_down = False
@@ -85,10 +83,11 @@ def _run_ui_thread(self, scene_file: str, headless: bool,
8583

8684
def _run_responsive_ui_thread(self) -> None:
8785
while True:
88-
with self._step_lock:
89-
if self._shutting_down or sim.simExtGetExitRequest():
90-
break
91-
sim.simExtStep(False)
86+
if not self.running:
87+
with utils.step_lock:
88+
if self._shutting_down or sim.simExtGetExitRequest():
89+
break
90+
sim.simExtStep(False)
9291
time.sleep(0.01)
9392
# If the exit request was from the UI, then call shutdown, otherwise
9493
# shutdown caused this thread to terminate.
@@ -221,7 +220,7 @@ def step(self) -> None:
221220
If the physics simulation is not running, then this will only update
222221
the UI.
223222
"""
224-
with self._step_lock:
223+
with utils.step_lock:
225224
sim.simExtStep()
226225

227226
def step_ui(self) -> None:
@@ -231,7 +230,7 @@ def step_ui(self) -> None:
231230
simulation is running.
232231
This is only applicable when PyRep was launched without a responsive UI.
233232
"""
234-
with self._step_lock:
233+
with utils.step_lock:
235234
sim.simExtStep(False)
236235

237236
def set_simulation_timestep(self, dt: float) -> None:

pyrep/robots/robot_component.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pyrep.objects.shape import Shape
44

5-
from pyrep.backend import sim
5+
from pyrep.backend import sim, utils
66
from pyrep.const import JointType
77
from pyrep.objects.object import Object
88
from pyrep.objects.joint import Joint
@@ -24,7 +24,6 @@ def __init__(self, count: int, name: str, joint_names: List[str],
2424
self.joints = [Joint(jname + suffix)
2525
for jname in joint_names]
2626
self._joint_handles = [j.get_handle() for j in self.joints]
27-
self._config_tree = self.get_configuration_tree()
2827

2928
def copy(self) -> 'RobotComponent':
3029
"""Copy and pastes the arm in the scene.
@@ -76,21 +75,47 @@ def get_joint_positions(self) -> List[float]:
7675
"""
7776
return [j.get_joint_position() for j in self.joints]
7877

79-
def set_joint_positions(self, positions: List[float]) -> None:
78+
def set_joint_positions(self, positions: List[float],
79+
disable_dynamics: bool = False) -> None:
8080
"""Sets the intrinsic position of the joints.
8181
8282
See :py:meth:`Joint.set_joint_position` for more information.
8383
84+
:param disable_dynamics: If True, then the position can be set even
85+
when the joint mode is in Force mode. It will disable dynamics,
86+
move the joint, and then re-enable dynamics.
87+
8488
:param positions: A list of positions of the joints (angular or linear
8589
values depending on the joint type).
8690
"""
8791
self._assert_len(positions)
88-
sim.simSetConfigurationTree(self._config_tree)
92+
if not disable_dynamics:
93+
[sim.simSetJointPosition(jh, p) # type: ignore
94+
for jh, p in zip(self._joint_handles, positions)]
95+
return
96+
97+
is_model = self.is_model()
98+
if not is_model:
99+
self.set_model(True)
100+
101+
prior = sim.simGetModelProperty(self.get_handle())
102+
p = prior | sim.sim_modelproperty_not_dynamic
103+
# Disable the dynamics
104+
sim.simSetModelProperty(self._handle, p)
105+
with utils.step_lock:
106+
sim.simExtStep(True) # Have to step for changes to take effect
107+
89108
[sim.simSetJointPosition(jh, p) # type: ignore
90109
for jh, p in zip(self._joint_handles, positions)]
91-
[j.set_joint_target_position(p)
110+
[j.set_joint_target_position(p) # type: ignore
92111
for j, p in zip(self.joints, positions)]
93112

113+
# Re-enable the dynamics
114+
sim.simSetModelProperty(self._handle, prior)
115+
self.set_model(is_model)
116+
with utils.step_lock:
117+
sim.simExtStep(True) # Have to step for changes to take effect
118+
94119
def get_joint_target_positions(self) -> List[float]:
95120
"""Retrieves the target positions of the joints.
96121

0 commit comments

Comments
 (0)