Calling a subroutine from python

Hi. I cant get this code to work. Anyone got any ideas?

If I move the statement robot.callSubRoutine(“change_base_start”)

to another place in the script it runs but within the function test it does not.

This is the error

File “C:\Program Files\Visual Components\Visual Components Professional 4.1\Python\Commands\vcHelpers\Robot2.py”, line 888, in callSubRoutine
if self.RecordRSL:
ReferenceError: You cannot suspend script in this scope.

here is the code

from vcHelpers.Robot2 import *

robot = getRobot()

def OnRun():
prop2.OnChanged = test

def test(prop2):
robot.callSubRoutine(“change_base_start”)

Simliarly if I try to call a routine in another method:

The error message is:

ReferenceError: You cannot suspend script in this scope.

the code is:

from vcScript import *

robot_executor = comp.findBehaviour(“Executor”)
robot_executor.IsEnabled = False

def test(prop2):
routine = robot_executor.Program.findRoutine(“Pick”)
if routine:
robot_executor.callRoutine(routine)

prop2.OnChanged = test

 

So is seems after further testing the python script with call a subroutine from within a function. It wont work if I am setting a variable from a property to call the function.

So the part which doesn’t work is setting the variable from a property drop down box on the robot model.

prop2 = comp.getProperty(“PartChoice”)
prop2 = comp.createProperty(VC_STRING, ‘PartChoice’, VC_PROPERTY_STEP)
prop2.StepValues = [‘part1’…etc

def test(prop2):
routine = robot_executor.Program.findRoutine(“Pick”)
if routine:
robot_executor.callRoutine(routine)

prop2.OnChanged = test

 

I am therefore thinking of another way to sent the request for parts

Hey Calvin,

you can only execute time consuming functions like delay(), callRoutine(), etc. in the OnRun(): method of a python script.

Therefore you’ll need to forward/check the change of the property to/in OnRun(): and call the routine of the robot from there.

Check the API reference for vcExecutor in 4.1. I think you are now able to call a subroutine and give an optional argument to not suspend the script, i.e. don’t wait for the subroutine to finish and continue the execution of script.

Use caution though since you do not want to interrupt the executor when a new call is made before the end of the previous one.

If you are able to, check line 888 in vcHelpers.Robot2 source code to see why the error is given.