Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce uORB::SubscriptionBlocking and use in navigator and simulator #12371

Merged
merged 1 commit into from
Jan 22, 2020

Conversation

dagar
Copy link
Member

@dagar dagar commented Jun 29, 2019

This PR introduces a new uORB Subscription class that has a blocking update. This can be used in place of the traditional orb subscriptions and polling.

Also contained in this PR are the other minor uORB changes needed for logger. #12123

Example

Existing orb + poll usage

int local_pos_sub = orb_subscribe(ORB_ID(vehicle_local_position));

/* wakeup source(s) */
px4_pollfd_struct_t fds[1] = {};

/* Setup of loop */
fds[0].fd = local_pos_sub;
fds[0].events = POLLIN;

/* rate-limit position subscription to 20 Hz / 50 ms */
orb_set_interval(local_pos_sub, 50);

while (!should_exit()) {

/* wait for up to 1000ms for data */
int pret = px4_poll(&fds[0], (sizeof(fds) / sizeof(fds[0])), 1000);

if (pret == 0) {
/* Let the loop run anyway, don't do `continue` here. */

} else if (pret < 0) {
/* this is undesirable but not much we can do - might want to flag unhappy status */
PX4_ERR("poll error %d, %d", pret, errno);
px4_usleep(10000);
continue;

} else {
if (fds[0].revents & POLLIN) {
/* success, local pos is available */
orb_copy(ORB_ID(vehicle_local_position), local_pos_sub, &_local_pos);
}
}

// DO THE WORK
}

orb_unsubscribe(local_pos_sub);

Simplifies to this (using PR)

uORB::SubscriptionBlocking<vehicle_local_position_s> local_pos_sub{ORB_ID(vehicle_local_position), 50};

while (!should_exit()) {

// wait for data
if (!local_pos_sub.updateBlocking(_local_pos)) {
continue;
}

// DO THE WORK
}

@dagar dagar added this to the Release v1.10.0 milestone Jun 29, 2019
@dagar dagar requested review from bkueng and julianoes June 29, 2019 17:04
@dagar dagar force-pushed the pr-uorb_SubscriptionBlocking branch 4 times, most recently from 0f9e282 to f9be191 Compare June 29, 2019 17:28
@dagar
Copy link
Member Author

dagar commented Jun 29, 2019

One point to discuss is the desired timeout behavior. There might be exceptions that I haven't come across, but in our orb related poll usage there's a timeout that does nothing but a fresh poll setup. So the question is if we need to preserve the ability to do anything else on a timeout, and if that timeout even needs to configurable.

@dagar
Copy link
Member Author

dagar commented Jun 29, 2019

@AuterionWrikeBot keeps helpfully clobbering the formatting in my example.

@dagar
Copy link
Member Author

dagar commented Jun 30, 2019

Out of flash on fmu-v2 again.

@dagar dagar changed the title introduce uORB::SubscriptionBlocking and use in navigator and simulator [WIP]: introduce uORB::SubscriptionBlocking and use in navigator and simulator Jun 30, 2019
@julianoes
Copy link
Contributor

@dagar ok what do you need me to do here? Review or do we need to disable something else for v2?

@dagar
Copy link
Member Author

dagar commented Jul 1, 2019

@dagar ok what do you need me to do here? Review or do we need to disable something else for v2?

I'll ping you again once it's been resolved.

@dagar dagar removed request for bkueng and julianoes July 1, 2019 14:20
@dagar dagar force-pushed the pr-uorb_SubscriptionBlocking branch from e6787e7 to 440d307 Compare July 26, 2019 17:47
@dagar dagar force-pushed the pr-uorb_SubscriptionBlocking branch 3 times, most recently from 0e0773f to 0c401f7 Compare August 1, 2019 01:28
@dagar dagar changed the title [WIP]: introduce uORB::SubscriptionBlocking and use in navigator and simulator introduce uORB::SubscriptionBlocking and use in navigator and simulator Aug 1, 2019
@dagar dagar requested review from julianoes and a team August 1, 2019 01:33
@dagar
Copy link
Member Author

