Subscribing sensor data issues

Hi there!
Well, i’m following the PX4 Dev Guide and i’m trying to complete the step 5 of the writing an application tutorial, which is subscribing my sensor data, the problem is that when i want to compile my code using make it gives me this error:

– Build files have been written to: /home/gesem/Firmware/build_posix_sitl_default
[503/520] Building C object src/examples/px4_s…xamples__px4_simple_app.dir/px4_simple_app.c.o
FAILED: /usr/bin/cc -DBUILD_URI=localhost -DCONFIG_ARCH_BOARD_SITL -DMODULE_NAME=“px4_simple_app” -DPX4_MAIN=px4_simple_app_app_main -D__DF_LINUX -D__PX4_LINUX -D__PX4_POSIX -D__STDC_FORMAT_MACROS -Dnoreturn_function=“attribute((noreturn))” -I. -Isrc -Isrc/modules -Isrc/modules/px4_messages -I…/mavlink/include/mavlink -I…/src -I…/src/drivers/boards/sitl -I…/src/include -I…/src/lib -I…/src/lib/DriverFramework/framework/include -I…/src/modules -I…/src/platforms -I…/src/lib/matrix -I…/src/modules/systemlib -I…/src/platforms/posix/include -Iexternal/Install/include -g -std=gnu99 -fno-common -Wall -Werror -Wextra -Wno-sign-compare -Wshadow -Wfloat-equal -Wpointer-arith -Wmissing-declarations -Wno-unused-parameter -Werror=format-security -Werror=array-bounds -Wfatal-errors -Werror=unused-variable -Werror=reorder -Werror=uninitialized -Werror=init-self -Werror=unused-but-set-variable -Wformat=1 -Wdouble-promotion -Werror=double-promotion -Wbad-function-cast -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -fvisibility=hidden -include visibility.h -O2 -fno-strict-aliasing -fomit-frame-pointer -funsafe-math-optimizations -ffunction-sections -fdata-sections -fno-strength-reduce -fno-builtin-printf -O2 -fno-strict-aliasing -fomit-frame-pointer -funsafe-math-optimizations -ffunction-sections -fdata-sections -fno-strength-reduce -fno-builtin-printf -std=gnu99 -MMD -MT src/examples/px4_simple_app/CMakeFiles/examples__px4_simple_app.dir/px4_simple_app.c.o -MF src/examples/px4_simple_app/CMakeFiles/examples__px4_simple_app.dir/px4_simple_app.c.o.d -o src/examples/px4_simple_app/CMakeFiles/examples__px4_simple_app.dir/px4_simple_app.c.o -c …/src/examples/px4_simple_app/px4_simple_app.c
…/src/examples/px4_simple_app/px4_simple_app.c: In function ‘px4_simple_app_main’:
…/src/examples/px4_simple_app/px4_simple_app.c:65:11: error: unused variable ‘poll_ret’ [-Werror=unused-variable]
int poll_ret = px4_poll(fds, 1, 1000);
^
compilation terminated due to -Wfatal-errors.
cc1: all warnings being treated as errors
[503/520] Building CXX object src/modules/bott…les/modules__bottle_drop.dir/bottle_drop.cpp.o
ninja: build stopped: subcommand failed.
Makefile:155: recipe for target ‘posix_sitl_default’ failed
make: *** [posix_sitl_default] Error 1

the code that now i have in my px4_simple_app.c is this:

__EXPORT int px4_simple_app_main(int argc, char *argv[]);

int px4_simple_app_main(int argc, char *argv[]){
PX4_INFO(“HOLA MUNDO”);

int sensor_sub_fd = orb_subscribe(ORB_ID(sensor_combined));

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

while (true) {
/* wait for sensor update of 1 file descriptor for 1000 ms (1 second) /
int poll_ret = px4_poll(fds, 1, 1000);
if (fds[0].revents & POLLIN) {
/
obtained data for the first file descriptor /
struct sensor_combined_s raw;
/
copy sensors raw data into local buffer */
orb_copy(ORB_ID(sensor_combined), sensor_sub_fd, &raw);
PX4_INFO(“Accelerometer:\t%8.4f\t%8.4f\t%8.4f”,
(double)raw.accelerometer_m_s2[0],
(double)raw.accelerometer_m_s2[1],
(double)raw.accelerometer_m_s2[2]);
}
}
return 0;
}

