How to adjust the sample rate of distance sensor (rangefinder)

I follow the Pixhawk instruction (by this link:Distance Sensors (Rangefinders) · PX4 v1.9.0 User Guide)
and run the rangefinder successfully.

The sensor type is Lidar-Lite

I am curious about how to increase the sampling rate.
Now my sample rate is only 2 hz

Also, I am confused why the QGroundControl Analyze Tools detect the distance sensor only 0.2~0.8 Hz, no matter in real world or simulation ( the simulation and real Lidar should be 10 hz)

Thanks a lot

I enable rangefinder in MAVROS by this way: How to Read Distance Sensor Data from MAVROS Topic
I set the I2C connection in QGroundControl by this way: Cannnot find the distance_sensor in the QGround control

In Ardupilot firmware, people suggest,

rosrun mavros mavsys message_interval --id XX --rate YY

by this way: How to adjust the sample rate of distance sensor (rangefinder) - ArduCopter - ArduPilot Discourse

I also examine mavlink_main.cpp by this way: Increase stream rate of HIGHRES_IMU message

Someone change the sampling rate of IMU by edit this file /fs/microsd/etc/extras.txt
ref:System Startup | PX4 User Guide

Increase the sampling rate in Gazebo simulation increase sensor rate SITL · Issue #980 · mavlink/mavros · GitHub

How to change the correct publisher name: How to use distance_sensor? send distance Sensor data to FCU for use as altitude · Issue #664 · mavlink/mavros · GitHub

@AlexWUrobot …I am also trying to put my distance sensor VL53L0x into operation for collision prevention. QGC shows the distance measured but the drone in POSITION mode does not handle collision detection !! Any clues ?

In your case, you have to make sure that the drone is flying in POSITION MODE for the distance sensor to work .

Where did you install MAVROS. is it on a Raspberry Pi ?

Thanks for the reply
I install MAVROS on Jetson TX1. (with Ubuntu 18.04 and Python 2.7)
I fly the drone through offboard mode (means that Jetson TX1 computer controls the Pixhawk FMUv5 version to fly the drone)
I ran the Python script so I can subscribe the /mavros/distance_sensor/hrlv_ez4_pub
ref: MAVROS Offboard control example (Python) | PX4 User Guide

great info !! Actually i am a bit confused . Is it better to go the MavROS way or the MAVSDK way to control the Quadcopter in offboard mode …

I have not tried MAVSDK, but I think this link will be helpful:Difference MAVLink and MAVROS

She said:

MAVROS is the bridge between MAVLink and ROS. If you’re using ROS for whatever robotics you’re doing, then MAVROS is great.
If all you want is to write a simple C++ program to talk to a PX4 drone, then you should consider using the Dronecode SDK

thank you for the info…

The solution may be to edit the SD card in the Pixhawk
But I only find how to change the IMU instead of the distance sensor now.

cd /fs/microsd/etc
echo "mavlink stream -d /dev/ttyACM0 -s HIGHRES_IMU -r 100" > extras.txt

What about accessing Lidarlite from the Mavlink console menu in QGC . There is an option to adjust the bus frequency

Thanks for the info. I also found this but the unit is kHz, so I guess it may be for communication rate, instead of sampling rate. Distance Sensor · PX4 Developer Guide

I will still try that. Thanks for the reminder again.

I found some people have the similar issue
ref: lidar lite slow read rate · Issue #367 · intel-aero/meta-intel-aero · GitHub

My publish rate of (/mavros/distance_sensor/lidarlite_pub) is 10 hz in the simulation, but only 2 Hz in the real world, when readling from Pixhawk 4 (fmuV5).

If we assume the lidar-lite works well (default sampling rate 10Hz)
I would like to guess the issue may be due to the setting on Pixhawk?

Thus, I screenshot the other parameters of distance sensor (rangefinder) in the QGroundControl

I also guess the issue may be due to this revision on LidarLiteI2C.h
ref: increased rate of Lidar Lite driver over I2C · PX4/PX4-Autopilot@7ca16cd · GitHub

Maybe, I should change it to 10 ms and then

cd PX4-Autopilot
make px4_fmu-v5_default

ref: Building PX4 Software | PX4 User Guide

Great info…My concern is how do you built the px4 after changing the value …Do you have a complete procedure for doing this through Nuttx shell

I am still working for this issue.
I further tested the lidar-lite-v3 sensor with the arduino UNO by the below wiring

After Arduino measurement, the sampling rate is about 80~90 Hz.
video: Arduino Test Lidar lite v3 sampling rate 2023 05 13 - YouTube

The code is used from the link:

 * LIDARLite I2C Example
 * Author: Garmin
 * Modified by: Shawn Hymel (SparkFun Electronics)
 * Date: June 29, 2017
 * Read distance from LIDAR-Lite v3 over I2C
 * See the Operation Manual for wiring diagrams and more information:

#include <Wire.h>
#include <LIDARLite.h>

// Globals
LIDARLite lidarLite;
int cal_cnt = 0;
unsigned long previousMillis = 0; // variable to store the previous time

void setup()
  Serial.begin(9600); // Initialize serial connection to display distance readings

  lidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz
  lidarLite.configure(0); // Change this number to try out alternate configurations

void loop()
  int dist;
  unsigned long currentMillis = millis(); // varia

  // At the beginning of every 100 readings,
  // take a measurement with receiver bias correction
  if ( cal_cnt == 0 ) {
    dist = lidarLite.distance();      // With bias correction
  } else {
    dist = lidarLite.distance(false); // Without bias correction

  // Increment reading counter
  cal_cnt = cal_cnt % 100;

  // Display distance
  Serial.println(" cm");

  Serial.println(currentMillis - previousMillis); // display time elapsed for line 1
  previousMillis = currentMillis;


I think I find something useful in the QGroundcontrol.
In the MAVlink tab, the telemetry stream rates for raw sensors is 2Hz

rosrun mavros mavsys rate --all 50

ref: increase/change mavros local_position pose publish frequency - ROS Answers: Open Source Q&A Forum

I find
If using wireless telemetry, the MAVLink Inspector can detect distance sensor about 0.8Hz
If using micro usb, the MAVLink Inspector can detect distance sensor 10 Hz

Thus, the lidar works well and QGroundcontrol works OK.
The only issue may be MAVROS publisher frequency

There is the solution !!!
To increase the sampling
The SER_TEL2_BAUD need to be increased. (which was 115200)
The MAV_1_RATE need to be increased.(which was 0)

PX4-autopilot automatically half-size the data, if the communication is too low.

In a serial communication system, the data rate is measured in bits per second (baud rate). However, it’s important to note that a byte consists of 8 bits.
At a baud rate of 921,600, the maximum number of bits that can be transmitted per second is 921,600. To determine the number of bytes transferred per second, you need to divide the baud rate by 8 (8 bits per byte).
So, at a baud rate of 921,600, the maximum data transfer rate in bytes per second would be:
921,600 bits/second / 8 bits/byte = 115,200 bytes/second
Therefore, at a baud rate of 921,600, you can transfer a maximum of 115,200 bytes per second.

1 Like