Skip to content

Commit 2ba3a79

Browse files
committed
Moved crazy CBiRRT serialization into CBiRRT.
Nothing-- I repeat NOTHING-- other than CBiRRT should use this serialization of TSRs. Everything should use JSON, YAML, or a dict.
1 parent 01b169d commit 2ba3a79

File tree

2 files changed

+82
-82
lines changed

2 files changed

+82
-82
lines changed

src/prpy/planning/cbirrt.py

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2929
# POSSIBILITY OF SUCH DAMAGE.
3030

31-
import copy, logging, numpy, openravepy, os, tempfile
31+
import numpy, openravepy
3232
from ..util import SetTrajectoryTags
3333
from base import (BasePlanner, PlanningError, UnsupportedPlanningError,
3434
PlanningMethod, Tags)
3535
import prpy.kin, prpy.tsr
3636

37+
3738
class CBiRRTPlanner(BasePlanner):
3839
def __init__(self):
3940
super(CBiRRTPlanner, self).__init__()
@@ -109,7 +110,7 @@ def PlanToEndEffectorOffset(self, robot, direction, distance,
109110
# 'object frame w' is at ee, z pointed along direction to move
110111
H_world_w = prpy.kin.H_from_op_diff(H_world_ee[0:3,3], direction)
111112
H_w_ee = numpy.dot(prpy.kin.invert_H(H_world_w), H_world_ee)
112-
113+
113114
# Serialize TSR string (goal)
114115
Hw_end = numpy.eye(4)
115116
Hw_end[2,3] = distance
@@ -135,7 +136,7 @@ def PlanToEndEffectorOffset(self, robot, direction, distance,
135136
Bw = Bw,
136137
manip = robot.GetActiveManipulatorIndex())
137138
traj_tsr_chain = prpy.tsr.tsr.TSRChain(constrain=True, TSRs=[trajtsr])
138-
139+
139140
return self.Plan(robot,
140141
psample=0.1,
141142
tsr_chains=[goal_tsr_chain, traj_tsr_chain],
@@ -227,7 +228,7 @@ def Plan(self, robot, smoothingitrs=None, timelimit=None, allowlimadj=0,
227228
robot.GetActiveDOF(), len(start_config)
228229
)
229230
)
230-
231+
231232
args += ['jointstarts'] + self.serialize_dof_values(start_config)
232233

233234
if jointgoals is not None:
@@ -239,12 +240,12 @@ def Plan(self, robot, smoothingitrs=None, timelimit=None, allowlimadj=0,
239240
robot.GetActiveDOF(), len(goal_config)
240241
)
241242
)
242-
243+
243244
args += ['jointgoals'] + self.serialize_dof_values(goal_config)
244245

245246
if tsr_chains is not None:
246247
for tsr_chain in tsr_chains:
247-
args += [ 'TSRChain', tsr_chain.serialize() ]
248+
args += ['TSRChain', _SerializeTSRChain(tsr_chain)]
248249

