From a310980ad2206cab09977713533cef7b3c816b98 Mon Sep 17 00:00:00 2001
From: Matthias Grob <maetugr@gmail.com>
Date: Tue, 13 Nov 2018 06:40:12 +0000
Subject: [PATCH] mc_pos_control: refactor loop polling

---
 .../mc_pos_control/mc_pos_control_main.cpp    | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/src/modules/mc_pos_control/mc_pos_control_main.cpp b/src/modules/mc_pos_control/mc_pos_control_main.cpp
index fd1bc1add3..39733976e5 100644
--- a/src/modules/mc_pos_control/mc_pos_control_main.cpp
+++ b/src/modules/mc_pos_control/mc_pos_control_main.cpp
@@ -593,21 +593,16 @@ MulticopterPositionControl::run()
 	// Let's be safe and have the landing gear down by default
 	_att_sp.landing_gear = vehicle_attitude_setpoint_s::LANDING_GEAR_DOWN;
 
-	// wakeup source
-	px4_pollfd_struct_t fds[1];
-
-	fds[0].fd = _local_pos_sub;
-	fds[0].events = POLLIN;
+	// setup file descriptor to poll the local position as loop wakeup source
+	px4_pollfd_struct_t poll_fd = {.fd = _local_pos_sub, .events = POLLIN};
 
 	while (!should_exit()) {
-		// wait for up to 20ms for data
-		int pret = px4_poll(&fds[0], (sizeof(fds) / sizeof(fds[0])), 20);
-
-		// pret == 0: go through the loop anyway to copy manual input at 50 Hz.
-
+		// poll for new data on the local position state topic (wait for up to 20ms)
+		const int poll_return = px4_poll(&poll_fd, 1, 20);
+		// poll_return == 0: go through the loop anyway to copy manual input at 50 Hz
 		// this is undesirable but not much we can do
-		if (pret < 0) {
-			PX4_ERR("poll error %d, %d", pret, errno);
+		if (poll_return < 0) {
+			PX4_ERR("poll error: %d, %s", poll_return, strerror(errno));
 			continue;
 		}
 
-- 
GitLab