I am programming a robot with QT (C++). I have a function move_gripper() which closes or opens the gripper (it depends on the parameter) and a function relative_motion() which lets the end effector move downwards. What I want to do is to close the gripper ( calling the move_gripper() function ) while moving the robot downwards ( calling the relative_motion() function ) The execution time of the functions are not the same ( let's say move_gripper() lasts 2 seconds while relative_motions() lasts 3 seconds ). I want the two functions to start the execution at the same time.
Asked
Active
Viewed 361 times
0
-
Does this answer your question? [Simple example of threading in C++](https://stackoverflow.com/questions/266168/simple-example-of-threading-in-c) – Ervin Szilagyi May 15 '20 at 15:51
-
The QT's documentation is huge and great, so you will have to start looking at there (i.e. threads https://doc.qt.io/qt-5/threads-technologies.html) – Jose May 15 '20 at 15:51
-
2Threads (as recommended above) are a solution but you also might like to conciser a single threaded (state machine) approach. Where in the main event loop you move the gripper a small way towards the desired position and continue round the loop processing other events. – Richard Critten May 15 '20 at 15:54
-
If you need two things to run in parallel, then running those two things in two separate processes or two threads are obvious solutions. You can also use a state machine in many cases to alternate between tasks. Co-routines is another option. – Jesper Juhl May 15 '20 at 16:08
-
1Ditto what Richard Critten said: Unless your code is actively generating PWM waveforms for the motors, then your code (or, some library that your code calls) probably is sending commands to external motor controllers and then maybe watching status bits and waiting for the motors to reach target positions. If you want to _coordinate_ the motion of two or more motors, then it might make more sense to do all of that in a single thread. The problems where multi-threading fits best are the ones where threads only infrequently need to know or care what other threads are doing. – Solomon Slow May 15 '20 at 16:26
-
Are your functions blocking, I meant wait that action is fully finished, or just start the action? – Jarod42 May 15 '20 at 18:00
1 Answers
0
In computer theory there is no real "simultaneously". The common approach is to use threads. Those threads are executed, halted, switched just so fast it looks like they are executed simultaneously.
But still there are some things which can be done wrong which make the usage of threads even worse than not to use them.
So I would say first get some knowledge about threads (see the link: https://doc.qt.io/qt-5/qthread.html) and create 2 threads, one for each channel and do your work in there.

bogdyname
- 358
- 2
- 10
-
On a multi core machine it’s fair to say at least some of the threads are running literally simultaneously (one on each core) – Jeremy Friesner May 15 '20 at 23:45
-