249250
# FIXME: Why can't we write to anything other than cmovetraj.txt or
250251
# /tmp/cmovetraj.txt with CBiRRT?
@@ -258,7 +259,7 @@ def Plan(self, robot, smoothingitrs=None, timelimit=None, allowlimadj=0,
258259

259260
if not response.strip().startswith('1'):
260261
raise PlanningError('Unknown error: ' + response)
261-
262+
262263
# Construct the output trajectory.
263264
with open(traj_path, 'rb') as traj_file:
264265
traj_xml = traj_file.read()
@@ -290,4 +291,75 @@ def serialize_dof_values(dof_values):
290291
return [ str(len(dof_values)),
291292
' '.join([ str(x) for x in dof_values]) ]
292293

293-
294+
295+
def SerializeTransform12Col(tm, format='%.5f'):
296+
return ' '.join([(format % (i,)) for i in tm[0:3, :].T.reshape(12)])
297+
298+
299+
def SerializeArray(a, format='%.5f'):
300+
return ' '.join([(format % (i,)) for i in a.reshape(-1)])
301+
302+
303+
def SerializeTSR(self):
304+
"""
305+
Function for Serializing TSRs for CBIRRT.
306+
307+
SerializeTSR(manipindex,bodyandlink,T0_w,Tw_e,Bw)
308+
309+
Input:
310+
manipindex (int): the 0-indexed index of the robot's manipulator
311+
bodyandlink (str): body and link which is used as the 0 frame. Format
312+
'body_name link_name'. For world frame, specify 'NULL'
313+
T0_w (double 4x4): transform matrix of the TSR's reference frame relative
314+
to the 0 frame
315+
Tw_e (double 4x4): transform matrix of the TSR's offset frame relative to
316+
the w frame
317+
Bw (double 1x12): bounds in x y z roll pitch yaw.
318+
Format: [x_min, x_max, y_min, y_max ...]
319+
320+
Output:
321+
outstring (str): string to use for SerializeTSRChain function
322+
"""
323+
return '%d %s %s %s %s' % (self.manipindex, self.bodyandlink,
324+
SerializeTransform12Col(self.T0_w),
325+
SerializeTransform12Col(self.Tw_e),
326+
SerializeArray(self.Bw))
327+
328+
329+
def SerializeTSRChain(self):
330+
"""
331+
Function for Serializing TSR Chains for CBIRRT.
332+
333+
_SerializeTSRChain(bSampleFromChain, bConstrainToChain,
334+
numTSRs, allTSRstring,
335+
mimicbodyname, mimicbodyjoints)
336+
337+
Input:
338+
bSampleStartFromChain (0/1): 1: Use this chain for sampling start configs
339+
0: Ignore for sampling starts
340+
bSampleGoalFromChain (0/1): 1: Use this chain for sampling goal configs
341+
0: Ignore for sampling goals
342+
bConstrainToChain (0/1): 1: Use this chain for constraining configs
343+
0: Ignore for constraining
344+
numTSRs (int): Number of TSRs in this chain (must be > 0)
345+
allTSRstring (str): string of concatenated TSRs from SerializeTSR.
346+
Should be like [TSRstring 1 ' ' TSRstring2 ...]
347+
mimicbodyname (str): name of associated mimicbody for this chain
348+
(NULL if none associated)
349+
mimicbodyjoints (int [1xn]): 0-indexed indices of mimicbody's joints that
350+
are mimiced (INCREASING AND CONSECUTIVE)
351+
352+
Output:
353+
outstring (str): string to include in call to cbirrt planner
354+
"""
355+
allTSRstring = ' '.join([SerializeTSR(tsr) for tsr in self.TSRs])
356+
numTSRs = len(self.TSRs)
357+
outstring = '%d %d %d' % (int(self.sample_start),
358+
int(self.sample_goal),
359+
int(self.constrain))
360+
outstring += ' %d %s' % (numTSRs, allTSRstring)
361+
outstring += ' ' + self.mimicbodyname
362+
if len(self.mimicbodyjoints) > 0:
363+
outstring += ' %d %s' % (len(self.mimicbodyjoints),
364+
SerializeArray(self.mimicbodyjoints))
365+
return outstring

src/prpy/tsr/tsr.py

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -30,62 +30,9 @@
3030
import numpy.random
3131
import kin
3232

33-
"""
34-
Functions for Serializing TSRs and TSR Chains
35-
36-
SerializeTSR(manipindex,bodyandlink,T0_w,Tw_e,Bw)
37-
38-
Input:
39-
manipindex (int): the 0-indexed index of the robot's manipulator
40-
bodyandlink (str): body and link which is used as the 0 frame. Format
41-
'body_name link_name'. To use world frame, specify 'NULL'
42-
T0_w (double 4x4): transform matrix of the TSR's reference frame relative to
43-
the 0 frame
44-
Tw_e (double 4x4): transform matrix of the TSR's offset frame relative to the
45-
w frame
46-
Bw (double 1x12): bounds in x y z roll pitch yaw.
47-
Format: [x_min, x_max, y_min, y_max ...]
48-
49-
Output:
50-
outstring (str): string to use for SerializeTSRChain function
51-
52-
SerializeTSRChain(bSampleFromChain,
53-
bConstrainToChain,
54-
numTSRs,
55-
allTSRstring,
56-
mimicbodyname,
57-
mimicbodyjoints)
58-
59-
Input:
60-
bSampleStartFromChain (0/1): 1: Use this chain for sampling start configs
61-
0: Ignore for sampling starts
62-
bSampleGoalFromChain (0/1): 1: Use this chain for sampling goal configs
63-
0: Ignore for sampling goals
64-
bConstrainToChain (0/1): 1: Use this chain for constraining configs
65-
0: Ignore for constraining
66-
numTSRs (int): Number of TSRs in this chain (must be > 0)
67-
allTSRstring (str): string of concetenated TSRs generated using SerializeTSR.
68-
Should be like [TSRstring 1 ' ' TSRstring2 ...]
69-
mimicbodyname (str): name of associated mimicbody for this chain
70-
(NULL if none associated)
71-
mimicbodyjoints (int [1xn]): 0-indexed indices of the mimicbody's joints that
72-
are mimiced (MUST BE INCREASING AND CONSECUTIVE)
73-
74-
Output:
75-
outstring (str): string to include in call to cbirrt planner
76-
"""
77-
78-
79-
def SerializeTransform12Col(tm, format='%.5f'):
80-
return ' '.join([(format % (i,)) for i in tm[0:3, :].T.reshape(12)])
81-
82-
83-
def SerializeArray(a, format='%.5f'):
84-
return ' '.join([(format % (i,)) for i in a.reshape(-1)])
85-
86-
87-
class TSR(object): # force new-style class
8833

34+
class TSR(object):
35+
""" A Task-Space-Region (TSR) represents a motion constraint. """
8936
def __init__(self, T0_w=None, Tw_e=None, Bw=None,
9037
manip=None, bodyandlink='NULL'):
9138
if T0_w is None:
@@ -135,12 +82,6 @@ def sample(self, vals=None):
13582
trans = numpy.dot(numpy.dot(self.T0_w, Tw), self.Tw_e)
13683
return trans
13784

138-
def serialize(self):
139-
return '%d %s %s %s %s' % (self.manipindex, self.bodyandlink,
140-
SerializeTransform12Col(self.T0_w),
141-
SerializeTransform12Col(self.Tw_e),
142-
SerializeArray(self.Bw))
143-
14485
def to_dict(self):
14586
""" Convert this TSR to a python dict. """
14687
return {
@@ -235,19 +176,6 @@ def __init__(self, sample_start=False, sample_goal=False, constrain=False,
235176
def append(self, tsr):
236177
self.TSRs.append(tsr)
237178

238-
def serialize(self):
239-
allTSRstring = ' '.join([tsr.serialize() for tsr in self.TSRs])
240-
numTSRs = len(self.TSRs)
241-
outstring = '%d %d %d' % (int(self.sample_start),
242-
int(self.sample_goal),
243-
int(self.constrain))
244-
outstring += ' %d %s' % (numTSRs, allTSRstring)
245-
outstring += ' ' + self.mimicbodyname
246-
if len(self.mimicbodyjoints) > 0:
247-
outstring += ' %d %s' % (len(self.mimicbodyjoints),
248-
SerializeArray(self.mimicbodyjoints))
249-
return outstring
250-
251179
def to_dict(self):
252180
""" Construct a TSR chain from a python dict. """
253181
return {

0 commit comments

Comments
 (0)