-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqptestArduinoInterface.py
More file actions
324 lines (271 loc) · 13.6 KB
/
Copy pathmqptestArduinoInterface.py
File metadata and controls
324 lines (271 loc) · 13.6 KB
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
317
318
319
320
321
322
323
324
#!/usr/bin/env python
###
# KINOVA (R) KORTEX (TM)
#
# Copyright (c) 2019 Kinova inc. All rights reserved.
#
# This software may be modified and distributed
# under the terms of the BSD 3-Clause license.
#
# Refer to the LICENSE file for details.
#
###
import sys
import rospy
import time
from kortex_driver.srv import *
from kortex_driver.msg import *
from std_msgs.msg import Float32
from std_msgs.msg import Int16
from sensor_msgs.msg import Joy
class ExampleFullArmMovement:
def __init__(self):
try:
rospy.init_node('example_full_arm_movement_python')
self.HOME_ACTION_IDENTIFIER = 2
# Subscriber
#rospy.Subscriber('/joint_angle',Float32,self.example_send_joint_angles)
rospy.Subscriber('/potAngles',Int16,self.example_send_joint_angles,queue_size=1)
#rospy.Subscriber('/joy',Joy,self.example_send_gripper_command)
# Get node params
self.robot_name = rospy.get_param('~robot_name', "my_gen3")
self.degrees_of_freedom = rospy.get_param("/" + self.robot_name + "/degrees_of_freedom", 7)
self.is_gripper_present = rospy.get_param("/" + self.robot_name + "/is_gripper_present", False)
rospy.loginfo("Using robot_name " + self.robot_name + " , robot has " + str(self.degrees_of_freedom) + " degrees of freedom and is_gripper_present is " + str(self.is_gripper_present))
# Init the action topic subscriber
self.action_topic_sub = rospy.Subscriber("/" + self.robot_name + "/action_topic", ActionNotification, self.cb_action_topic)
self.last_action_notif_type = None
# Init the services
clear_faults_full_name = '/' + self.robot_name + '/base/clear_faults'
rospy.wait_for_service(clear_faults_full_name)
self.clear_faults = rospy.ServiceProxy(clear_faults_full_name, Base_ClearFaults)
read_action_full_name = '/' + self.robot_name + '/base/read_action'
rospy.wait_for_service(read_action_full_name)
self.read_action = rospy.ServiceProxy(read_action_full_name, ReadAction)
execute_action_full_name = '/' + self.robot_name + '/base/execute_action'
rospy.wait_for_service(execute_action_full_name)
self.execute_action = rospy.ServiceProxy(execute_action_full_name, ExecuteAction)
set_cartesian_reference_frame_full_name = '/' + self.robot_name + '/control_config/set_cartesian_reference_frame'
rospy.wait_for_service(set_cartesian_reference_frame_full_name)
self.set_cartesian_reference_frame = rospy.ServiceProxy(set_cartesian_reference_frame_full_name, SetCartesianReferenceFrame)
play_cartesian_trajectory_full_name = '/' + self.robot_name + '/base/play_cartesian_trajectory'
rospy.wait_for_service(play_cartesian_trajectory_full_name)
self.play_cartesian_trajectory = rospy.ServiceProxy(play_cartesian_trajectory_full_name, PlayCartesianTrajectory)
play_joint_trajectory_full_name = '/' + self.robot_name + '/base/play_joint_trajectory'
rospy.wait_for_service(play_joint_trajectory_full_name)
self.play_joint_trajectory = rospy.ServiceProxy(play_joint_trajectory_full_name, PlayJointTrajectory)
send_gripper_command_full_name = '/' + self.robot_name + '/base/send_gripper_command'
rospy.wait_for_service(send_gripper_command_full_name)
self.send_gripper_command = rospy.ServiceProxy(send_gripper_command_full_name, SendGripperCommand)
activate_publishing_of_action_notification_full_name = '/' + self.robot_name + '/base/activate_publishing_of_action_topic'
rospy.wait_for_service(activate_publishing_of_action_notification_full_name)
self.activate_publishing_of_action_notification = rospy.ServiceProxy(activate_publishing_of_action_notification_full_name, OnNotificationActionTopic)
except:
self.is_init_success = False
else:
self.is_init_success = True
def cb_action_topic(self, notif):
self.last_action_notif_type = notif.action_event
def wait_for_action_end_or_abort(self):
while not rospy.is_shutdown():
if (self.last_action_notif_type == ActionEvent.ACTION_END):
#rospy.loginfo("Received ACTION_END notification")
return True
elif (self.last_action_notif_type == ActionEvent.ACTION_ABORT):
#rospy.loginfo("Received ACTION_ABORT notification")
return False
#else:
#time.sleep(0.01)
def example_subscribe_to_a_robot_notification(self):
# Activate the publishing of the ActionNotification
req = OnNotificationActionTopicRequest()
rospy.loginfo("Activating the action notifications...")
try:
self.activate_publishing_of_action_notification(req)
except rospy.ServiceException:
rospy.logerr("Failed to call OnNotificationActionTopic")
return False
else:
rospy.loginfo("Successfully activated the Action Notifications!")
rospy.sleep(1.0)
return True
def example_clear_faults(self):
try:
self.clear_faults()
except rospy.ServiceException:
rospy.logerr("Failed to call ClearFaults")
return False
else:
rospy.loginfo("Cleared the faults successfully")
rospy.sleep(2.5)
return True
def example_home_the_robot(self):
# The Home Action is used to home the robot. It cannot be deleted and is always ID #2:
self.last_action_notif_type = None
req = ReadActionRequest()
req.input.identifier = self.HOME_ACTION_IDENTIFIER
try:
res = self.read_action(req)
except rospy.ServiceException:
rospy.logerr("Failed to call ReadAction")
return False
# Execute the HOME action if we could read it
else:
# What we just read is the input of the ExecuteAction service
req = ExecuteActionRequest()
req.input = res.output
rospy.loginfo("Sending the robot home...")
try:
self.execute_action(req)
except rospy.ServiceException:
rospy.logerr("Failed to call ExecuteAction")
return False
else:
return self.wait_for_action_end_or_abort()
def example_set_cartesian_reference_frame(self):
self.last_action_notif_type = None
# Prepare the request with the frame we want to set
req = SetCartesianReferenceFrameRequest()
req.input.reference_frame = CartesianReferenceFrame.CARTESIAN_REFERENCE_FRAME_MIXED
# Call the service
try:
self.set_cartesian_reference_frame()
except rospy.ServiceException:
rospy.logerr("Failed to call SetCartesianReferenceFrame")
return False
else:
rospy.loginfo("Set the cartesian reference frame successfully")
# Wait a bit
rospy.sleep(0.25)
return True
#Currently, this function send robot to a position base on the last position
#Not the absoute position
def example_send_cartesian_pose(self,dx,dy,dz):
self.last_action_notif_type = None
# Get the actual cartesian pose to increment it
# You can create a subscriber to listen to the base_feedback
# Here we only need the latest message in the topic though
feedback = rospy.wait_for_message("/" + self.robot_name + "/base_feedback", BaseCyclic_Feedback)
req = PlayCartesianTrajectoryRequest()
#dx,dy,dz should all be something like 0.1-0.5 the unit is probably meter
req.input.target_pose.x = feedback.base.commanded_tool_pose_x + dx
req.input.target_pose.y = feedback.base.commanded_tool_pose_y + dy
req.input.target_pose.z = feedback.base.commanded_tool_pose_z + dz
req.input.target_pose.theta_x = feedback.base.commanded_tool_pose_theta_x
req.input.target_pose.theta_y = feedback.base.commanded_tool_pose_theta_y
req.input.target_pose.theta_z = feedback.base.commanded_tool_pose_theta_z
pose_speed = CartesianSpeed()
pose_speed.translation = 0.1
pose_speed.orientation = 15
# The constraint is a one_of in Protobuf. The one_of concept does not exist in ROS
# To specify a one_of, create it and put it in the appropriate list of the oneof_type member of the ROS object :
req.input.constraint.oneof_type.speed.append(pose_speed)
# Call the service
rospy.loginfo("Sending the robot to the cartesian pose...")
try:
self.play_cartesian_trajectory(req)
except rospy.ServiceException:
rospy.logerr("Failed to call PlayCartesianTrajectory")
return False
else:
return self.wait_for_action_end_or_abort()
def example_send_joint_angles(self, angle_received):
#rospy.loginfo("Receive Angle:" + str(angle_received.axes[0]))
self.last_action_notif_type = None
# Create the list of angles
req = PlayJointTrajectoryRequest()
# Here the arm is vertical (all zeros)
for i in range(self.degrees_of_freedom):
temp_angle = JointAngle()
temp_angle.joint_identifier = i
if i == 0:
temp_angle.value = angle_received.data
else:
temp_angle.value = 50
req.input.joint_angles.joint_angles.append(temp_angle)
# Send the angles
#rospy.loginfo("Sending the robot vertical...")
try:
self.play_joint_trajectory(req)
except rospy.ServiceException:
rospy.logerr("Failed to call PlayJointTrajectory")
return False
else:
return self.wait_for_action_end_or_abort()
def example_send_gripper_command(self, value):
# Initialize the request
# Close the gripper
#rospy.loginfo("Receive Angle:" + str(value.buttons[5]))
req = SendGripperCommandRequest()
finger = Finger()
finger.finger_identifier = 1
finger.value = value.buttons[5]
req.input.gripper.finger.append(finger)
req.input.mode = GripperMode.GRIPPER_POSITION
#rospy.loginfo("Sending the gripper command...")
# Call the service
try:
self.send_gripper_command(req)
except rospy.ServiceException:
rospy.logerr("Failed to call SendGripperCommand")
return False
else:
#time.sleep(0.5)
return True
def main(self):
# For testing purposes
success = self.is_init_success
try:
rospy.delete_param("/kortex_examples_test_results/full_arm_movement_python")
except:
pass
if success:
#*******************************************************************************
# Make sure to clear the robot's faults else it won't move if it's already in fault
success &= self.example_clear_faults()
#*******************************************************************************
#*******************************************************************************
# Activate the action notifications
success &= self.example_subscribe_to_a_robot_notification()
#*******************************************************************************
#*******************************************************************************
# Move the robot to the Home position with an Action
success &= self.example_home_the_robot()
#*******************************************************************************
#*******************************************************************************
# Example of gripper command
# Let's fully open the gripper
if self.is_gripper_present:
success &= self.example_send_gripper_command(0.0)
else:
rospy.logwarn("No gripper is present on the arm.")
#*******************************************************************************
#*******************************************************************************
# Set the reference frame to "Mixed"
success &= self.example_set_cartesian_reference_frame()
# Example of cartesian pose
# Let's make it move in Z
#*******************************************************************************
#*******************************************************************************
# Example of angular position
# Let's send the arm to vertical position
#success &= self.example_send_joint_angles(30)
#*******************************************************************************
#*******************************************************************************
# Example of gripper command
# Let's close the gripper at 50%
if self.is_gripper_present:
success &= self.example_send_gripper_command(0.5)
else:
rospy.logwarn("No gripper is present on the arm.")
# For testing purposes
rospy.set_param("/kortex_examples_test_results/full_arm_movement_python", success)
if not success:
rospy.logerr("The example encountered an error.")
def run(self):
#print "running"
rospy.spin()
if __name__ == "__main__":
ex = ExampleFullArmMovement()
ex.example_home_the_robot()
ex.run()