The while loop at the end of src/main.cpp runs at the highest rate possible without pausing after it has published data. This means the CPU usage is much higher than it needs to be because lidar->IsFrameReady() always evaluates to true once data has been received, so the loop always publishes the laser scan (even if the data hasn't changed).
This can be fixed by adding a sleep (specifically, rate.sleep()) to regulate the timing of the loop. The attached file shows the proposed changes to reduce the CPU usage. I have set the rate to 20 Hz because that is a reasonable amount greater than the maximum scanning frequency of the lidar (13 Hz), so there is no chance of skipped messages and the frequency of repeated messages is much lower.
The while loop at the end of
src/main.cpp
runs at the highest rate possible without pausing after it has published data. This means the CPU usage is much higher than it needs to be becauselidar->IsFrameReady()
always evaluates to true once data has been received, so the loop always publishes the laser scan (even if the data hasn't changed).This can be fixed by adding a sleep (specifically,
rate.sleep()
) to regulate the timing of the loop. The attached file shows the proposed changes to reduce the CPU usage. I have set the rate to 20 Hz because that is a reasonable amount greater than the maximum scanning frequency of the lidar (13 Hz), so there is no chance of skipped messages and the frequency of repeated messages is much lower.cpu_fix.patch