Usleep() issue

Hi,

I have created a new task in PX4 according to the template (Firmware/src/template/module) and I have removed everything from the while loop.

When I flash the firmware in the pixracer, I cannot receive and send data with qgroundcontrol to the pixracer except if I write ‘usleep(1)’ in the while loop, why?
But the issue with the ‘usleep(1)’ is that if I add the sending of a uart frame in the loop, the frames are separated by 2ms (2000us). If I remove the ‘usleep(1)’ there is no delay in between the frames but I cannot connect anymore with qgroundcontrol.

What should I do?
Thanks,
Alex

Without the sleep you create a busy loop and mess up the operation of the other tasks (no connection with qgc). In all px4 modules there is a px4_poll function that waits for a specific message to be published before running the loop. This function also has a timeout to run the loop in case no new messages arrives. This prevents busy loops and runs the loop just when you need it. I guess the data you are writing on the uart comes from a uorb message? If this is the case you can poll on this uorb message. The module was already doing this before you removed everything (https://github.com/PX4/Firmware/blob/d0c69efff995070cf58fa1be39319ec13d8b96c2/src/templates/module/module.cpp#L185).

1 Like

more info here: https://dev.px4.io/en/apps/hello_sky.html

1 Like