I am developing an application that is responsible of moving and managing robots over an UDP connection.
The application needs to:
- Read joystick/user input using SDL.
- Generate and send a control packet to the robot every 20 milliseconds (UDP)
- Receive and decode response packets from the robot (~20 msecs). This was implemented with the signal/slot mechanism and does not require a timer.
- Receive and process robot messages for debugging reasons. This is not time-regulated.
- Update the UI regularly to keep the user notified about the status of the robot (e.g. battery voltage). For most cases, I have also used Qt's signal/slot mechanism.
- Use a watchdog that disables the robot if no response is received after 1 second. The watchdog is reset when the application receives a robot packet (~20 msecs)
For the moment, I have implemented all of the above. However, the application fails to send the packets regularly when the watchdog is activated or when two or more QTimer objects are used. The application would generally work, but I would not consider it "production ready". I have tried to use the precision flags of the timers (Qt::Precise, Qt::Coarse and Qt::VeryCoarse), but I still experienced problems.
Notes:
- The code is generally well organized, there are no "god objects" in the code base (most source files are less than 150 lines long and only create the necessary dependencies).
- Most of the times, I use QTimer::singleShot() (e.g. I will only send the next packet once the current packet has been sent).
Where we use timers:
- To read joystick input (~50 msecs, precise timer)
- To send robot packets (~20 msecs, precise timer)
- To update some aspects of the UI (~500 msecs, coarse timer)
- To update the elapsed time since the robot was enabled (~100 msecs, precise timer)
- To implement a watchdog (put the application and robot in safe state if 1000 msecs have passed without a robot response)
- Note: the watchdog is feed when we receive a response packet from the robot (~20 msecs)
Do you have any recommendations for using QTimer objects with performance-critical code (any idea is welcome). Note that I have also tried to use different threads, but it has caused me more problems, since the application would not be in "sync", thus failing to effectively control the robots that we have tested.
Источник: https://stackoverflow.com/questions/329 ... oftware-qt
Мобильная версия