Create new driver for external sensor

Hi everyone,

I am new with the px4 source code, release version 1.14. I am trying to integrate an external sensor into the “src/drivers” of the source code. However, I am having problems building this module and building the attached parameter file.

What do I need to do to be able to build this module with its params.c file, so that the parameter can be displayed on the qgroundcontrol?

Thanks for reading.

Once the parameters are in PX4, they’ll be visible on QGC if they are used, and visible via Mavlink Console if not (used, or touched, meaning some module accessed them).

For new driver, see this: Driver Development | PX4 Guide (main)

For more basic info, see First Application Tutorial (Hello Sky) | PX4 Guide (main) and Module Template for Full Applications | PX4 Guide (main).

Also, consider using newer versions to make it easier for developers to help you.

Thanks for your advice!

I connect my sensor to uart4&i2c port to use UART communication. But I saw in default.px4board:
CONFIG_BOARD_TOOLCHAIN=“arm-none-eabi”
CONFIG_BOARD_ARCHITECTURE=“cortex-m7”
CONFIG_BOARD_ETHERNET=y
CONFIG_BOARD_SERIAL_GPS1=“/dev/ttyS0”
CONFIG_BOARD_SERIAL_GPS2=“/dev/ttyS7”
CONFIG_BOARD_SERIAL_TEL1=“/dev/ttyS6”
CONFIG_BOARD_SERIAL_TEL2=“/dev/ttyS4”
CONFIG_BOARD_SERIAL_TEL3=“/dev/ttyS1”
CONFIG_BOARD_SERIAL_EXT2=“/dev/ttyS3”
Does uart4&i2c port not match to any ttyS* port ? How can I modify the code to use uart4&i2c port ?
Thanks

See here: Serial Port Mapping | PX4 Guide (main)

It depends on which UARTs are being used by PX4 on your board. The linked page should be easy enough to follow.

1 Like

Hi dotanAtTrim,

I followed the instructions in this page. I switch my external sensor to connect to TEL1 port (“dev/ttyS6” in fmu-v6x). In QGroundControl, I switch MAV_0_CONFIG to TEL3 (103). But when I debug the driver of my external sensor (by eclipse), I found that the data received is not correct.

What should I do now ?

When I check the serial port /dev/ttyS6 (TEL1) to write bytes, I found the error Permission denied. I checked all the parameters in vehicle/parameters and not find any sensor (or mavlink) connect to TEL1 port.

How could I overcome this problem ?

I don’t understand where and how you’re trying to write bytes.
One guess I have is you have QGC open with Auto Connect enabled and it’s taking control over all serials connected to you PC. In this case try closing QGC, and open a the serial before QGC, or disable auto connect in the application’s settings.

I wrote a driver to communicate with the external sensor based on the tfmini driver in the distance_sensor folder. Instead of using ttyS3 as in the module, I used ttyS6 (TEL1) because I found this port was empty. Then I interacted with the sensor’s register to configure the returned data using the syntax: int ret = write(_fd, buffer, size). But the ret value was negative and returned an error of 13 corresponding to Permission Denied.

Do you have any advice?

Share your code or at least link to a specific line or file with a github permalink, I can’t keep guessing what you see in front of you and how you’re trying to implement anything.

What do you mean interact with the sensor?
The sensor itself returned error, or the write() function itself?

I think I found the solution. The reason for Permission denied error when I use function write() and read() maybe is that I don’t reopen the file descriptor in the Run() method. When I reopen it, I can interact with the register now.