publish_goal_marker.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 import roslib; roslib.load_manifest('jsk_interactive_test')
5 import rospy
6 import copy
7 import httplib, urllib2, urllib, json
8 from socket import gethostname
9 from getpass import getuser
10 
11 
12 from std_msgs.msg import ColorRGBA
13 from geometry_msgs.msg import Vector3, Pose, Point, Quaternion, PoseStamped
14 from visualization_msgs.msg import MarkerArray, Marker
17 from tf import transformations
18 import numpy, math
19 
21  poses = []
22  colors = []
23  status = []
24  size = 2
25  start_time = False
26  done_time = False
27 
28  def recordResult(self, ui_type, time):
29  #url = 'http://localhost:3000/interactive_marker_test'
30  url = 'http://jsk-db.herokuapp.com/interactive_marker_test'
31  params = {'ui_type': ui_type,
32  'user': '%s@%s' % (getuser(), gethostname()),
33  'time': time}
34  data = urllib.urlencode(params)
35  req = urllib2.Request(url, data)
36  try:
37  response = urllib2.urlopen(req)
38  except:
39  rospy.logfatal('failed to record result')
40 
41  def displayResult(self):
42  url = 'http://jsk-db.herokuapp.com/interactive_marker_test/json'
43  req = urllib2.Request(url)
44  try:
45  response = urllib2.urlopen(req)
46  data = json.loads(response.read())
47  rospy.loginfo("======================================================")
48  rospy.loginfo(" http://jsk-db.herokuapp.com/interactive_marker_test")
49  rospy.loginfo(" user / ui_type / time")
50  for d in data:
51  rospy.loginfo("%12s / %8s / %s sec", d['user'], d['ui_type'], d['time'])
52  rospy.loginfo("======================================================")
53  except:
54  rospy.logfatal('failed to record result')
55 
56  def processFeedback(self, feedback):
57  if not self.start_time:
58  self.start_time = rospy.get_rostime()
59  self.feedback = feedback
60  size = self.size
61  i = min(range(size*size*size),key=lambda i:numpy.linalg.norm(numpy.array([self.poses[i].position.x,self.poses[i].position.y,self.poses[i].position.z])-
62  numpy.array([feedback.pose.position.x,feedback.pose.position.y,feedback.pose.position.z])))
63  p = self.poses[i]
64  pos = numpy.linalg.norm(numpy.array([p.position.x,p.position.y,p.position.z])-
65  numpy.array([feedback.pose.position.x,feedback.pose.position.y,feedback.pose.position.z]))
66  #print transformations.quaternion_matrix([p.orientation.x,p.orientation.y,p.orientation.z,p.orientation.w])
67  #print transformations.quaternion_matrix([feedback.pose.orientation.x,feedback.pose.orientation.y,feedback.pose.orientation.z,feedback.pose.orientation.w])
68  #print transformations.quaternion_matrix([p.orientation.x,p.orientation.y,p.orientation.z,p.orientation.w])[0:3,2]
69  #print transformations.quaternion_matrix([feedback.pose.orientation.x,feedback.pose.orientation.y,feedback.pose.orientation.z,feedback.pose.orientation.w])[0:3,2]
70  rot = math.acos(numpy.dot(transformations.quaternion_matrix([p.orientation.x,p.orientation.y,p.orientation.z,p.orientation.w])[0:3,2],
71  transformations.quaternion_matrix([feedback.pose.orientation.x,feedback.pose.orientation.y,feedback.pose.orientation.z,feedback.pose.orientation.w])[0:3,2]))
72  #rot = numpy.linalg.norm(rvec)
73  rospy.loginfo('error %6.3f / %6.3f'%(pos, numpy.rad2deg(rot)))
74  if pos < self.position_threshold and rot < numpy.deg2rad(self.rotation_threshold):
75  self.colors[i] = ColorRGBA(1.0,0.2,0.2,0.8)
76  self.status[i] = rospy.get_rostime() - self.start_time
77 
78  def poseCB(self, msg):
79  self.server.setPose('control', msg.pose, msg.header)
80  self.server.applyChanges()
81  self.processFeedback(msg)
82 
83  def __init__(self):
84  pub_goal = rospy.Publisher('interactive_goal_marker', MarkerArray)
85  rospy.init_node('publish_interactive_goal_marker')
86  self.ui_type = rospy.get_param('~ui_type')
87  #
88  self.displayResult()
89  # setup
90  size = self.size
91  space = 0.5
92  hand_mesh = 'file://'+roslib.packages.get_pkg_dir('jsk_interactive_test')+'/scripts/RobotHand.dae'
93  hand_mesh_scale = Vector3(0.2, 0.2, 0.2)
94  goal_mesh = 'file://'+roslib.packages.get_pkg_dir('jsk_interactive_test')+'/scripts/Bottle.dae'
95  goal_mesh_scale = Vector3(1.0, 1.0, 1.0)
96 
97  self.position_threshold = rospy.get_param('~position_threshold', 0.01) # 1 [cm]
98  self.rotation_threshold = rospy.get_param('~rotation_threshold', 2) # 2 [deg]
99 
100  rospy.Subscriber('/goal_marker/move_marker', PoseStamped, self.poseCB)
101  #
102  for i in range(size*size*size):
103  x = space * ((i%(size) - size/2) + numpy.random.normal(0,0.1))
104  y = space * (((i/(size))%size - size/2) + numpy.random.normal(0,0.1))
105  z = space * ((i/(size*size) - size/2) + numpy.random.normal(0,0.1))
106  q = transformations.quaternion_from_euler(*numpy.random.normal(0,0.7,3))
107  self.poses.append(Pose(Point(x,y,z), Quaternion(*q)))
108  self.colors.append(ColorRGBA(0.8,0.8,0.8,0.8))
109  self.status.append(False)
110 
111  # interactive marker
112  server = InteractiveMarkerServer("goal_marker")
113  self.server = server
114  int_marker = InteractiveMarker()
115  int_marker.header.frame_id = "/map"
116  int_marker.name = "control"
117  int_marker.scale = 0.3
118 
119  mesh_marker = Marker()
120  mesh_marker.type = Marker.MESH_RESOURCE
121  mesh_marker.scale = hand_mesh_scale
122  mesh_marker.color = ColorRGBA(0.2,0.2,1.0,0.8)
123  mesh_marker.mesh_resource = hand_mesh
124 
125  target_marker = Marker()
126  target_marker.type = Marker.CYLINDER
127  target_marker.scale = Vector3(0.07,0.07,0.1)
128  target_marker.color = ColorRGBA(0.4,0.4,1.0,0.6)
129 
130  mesh_control = InteractiveMarkerControl()
131  mesh_control.always_visible = True
132  mesh_control.markers.append(mesh_marker)
133  mesh_control.markers.append(target_marker)
134 
135  int_marker.controls.append(mesh_control)
136 
137  if rospy.has_param(rospy.search_param('make_interactive_marker_arrow')) and rospy.get_param(rospy.search_param('make_interactive_marker_arrow')):
138  control = InteractiveMarkerControl()
139  control.orientation.w = 1
140  control.orientation.x = 1
141  control.orientation.y = 0
142  control.orientation.z = 0
143  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
144  int_marker.controls.append(copy.deepcopy(control))
145  control.orientation.w = 1
146  control.orientation.x = 0
147  control.orientation.y = 1
148  control.orientation.z = 0
149  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
150  int_marker.controls.append(copy.deepcopy(control))
151  control.orientation.w = 1
152  control.orientation.x = 0
153  control.orientation.y = 0
154  control.orientation.z = 1
155  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
156  int_marker.controls.append(copy.deepcopy(control))
157  control.orientation.w = 1
158  control.orientation.x = 1
159  control.orientation.y = 0
160  control.orientation.z = 0
161  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
162  int_marker.controls.append(copy.deepcopy(control))
163  control.orientation.w = 1
164  control.orientation.x = 0
165  control.orientation.y = 1
166  control.orientation.z = 0
167  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
168  int_marker.controls.append(copy.deepcopy(control))
169  control.orientation.w = 1
170  control.orientation.x = 0
171  control.orientation.y = 0
172  control.orientation.z = 1
173  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
174  int_marker.controls.append(copy.deepcopy(control))
175 
176  server.insert(int_marker, self.processFeedback)
177  server.applyChanges()
178 
179  # start rosmain
180  r = rospy.Rate(5.0)
181  while not rospy.is_shutdown():
182  array = MarkerArray()
183  for i in range(size*size*size):
184  msg = Marker()
185  msg.header.stamp = rospy.get_rostime()
186  msg.header.frame_id = '/map'
187  msg.mesh_use_embedded_materials = False
188  msg.mesh_resource = goal_mesh
189  msg.type = Marker.MESH_RESOURCE
190  msg.scale = goal_mesh_scale
191  msg.color = self.colors[i]
192  msg.pose = self.poses[i]
193  msg.lifetime = rospy.Time(10)
194  msg.id = i
195  array.markers.append(msg)
196  if self.done_time:
197  msg = Marker()
198  msg.header.stamp = rospy.get_rostime()
199  msg.header.frame_id = '/map'
200  msg.type = Marker.TEXT_VIEW_FACING
201  msg.scale.z = 0.14
202  msg.color.r = 1.0
203  msg.color.g = 1.0
204  msg.color.b = 1.0
205  msg.color.a = 1.0
206  msg.text = 'Done, time = %5.2f'%(self.done_time)
207  msg.lifetime = rospy.Time(10)
208  msg.id = size*size*size
209  array.markers.append(msg)
210  pub_goal.publish(array)
211  r.sleep()
212  if self.start_time:
213  rospy.loginfo("%2d/%d %5.2f"%(len(filter(lambda x: x != False, self.status)),len(self.status),float((rospy.get_rostime()-self.start_time).to_sec())))
214  if all(self.status):
215  if self.done_time:
216  rospy.loginfo("Done.. %5.2f"%self.done_time)
217  else:
218  self.done_time = (rospy.get_rostime()-self.start_time).to_sec()
219  self.recordResult(self.ui_type, self.done_time)
220  for s in self.status:
221  rospy.loginfo("%5.2f"%s.to_sec())
222 
223 if __name__ == '__main__':
224  try:
226  except rospy.ROSInterruptException:
227  pass
228 


jsk_interactive_test
Author(s): Kei Okada
autogenerated on Sat Mar 20 2021 03:03:46