Making an output pulse while moving the robot

Hello. I’m trying to make an output pulse while moving the robot in another part of the code. I have tried using threads, but it seems to complain that I have used up all 50/50 threads. I understand why waiting, which is basically what the pulse function does, can be problematic in a thread. I wonder if there is some way to get this functionality just using the robot script.

1 Like

Are you actually using 50 threads? Or are you creating the same thread over and over again? A thread sounds like the best option for what you are trying to do though.

A thread is definitely the way to go here. I highly doubt you’re actually using all 50 threads. What’s more likely is that you have the thread creation inside a loop. This can be a Polyscope Loop command, or what gets me more often, anywhere inside the main program, and that main program is set to “Loop infinitely.” Try putting your thread creation script (if that’s what you’re using) in the before start sequence so it only creates the thread once.

A really clean way to control a thread is something like this:

Thread myThread():
  while(True):
    while(not runMyThread):
      sync()
    end
    set_digital_out(1,True)
    sleep(SLEEP_DURATION)
    set_digital_out(1, False)
    sync()
  end
end

Now you just need to create a runMyThread variable, and set this true/false in your main program whenever you want to start pulsing this output. You’ll also want to put something in for SLEEP_DURATION for however long to want to pulse for.

1 Like

The info from @eric.feldmann is correct and here is a bit more info on this topic. If you use the SET command to pulse an output it ends up creating a thread for you (you just don’t see it). If you embed that within another thread you have a thread within a thread. This is probably what the polyscope compiler is complaining about. Don’t use the SET command to pulse the output. Use some script commands as Eric outlines. It shouldn’t matter if the thread is created in BeforeStart or somewhere else though. Just avoid the thread-within-a-thread problem.

Hello. Since I don’t have access to a real robot and simulation doesn’t work with IO, I tried to just change a variable in a thread. I think there was a couple of errors in your code, but this is mine:

thread myThread():
  while(True):
    while(not runMyThread):
      sync()
    end
    test = True
    sleep(0.5)
    test = False
    sleep(0.5)
    sync()
  end
end

It changes this variable very irregularly and it gets worse with more threads. I guess it is because the control of the robot interrupts the thread sometimes while it is trying to change the variable. A shame that UR controller doesn’t have true multithreading. Thank you for the help regardless.

Oops, you’re right. I capitalized my T in thread haha!

Your simulator will still show changes in the outputs, for what its worth. Also, it might still be worth trying this on your real robot when you get a chance. Sometimes it’s just the display of the variables that is delayed, but the actual action takes place as you’d expect.

It’s okay, it was easy to spot!
I’ll try it on a real robot sometime next week. I’ll get back if i notice something interesting.
Thank you for your time!