I’m really new at this, so will really appreciate if anyone can explain me what do i’m doing wrong, and how can i solve it, also if anyone can help me to understand in better way how does it work the uORB publish() / subscribe() messaging it will be fantastic!
I’ve already read the https://dev.px4.io/en/middleware/uorb.html and for the error I’ve already tried to erase the unused variable int poll_ret = px4_poll(fds, 1, 1000); but when i try to test the uORB subscription it tells me: px4_simple_app: command not found .

Thank you !

Hi @Gesem_Gudino_Mejia, Do you know which version of the PX4 source this is? Try updating to the latest master and rebuilding.

@dagar srry, what do you mean with version of the source ? when i build the original code that was in the px4_simple_app.c it builds correctly and when i test the uORB subscription as the dev guide, it works fine, but when i try to do the same with the code that’s up (without the int poll_ret = px4_poll(fds, 1, 1000); line) doesn’t work at all.

When did you checkout PX4? Try a fresh checkout from github to make sure you’re using the latest version.
Also make sure your compiler versions match what’s in the dev guide. https://dev.px4.io/en/setup/dev_env.html

yes, i followed the instructions from the dev guide as far, i began to follow the guide last week, i also have the GCC 5.4, I’ve already realized that it was a missing part in my code, now i’m just trying to understand how the publishing and subscribing code work’s or for what can use it.
my code end up like this, and its working well.
Thanks.

__EXPORT int px4_simple_app_main(int argc, char *argv[]);

int px4_simple_app_main(int argc, char *argv[])
{
PX4_INFO(“Hello Sky!”);

/* subscribe to sensor_combined topic /
int sensor_sub_fd = orb_subscribe(ORB_ID(sensor_combined));
/
limit the update rate to 5 Hz */
orb_set_interval(sensor_sub_fd, 200);

/* advertise attitude topic */
struct vehicle_attitude_s att;
memset(&att, 0, sizeof(att));
orb_advert_t att_pub = orb_advertise(ORB_ID(vehicle_attitude), &att);

/* one could wait for multiple topics with this technique, just using one here /
px4_pollfd_struct_t fds[] = {
{ .fd = sensor_sub_fd, .events = POLLIN },
/
there could be more file descriptors here, in the form like:
* { .fd = other_sub_fd, .events = POLLIN },
*/
};

int error_counter = 0;

while (true) {
/* wait for sensor update of 1 file descriptor for 1000 ms (1 second) */
int poll_ret = px4_poll(fds, 1, 1000);

  /* handle the poll result */
  if (poll_ret == 0) {
  	/* this means none of our providers is giving us data */
  	PX4_ERR("Got no data within a second");
  } else if (poll_ret < 0) {
  	/* this is seriously bad - should be an emergency */
  	if (error_counter < 10 || error_counter % 50 == 0) {
  		/* use a counter to prevent flooding (and slowing us down) */
  		PX4_ERR("ERROR return value from poll(): %d", poll_ret);
  	}
  	error_counter++;
  } else {
  	if (fds[0].revents & POLLIN) {
  		/* obtained data for the first file descriptor */
  		struct sensor_combined_s raw;
  		/* copy sensors raw data into local buffer */
  		orb_copy(ORB_ID(sensor_combined), sensor_sub_fd, &raw);
  		PX4_INFO("Accelerometer:\t%8.4f\t%8.4f\t%8.4f",
  			 (double)raw.accelerometer_m_s2[0],
  			 (double)raw.accelerometer_m_s2[1],
  			 (double)raw.accelerometer_m_s2[2]);
  		/* set att and publish this information for other apps*/
  		att.q[0] = raw.accelerometer_m_s2[0];
  		att.q[1] = raw.accelerometer_m_s2[1];
  		att.q[2] = raw.accelerometer_m_s2[2];
  		orb_publish(ORB_ID(vehicle_attitude), att_pub, &att);
  	}
  	/* there could be more file descriptors here, in the form like:
  	 * if (fds[1..n].revents & POLLIN) {}
  	 */
  }

}

return 0;
}