control the start and stop of the robot

Hello everyone, I want to use an external signal to control the start and stop of the robot, but I don’t know how to write this script. Can someone guide me?

I think you need to create a one-to-many interface and connect the signals through that interface. From there one you can do something like: triggerCondition(lambda: getTrigger() == signal and signal.Value) where signal is the signal object

Cheers

This snippet might come handy. Those signals that you want to wait (react to events) in python you must connect to the script.

from vcScript import *

comp = getComponent()

def OnRun():
  startSignal = comp.findBehaviour('MyStartSignal')
  waitSignal = comp.findBehaviour('MyWaitSignal')
  
  while True:
    delay(10.0)
    # fire a signal 
    startSignal.signal(True)
    # wait a signal
    triggerCondition(lambda: getTrigger() == waitSignal and waitSignal.Value == True)
    

 

@Jobw

You can also connect signals directly with signal connection editor without interfaces.

@eme

True, but if you want to map multiple signals at once, an interface can come in handy.

However, I think that @laiba can choose what suits his/her problem the best.

@eme Sorry, I didn’t make the problem clear. In fact, I want to implement the emergency stop function like real robot in the simulation. For example, use the Signal Tester component in the library to start and stop the robot. The key point is to stop and start again.

1 Like

I’m not sure if that is currently possible. I’ll ask around.

@eme thank you very much!

I’m afraid that it isn’t possible. To my knownledge the only way to approach this behavoir is to implement your own robot statements that can be interrupted.

Safer to do with external controller enabled. Is the attached layout and code enough to get you started? Because this is where my journey ends on this issue.

Code

from vcScript import *

def startStopRobot(prop):
if prop.Value == True:
rx.IsEnabled = False
j1,j2,j3,j4,j5,j6 = [j.CurrentValue for j in rc.Joints]
rc.moveImmediate(j1,j2,j3,j4,j5,j6)
else:
rx.IsEnabled = True
rx.update()

comp = getComponent()
rx = comp.getBehaviour(“Executor”)
rc = comp.getBehaviour(“Controller”)
stop = comp.getProperty(“StopRobot”)
stop.OnChanged = startStopRobot

def OnRun():
stop.Value = False

Notes:
  • Use IsEnabled in Robot Executor. It's inherited Enabled property won't disable its execution. When you set IsEnabled to False, you halting the execution of the program not the current statement. After the current statement is finished, the executor will not execute the next statement.
  • Use moveImmediate() with Robot Controller to completely stop the robot. Others can provide best way to optimize the call with least amount of latency in script if you take this approach.
  • I kept the script super simple and light, so use it for learning. You could expand on the concept by using heartbeat in robot controller say at 1000hz to reach a comfortable solution.

Important:
  • Beware of access violation and conflict with source code executing robot movements. This is one reason to pursue an approach that sets the Robot Executor to use an external controller. Otherwise, you will eventually encounter this message because of C++ wrapper for the script.
![](upload://sBCPpkuAI15cvrZ6JSfUU6bSOdB.png)

Layout

It is attached. Refer to PythonScript #2 in robot and its StopRobot component property. By default, the robot executor is set loop during simulation. So turn StopRobot on/off to test. You will eventually encounter the runtime violation I mentioned earlier, so others can propose the path to the mountain top or implement your own solution that uses an external controller.

Test-Start-and-Stop-Robot.vcmx (53 KB)

@zesty Thank you very much!

@zesty Can I start or stop the servo right away? (the statement currently executed is not finished)

 

 

Hello,the robot stop but can not start again by your code, can you give some suggestion? Thanks a lot!

4 years later… sorry, my friend. too much time has passed. best if others help.