Skip to content

Commit 8cbfa84

Browse files
committed
Merge pull request #286 from personalrobotics/bugfix/postprocess_reuse
Reuse postprocessing environments in a safer way.
2 parents 14d5db5 + 1a3c41a commit 8cbfa84

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/prpy/base/robot.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2828
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2929
# POSSIBILITY OF SUCH DAMAGE.
30-
30+
import collections
3131
import functools, logging, openravepy, numpy
3232
from .. import bind, named_config, exceptions, util
3333
from ..clone import Clone, Cloned
@@ -42,6 +42,12 @@
4242

4343

4444
class Robot(openravepy.Robot):
45+
46+
_postprocess_envs = collections.defaultdict(openravepy.Environment)
47+
"""
48+
Mapping from robot environments to plan postprocessing environments.
49+
"""
50+
4551
def __init__(self, robot_name=None):
4652
self.actions = None
4753
self.planner = None
@@ -165,6 +171,7 @@ def CloneBindings(self, parent):
165171
self.base_manipulation = openravepy.interfaces.BaseManipulation(self)
166172
self.task_manipulation = openravepy.interfaces.TaskManipulation(self)
167173

174+
168175
def AttachController(self, name, args, dof_indices, affine_dofs, simulated):
169176
"""
170177
Create and attach a controller to a subset of this robot's DOFs. If
@@ -286,13 +293,15 @@ def PostProcessPath(self, path,
286293
logger.debug('Detected "%s" tag on trajectory: Setting smooth'
287294
' = True', Tags.SMOOTH)
288295

289-
# Lazily create an environment for post-processing.
290-
# This is faster than creating a new environment for every operation.
291-
if not hasattr(self, '_postprocess_env'):
292-
self._postprocess_env = openravepy.Environment()
296+
# Since we don't want to endlessly create postprocessing environments,
297+
# we maintain a map that uniquely associates each OpenRAVE environment
298+
# with a given postprocessing environment. This way, if we re-clone
299+
# into a previously used environment, we will not create a new one.
300+
postprocess_env = Robot._postprocess_envs[
301+
openravepy.RaveGetEnvironmentId(self.GetEnv())]
293302

294303
with Clone(self.GetEnv(),
295-
clone_env=self._postprocess_env) as cloned_env:
304+
clone_env=postprocess_env) as cloned_env:
296305
cloned_robot = cloned_env.Cloned(self)
297306

298307
# Planners only operate on the active DOFs. We'll set any DOFs

0 commit comments

Comments
 (0)