Qt class design for cross-threading

Description of the problem : to track the object using the camera and accordingly move the camera in azimuth and height.

Process : the camera receives images of the object .... each frame of the camera is processed to search for the object (which should be tracked ...) and the information generated in each frame is transmitted to a mechanical device (cardan ...) to move the camera in pan mode and tilt ...

Design : the main Gui runs in the stream, and the camera and cardan in the other two streams ... the information generated in the camera stream is transmitted to the thread of the cardan shaft and the same thread (camera thread ...) displays the tracked image ...

CONSTRAINT: display speed is faster than cardan speed ... so in every frame ...

  • Frame
  • It is processed and displayed (with a speed of 10 ms).
  • information generated is transmitted to gimbal
  • the corrugated thread is suspended for a longer time (100 ms) to allow the mechanical parts to move and to ensure that the new update does not reach the cardan until it completes its movement.

the following diagram shows the construction of the classes and connections of signals and slots in Qt ..

enter image description here

suggest the best way to solve the problem, if possible ...

ERROR : sometimes I get an error message ...

QObject :: killTimers: timers cannot be stopped from another thread

+4
source share
1 answer

I just need to avoid calling such functions between a stream, here is the idea, so I would do to safely exchange a lot of data.

and you should have a state machine with both threads, access to the shared variable to be locked (use pointer for the main thread, which points to the ping or pong buffer + mutex for your lock)

so you can change the way data is used, the β€œdisplay” should always use the same pointer. It should get a lock at the start of reading and letting go after.

The camera should write in another buffer. After the camera fails, the camera thread should try to set a lock for the mutex, and if it acquires it (that is, another stream does not read another buffer), then the camera stream should take a lock and change the buffer. pointed to by the pointer used by the main thread)

This way you have no connection between threads, just a mutual exception.

The biggest risk is to never get the lock from the camera thread, here is the solution β†’ To limit the lock time, I advise you to have a second buffer in each thread, so the lock time will be reduced to the duration of "memcpy". Add a bool to the structure, this says if your camera updated the data or not.

If so, the main stream copies the data. (and the camera cannot change pointers at this time), but as soon as this is done, the main thread also reset bool to false. And until this bool returns to the truth, you do not need to block copying data. (now this is a lock-free exchange) Note that this is only true because reading bool is an atomic action.

I hope this helps

(sorry for my English)

0
source

Source: https://habr.com/ru/post/1436667/


All Articles