Getting Separate Data from Multiple Sensors

Hey all,
I was wondering how one would go about getting individual sensor data. My drone project objectives involve aligning my drone with the direction of an external magnetometer. Although I have gone through the code extensively, I cannot seem to find the file/subroutine/UORB call where I can subscribe or get individual sensor/mag data. In addition I would like to find out if there is a single algorithm which handles the prioritization of all external/internal sensors. Please help for I am in dire need of an asist here thanks!


what do you mean by getting individual sensor data? Is there already a driver reading the data from your magnetometer and publishing it to a topic? Then you could just subscribe to the topic, copy the whole thing and assign the value of the magnetometer struct to a single variable like this:

int mag_sub = orb_subscribe(ORB_ID(sensor_mag));
sensor_mag_s temp;
orb_copy(ORB_ID(sensor_mag), mag_sub, &temp);
float mag_in_x_direction = temp.x;

If there’s no driver, you would have to build one:

Do you mean a specific sensor?

Thanks for the fast response! I meant when trying to program pixhawk (Im using Pixhawk 2), in the source code given, even though drivers exist for the model of the Magnetometer module I am using, there is no subscribe method to obtain data from that one specific sensor right?

And if understood your answer, I have to create seperate drivers for all the sensors that I need data from right?

Also is there any subscribe methodology in the source code to identify each sensor uniquely? (for example if I connected two magentometers of the same model through an I2C splitter how would I identify each sensor separately in the code?)


any update would be greatly appreciated.

Yes. Two specific sensors to be exact. So the board I am working with is a Pixhawk 2 and connected to the I2C port (using an I2C splitter) is two magnetometers (one is the compass modules mag and the other is an external mag). I want to compare data from both these sensors.

The sensor_mg uorb msg containts the a device_id field which is a unique identification that doesn’t change at reboot.

You can subscribe to all sensor_mag and look for the one with the correct ID

Im so sorry for my lack of knowledge, but how would I refer or identify each sensor separately? In sensor_mag.msg or the .h file auto-generated does not have an array or some data structure that stores each mag ID and corresponding data is there?

That’s no problem. Start by getting familiar with the uorb messaging systems:

After that if you still have question don’t hesitate to ask.

1 Like

Thanks man! Will do!

Thanks a tonne for that dude, I kinda figured it out. The for loop that I put together to read all mag data is as follows:

	for(int i = 0; i < 3; i++){
	int sensor_sub_fd = orb_subscribe_multi(ORB_ID(sensor_mag),i);

	PX4_INFO("%X", sensor_sub_fd);

	orb_set_interval(sensor_sub_fd, 1000);

	struct sensor_mag_s reading;

	memset(&reading, 0, sizeof(reading));

	//orb_advert_t reading_pub = orb_advertise(ORB_ID(sensor_mag), &reading);

	px4_pollfd_struct_t fds[] = {
		{ .fd = sensor_sub_fd,   .events = POLLIN },

	error_counter = 0;
	for (int j = 0; j < 10; j++) {
		int poll_ret = px4_poll(fds, 1, 1100);

		if (poll_ret == 0) {
			PX4_ERR("Got no data within a second");

		} else if (poll_ret < 0) {
			if (error_counter < 10 || error_counter % 50 == 0) {
				PX4_ERR("ERROR return value from poll(): %d", poll_ret);


		} else {

			if (fds[0].revents & POLLIN) {
				struct sensor_mag_s raw;
				orb_copy(ORB_ID(sensor_mag), sensor_sub_fd, &raw);


The problem with this is even if I expand the outer for-loop to include 10 repetitions, the output of the program only returns data for two mags. It will always return the internal mags results and only that of one external mag. I have two external mags connected to the I2C port (one in-built on the GPS module and one external). My objective is to get the readings from the external mag and to compare and adjust the drone’s bearings to match that mag. Therefore, I need to get readings from all connected mags on the I2C.

I would make sure that the additional MAG functions and publish correctly.
How did you integrated this additional device?

I connected an I2C splitter to the I2C port of the Pix and connected both the MAG of the GPS as well as the external MAG to the splitter.