-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
294 lines (261 loc) · 11.4 KB
/
test.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
import json
import time
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.models import CloudToDeviceMethod
# from URGripper.ur_gripper import UrGripper
# from azure_iot.URGripper import URGripper
# from URGripper.cmodel_urcap import RobotiqCModelURCap
from model.joint_position_model import JointPositionModel
from model.move_j_command_model import MoveJCommandModel
from model.test.PayloadResponseModel import PayloadResponseModel
home_joint_position_model = JointPositionModel.get_joint_position_model_using_arguments(
base=270, shoulder=-110, elbow=150, wrist1=-130, wrist2=270, wrist3=0)
retract_ct_01_joint_position_model = JointPositionModel.get_joint_position_model_using_arguments(
base=270.80, shoulder=-89.49, elbow=150.18, wrist1=-58.70, wrist2=92.85, wrist3=180)
target_ct_01_joint_position_model = JointPositionModel.get_joint_position_model_using_arguments(
base=272.88, shoulder=-75.48, elbow=131.96, wrist1=-54.50, wrist2=90.90, wrist3=179.95)
cg_joint_position_model = JointPositionModel.get_joint_position_model_using_arguments(
base=129.8, shoulder=-98.45, elbow=164.32, wrist1=-66.57, wrist2=43.25, wrist3=180)
retract_bottom_position_model = JointPositionModel.get_joint_position_model_using_arguments(
base=119.65, shoulder=-132.21, elbow=122.03, wrist1=10.9, wrist2=29.33, wrist3=179.34)
target_bottom_position_model = JointPositionModel.get_joint_position_model_using_arguments(
base=150.09, shoulder=-107.03, elbow=110.14, wrist1=-2.62, wrist2=59.79, wrist3=179.77)
connection_string = ("HostName=cobot-iot-hub.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=PAhVCG2kzRH+pQDKinnpwcO1M5T/YOp9RAIoTMjZuzw=")
ur_cobot = "URCobot"
ur_gripper = "URGripper"
class LoadingHandler:
def __init__(self, successor=None):
self.successor = successor
def handle(self):
if self.successor:
self.successor.handle()
def move_j(joint_position_model_array):
move_j_command_model = MoveJCommandModel.get_move_j_command_model_using_arguments(
acceleration=0.5, velocity=0.5, time_s=0, blend_radius=0,
joint_position_model_array=joint_position_model_array)
method_payload = json.dumps(move_j_command_model, default=lambda o: o.__dict__, indent=1)
device_method = CloudToDeviceMethod(method_name="MoveJCommand", payload=method_payload)
registry_manager = IoTHubRegistryManager(connection_string)
response = registry_manager.invoke_device_method(ur_cobot, device_method)
json_response = json.loads(response.payload, object_hook=lambda d: PayloadResponseModel(**d))
print(json_response)
return json_response
def open_gripper():
device_method = CloudToDeviceMethod(method_name="OpenGripperCommand")
registry_manager = IoTHubRegistryManager(connection_string)
response = registry_manager.invoke_device_method(ur_gripper, device_method)
json_response = json.loads(response.payload, object_hook=lambda d: PayloadResponseModel(**d))
print(json_response)
return json_response
def close_gripper():
device_method = CloudToDeviceMethod(method_name="CloseGripperCommand")
registry_manager = IoTHubRegistryManager(connection_string)
response = registry_manager.invoke_device_method(ur_gripper, device_method)
json_response = json.loads(response.payload, object_hook=lambda d: PayloadResponseModel(**d))
print(json_response)
return json_response
def activate_gripper():
device_method = CloudToDeviceMethod(method_name="ActivateGripperCommand")
registry_manager = IoTHubRegistryManager(connection_string)
response = registry_manager.invoke_device_method(ur_gripper, device_method)
json_response = json.loads(response.payload, object_hook=lambda d: PayloadResponseModel(**d))
print(json_response)
return json_response
def pause_command():
print("enable_free_drive")
device_method = CloudToDeviceMethod(method_name="PauseCommand")
registry_manager = IoTHubRegistryManager(connection_string)
response = registry_manager.invoke_device_method(ur_cobot, device_method)
json_response = json.loads(response.payload, object_hook=lambda d: PayloadResponseModel(**d))
print(json_response)
return json_response
def start_free_drive_gripper():
print(connection_string)
device_method = CloudToDeviceMethod(method_name="StartFreeDriveModeCommand")
registry_manager = IoTHubRegistryManager(connection_string)
response = registry_manager.invoke_device_method(ur_cobot, device_method)
json_response = json.loads(response.payload, object_hook=lambda d: PayloadResponseModel(**d))
print(json_response)
#
# class MoveHomeToRetractUnloadingPositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [home_joint_position_model, retract_ct_01_joint_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# super().handle()
# except Exception:
# raise Exception
#
#
# class MoveRetractUnloadingToTargetUnloadingPositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [retract_ct_01_joint_position_model, target_ct_01_joint_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# super().handle()
# except Exception:
# raise Exception
#
#
# class MoveTargetUnloadingToRetractUnloadingPositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [target_ct_01_joint_position_model, retract_ct_01_joint_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# super().handle()
# except Exception:
# raise Exception
#
#
# class MoveRetractUnloadingToCgPositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [retract_ct_01_joint_position_model, cg_joint_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# super().handle()
# except Exception:
# raise Exception
#
#
# class MoveCgToRetractLoadingPositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [cg_joint_position_model, retract_bottom_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# super().handle()
# except Exception:
# raise Exception
#
#
# class MoveRetractLoadingToTargetLoadingPositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [retract_bottom_position_model, target_bottom_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# super().handle()
# except Exception:
# raise Exception
#
#
# class MoveTargetLoadingToRetractLoadingPositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [target_bottom_position_model, retract_bottom_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# super().handle()
# except Exception:
# raise Exception
#
#
# class MoveRetractLoadingToCgPositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [retract_bottom_position_model, cg_joint_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# super().handle()
# except Exception:
# raise Exception
#
#
# class MoveCgToHomePositionHandler(LoadingHandler):
# def handle(self):
# try:
# joint_position_model_array = [cg_joint_position_model, home_joint_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# except Exception:
# raise Exception
#
#
# class ActivateGripperHandler(LoadingHandler):
# def handle(self):
# try:
# ur_gripper = UrGripper("10.2.12.109")
# ur_gripper.activate()
# super().handle()
# except Exception:
# raise Exception
#
#
# class OpenGripperHandler(LoadingHandler):
# def handle(self):
# try:
# ur_gripper = UrGripper("127.0.0.1")
# ur_gripper.open_gripper()
# super().handle()
# except Exception:
# raise Exception
#
#
# class CloseGripperHandler(LoadingHandler):
# def handle(self):
# try:
# ur_gripper = UrGripper("127.0.0.1")
# ur_gripper.close_gripper()
# super().handle()
# except Exception:
# raise Exception
if __name__ == '__main__':
start_free_drive_gripper()
# joint_position_model_array = [home_joint_position_model, retract_ct_01_joint_position_model]
# move_j(joint_position_model_array=joint_position_model_array)
# open_gripper()
# pause_command()
# ur_gripper = URGripper("127.0.0.1")
# ur_gripper.open_gripper()
# ur_gripper.close_gripper()
# close_gripper_payload_response_model = close_gripper()
# print(close_gripper_payload_response_model)
#
# move_j_home_to_approach_position_payload_response_model = move_j_home_to_approach_position()
# print(move_j_home_to_approach_position_payload_response_model)
#
# open_gripper_payload_response_model = open_gripper()
# print(open_gripper_payload_response_model)
# Client code
# handlerA = ActivateGripperHandler()
# handlerB = CloseGripperHandler()
# handlerC = MoveHomeToRetractUnloadingPositionHandler()
# handlerD = OpenGripperHandler()
# handlerE = MoveRetractUnloadingToTargetUnloadingPositionHandler()
# handlerF = CloseGripperHandler()
# handlerG = MoveTargetUnloadingToRetractUnloadingPositionHandler()
# handlerH = MoveRetractUnloadingToCgPositionHandler()
# # handlerE = MoveCgToRetractLoadingPositionHandler()
# # handlerF = MoveRetractLoadingToTargetLoadingPositionHandler()
# # GripperHandlerC = OpenGripperHandler()
# # handlerG = MoveTargetLoadingToRetractLoadingPositionHandler()
# # GripperHandlerD = CloseGripperHandler()
# # handlerH = MoveRetractLoadingToCgPositionHandler()
# # handlerI = MoveCgToHomePositionHandler()
#
#
# handlerA.successor = handlerB
# handlerB.successor = handlerC
# handlerC.successor = handlerD
# handlerD.successor = handlerE
# handlerE.successor = handlerF
# handlerF.successor = handlerG
# handlerG.successor = handlerH
# handlerD.successor = handlerE
# handlerE.successor = handlerF
# handlerF.successor = GripperHandlerC
# GripperHandlerC.successor = handlerG
# handlerG.successor = GripperHandlerD
# GripperHandlerD.successor = handlerH
# handlerH.successor = handlerI
# handlerA.handle()
# try:
# # robot = urx.Robot("192.168.1.6")
# robot = urx.Robot("10.2.12.109")
# # robot = urx.Robot("localhost")
# print("Robot object is available as robot or r")
# except Exception:
# print("exception")
# gripper.move_and_wait_for_pos(100, 255, 255)
# gripper.move_and_wait_for_pos(150, 255, 255)
# gripper.move_and_wait_for_pos(100, 255, 255)
# robot = URGripper.Robot("10.2.12.109")
# robotic_gripper = Robotiq_Two_Finger_Gripper(robot)
# robotic_gripper.close_gripper()
# robot.send_program(robotic_gripper._get_new_urscript)
# robot.close()