Skip to content

Commit 0723ca0

Browse files
psardin001florent-lamiraux
authored andcommitted
fix gripper-to-joint mapping in SecurityMargins and contact surfaces
1 parent bd730a3 commit 0723ca0

File tree

1 file changed

+77
-22
lines changed

1 file changed

+77
-22
lines changed

src/pyhpp/manipulation/security_margins.py

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,47 +45,101 @@ def __init__(self, problem, factory, robotsAndObjects, robot):
4545

4646
def computeJoints(self):
4747
self.robotToJoints = dict()
48-
jointNames = self.robot.model().names
48+
model = self.robot.model()
49+
jointNames = list(model.names)
4950
for ro in self.robotsAndObjects:
5051
le = len(ro)
51-
self.robotToJoints[ro] = [
52-
n
53-
for n in jointNames
54-
if n[:le] == ro and (len(n) == le or n[le] in self.separators)
55-
]
52+
self.robotToJoints[ro] = list(
53+
filter(
54+
lambda n: n[:le] == ro and n[le] in self.separators,
55+
jointNames,
56+
)
57+
)
5658
self.robotToJoints["universe"] = ["universe"]
5759
self.jointToRobot = dict()
5860
for ro, joints in self.robotToJoints.items():
5961
for j in joints:
6062
self.jointToRobot[j] = ro
6163

64+
def _getChildJoints(self, jointName):
65+
model = self.robot.model()
66+
try:
67+
jointId = model.getJointId(jointName)
68+
except Exception:
69+
return []
70+
71+
childNames = []
72+
if jointId < len(model.children):
73+
for childId in model.children[jointId]:
74+
if childId < len(model.names):
75+
childNames.append(model.names[childId])
76+
return childNames
77+
78+
def _getGripperJoint(self, gripperName):
79+
model = self.robot.model()
80+
if model.existFrame(gripperName):
81+
frameId = model.getFrameId(gripperName)
82+
frame = model.frames[frameId]
83+
parentJointId = frame.parentJoint
84+
if parentJointId < len(model.names):
85+
return model.names[parentJointId]
86+
return None
87+
6288
def computeGrippers(self):
6389
self.gripperToRobot = dict()
6490
self.gripperToJoints = dict()
65-
6691
for g in self.factory.grippers:
67-
for joint_name in self.robot.model().names:
68-
if g.startswith(joint_name):
69-
self.gripperToRobot[g] = self.jointToRobot.get(
70-
joint_name, "unknown"
71-
)
72-
self.gripperToJoints[g] = [joint_name]
73-
break
92+
j = self._getGripperJoint(g)
93+
if j is not None and j in self.jointToRobot:
94+
self.gripperToRobot[g] = self.jointToRobot[j]
95+
childJoints = self._getChildJoints(j)
96+
self.gripperToJoints[g] = [j] + childJoints
97+
else:
98+
for joint_name in self.robot.model().names:
99+
if g.startswith(joint_name) and joint_name in self.jointToRobot:
100+
self.gripperToRobot[g] = self.jointToRobot[joint_name]
101+
childJoints = self._getChildJoints(joint_name)
102+
self.gripperToJoints[g] = [joint_name] + childJoints
103+
break
74104

75105
def computePossibleContacts(self):
76-
self.contactSurfaces = {k: [] for k in self.robotToJoints.keys()}
77-
self.possibleContacts = [
78-
(o1, o2)
79-
for o1, l1 in self.contactSurfaces.items()
80-
for o2, l2 in self.contactSurfaces.items()
81-
if o1 != o2 and len(l1) > 0 and len(l2) > 0
82-
]
106+
self.contactSurfaces = dict()
107+
for k in self.robotToJoints.keys():
108+
self.contactSurfaces[k] = list()
109+
110+
for io, obj_name in enumerate(self.factory.objects):
111+
if io < len(self.factory.contactsPerObjects):
112+
for surface_name in self.factory.contactsPerObjects[io]:
113+
if obj_name in self.contactSurfaces:
114+
self.contactSurfaces[obj_name].append(surface_name)
115+
116+
for surface_name in self.factory.envContacts:
117+
found = False
118+
for ro in list(self.robotsAndObjects) + ["universe"]:
119+
for sep in self.separators:
120+
prefix = ro + sep
121+
if surface_name.startswith(prefix):
122+
if ro in self.contactSurfaces:
123+
self.contactSurfaces[ro].append(surface_name)
124+
found = True
125+
break
126+
if found:
127+
break
128+
if not found:
129+
self.contactSurfaces["universe"].append(surface_name)
130+
131+
self.possibleContacts = list()
132+
for o1, l1 in self.contactSurfaces.items():
133+
for o2, l2 in self.contactSurfaces.items():
134+
if o1 != o2 and len(l1) > 0 and len(l2) > 0:
135+
self.possibleContacts.append((o1, o2))
83136

84137
def setSecurityMarginBetween(self, obj1, obj2, margin):
85138
self.marginMatrix[frozenset([obj1, obj2])] = margin
86139

87140
def getSecurityMarginBetween(self, obj1, obj2):
88-
return self.marginMatrix.get(frozenset([obj1, obj2]), self.defaultMargin)
141+
key = frozenset([obj1, obj2])
142+
return self.marginMatrix.get(key, self.defaultMargin)
89143

90144
def getActiveConstraintsAlongEdge(self, edge):
91145
factory = self.factory
@@ -150,6 +204,7 @@ def apply(self):
150204
)
151205

152206
constraints = self.getActiveConstraintsAlongEdge(edge)
207+
153208
for g, ro1 in constraints["grasp"]:
154209
if g in self.gripperToJoints and ro1 in self.robotToJoints:
155210
for j1 in self.robotToJoints[ro1]:

0 commit comments

Comments
 (0)