-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvelocity_runner.py
316 lines (242 loc) · 9.51 KB
/
velocity_runner.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import argparse
import numpy as np
import numpy.random as npr
import sys
import time
import re
import rospy
import copy
import glob
from geometry_msgs.msg import TwistStamped
from std_msgs.msg import Header
import pickle
import multiprocessing
from multiprocessing import Process
from real_camera import *#
#from record_saver import *
import copy
import geometry_msgs.msg
from geometry_msgs.msg import Point,Pose, Quaternion
from std_msgs.msg import String
import roslib
roslib.load_manifest('joint_listener')
from joint_listener.srv import *
import moveit_commander
from tf.transformations import quaternion_from_euler,euler_from_quaternion
from gripper_client import *
from shell import *
import glob
import random
from PIL import Image
from moveit import *
joint_names = ["torso_lift_joint", "shoulder_pan_joint", "shoulder_lift_joint", "upperarm_roll_joint", "elbow_flex_joint", "forearm_roll_joint", "wrist_flex_joint", "wrist_roll_joint"]
scale_control = 0.5
parser = argparse.ArgumentParser()
parser.add_argument('--episodes')
parser.add_argument('--timesteps')
parser.add_argument('--saveinterval')
parser.add_argument('--savepath')
parser.add_argument('--port')
parser.add_argument('--startepisode')
def start_data_collection(episodes, timesteps, saveinterval, save_path,port, startepisode, camera):
pub = rospy.Publisher('/arm_controller/cartesian_twist/command', TwistStamped)
rospy.init_node('send_velocities')
rate = rospy.Rate(2)
moveit = MoveIt()
moveit.get_ee_bounds(fixed=True)
# if not good_bounds:
# return
max_torso_height()
moveit.get_start_point(fixed=True)
# if not good_start:
# return
start_time = -1
total_errors = 0
all_images = []
all_actions = []
all_vel_actions = []
all_qts = []
all_ee_qts = []
episode = 0
while episode < episodes:
#THIS IS SPECIFICALLY FOR THE PUSHING DATASET
r_number = random.random()
if(r_number < 1):
random_start = False
corner_start = False
else:
random_start = True
corner_start = False
#moveit.trace_perimeter()
#if (episode +1 ) % 5 == 0:
# moveit.shuffle_reset()
while moveit.not_at_start():
moveit.go_to_start(rand=random_start, corner=corner_start)
time.sleep(1)
images = []
actions = []
vel_actions = []
qts = []
ee_qts = []
move_gripper(1)
if episode % 20 == 0:
max_torso_height()
#moveit.shuffle_reset()
#moveit.trace_perimeter()\
true_start = int(startepisode) + int(episode)
print("Starting episode %d" % true_start)
no_errors = True
#check that start position is correct, otherwise retry
for timestep in range(timesteps):
print(timestep)
if not moveit.not_at_start():
print("Still at start!")
position, velocity, effort = get_joint_states()
current_pose = moveit.get_ee_pose()
if current_pose.position.x < moveit.lower_point.position.x:
twist_command = new_twist_command(x=scale_control)
elif current_pose.position.y < moveit.lower_point.position.y:
twist_command = new_twist_command(y=scale_control)
elif current_pose.position.x > moveit.upper_point.position.x:
twist_command = new_twist_command(x=-scale_control)
elif current_pose.position.y > moveit.upper_point.position.y:
twist_command = new_twist_command(y=-scale_control)
else:
twist_command = random_twist_command()
image = None
while image is None:
image = camera.capture(timestep)
current_pose = moveit.get_ee_pose()
ee_vel = [twist_command.twist.linear.x, twist_command.twist.linear.y, twist_command.twist.linear.z]
ee_pos = [current_pose.position.x, current_pose.position.y, current_pose.position.z]
#DEBUG
#image = np.zeros((100,100,3))
# print("Captured image for %d" % timestep)
# im = Image.fromarray(image[...,[2,1,0]])
# im.save(save_path + '/images/raw_images/' + str(timestep) + '.jpeg')
if current_pose.position.z < 0.92 or current_pose.position.z > 1:
print(current_pose.position.z)
print("Z bounds violated, restarting episode")
total_errors += 1
no_errors = False
break
if moveit.orientation_violated():
print("Orientation violated, restarting episode")
total_errors += 1
no_errors = False
break
images.append(image)
if timestep is not timesteps - 1:
actions.append(ee_vel)
vel_actions.append(velocity)
qts.append(position)
ee_qts.append(ee_pos)
position, _, _ = get_joint_states()
#publish the command, then get your current vel, append all your info, then sleep
pub.publish(twist_command)
_, velocity, _ = get_joint_states()
rate.sleep()
twist_command = new_twist_command()
pub.publish(twist_command)
if no_errors:
all_images.append(images)
all_actions.append(actions)
all_vel_actions.append(vel_actions)
all_qts.append(qts)
all_ee_qts.append(ee_qts)
print("Collected single set for episode %d" % episode)
if (episode % saveinterval) == 0:
time_interval = str(startepisode + start_time) + '-' + str(startepisode+episode)
pickle.dump(all_images,open(save_path + '/images/images_' + time_interval + '.pickle','wb'))
pickle.dump(all_actions,open(save_path + '/actions/actions_' + time_interval + '.pickle','wb'))
pickle.dump(all_vel_actions,open(save_path + '/vel_actions/vel_actions_' + time_interval + '.pickle','wb'))
pickle.dump(all_qts,open(save_path + '/qts/qts_' + time_interval + '.pickle','wb'))
pickle.dump(all_ee_qts,open(save_path + '/ee_qts/ee_qts_' + time_interval + '.pickle','wb'))
all_images = []
all_actions = []
all_vel_actions = []
all_qts = []
all_ee_qts = []
start_time = episode
del images
del actions
del vel_actions
del qts
del ee_qts
twist_command = new_twist_command()
pub.publish(twist_command)
if no_errors:
episode += 1
del all_images
del all_actions
del all_vel_actions
del all_qts
del all_ee_qts
camera.close()
print("Total errors: %d" % total_errors)
def random_twist_command():
control = npr.uniform(low=-.3, high=scale_control, size=(2))
twist = TwistStamped()
twist.header.frame_id = 'base_link'
twist.twist.linear.x = npr.uniform(low=-.3, high=scale_control)
twist.twist.linear.y = npr.uniform(low=-scale_control, high=scale_control)
twist.twist.linear.z = 0
return twist
def new_twist_command(x=0, y=0, z=0):
twist = TwistStamped()
twist.header.frame_id = 'base_link'
twist.twist.linear.x = x
twist.twist.linear.y = y
twist.twist.linear.z = z
return twist
#returns velocity to a fixed pose
def fixed_twist_command(moveit, fixed_pose):
twist = TwistStamped()
current_pose = moveit.get_ee_pose()
twist.header.frame_id = 'base_link'
twist.twist.linear.x = fixed_pose.position.x - current_pose.position.x
twist.twist.linear.y = fixed_pose.position.y - current_pose.position.y
twist.twist.linear.z = fixed_pose.position.z - current_pose.position.z
return twist
def get_joint_states():
rospy.wait_for_service("return_joint_states")
try:
s = rospy.ServiceProxy("/return_joint_states", ReturnJointStates)
resp = s(joint_names)
except rospy.ServiceException:
print("error when calling return_joint_states: %s")
sys.exit(1)
for (ind, joint_name) in enumerate(joint_names):
if(not resp.found[ind]):
print("joint %s not found!"%joint_name)
return (np.array(resp.position), np.array(resp.velocity), np.array(resp.effort))
def max_torso_height():
rospy.wait_for_service("send_torso_height")
try:
s = rospy.ServiceProxy("/send_torso_height", SendTorsoHeight)
resp = s(0.37)
except rospy.ServiceException:
print("error when calling send_torso_height")
sys.exit(1)
return resp
def main(episodes, timesteps, saveinterval, save_path,port, startepisode):
try:
camera = RealCamera(port)
processes = camera.processes
processes.append(Process(target=start_data_collection, args=(episodes, timesteps, saveinterval, save_path, port, startepisode, camera)))
for p in processes:
p.start()
print("Starting all processes")
for p in processes:
p.join()
except KeyboardInterrupt:
camera.close()
try:
moveit_commander.roscpp_shutdown()
sys.exit(0)
except SystemExit:
moveit_commander.roscpp_shutdown()
os._exit(0) # pylint: disable=protected-access
if __name__ == "__main__":
args = parser.parse_args()
main(int(args.episodes), int(args.timesteps), int(args.saveinterval), args.savepath, int(args.port), int(args.startepisode))