import math
import re
[docs]
class Position:
def __init__(self, *, x=0, y=0, z=0, rx=0, ry=0, rz=0):
self._x = x
self._y = y
self._z = z
self._rx = rx
self._ry = ry
self._rz = rz
@property
def x(self):
return self._x
@property
def y(self):
return self._y
@property
def z(self):
return self._z
@property
def rx(self):
return self._rx
@property
def ry(self):
return self._ry
@property
def rz(self):
return self._rz
[docs]
class Instruction:
def __init__(self, *, index, position):
self._index = index
self._position = position
self._velocity = 0.0
self._frequency = 0.0
[docs]
def setVelocity(self, value):
self._velocity = value
[docs]
def setFrequency(self, value):
self._frequency = value
@property
def index(self):
return self._index
@property
def position(self):
return self._position
@property
def velocity(self):
return self._velocity
@property
def frequency(self):
return self._frequency
[docs]
class RobotInstructions:
def __init__(self, filePath):
self._filePath = filePath
[docs]
def importInstructions(self):
self._readFileLines()
self._readPositions()
self._readVelsAndFreqs()
[docs]
def exportInstructions(self, userProcess):
number = 1
X = 0.0
Y = 0.0
Z = 0.0
W = 0.0
P = 0.0
R = 0.0
instructions = []
positions = []
items = userProcess
for idx in range(len(items)):
prevItem = items[idx - 1] if idx > 0 else None
item = items[idx]
nextItem = items[idx + 1] if idx < len(items) - 1 else None
line = str(number) + ':L P[' + str(number) + '] R[' + str(item.velocity) + ']mm/sec CNT100 RTCP'
if prevItem is not None:
if prevItem.frequency == 0.0:
if item.frequency != 0.0:
line += ' LASER ON'
if nextItem is not None:
if nextItem.frequency == 0.0:
if item.frequency != 0.0:
line += ' LASER OFF'
line += ';\n'
instructions.append(line)
X += item.dx
Y += item.dy
Z += item.dz
W += item.rx
P += item.ry
R += item.rz
line = ''
line += 'P[' + str(number) + '] {' + '\n'
line += ' GP1 :' + '\n'
line += ' UF : 5, UT : 5, CONFIG : \'F U T,0,0,0\',' + '\n'
line += ' X = ' + str(X) + ' mm, Y = ' + str(Y) + 'mm, Z = ' + str(Z) + ' mm,' + '\n'
line += ' W = ' + str(W) + ' deg, P = ' + str(P) + ' deg, R = ' + str(R) + ' deg' + '\n'
line += '};' + '\n'
number += 1
positions.append(line)
with open(self._filePath, 'w') as file:
lines = []
lines.extend(instructions)
lines.append('/POS' + '\n')
lines.extend(positions)
file.writelines(lines)
def _readFileLines(self):
self._fileLines = []
self._fileData = ''
try:
with open(self._filePath, 'r') as file:
self._fileData = file.read()
with open(self._filePath, 'r') as file:
self._fileLines = file.readlines()
except Exception:
pass
def _readPositions(self):
self._instructions = []
positionsBlockPattern = re.compile(r'/POS(.*?)/END', re.DOTALL)
match = positionsBlockPattern.search(self._fileData)
if match:
positionsBlockContent = match.group(1).strip()
positionPattern = re.compile(r'P\[[0-9]+\].*?\n\}\;', re.DOTALL)
positions = positionPattern.findall(positionsBlockContent)
for position in positions:
index = int(re.findall(r'P\[([0-9]+)\]', position)[0])
coordsPattern = re.compile(r'X.*?([\d.-]+).*?Y.*?([\d.-]+).*?Z.*?([\d.-]+)')
match = coordsPattern.search(position)
if match:
x = float(match.group(1))
y = float(match.group(2))
z = float(match.group(3))
coordsPattern = re.compile(r'W.*?([\d.-]+).*?P.*?([\d.-]+).*?R.*?([\d.-]+)')
match = coordsPattern.search(position)
if match:
rx = math.radians(float(match.group(1)))
ry = math.radians(float(match.group(2)))
rz = math.radians(float(match.group(3)))
position = Position(x=x, y=y, z=z,
rx=rx, ry=ry, rz=rz)
self._instructions.append(
Instruction(index=index,
position=position)
)
def _readVelsAndFreqs(self):
lineIdx = 0
laserON = False
for line in self._fileLines:
lineIdx += 1
if '/POS' in line:
break
index = False
try:
index = int(re.findall(r'P\[([0-9]+)\]', line)[0])
except Exception:
continue
if 'LASER ON' in line:
if 'OFF' in line:
laserON = False
else:
laserON = True
if 'mm/sec' in line:
velVar = re.findall(r'\w(.*?)mm/sec\b', line)[0]
velVar = ':' + velVar.split()[-1]
velVar = re.escape(velVar)
for line in reversed(self._fileLines[1:lineIdx]):
velocity = re.findall(velVar + r'(.*)', line)
if len(velocity) > 0:
pattern = r'\b\d+\.?\d*\b'
velocity = float(re.findall(pattern, velocity[0])[0])
break
else:
velocity = 0.0
for instruction in self._instructions:
if index == instruction.index:
instruction.setVelocity(velocity)
if laserON:
instruction.setFrequency(1.0)
else:
instruction.setFrequency(0.0)
break
@property
def instructions(self):
return self._instructions