RuntimeError: Tasklet is already scheduled

I am trying to simulate emergency stop in a process. If a HMI button is pressed, it should automatically pause/resume the process. The pause works, but the resume doesn’t work and I think it’s because of the While True loop.

Could anyone please suggest some modifications?

Component: Red robot
Expected Behaviour: Process stops/resumes when signal is True/False respectively
Actual behaviour: Process stops when the signal is true or false
File: 08-02-2ndAttempt.vcmx (1.4 MB)
Version: 4.5 Premium

Code:

# Emergency Stop Function
def CallEmergencyStop(signal):
  global doStop
  if signal.Value == True:
    rx.IsEnabled = False
    print("inside doStop IF")
    doStop = True
  elif signal.Value == False:
    rx.IsEnabled = True
    print("inside doStop ELSE")
  else:
    print("Check line 50 for error")

def OnSignal(powerSignal):
  global doStop
  doStop = False
  rx.IsEnabled = True
  while True:
    condition(lambda:doStop)
    BreakTaskletExecution()
    mt = rc.createTarget()    
    mt.MotionType = VC_MOTIONTARGET_MT_JOINT 
    mt.UseJoints = True 
    jvals = [j.CurrentValue for j in rc.Joints]
    mt.JointValues = jvals 
    rc.addTarget(mt)
    rc.moveImmediate()
    doStop = False
    print("Inside while Loop")

if powerSignal.Value == True:
  CallEmergencyStop(powerSignal)
elif powerSignal.Value == False:
   resumeRun
   print("resuming...")

I managed to get it working! Here’s the solution:
I designed the simulation using PlugNPlay and then set some empty statements in the executor. So the button was toggling the robot executor and thus mimicking emergency stop for the entire simulation.

The only problem I have right now is that I have to manually resume the simulation after pressing the button, I tired ResumeRun() but to no avail. Any suggestions to how I could resume the simulation by the press of a button within the simulation?

Updated File:
09-02.vcmx (1.4 MB)

Updated Code:

# Define the powerSignal
powerSignal = comp.getBehaviour("PowerSignal")
rx = comp.findBehaviour("Executor")
rc = rx.Controller

# Emergency Stop Function
def CallEmergencyStop(signal):
  if signal.Value == False:
    rx.IsEnabled = True
    print("inside doStop IF")
    print rx.IsEnabled
  elif signal.Value == True:
    rx.IsEnabled = False
    resumeRun()
    print("inside doStop ELSE")
    print rx.IsEnabled
  else:
    print("Check line 50 for error")

# Catch the power signal
def OnSignal(powerSignal):
  CallEmergencyStop(powerSignal)

# Set the default settings:
def OnContinue():
  #if getComponent().getBehaviour("PowerSignal").Value == False:
  getComponent().findBehaviour("Executor").IsEnabled = True
  print("line 61")