Cannot publish data into uORB topic

Hi, I’m writing a new driver for an airspeed sensor. To understand how uORB works, I wrote these lines in collect() function.

	float aspd = 12.02f;

	airspeed_s airspeed;
	airspeed.timestamp = hrt_absolute_time();
	airspeed.indicated_airspeed_m_s = aspd;
	airspeed.true_airspeed_m_s = aspd;
	airspeed.air_temperature_celsius = -1000.0;
	airspeed.confidence = 1.0f;

	if (_airspeed_pub == nullptr) {
			_airspeed_pub = orb_advertise(ORB_ID(airspeed), &airspeed);

		} else {
			orb_publish(ORB_ID(airspeed), _airspeed_pub, &airspeed);
		}

I thought indicated_airspeed_m_s and true_airspeed_m_s should be 12.02, but after I start the driver, airspeed never changes and the result of’
listener airspeed
is like this.

What part of the code could be wrong?

Thank you.

Hi
there might be some other driver that published data as well (airspeed_selector). You can check with listener airspeed before you start your driver.

1 Like

Thanks for your reply!

I tried but no other driver seem to publish data.

Which version of PX4 do you use? With my 1.9.2 it works (I just added the definition for _airspeed_pub as nullptr).
In your first screenshot is the timestamp parameter missing, did you remove it?

1 Like

I just added
_airspeed_pub = nullptr
and it worked! Thank you!

Can I ask another question?(I apologize if i should’ve make another topic)
The airspeed data is continuously coming at 10Hz from arduino via i2c. The data is 4byte. To read this data, can I use transfer(nullptr, 0, &val[0], 4);, or is there any other function to use?

Thanks.

I haven’t worked with direct I2C on Pixhawk yet, so I can’t give you full answer.
But your questions sounds like, there’s something crucial missing: I2C is a Master-Slave Protocol. The Master (your Pixhawk) sends a request message to the slave (Arduino) and then the slave gives a response. So the Slave cannot just send data continously, but it has to be asked by the Master every 10Hz. As an alternative you could use SPI (supported by Arduino and Pixhawk).

If you still want to use I2C, be aware of how to use the I2C class. You want to use the method “transfer” but it’s protected as declared in the header file (https://github.com/PX4/Firmware/blob/07d656e971a72d1202651dfd3b4642736fb078d7/src/lib/drivers/device/posix/I2C.hpp). So you cannot just create an I2C object and use the transfer function but you need to create a class thats derived form the I2C class. There you would be able to use the transfer function. But I would still advise using SPI.

What you basically want to do is to build a driver. There are some explanations for this in the PX4 Documentations: https://dev.px4.io/v1.9.0/en/middleware/drivers.html#creating-a-driver

1 Like

Thank you for the helpful advice! I am still a newbie programmer so it’s really appreciated.

Yes, I understand that I2C slave can’t just send data. So at first I tried to look for a method in px4 system which is equivalent to Wire.requestFrom() function included in Wire library for Arduino.

I’ll look for drivers which use SPI and try to understand them.