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()

You can use a timer and when performing MoveRight() set the timer on 5 seconds, and after timer ends, perform Moveleft().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top