I am working on a robotics project with C++ and OpenCV. In this step, I have faced a problem which consists of:
I have two methods moveRight()
and moveLeft()
that I called successively in my code, but the problem is that the second one does not run, because the first one needs time (the time of robot movement), but when I put Sleep(5000)
between them (I guessed that five seconds are enough for the movement), all is OK.
What is a programming solution that avoid the use of Sleep
(because it makes some other problems)?
You can try to add a layer of indirection. Add a queue of actions to perform, enqueue actions to moveLeft and moveRight, and somewhere else (different thread) execute actions from the queue correctly by waiting for previous action to complete before you do next action. Ideally you need a way to check if action is finished, so you can code it in a event based fashion.
You should never "guess" in robotics. You should KNOW, MEASURE how long your movement takes and use that in your code. For instance call moveRight()
often and have it check how long it has been running. Make it return true
when it's running and use that as a condition to call moveLeft()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With