forked from BuddhiGamage/chess_robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.py
More file actions
208 lines (164 loc) · 7.45 KB
/
move.py
File metadata and controls
208 lines (164 loc) · 7.45 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
#! /usr/bin/env python3
###
# KINOVA (R) KORTEX (TM)
#
# Copyright (c) 2021 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 os
import time
import threading
from kortex_api.autogen.client_stubs.BaseClientRpc import BaseClient
from kortex_api.autogen.client_stubs.BaseCyclicClientRpc import BaseCyclicClient
from kortex_api.autogen.messages import Base_pb2, BaseCyclic_pb2, Common_pb2
# Maximum allowed waiting time during actions (in seconds)
TIMEOUT_DURATION = 30
# Create closure to set an event after an END or an ABORT
def check_for_end_or_abort(e):
"""Return a closure checking for END or ABORT notifications
Arguments:
e -- event to signal when the action is completed
(will be set when an END or ABORT occurs)
"""
def check(notification, e = e):
print("EVENT : " + \
Base_pb2.ActionEvent.Name(notification.action_event))
if notification.action_event == Base_pb2.ACTION_END \
or notification.action_event == Base_pb2.ACTION_ABORT:
e.set()
return check
def example_move_to_home_position(base):
# Make sure the arm is in Single Level Servoing mode
base_servo_mode = Base_pb2.ServoingModeInformation()
base_servo_mode.servoing_mode = Base_pb2.SINGLE_LEVEL_SERVOING
base.SetServoingMode(base_servo_mode)
# Move arm to ready position
print("Moving the arm to a safe position")
action_type = Base_pb2.RequestedActionType()
action_type.action_type = Base_pb2.REACH_JOINT_ANGLES
action_list = base.ReadAllActions(action_type)
action_handle = None
for action in action_list.action_list:
if action.name == "Retract":
action_handle = action.handle
if action_handle == None:
print("Can't reach safe position. Exiting")
return False
e = threading.Event()
notification_handle = base.OnNotificationActionTopic(
check_for_end_or_abort(e),
Base_pb2.NotificationOptions()
)
base.ExecuteActionFromReference(action_handle)
finished = e.wait(TIMEOUT_DURATION)
base.Unsubscribe(notification_handle)
if finished:
print("Safe position reached")
else:
print("Timeout on action notification wait")
return finished
def populateCartesianCoordinate(waypointInformation):
waypoint = Base_pb2.CartesianWaypoint()
waypoint.pose.x = waypointInformation[0]
waypoint.pose.y = waypointInformation[1]
waypoint.pose.z = waypointInformation[2]
waypoint.blending_radius = waypointInformation[3]
waypoint.pose.theta_x = waypointInformation[4]
waypoint.pose.theta_y = waypointInformation[5]
waypoint.pose.theta_z = waypointInformation[6]
waypoint.reference_frame = Base_pb2.CARTESIAN_REFERENCE_FRAME_BASE
return waypoint
def example_trajectory(base, base_cyclic):
base_servo_mode = Base_pb2.ServoingModeInformation()
base_servo_mode.servoing_mode = Base_pb2.SINGLE_LEVEL_SERVOING
base.SetServoingMode(base_servo_mode)
product = base.GetProductConfiguration()
waypointsDefinition = tuple(tuple())
if( product.model == Base_pb2.ProductConfiguration__pb2.MODEL_ID_L53
or product.model == Base_pb2.ProductConfiguration__pb2.MODEL_ID_L31):
if(product.model == Base_pb2.ProductConfiguration__pb2.MODEL_ID_L31):
kTheta_x = 90.6
kTheta_y = -1.0
kTheta_z = 150.0
waypointsDefinition = ( (0.439, 0.194, 0.448, 0.0, kTheta_x, kTheta_y, kTheta_z),
(0.200, 0.150, 0.400, 0.0, kTheta_x, kTheta_y, kTheta_z),
(0.350, 0.050, 0.300, 0.0, kTheta_x, kTheta_y, kTheta_z))
else:
kTheta_x = 90.0
kTheta_y = 0.0
kTheta_z = 90.0
waypointsDefinition = ( (0.7, 0.0, 0.5, 0.0, kTheta_x, kTheta_y, kTheta_z),
(0.7, 0.0, 0.33, 0.1, kTheta_x, kTheta_y, kTheta_z),
(0.7, 0.48, 0.33, 0.1, kTheta_x, kTheta_y, kTheta_z),
(0.61, 0.22, 0.4, 0.1, kTheta_x, kTheta_y, kTheta_z),
(0.7, 0.48, 0.33, 0.1, kTheta_x, kTheta_y, kTheta_z),
(0.63, -0.22, 0.45, 0.1, kTheta_x, kTheta_y, kTheta_z),
(0.65, 0.05, 0.33, 0.0, kTheta_x, kTheta_y, kTheta_z))
else:
print("Product is not compatible to run this example please contact support with KIN number bellow")
print("Product KIN is : " + product.kin())
waypoints = Base_pb2.WaypointList()
waypoints.duration = 0.0
waypoints.use_optimal_blending = False
index = 0
for waypointDefinition in waypointsDefinition:
waypoint = waypoints.waypoints.add()
waypoint.name = "waypoint_" + str(index)
waypoint.cartesian_waypoint.CopyFrom(populateCartesianCoordinate(waypointDefinition))
index = index + 1
# Verify validity of waypoints
result = base.ValidateWaypointList(waypoints);
if(len(result.trajectory_error_report.trajectory_error_elements) == 0):
e = threading.Event()
notification_handle = base.OnNotificationActionTopic( check_for_end_or_abort(e),
Base_pb2.NotificationOptions())
print("Moving cartesian trajectory...")
base.ExecuteWaypointTrajectory(waypoints)
print("Waiting for trajectory to finish ...")
finished = e.wait(TIMEOUT_DURATION)
base.Unsubscribe(notification_handle)
if finished:
print("Cartesian trajectory with no optimization completed ")
e_opt = threading.Event()
notification_handle_opt = base.OnNotificationActionTopic( check_for_end_or_abort(e_opt),
Base_pb2.NotificationOptions())
waypoints.use_optimal_blending = True
base.ExecuteWaypointTrajectory(waypoints)
print("Waiting for trajectory to finish ...")
finished_opt = e_opt.wait(TIMEOUT_DURATION)
base.Unsubscribe(notification_handle_opt)
if(finished_opt):
print("Cartesian trajectory with optimization completed ")
else:
print("Timeout on action notification wait for optimized trajectory")
return finished_opt
else:
print("Timeout on action notification wait for non-optimized trajectory")
return finished
else:
print("Error found in trajectory")
result.trajectory_error_report.PrintDebugString();
def main():
# Import the utilities helper module
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import utilities
# Parse arguments
args = utilities.parseConnectionArguments()
# Create connection to the device and get the router
with utilities.DeviceConnection.createTcpConnection(args) as router:
# Create required services
base = BaseClient(router)
base_cyclic = BaseCyclicClient(router)
# Example core
success = True
success &= example_move_to_home_position(base)
success &= example_trajectory(base, base_cyclic)
return 0 if success else 1
# if __name__ == "__main__":
# exit(main())