forked from royf/hil-mt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsr.py
282 lines (240 loc) · 10.4 KB
/
hsr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import math
import numpy as np
import vision
from utils import DictTree
try:
import ar_markers
import control_msgs.msg as ctrl_msg
import cv2
import cv_bridge
import rospy
import sensor_msgs.msg as sens_msg
import tmc_control_msgs.msg as tmc_msg
import trajectory_msgs.msg as traj_msg
except ImportError, e:
print(e)
import env
V = 0.07
OMEGA = math.pi / 5.
class MoveArm(env.Action):
arg_in_len = 5
ret_out_len = 0
@staticmethod
def apply(hsr, arg):
arm_lift, arm_flex, arm_roll, wrist_flex, wrist_roll = arg[:5]
point = traj_msg.JointTrajectoryPoint()
point.positions = [arm_lift, arm_flex, arm_roll, wrist_flex, wrist_roll]
point.velocities = [0] * 5
point.time_from_start = rospy.Time(0.5)
traj = ctrl_msg.FollowJointTrajectoryActionGoal()
traj.goal.trajectory.joint_names = ['arm_lift_joint', 'arm_flex_joint', 'arm_roll_joint', 'wrist_flex_joint', 'wrist_roll_joint']
traj.goal.trajectory.points = [point]
hsr.publishers['arm'].publish(traj)
rospy.sleep(1.)
while True:
error = hsr.subscribers['arm_pose'].callback.data['error']
if all(abs(x) <= 1e-2 for x in error):
break
rospy.sleep(0.1)
class MoveBaseAbs(env.Action):
arg_in_len = 5
ret_out_len = 0
@staticmethod
def apply(hsr, arg):
x, y, theta, v, omega = arg[:5]
x0, y0, theta0 = hsr.subscribers['base_pose'].callback.data['actual']
dx = ((x - x0) ** 2 + (y - y0) ** 2) ** .5
dtheta = abs(theta - theta0)
t = max(dx / v, dtheta / omega)
acceleration = 0.05
point = traj_msg.JointTrajectoryPoint()
point.positions = [x, y, theta]
point.accelerations = [acceleration] * 3
point.effort = [1]
point.time_from_start = rospy.Time(t)
traj = ctrl_msg.FollowJointTrajectoryActionGoal()
traj.goal.trajectory.joint_names = ['odom_x', 'odom_y', 'odom_t']
traj.goal.trajectory.points = [point]
hsr.publishers['base'].publish(traj)
rospy.sleep(1.)
while True:
error = hsr.subscribers['base_pose'].callback.data['error']
if all(abs(x) <= 1e-6 for x in error):
break
rospy.sleep(0.1)
class MoveBaseRel(env.Action):
arg_in_len = 5
ret_out_len = 0
@staticmethod
def apply(hsr, arg):
x, y, theta, v, omega = arg[:5]
x0, y0, theta0 = hsr.subscribers['base_pose'].callback.data['actual']
x, y, theta = (
x * math.cos(theta0) - y * math.sin(theta0) + x0,
x * math.sin(theta0) + y * math.cos(theta0) + y0,
theta + theta0)
MoveBaseAbs.apply(hsr, [x, y, theta, v, omega])
class MoveGripper(env.Action):
arg_in_len = 1
ret_out_len = 0
@staticmethod
def apply(hsr, arg):
[close] = arg[:1]
if close:
rospy.sleep(1.)
grip = tmc_msg.GripperApplyEffortActionGoal()
grip.goal.effort = -0.1 if close > 0.5 else 0.1
hsr.publishers['grip'].publish(grip)
rospy.sleep(1.)
class MoveHead(env.Action):
arg_in_len = 2
ret_out_len = 0
@staticmethod
def apply(hsr, arg):
tilt, pan = arg[:2]
point = traj_msg.JointTrajectoryPoint()
point.positions = [tilt, pan]
point.velocities = [0] * 2
point.time_from_start = rospy.Time(0.5)
traj = ctrl_msg.FollowJointTrajectoryActionGoal()
traj.goal.trajectory.joint_names = ['head_tilt_joint', 'head_pan_joint']
traj.goal.trajectory.points = [point]
hsr.publishers['head'].publish(traj)
rospy.sleep(2.)
class LocateObject(env.Action):
arg_in_len = 3
ret_out_len = 4
@staticmethod
def apply(hsr, arg):
motion_cnt, obj_class, obj_color = arg[:3]
img = hsr.subscribers['head_cam'].callback.data
objs = hsr.vision.get_objs(img, 0.1)
objs = list(sorted((obj for obj in objs if obj['class'] in hsr.obj_classes_filter), key=lambda o: o['bottom'], reverse=True))
detected_obj_class = hsr.obj_classify([[motion_cnt, obj['left'], obj['top'], obj['right'], obj['bottom']] for obj in objs])
for i, obj in enumerate(objs):
obj['class_idx'] = detected_obj_class[i]
obj['mean_color'] = img[obj['top']:obj['bottom'], obj['left']:obj['right'], :].mean(0).mean(0) - img.mean(0).mean(0)
obj['color_idx'] = 1 + np.argmax(obj['mean_color'])
for i, obj in enumerate(objs):
box_color = (255, 0, 0)
cv2.rectangle(img, (obj['left'], obj['top']), (obj['right'], obj['bottom']), box_color, 4)
cv2.putText(
img, "{} {}".format(hsr.obj_colors[obj['color_idx']], hsr.obj_classes[obj['class_idx']]),
(obj['left'], obj['top']), cv2.FONT_HERSHEY_PLAIN, 2, box_color, 1, 8)
cv2.imshow('head', img)
cv2.waitKey(3)
if abs(obj_class - hsr.obj_classes.index(None)) > 0.5:
objs = [obj for obj in objs if abs(obj['class_idx'] - obj_class) < 0.5]
if abs(obj_color - hsr.obj_colors.index(None)) > 0.5:
objs = [obj for obj in objs if abs(obj['color_idx'] - obj_color) < 0.5]
if len(objs) > 0:
obj = objs[0]
box_color = (0, 255, 0)
cv2.rectangle(img, (obj['left'], obj['top']), (obj['right'], obj['bottom']), box_color, 4)
cv2.putText(
img, "{} {}".format(hsr.obj_colors[obj['color_idx']], hsr.obj_classes[obj['class_idx']]),
(obj['left'], obj['top']), cv2.FONT_HERSHEY_PLAIN, 2, box_color, 1, 8)
return [obj['class_idx'], obj['color_idx'], (obj['left'] + obj['right']) / 2, (obj['top'] + obj['bottom']) / 2]
else:
return [0, 0, 0, 0]
class LocateMarkers(env.Action):
arg_in_len = 0
ret_out_len = 16
@staticmethod
def apply(hsr, arg):
while True:
rospy.sleep(5.)
img = hsr.subscribers['head_cam'].callback.data
_, img = cv2.threshold(img, 32, 255, cv2.THRESH_BINARY)
markers = {marker.id: marker for marker in ar_markers.detect_markers(img)}.values()
for marker in markers:
marker.draw_contour(img)
cv2.imshow('head', img)
cv2.waitKey(3)
if len(markers) == 2:
return sort_markers(sum([list(marker.contours.flat) for marker in markers], []))
else:
print('found {} markers'.format(len(markers)))
rospy.sleep(1.)
def sort_markers(markers, sort_between_markers=True):
assert len(markers) == 16
markers = [int(x) for x in markers]
res = []
for marker in [markers[i:i + 8] for i in range(0, 16, 8)]:
# sort 4 points from left to right
points_xy = sorted(marker[i:i + 2] for i in range(0, 8, 2))
# sort each pair of points from top to bottom, and flatten
res.append(sum(sum((sorted(points_xy[i:i + 2], key=lambda xy: xy[1]) for i in range(0, 4, 2)), []), []))
if sort_between_markers:
return sum(sorted(res), [])
else:
return sum(res, [])
class HSREnv(env.Env):
actions = [
MoveArm,
MoveBaseAbs,
MoveBaseRel,
MoveGripper,
MoveHead,
LocateObject,
LocateMarkers,
]
action_lookup = {action.__name__: action for action in actions}
def __init__(self):
rospy.init_node('main', anonymous=True)
self.subscribers = {
'head_cam': rospy.Subscriber('/hsrb/head_r_stereo_camera/image_raw', sens_msg.Image, ImageCallback(), queue_size=1),
'arm_pose': rospy.Subscriber('/hsrb/arm_trajectory_controller/state', ctrl_msg.JointTrajectoryControllerState, PoseCallback(), queue_size=1),
'base_pose': rospy.Subscriber('/hsrb/omni_base_controller/state', ctrl_msg.JointTrajectoryControllerState, PoseCallback(), queue_size=1),
}
self.publishers = {
'arm': rospy.Publisher('/hsrb/arm_trajectory_controller/follow_joint_trajectory/goal', ctrl_msg.FollowJointTrajectoryActionGoal, queue_size=1),
'base': rospy.Publisher('/hsrb/omni_base_controller/follow_joint_trajectory/goal', ctrl_msg.FollowJointTrajectoryActionGoal, queue_size=1),
'grip': rospy.Publisher('/hsrb/gripper_controller/grasp/goal', tmc_msg.GripperApplyEffortActionGoal, queue_size=1),
'head': rospy.Publisher('/hsrb/head_trajectory_controller/follow_joint_trajectory/goal', ctrl_msg.FollowJointTrajectoryActionGoal, queue_size=1),
}
try:
while (any(publisher.get_num_connections() == 0 for publisher in self.publishers.values())
or any(subscriber.get_num_connections() == 0 or subscriber.callback.data is None for subscriber in self.subscribers.values())):
if rospy.is_shutdown():
raise ValueError()
print('Waiting for: {}'.format(
[name for name, publisher in self.publishers.items() if publisher.get_num_connections() == 0] +
[name for name, subscriber in self.subscribers.items() if subscriber.get_num_connections() == 0 or subscriber.callback.data is None]))
rospy.sleep(0.1)
except KeyboardInterrupt:
rospy.loginfo(KeyboardInterrupt)
raise
self.vision = vision.Yolo()
self.obs = None
def observe(self):
return self.obs
def step(self, act_name, act_arg):
self.obs = self.action_lookup[act_name].apply(self, act_arg)
def record(self):
return [
self.subscribers['arm_pose'].callback.data,
self.subscribers['base_pose'].callback.data,
]
class DataCallback(object):
def __init__(self, sleep=0.1):
self.data = None
self.sleep = sleep
def __call__(self, data):
self.data = data
rospy.sleep(self.sleep)
class ImageCallback(DataCallback):
def __init__(self, sleep=0.1):
super(ImageCallback, self).__init__(sleep)
self.bridge = cv_bridge.CvBridge()
def __call__(self, img):
img = self.bridge.imgmsg_to_cv2(img, 'bgr8')
img = cv2.resize(img, (920, 690), interpolation=cv2.INTER_CUBIC)
super(ImageCallback, self).__call__(img)
class PoseCallback(DataCallback):
def __call__(self, data):
pose = DictTree(
actual=data.actual.positions,
error=data.error.positions,
)
super(PoseCallback, self).__call__(pose)