-
Notifications
You must be signed in to change notification settings - Fork 0
/
yuAction.py
233 lines (210 loc) · 5.95 KB
/
yuAction.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
#! /usr/bin/python3
# -*- coding: utf-8 -*-
'''
| author:
| Belal HMEDAN,
| LIG lab/ Marvin Team,
| France, 2021.
| Action Handeling script.
'''
import subprocess
from RWS4YuMi import RWS4YuMi
from time import time
from PyQt5.QtCore import QTimer
#======================
# class actionHandler |
#======================
class actionHandler():
def __init__(self, plan):
"""
Class actionHandler: map the plan into Rapid code.
---
Parameters:
@param: plan, list of strings list, each string list is an action with parameters.
"""
self.plan = plan
self.actionTimer = None
self.api = RWS4YuMi()
def pointCoords(self, point, filepath='points.txt', name=None):
"""
Function: pointCoords, to map the string point 'p_yy_xx' into robtarget.
---
Parameters:
@param: point, string, the point to be written to RAPID code: ['p_yy_xx', 'p_yy_xxVS', 'p_yy_xxHA', 'p_yy_xxHC']
@param: filepath, string, thae path to the RAPID code to be modified.
---
@return: None.
"""
with open(filepath, 'r') as fp:
line = True
while(line):
line = fp.readline()
if(len(line.split())>2):
if(line.split()[2]==point):
if name is not None:
line = line.split()
line[2] = name
line = " ".join(line)
return line
print("Point {} Not Found!".format(point))
return None
def writeLine(self, point, filepath='Rapid/currLeft.mod'):
"""
Function: writeLine, to write the point robtarget into the RAPID code.
---
Parameters:
@param: point, string, the point to be written to RAPID code: ['p_yy_xx', 'p_yy_xxVS', 'p_yy_xxHA', 'p_yy_xxHC']
@param: filepath, string, thae path to the RAPID code to be modified.
---
@return: None.
"""
name = 'pickpoint'
lineIndex = 10
if len(point)>7 :
name = 'placepoint'
lineIndex = 11
line = " "+self.pointCoords(point, name=name)
with open(filepath, 'r') as fp:
codeList = fp.readlines()
with open(filepath, 'w') as fp:
codeList[lineIndex-1] = line+"\n"
fp.writelines(codeList)
def actionInterpreter(self, verbose=False):
"""
Function: actionInterpreter, to interpret the plan into pick & place positions, and neighbours.
---
Parameters:
@param: None
---
@return: neighborhood, list of point strings to be passed to the GUI.
"""
rotAngle = 0
legoDict = {
# y_2x2
'y_2x2_11' : 'p_20_00',
'y_2x2_12' : 'p_21_00',
'y_2x2_13' : 'p_22_00',
'y_2x2_14' : 'p_23_00',
# b_2x2
'b_2x2_41' : 'p_20_06',
'b_2x2_42' : 'p_21_06',
'b_2x2_43' : 'p_22_06',
'b_2x2_44' : 'p_23_06',
# y_2x4
'y_2x4_21_L' : 'p_21_02',
'y_2x4_22_L' : 'p_23_02',
'y_2x4_31_L' : 'p_21_04',
'y_2x4_32_L' : 'p_23_04',
'y_2x4_21_R' : 'p_20_02',
'y_2x4_22_R' : 'p_22_02',
'y_2x4_31_R' : 'p_20_04',
'y_2x4_32_R' : 'p_22_04',
# b_2x4
'b_2x4_51_R' : 'p_18_08',
'b_2x4_52_R' : 'p_20_08',
'b_2x4_53_R' : 'p_22_08',
'b_2x4_41_R' : 'p_18_06',
'b_2x4_51_L' : 'p_19_08',
'b_2x4_52_L' : 'p_21_08',
'b_2x4_53_L' : 'p_23_08',
'b_2x4_41_L' : 'p_19_06'
}
rotDict = {
# 2x2
'p_20_00' : ['HA', 'HC'],
'p_21_00' : ['HA', 'HC'],
'p_22_00' : ['HA', 'HC'],
'p_23_00' : ['HA', 'HC'],
'p_20_06' : ['HA', 'HC'],
'p_21_06' : ['HA', 'HC'],
'p_22_06' : ['HA', 'HC'],
'p_23_06' : ['HA', 'HC'],
# 2x4
'p_23_02' : ['HA', 'HC'],
'p_22_02' : ['HA', 'HC'],
'p_21_02' : ['HA', 'HC'],
'p_20_02' : ['HA', 'HC'],
'p_23_08' : ['HA', 'HC'],
'p_22_08' : ['HA', 'HC'],
'p_21_08' : ['HA', 'HC'],
'p_20_08' : ['HA', 'HC'],
'p_19_08' : ['HA', 'HC'],
'p_18_08' : ['HA', 'HC'],
'p_23_04' : ['HA', 'HC'],
'p_22_04' : ['HA', 'HC'],
'p_21_04' : ['HA', 'HC'],
'p_20_04' : ['HA', 'HC'],
'p_19_06' : ['HA', 'HC'],
'p_18_06' : ['HA', 'HC'],
# swap
'p_07_06' : ['HC', 'HC'],
'p_07_07' : ['HC', 'HC']
}
# action is a list of action and related params
for action in self.plan:
action = action[action.index('(')+1:action.index(')')].split()
# if(verbose):
# print(action)
if action[0]=='hold_gripper':
pickPos = legoDict[action[1]]
elif action[0]=='hold_gripper_right':
pickPos = legoDict[action[1]+'_R']
elif action[0]=='hold_gripper_left':
pickPos = legoDict[action[1]+'_L']
elif action[0] in ['put_2x2_v', 'put_2x2_h']:
placePos = action[-3]+"VS"
# print(placePos)
neighborhood = action[-3:]
elif action[0] in ['put_upper_4x2', 'put_lower_4x2', 'put_left_2x4', 'put_right_2x4']:
placePos = action[-4]+"VS"
# print(placePos)
neighborhood = action[-4:]
elif action[0] in ['rotate_2x2_loaded_gripper_v2h', 'rotate_2x4_right_loaded_gripper_v2h_clk', 'rotate_2x4_left_loaded_gripper_v2h_clk']:
rotAngle = 90
elif action[0] in ['rotate_2x4_right_loaded_gripper_v2h_anticlk', 'rotate_2x4_left_loaded_gripper_v2h_anticlk']:
rotAngle = -90
if rotAngle == 90:
placePos = placePos[:-2]+rotDict[pickPos][1]
elif rotAngle == -90:
placePos = placePos[:-2]+rotDict[pickPos][0]
self.writeLine(pickPos)
self.writeLine(placePos)
if verbose:
print("points: ",pickPos, placePos)
return neighborhood
def action(self):
"""
Function: action, to excute an action by sending RAPID code to the robot.
---
Parameters:
@param: None
---
@return: None.
"""
apiPath = "G:/Grenoble/Semester_4/Project_Codes/Theabut_API/rwsclient4yu"
cmd = ['gradlew', 'run']
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, cwd=apiPath)
# output = result.stdout.decode('utf-8')[1:]
# print(output)
def actionEnded(self):
"""
Function: actionEnded, to check if the robot action has ended.
---
Parameters:
@param: None
---
@return: boolean, True if the action ended, False in case of Errors.
"""
status = self.api.actionStatus()
if status!='stopped':
return False
elif status=='stopped':
return True
#----------------------------------
# ah = actionHandler([])
# # # # ah.actionMapper()
# # # # tic = time()
# ah.action()
# if(ah.actionEnded()):
# toc = time()
# print('Done within: {} seconds.'.format(round(toc-tic, 3)))