What else needs to be done at PX4 side for collision prevention

Hello. I am trying to setup collision prevention using distance information from ultrasonic sensors. I have a flight management unit (FMU) running PX4 and a microcontroller sending MAVLink messages to the FMU. I have yet to decide how I would connect the microcontroller to the FMU (by UART or SPI).

I am following instructions on the this page (https://docs.px4.io/v1.9.0/en/computer_vision/collision_prevention.html). The only thing it mentions to be done at the PX4 side is to configure the MPC_COL_PREV_D parameter. I am wondering if there is anything else that I need to add at the PX4 side. For example, how does PX4 know where the MAVLink messages of distance information is coming from? I suppose that the code for receiving MAVLink messages from UART could be different from the code for receiving MAVLink messages from SPI. Does PX4 already has code to receive MAVLink messages from any ports and decode the messages?

I apologize if my question is not clear or if it is too naive. I am quite new to PX4 and any help will be appreciated.

@chipper PX4 needs to received a OBSTACLE_DISTANCE mavlink message (https://mavlink.io/en/messages/common.html#OBSTACLE_DISTANCE). The code to received mavlink messages it’s already there (see ~/Firmware/src/modules/mavlink/mavlink_receiver.cpp)

Thank you @mrivi. It is quite helpful. I could see that ~/Firmware/src/modules/mavlink/mavlink_receiver.cpp shows how PX4 handles different types of MAVLink messages. Still, I am wondering how different bus protocols (e.g. UART or SPI) are handled. Would this be handled by the mavlink module or the uORB module?

The message comes into PX4 from from an external system via MAVLink (as the OBSTACLE_DISTANCE message). It then gets turned into a uorb message, which is what the collision prevention system actually uses.

MAVLink doesn’t care about the bus that the incoming message arrives over - this is assigned as described here: http://docs.px4.io/master/en/peripherals/mavlink_peripherals.html

If your sensor was internal (in this case it is not) then you would write the uorb topic directly in your rangefinder driver.

Docs here: http://docs.px4.io/master/en/computer_vision/collision_prevention.html#overview

Thank you @hamishwillee! The mavlink_peripherals page is what I have been looking for! From the description, it seems that PX4 firmware always assume it is a UART when referring to a “Serial” port, right?

Yes, I think so. Even if you’re connecting over wifi the interface is a UART.

Thank you @hamishwillee. When you say “Even if you’re connecting over wifi the interface is a UART”, you mean that I will need a wifi-UART bridge that would convert wifi to UART, right?

If I need to use other serial buses (such as SPI) other than UART for MAVLink, it means that I will need to modify the PX4 firmware to add that driver code, right?

I don’t know why you would need to use SPI, but if you can’t assign it as a serial port then yes, you will need to write a driver.

@bkueng This is correct right?

Yes, PX4 does not support MAVLink over SPI. The easiest way to add it would be to write a SPI driver that provides a device that acts like an UART. What is your reason to go for SPI instead of UART?

Thank you @bkueng and @hamishwillee. The main reason why I was exploring options other than UART for MAVLink is that the UART ports in my FMU were already used for telemetry. I was in an early stage of deciding what we would need in the system and thus was trying to get an idea of how these different options would cost. Thanks a lot for your help!