Unable to start custom i2c distance sensor

Hello.
I’m trying to create an I2C driver for the TFMini Plus (distance sensor).

I have already managed to obtain the measurement and display it by running the command tfmini_i2c start -a 82 -R -X followed by the command listener distance_sensor

My TFMini Plus has the 0x52 Hex address (82 Dec)

However, I have not yet managed to make it run automatically when the firmware starts. I understand that this is done by adding the line tfmini_i2c start -a 82 -R 0 -X to the file ROMFS/px4fmu_common/init.d/rc.sensors
I currently have these lines in that file:
if param greater -s SENS_TFMINI_I2C 0
then
tfmini_i2c -a 82 -R 0 -X start
fi

The SENS_TFMINI_I2C parameter is a parameter I created in the tfmini_i2c driver, and I have it set to 1.

If I put that same piece of code in the ROMFS/px4fmu_common/init.d/rcS file, the module does start (I’m checking by running the command tfmini_i2c status).

Am I doing something wrong or missing some detail?

The code I’m using is based in v1.16.0

Here’s a repo with the changes I’ve made at the moment:

Have you tried checking with dmesg to see what is going on during startup? Or alternatively, use a serial port to get printf output as you boot up.

You just have to set the set the console to a UART, like so:

Hi Julian. Thanks for your reply.

I see the message “no instance started (no device on bus?)”. So I guess my device is not being detected during startup for some reason.

After some testing, I found that if I put a 50 ms delay just before calling the rc.sensors file (from rcS file), it works:

       usleep 50000                # <-Here I set the delay
       #        
       # board sensors: rc.sensors
       #
       set BOARD_RC_SENSORS ${R}etc/init.d/rc.board_sensors
       if [ -f $BOARD_RC_SENSORS ]
       then
           echo "Board sensors: ${BOARD_RC_SENSORS}"
           . $BOARD_RC_SENSORS
       fi
       unset BOARD_RC_SENSORS

Even so, I still don’t understand why it works with that delay. Maybe some other module/process is occupying the bus at the same time?

I also tried with 10ms and less, but it did not detect my sensor that way.

I’m using a CubeOrange as flight controller btw

I’m not sure. Could it be that the sensor takes a while to be ready?

1 Like

It was a good guess, I overlooked it.

The sensor must wait 100ms before receiving commands to initialize correctly.

I removed the delay from rCS and moved it to the init method of my driver, just before calling I2C::init().

It works correctly now, thank you very much for your time.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.