dagar commented Sep 30, 2019

Updated LPE to kill off uORB::SubscriptionPollable entirely.

@dagar dagar force-pushed the pr-uorb_SubscriptionBlocking branch from 42c17eb to d403620 Compare September 30, 2019 22:50
if (poll_return < 0) {
PX4_ERR("poll error %d %d", poll_return, errno);
// wait for new data on the local position state topic
if (!_local_pos_sub.updateBlocking(_local_pos)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the previous implementation we still ran this loop on timeout, with the new one we're just continuing, right? Do we know the implications?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partially, but we need to be careful and think it through module by module. In the past for mc_pos_control this was important for stabilized/manual mode, but that's been moved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaEtUgR could you check?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is solved in a different way now right? https://github.com/PX4/Firmware/blob/f956bafa4eda7ceb298790ef0fff5540fd3c589f/src/modules/mc_pos_control/mc_pos_control_main.cpp#L535
I think it's much better to run the controller up on new estimation data but we need to make sure there's instant and reliable emergency handling in case of a timeout. Last resort the attitude controller should keep the vehicle stable level if attitude setpoints are missing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it will need to be updated, although that entire topic needs to be reviewed. Commander and mc_pos_control have separate notions of position and velocity validity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, mc_pos_control was pulled out of this PR (moved to a WQ).

int ret_mutex_init = pthread_mutex_init(&_mutex, &attr);
{
PX4_ERR("pthread_mutex_init failed, status=%d", ret_mutex_init);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pthread_mutexattr_destroy is missing here.

src/modules/uORB/SubscriptionBlocking.hpp Show resolved Hide resolved
// otherwise wait with timeout based on interval
struct timespec ts;

if (px4_clock_gettime(CLOCK_REALTIME, &ts) != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be based on the monotonic clock?

And then we need to set pthread_condattr_setclock (&cond_attr, CLOCK_MONOTONIC) but that's probably only possible on Posix but not NuttX.
https://github.com/PX4/Firmware/blob/07d656e971a72d1202651dfd3b4642736fb078d7/platforms/posix/src/px4/common/px4_sem.cpp#L65

Copy link
Member Author

@dagar dagar Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLOCK_MONOTONIC is available on NuttX, but needs to be added to several boards. Trying it now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLOCK_MONOTONIC hangs pthread_cond_timedwait, which appears to use CLOCK_REALTIME internally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right but can we use it for Posix then?

@dagar
Copy link
Member Author

dagar commented Oct 28, 2019

The estimators (attq and lpe) might as well transition straight to a WQ with callbacks. #13287

@dagar dagar force-pushed the pr-uorb_SubscriptionBlocking branch from 790cd1f to 4dcd9c2 Compare October 28, 2019 20:57
@dagar
Copy link
Member Author

dagar commented Oct 28, 2019

Moving camera_feedback to a WQ. #13044

@dagar dagar force-pushed the pr-uorb_SubscriptionBlocking branch from 4dcd9c2 to 94d091f Compare October 28, 2019 21:16
@dagar dagar force-pushed the pr-uorb_SubscriptionBlocking branch from 94d091f to 50f89bc Compare November 22, 2019 20:20
@dagar dagar force-pushed the pr-uorb_SubscriptionBlocking branch from 50f89bc to f85f314 Compare January 22, 2020 21:09
@dagar
Copy link
Member Author

dagar commented Jan 22, 2020

I'm going to merge uORB::SubscriptionBlocking without the remaining use cases (commander low priority and simulator) so we can start experimenting with it and stop rebasing this PR.

@dagar dagar merged commit 98042bf into PX4:master Jan 22, 2020
@dagar dagar deleted the pr-uorb_SubscriptionBlocking branch January 22, 2020 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants