Hi!
When I try to run my add-on, even if I just use the vcHelpers.Robot2.getRobot()function, the graphical interface for simulation has no affect on the robot i used it on.
To use collision detection despite this, I tried to do the following.
from vcCommand import *
from vcHelpers.Application import *
from vcHelpers.Robot2 import *
app = getApplication()
cmd = getCommand()
simu = getSimulation()
DIALOG_NAME = 'Collision'
move_btn=None
def HandleCollision(args):
print('collision')
print(args)
def HandleOnPosition(args):
print('on position')
print(args)
def Collision(robot):
robot = next((c for c in app.Components if c.Name == robot), None)
robot_obj = getRobot(robot)
controller = robot_obj.Controller
executor = robot_obj.Executor
controller.OnPosition = HandleOnPosition
POINTS = []
for statement in robot_obj.Program.MainRoutine.Statements:
for point in statement.Positions:
POINTS.append(
{
"name": point.Name,
"x": point.PositionInWorld.P.X,
"y": point.PositionInWorld.P.Y,
"z": point.PositionInWorld.P.Z,
"rx": point.PositionInWorld.WPR.X,
"ry": point.PositionInWorld.WPR.Y,
"rz": point.PositionInWorld.WPR.Z,
"configuration": point.Configuration,
"mode": statement.Type
}
)
print(POINTS)
if len(POINTS) == 0:
print('Kérlek adj meg célhelyzeteket')
return
controller.clearTargets()
controller.move()
for point in POINTS:
target = controller.createTarget()
if point['mode'] == 'PtpMotion':
target.MotionType =VC_MOTIONTARGET_MT_JOINT
else:
target.MotionType = VC_MOTIONTARGET_MT_LINEAR
config_names = getConfigNames(executor, controller)
wanted_config = point['configuration']
config_index = config_names.index(wanted_config)
target.RobotConfig = config_index
robot_inverse = robot.WorldPositionMatrix
robot_inverse.invert()
target_matrix = vcMatrix.new()
target_matrix.translateRel( point['x'], point['y'], point['z'])
target_matrix.setWPR(point['rx'], point['ry'], point['rz'])
target.Target = robot_inverse * target_matrix
#controller.moveTo(target)
print('finished')
#controller.move()
print('collision stuff')
detector = simu.newCollisionDetector()
detector.NodeListA = [(robot, VC_NODELIST_INCLUDE, VC_NODELIST_TREE )]
detector.NodeListB = [(simu.World, VC_NODELIST_INCLUDE, VC_NODELIST_TREE ),(robot,VC_NODELIST_EXCLUDE, VC_NODELIST_TREE )]
detector.Active
print('detector set')
simu.OnCollision = HandleCollision
print('handler set')
#controller.move()
simu.reset()
simu.run()
simu.update()
app.render()
def OnStart():
global move_btn
dlg = getDialog(DIALOG_NAME)
robots = [c for c in app.Components if c.Category == 'Robots']
dlg = getDialog(DIALOG_NAME)
robot_name = dlg.addProperty(
'::Select robot:', VC_STRING, None, VC_PROPERTY_STEP)
robot_name.StepValues = [r.Name for r in robots]
move_btn = dlg.addProperty('::Move', VC_BUTTON, Collision)
move_btn.OnChanged = lambda _: Collision(robot_name.Value)
dlg.showTabs()
dlg.show()
When the execution gets to the commented out controller.move() or controller.moveTo() it throws a ReferenceError: Events cannot be registered in this scope. error, but earlier in this code, it works fine.
Is there a better way to do this?
Tom