AlessioMorale / ld06_lidar

ROS1 package for LDRobotic LD06 Lidar
37 stars 25 forks source link

High CPU usage caused by while loop at end of main.cpp #18

Closed JolonB closed 2 years ago

JolonB commented 2 years ago

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.

cpu_fix.patch

AlessioMorale commented 2 years ago

Hi @JolonB sorry for the late reply, thanks for spotting this issue. I just observed the very same issue on a Raspberry. I'll fix this pretty soon.