# Using a thread to stop a linear movement

**URL:** <https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603>\
**Category:** Technical Questions\
**Created:** [March 25, 2023, 2:59pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603 "2023-03-25T14:59:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [March 25, 2023, 2:59pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/1 "2023-03-25T14:59:41Z")

</div>

In (pseudo-) script code I have:

global stopped = False

thrd = run my\_thread()  
movel( … some long movement…)  
kill thrd

if stopped = True:  
…do\_some\_intermediate\_stuff()  
movel(…finish the move…)  
end

thread my\_thread():  
while true:  
if read\_some\_input() = true:  
stopl()  
stopped = true  
return false  
end  
sleep()  
end  
return false  
end

The goal is, to be able to interrupt a lengthy movel() with a digital input and then later on continue that move to finish. Would this be a legitimate way of doing this? If not, what would be theproper solution here?

---

<div class="post-metadata">

**Author:** ![MikeM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mikem/32/3114_2.png) [@MikeM](https://forum.universal-robots.com/u/MikeM)\
**Post date:** [March 26, 2023, 1:48am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/2 "2023-03-26T01:48:50Z")

</div>

Hmm I think I’ve seen some people use speedl() to achieve a start-stop option in a program. Not sure how it works but it might have an additional thread checking the distance to the final target point then use the stopl() or speedl(0) URscript.

Another option might be an external input triggering a “pause” and another input a “start” command.

Third option would be setting the speed scale slider to like 1% (but I think this is kind of cheating and it also slows ALL threads and the main program to 1% speed as well).

There’s probably a few different ways to achieve what you are trying to do, and the URcommunity might have some better options. If someone can’t help you try reaching out to one of UR’s application engineers they should be able to get you headed in the right direction, anyways best of luck!

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [March 26, 2023, 2:43pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/3 "2023-03-26T14:43:40Z")

</div>

Hi Mike,

Thanks for your reply. speedl() has issues related to calculating the speed vector, see some of my other posts. And your suggestion uses a thread, just like my solution, so what would be the benefit? Timing a stop based upon velocity might not be accurate.

pause/ start the program achieves the movel() being paused and resumed, but I can’t do any intermediate stuff then…

the speed slider does indeed slow everything down (but not to a stop), which is not acceptable to my application.

I posted a feature request, asking for movel() to be augmented with a check for a digital input. It would simplify a lot… but haven’t had any response on that. Maybe you could place an “upvote” for it?

Anyway, thanks for you answer, I appreciate the time you’ve taken and the suggestions you’ve made.

---

<div class="post-metadata">

**Author:** ![efn](https://avatars.discourse-cdn.com/v4/letter/e/35a633/32.png) [@efn](https://forum.universal-robots.com/u/efn)\
**Post date:** [March 27, 2023, 4:22am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/4 "2023-03-27T04:22:08Z")

</div>

This is how I would achieve it, but it might not meet all your needs. 🙂

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/5/57c51196b975597782a0066a549487aa788ef417.png)

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [March 27, 2023, 10:07am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/5 "2023-03-27T10:07:23Z")

</div>

Hi @efn

Thanks for this, looks really nice! I’m on an older CB3 model, but it looks like I should get this going.

---

<div class="post-metadata">

**Author:** ![efn](https://avatars.discourse-cdn.com/v4/letter/e/35a633/32.png) [@efn](https://forum.universal-robots.com/u/efn)\
**Post date:** [March 27, 2023, 10:19am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/6 "2023-03-27T10:19:17Z")

</div>

Glad you could use it. All the commands are present on CB3, as well, so shouldn’t be an issue at all. 🙂

---

<div class="post-metadata">

**Author:** ![miwa](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/miwa/32/2986_2.png) [@miwa](https://forum.universal-robots.com/u/miwa)\
**Post date:** [March 27, 2023, 11:31am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/7 "2023-03-27T11:31:19Z")

</div>

FYI, the units for stopl() is m/s2, so a value of 2000 will be an incredibly fast deceleration. Ideally, you shouldn’t need a value higher than 2.5.

---

<div class="post-metadata">

**Author:** ![efn](https://avatars.discourse-cdn.com/v4/letter/e/35a633/32.png) [@efn](https://forum.universal-robots.com/u/efn)\
**Post date:** [March 27, 2023, 12:09pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/8 "2023-03-27T12:09:37Z")

</div>

@miwa Ah, right. That’s good to know. I have always wondered about the units of that function. 🙂

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [March 27, 2023, 1:54pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/9 "2023-03-27T13:54:17Z")

</div>

I have one final question…

The if() statement is in your main program structure tree and can have the “check continuously” property.

How do I set this property in script code?

---

<div class="post-metadata">

**Author:** ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)\
**Post date:** [March 27, 2023, 4:19pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/10 "2023-03-27T16:19:58Z")

</div>

That property in script code generates underlying threads. It’s not as easy as just setting an input parameter. I would recommend writing a totally empty urp file, containing only a continuously checked if statement so you can see how Polyscope is generating that bit of script

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [April 4, 2023, 10:11am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/11 "2023-04-04T10:11:17Z")

</div>

So today I spend a lot of time getting this to work via scripts code.

I hit a brick wall. The problem I have now, is that I cannot stop the robot from the thread because I get the “robot is being controlled from aaother thread” error message.

See attached photos for my actual script code and error message. I’m on a UR10, CB 3 running 3.15.9

The I/O’s get set as expected… It’s just the stopl() that doesn’t work.

![ThreadScript](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/d/d7ff0f75ca2d6636f41bd26f4ac83aee3e563d92.jpeg)  
 ![MainScript](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/c/cf8058f6b2f11a85d11f2435a400a2af101cfef8.png)

 ![ErrorCode](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/6/60a603481487e0654e0f0373f7595a3130a73696.jpeg)

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [April 4, 2023, 11:18am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/12 "2023-04-04T11:18:37Z")

</div>

Dear,

I also tried Eric’ @eric.feldmann solution, but on the CB3 this doesn’t work… the script generated does use threads, but they do not interrupt the movel(), see photo.

So I/m back to my original request for a movel() version that is to be interrupted by a digital input… (feature request)

 ![IMG_6121](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/1/117bf488ef3117b2e07efd77637864341efe9c47.jpeg)

---

<div class="post-metadata">

**Author:** ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)\
**Post date:** [April 4, 2023, 12:52pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/13 "2023-04-04T12:52:59Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/9/98e4d04b8a68599d28ca12d77282191e6f6d4e16.png)

I can’t speak for the CB series, since I only have eSeries, but I can confirm something like this works. I don’t know what your application is, but this code, for example, will cycle through the first three waypoints continuously, and if I toggle output0 on, it quits out of wherever it was and goes to waypoint 4.

Pretty sure I had the same issue as you with the “thread controlling” thing, and setting it up for sort of reverse logic like you see here fixed it. Where I’m saying "run the path as long as you DON’T see the input. This means once the input DOES come on, the thread controlling the motion originally is killed, thus allowing a new one to take control.

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [April 7, 2023, 1:28pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/14 "2023-04-07T13:28:57Z")

</div>

It doesn’t work on the CB3 ☹

Can a movel() be interrupted at all? could someone from UR respond? How to solve the issue? Also, I made a feature request on this issue, but no reply on that either…

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [April 9, 2023, 3:28pm UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/15 "2023-04-09T15:28:24Z")

</div>

Problem solved by using speedl()

Thanks.

---

<div class="post-metadata">

**Author:** ![efn](https://avatars.discourse-cdn.com/v4/letter/e/35a633/32.png) [@efn](https://forum.universal-robots.com/u/efn)\
**Post date:** [April 21, 2023, 6:30am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/16 "2023-04-21T06:30:06Z")

</div>

Sorry for the late reply, but just to clarify; above solutions from Eric and me will work on CB3 as well as e-series.

You won’t be able to achieve the result from a thread, though.  
You’re looking at it slightly wrong: You have to “kill” the movement by exiting the If-command while executing the movement within. Hence the “Check expression continuously”. 🙂  
And the next command right after the killed movement has to be a stopl/stopj (works with if/else).

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [April 21, 2023, 9:52am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/17 "2023-04-21T09:52:17Z")

</div>

@efn

No, it doesn’t work on CB3. I tried literally everything, including reverse engineering if with check continuously. The script compiler generates a thread for that, but movel() can’t be stopped on a CB3, no matter what.

So it’s not possible unless you can provide me with a script showing the method that you’ve actually tested on a CB3.

PS.

speedl() didn’t work either, since on the CB3 the robot stops as soon as the speedl function returns (stopl() still required, though).

---

<div class="post-metadata">

**Author:** ![efn](https://avatars.discourse-cdn.com/v4/letter/e/35a633/32.png) [@efn](https://forum.universal-robots.com/u/efn)\
**Post date:** [April 24, 2023, 4:17am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/18 "2023-04-24T04:17:05Z")

</div>

I have used this trick probably a hundred times when seeking for stacks or just items in general with both the force function or a sensor on CB3s.  
So, yes. It is definitely possible, and it’s simple to do, too. I don’t know what, but you’re doing something wrong. 🙂

Here’s a video: [https://photos.app.goo.gl/CBDP5JjNVJamMUHQ6](https://photos.app.goo.gl/CBDP5JjNVJamMUHQ6)

Create the program yourself in PolyScope and extract the script from it.

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [April 24, 2023, 9:06am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/19 "2023-04-24T09:06:58Z")

</div>

Ok, thanks! I got the screenshot from your program and will give this a go.

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [April 25, 2023, 9:52am UTC](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603/20 "2023-04-25T09:52:06Z")

</div>

@efn

Today I tried your solution and I’m happy to report it works fine! I took the script code it generated and was able to include it in my own script code. Thanks for your help!

Max

[Next page](https://forum.universal-robots.com/t/using-a-thread-to-stop-a-linear-movement/27603.md?page=2)
