I have a problem with Visual Components when executing a while loop in python.
The general idea of what I am trying to do is, that a robot sends a continous bool signal to a connected gripper. The gripper is suppose to execute a set of commands until the robot is setting the continous signal back to False. Everytime I try this my Visual Components crashes and is no longer responding and I am forced to close it.
Does anyone know why this is?
I also attach a very very simple example I tried and VC still crashed.
from vcScript import *
comp = getComponent()
app = getApplication()
signal_start = comp.findBehaviour("Start")
def OnSignal( signal ):
if signal.Type == VC_BOOLEANSIGNAL:
if signal.Name == "Start" and signal.Value == True:
while True:
print ("Hello")
Just to point out, if you have a while True loop somewhere, anywhere (even outside VC ecosystem) the code will practically execute that command “as fast as it can do it”, causing it to hog quite a bit of resources. In VC side it would mean that you’re stuck executing that print command as fast as your computer can run it on a single thread causing you to get locked.
As for your signal idea, I would suggest doing it more towards “event” way, if it’s true, do a set of commands and then check if it’s still true AFTER the set of commands is done, otherwise stop and loop this instead (in the OnRun event). This should work as robot motions consume time which in return cause the while True loop not run “as fast as possible”.
Basically as a pseudo-code it would look something like this:
def OnRun():
while True:
condition(lambda: signal_to_wait.Value)
servo.moveJoint(joint_index, joint_value)
do remember to set the joint value to False afterwards or your while loop will run “pretty fast” again. Alternatively you can use triggerCondition