(How To) Using Python Based Move Preprocessors
(How To) Using Python Based Move Preprocessors
MachSim has the capability of processing each move with a custom Python script.
You can use it to:
a) change the axis values
b) synthesize new values. (Insert new moves in the simulation)
To enable it, add a line like this to the machine .xml file:
fileName is the path to python script that needs to be processed. It can be relative to machine directory or full path.
instance is the name of the python object declared in script. It will be used to process each move.
type is the type of preprocessing. When missing it is assumed that preprocessing will change axis values. When declared as
"inserter" it will insert additional moves into the simulation.
One important thing to remember is that changing axis values is done via method ProcessMove and insertion of new moves is
done via GetNewMoves nethod. The discussion further will treat methods separatelly as there are differencies in the parameters
passed to them.
a) xml tag
<preprocessor fileName="module1.py" instance="move_filter" />
b) module1.py
#-------------------------------------------------------
import sys
class Module1:
VERSION = 1
TYPE = "move"
AXIS = []
def __init__(self):
self.count = 0
self.f = open(__file__+".log", "w+")
move_filter = Module1()
#-------------------------------------------------------
Start machine simulation, load machine definition. It should be enough to get a call to move_filter.ProcessMove.
In the machine directory look for log file module1.py.log
It may contain a string similar with: python version 2.7.1
b) module1.py
#-------------------------------------------------------
class Module1:
VERSION = 1
TYPE = "move"
AXIS = []
move_filter = Module1()
#-------------------------------------------------------
Obviously the script above does nothing. It just declares the minimum necessary to change axis values.
<machine_definition>
<machine_data name="5AxHeadHead" version="1.7" units="metric" />
<preprocessor fileName="module1.py" instance="move_filter" />
<axis id="X" type="translation" ...
b) module1.py
#-------------------------------------------------------
class Module1:
VERSION = 1
TYPE = "move"
AXIS = ['X','Y','Z','C','B']
def __init__(self):
self.f = open(__file__+".log", "w+")
self.count = 0
return
operation: {
'comment': '"Sample 5 axis operation"',
'tool': {
'comment': ('"Sample tool"',),
'number': 1,
'name': 'tool'
},
'number': 1,
'transform': {
'workpiece_transform': (1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0),
'holder_transform': (1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, -12.0, 0.0, 0.0, 0.0, 1.0)
},
'size': 434}
move: {
'simulationIndex': 121,
'axisValue': {'Y': 1.5460000038146973, 'X': -48.779998779296875, 'C': 0.0, 'Z': 18.0, 'B': 0.0},
'globalIndex': 120
}
#-------------------------------------------------------
d) ProcessMove parameters:
operation [ 'tool' ] [ 'comment' ] => comment for the first tool of the current operation
operation [ 'tool' ] [ 'number' ] => number of the first tool of the current operation
operation [ 'tool' ] [ 'name' ] => name of the first tool of the current operation
operation [ 'transform' ] [ 'workpiece_transform' ] => a tuple with 16 values representing the 4x4 matrix applied to machine's
workpiece_transform.
operation [ 'transform' ] [ 'holder_transform' ] => a tuple with 16 values representing the 4x4 matrix applied to machine's
holder_transform.
move [ 'simulationIndex' ] => 1 based index of current move acczoss entire simulation
move [ 'globalIndex' ] = 0 based index of current move across entire simulation.
move [ 'axisValue' ] [ 'Y' ] => provide access to axis value. Changed value will be visible to machine simulation.
b) module1.py
#-------------------------------------------------------
class Module1:
VERSION = 1
TYPE = "move"
AXIS = ['X','Y','Z','C','B']
preserve_axes_limit = Module1()
#-------------------------------------------------------
a) xml tag
<preprocessor fileName="module1.py" instance="move_insert" type="inserter" />
b) module1.py
#-------------------------------------------------------
class Module1:
VERSION = 1
TYPE = "move"
AXIS = []
move_insert = Module1()
#-------------------------------------------------------
Obviously the script above does nothing. It just declares the minimum necessary to use insert of new values functionality. In this
mode modifing axis values of existing moves is not possible. Only isertion is possible.
a) xml tag
<preprocessor fileName="module1.py" instance="move_insert" type="inserter" />
b) module1.py
#-------------------------------------------------------
class Module1:
VERSION = 1
TYPE = "move"
AXIS = ['X','Y','Z','C','B']
def __init__(self):
self.f = open(__file__+".log", "w+")
self.count = 0
return
if move [ 'relativeIndex' ] == 0:
return insert_move_on_origin
move_insert = Module1()
#-------------------------------------------------------
move: {
'relativeIndex': 431,
'axisValue': {'Y': -6.306000232696533, 'X': -39.33300018310547, 'C': 0.0, 'Z': 14.050000190734863, 'B': 0.0}, 'time':
0.004000000189989805
}
nextmove: {
'relativeIndex': 431,
'axisValue': {'Y': -6.482999801635742, 'X': -45.29499816894531, 'C': 0.0, 'Z': 24.049999237060547, 'B': 0.0}, 'time':
0.006000000052154064
}
#-------------------------------------------------------
d) GetNewMoves parameters:
operation [ 'tool' ] [ 'comment' ] => comment for the first tool of the current operation
operation [ 'tool' ] [ 'number' ] => number of the first tool of the current operation
operation [ 'tool' ] [ 'name' ] => name of the first tool of the current operation
operation [ 'number' ] => current operation number
operation [ 'transform' ] [ 'workpiece_transform' ] => a tuple with 16 values representing the 4x4 matrix applied to machine's
workpiece_transform.
operation [ 'transform' ] [ 'holder_transform' ] => a tuple with 16 values representing the 4x4 matrix applied to machine's
holder_transform.
move [ 'relativeIndex' ] => 0 based index of current move across entire simulation
move [ 'axisValue' ] [ 'Y' ] => provide access to axis value. Changed value will NOT be visible to machine simulation.
move [ 'time' ] => time in seconds needed to complete this move when MachSim runs in time-base mode.
nextmove [ 'relativeIndex' ] => 0 based index of next move across entire simulation
nextmove [ 'axisValue' ] [ 'Y' ] => provide access to axis value. Changed value will NOT be visible to machine simulation.
nextmove [ 'time' ] => time in seconds needed to complete next move when MachSim runs in time-base mode.
insert_move_on_origin [ 'forwardInsertion' ] => 1 to insert move after current move or 0 to insert move before current move
insert_move_on_origin [ 'newMovesList' ] [ 0 ] [ 'axisValue' ] [ 'X' ] => X value for a new move at position 0 in list.
b) module1.py
#-------------------------------------------------------
class Module1:
VERSION = 1
TYPE = "move"
AXIS = ['X','Y','Z','C','B']
def __init__(self):
self.insert_moves_from_machine_origin = {
'forwardInsertion': 0,
'newMovesList': [{'axisValue': {'X':500, 'Y':500, 'Z':900, 'B':0, 'C':0} },
{'axisValue': {'X':500, 'Y':500, 'Z':850, 'B':0, 'C':0} }
]}
insert_new_moves = Module1()
#-------------------------------------------------------
LOGGING_ENABLED = True
def Log(text):
if LOGGING_ENABLED:
f = open(LOG_FILE, "a+t")
f.write(text)
f.close()
class DoorClosePreprocessorBase(object):
class DoorClosePreprocessor(object):
VERSION = 1
TYPE = "move"
AXIS = ["LH-Door", "RH-Door"]
def __init__(self):
stepsToClose = 50
self.__leftDoor = DoorClosePreprocessorBase("LH-Door", 0.0, -499.0, stepsToClose)
self.__rightDoor = DoorClosePreprocessorBase("RH-Door", 0.0, 499.0, stepsToClose)
class WarningLightPreprocessor(object):
VERSION = 1
TYPE = "move"
AXIS = ["Light"]
doorClosePreprocessor = DoorClosePreprocessor()
warningLightPreprocessor = WarningLightPreprocessor()
Log("Preprocessor module imported - %s\n" % time.strftime("%a, %d %B %Y, %H:%M:%S"))