Keeping a drone a certain distance from the ceiling, avoid crash on the ceiling


I have a quadcopter with a PX4 and two TeraRanger Evo sensors, one on it’s bottom that can read until 60m and one on the top with high accuracity and I want to keep the drone at a certain distance from the ceiling, limited by software, so the pilot can’t crash the drone into it.

I could do it with a Raspberry Pi 4 using MavRos, but I would like to know if something like this is possible with just the PX4 and the sensors connected to it.

The idea is to use the sensor with the 60m range to keep a stable heigh during the fligth but if the pilot tries to crash the drone into the ceiling the high accuracity sensor detects the ceiling and stops the drone.

Thank you!

I haven’t used this particular sensor but a quick look at the source code reveals that the driver for it has already been written (teraranger). Based on its datasheet the sensor appears to support modifying the base I2C address so that multiple instances of this sensor can coexist on a I2C bus, so you should be able to do this all within PX4. The I2C address change is persistent after loss of power so you don’t need extra driver code to configure the sensors every time at boot-up; this can be done initially before connecting the sensors to PX4.

Ideally the teraranger driver should support multiple instances directly like in the case of mb12xx or vl53l0x. Each sensor would run configured with its own I2C address and its rotation indicator (distance_sensor_s::ROTATION_DOWNWARD_FACING by default).

To do this all within PX4 you would use the other drivers in the distance_sensor directory as reference and compile a custom PX4 firmware with the necessary changes to the teraranger driver to configure multiple sensors with different addresses and orientations.