Skip to content

Commit a965df4

Browse files
committed
Integrating vncc into perception pipeline
1 parent 5514d2c commit a965df4

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

src/prpy/perception/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import functools
44

5+
class PerceptionException(Exception):
6+
pass
7+
58
class PerceptionMethod(object):
69

710
def __init__(self, func):

src/prpy/perception/vncc.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import rospy
2+
import vncc_msgs.srv
3+
import vncc_msgs.msg
4+
import math
5+
import tf
6+
import tf.transformations as transformations
7+
import numpy
8+
import os.path
9+
10+
11+
def MsgToPose(msg):
12+
13+
#Parse the ROS message to a 4x4 pose format
14+
15+
#Get translation and rotation (from Euler angles)
16+
pose = transformations.euler_matrix(msg.roll*0.0,msg.pitch*0.0,msg.yaw*0.0)
17+
18+
pose[0,3] = msg.pt.x
19+
pose[1,3] = msg.pt.y
20+
pose[2,3] = msg.pt.z
21+
22+
return pose
23+
24+
25+
class VnccDetector(object):
26+
27+
def __init__(self, env, kinbody_directory,
28+
service_namespace=None,
29+
detection_frame=None,world_frame=None):
30+
31+
32+
# Initialize a new ros node if one has not already been created
33+
try:
34+
rospy.init_node('vncc_detector', anonymous=True)
35+
except rospy.exceptions.ROSException:
36+
pass
37+
38+
#For getting transforms in world frame
39+
if detection_frame is not None and world_frame is not None:
40+
self.listener = tf.TransformListener()
41+
else:
42+
self.listener = None
43+
44+
if service_namespace is None:
45+
service_namespace='/vncc'
46+
47+
self.detection_frame = detection_frame
48+
self.world_frame = world_frame
49+
self.service_namespace = service_namespace
50+
51+
self.env = env
52+
self.kinbody_directory = kinbody_directory
53+
self.kinbody_to_query_map = {'plastic_plate': 'plate',
54+
'plastic_bowl': 'bowl'}
55+
56+
def LocalToWorld(self,pose):
57+
#Get pose w.r.t world frame
58+
self.listener.waitForTransform(self.world_frame,self.detection_frame,
59+
rospy.Time(),rospy.Duration(10))
60+
t, r = self.listener.lookupTransform(self.world_frame,self.detection_frame,rospy.Time(0))
61+
62+
#Get relative transform between frames
63+
offset_to_world = numpy.matrix(transformations.quaternion_matrix(r))
64+
offset_to_world[0,3] = t[0]
65+
offset_to_world[1,3] = t[1]
66+
offset_to_world[2,3] = t[2]
67+
68+
#Compose with pose to get pose in world frame
69+
result = numpy.array(numpy.dot(offset_to_world, pose))
70+
71+
return result
72+
73+
74+
def GetDetection(self,obj_name):
75+
76+
#Call detection service for a particular object
77+
detect_vncc = rospy.ServiceProxy(self.service_namespace+'/get_vncc_detections',
78+
vncc_msgs.srv.GetDetections)
79+
80+
detect_resp = detect_vncc(object_name=obj_name)
81+
82+
if detect_resp.ok == False:
83+
return None
84+
#Assumes one instance of object
85+
result = MsgToPose(detect_resp.detections[0])
86+
if (self.detection_frame is not None and self.world_frame is not None):
87+
result = self.LocalToWorld(result)
88+
89+
return result
90+
91+
def DetectObject(self, obj_name):
92+
93+
if obj_name not in self.kinbody_to_query_map:
94+
from prpy.perception.base import PerceptionException
95+
raise PerceptionException('Unknown object')
96+
97+
query_name = self.kinbody_to_query_map[obj_name]
98+
obj_pose = self.GetDetection(query_name)
99+
100+
if self.env.GetKinBody(obj_name) is None:
101+
from prpy.rave import add_object
102+
kinbody_file = os.path.join('objects', '%s.kinbody.xml' % obj_name)
103+
new_body = add_object(
104+
self.env,
105+
obj_name,
106+
os.path.join(self.kinbody_directory, kinbody_file))
107+
108+
body = self.env.GetKinBody(obj_name)
109+
body.SetTransform(obj_pose)
110+
return body

0 commit comments

Comments
 (0)