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

Use AP::airspeed() to get airspeed sensor, rather than going via AP_AHRS #15108

Merged
merged 5 commits into from Dec 7, 2020

Conversation

peterbarker
Copy link
Contributor

Setting the pointer in the AHRS predates our use of singletons.

Some of the users of this should probably be failling back to using a synthetic airspeed estimate instead of giving up if we don't have a true airspeed sensor.

Tools/Replay/Replay.cpp Outdated Show resolved Hide resolved
@@ -697,7 +697,7 @@ AP_AHRS_DCM::drift_correction(float deltat)
}
float airspeed;
if (airspeed_sensor_enabled()) {
airspeed = _airspeed->get_airspeed();
airspeed = AP::airspeed()->get_airspeed();
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs a null check. Not all vehicles instantiate airspeed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

airspeed_sensor_enabled() does that. Further, we're already dereferencing a pointer :-)

I do want to rename airspeed_sensor_enabled() to using_airspeed_sensor().

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant AP_::airspeed() could be nullptr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it can't; airspeed_sensor_enabled checks for nullptr. It already checks _airspeed for nullptr, and has been changed to check the singleton instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the same argument that was discussed on yesterday's dev call. The function should return the result when true, in this case a non-null ptr. Having a function check something, and then at a later time it is assumed true. That's not thread safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where the check is done is irrelevant. If you want to do a nullptr check before calling a method on it then you need some sort of lock on that pointer or critical section of code.

Copy link
Contributor

@magicrub magicrub left a comment

Choose a reason for hiding this comment

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

needs nullptr checking

@@ -285,12 +277,14 @@ class AP_AHRS
// return true if airspeed comes from an airspeed sensor, as
// opposed to an IMU estimate
bool airspeed_sensor_enabled(void) const {
const AP_Airspeed * _airspeed = AP::airspeed();
Copy link
Contributor

Choose a reason for hiding this comment

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

unnecessary whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

return _airspeed != nullptr && _airspeed->use() && _airspeed->healthy();
}

// return true if airspeed comes from a specific airspeed sensor, as
// opposed to an IMU estimate
bool airspeed_sensor_enabled(uint8_t airspeed_index) const {
const AP_Airspeed * _airspeed = AP::airspeed();
Copy link
Contributor

Choose a reason for hiding this comment

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

unnecessary whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -697,7 +697,7 @@ AP_AHRS_DCM::drift_correction(float deltat)
}
float airspeed;
if (airspeed_sensor_enabled()) {
airspeed = _airspeed->get_airspeed();
airspeed = AP::airspeed()->get_airspeed();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the same argument that was discussed on yesterday's dev call. The function should return the result when true, in this case a non-null ptr. Having a function check something, and then at a later time it is assumed true. That's not thread safe.

@@ -1049,14 +1049,14 @@ bool AP_AHRS_DCM::get_position(struct Location &loc) const
// return an airspeed estimate if available
bool AP_AHRS_DCM::airspeed_estimate(float &airspeed_ret) const
{
return airspeed_estimate(_airspeed?_airspeed->get_primary():0, airspeed_ret);
return airspeed_estimate(AP::airspeed()?AP::airspeed()->get_primary():0, airspeed_ret);
Copy link
Contributor

Choose a reason for hiding this comment

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

should not be checking if non-zero, should explicitly looking for nullptr

Copy link
Contributor

Choose a reason for hiding this comment

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

avoid the double call to singleton:
auto *airspeed = xxxxx;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -1049,14 +1049,14 @@ bool AP_AHRS_DCM::get_position(struct Location &loc) const
// return an airspeed estimate if available
bool AP_AHRS_DCM::airspeed_estimate(float &airspeed_ret) const
{
return airspeed_estimate(_airspeed?_airspeed->get_primary():0, airspeed_ret);
return airspeed_estimate(AP::airspeed()?AP::airspeed()->get_primary():0, airspeed_ret);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return airspeed_estimate(AP::airspeed()?AP::airspeed()->get_primary():0, airspeed_ret);
return airspeed_estimate((AP::airspeed() != nullptr)?AP::airspeed()->get_primary():0, airspeed_ret);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

}

// return an airspeed estimate from a specific airspeed sensor instance if available
bool AP_AHRS_DCM::airspeed_estimate(uint8_t airspeed_index, float &airspeed_ret) const
{
if (airspeed_sensor_enabled(airspeed_index)) {
airspeed_ret = _airspeed->get_airspeed(airspeed_index);
airspeed_ret = AP::airspeed()->get_airspeed(airspeed_index);
Copy link
Contributor

Choose a reason for hiding this comment

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

AP::airspeed() needs nullptr check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is checked in airspeed_sensor_enabled.

I'm looking at refactoring a whole bunch of this stuff and would prefer to not make too many changes. Note that _airspeed here is already a pointer, so the nullptr check better work already!

@@ -2231,6 +2231,7 @@ uint8_t AP_AHRS_NavEKF::get_active_airspeed_index() const
}
#endif
// for the rest, let the primary airspeed sensor be used
const AP_Airspeed * _airspeed = AP::airspeed();
Copy link
Contributor

Choose a reason for hiding this comment

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

unnecessary whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -516,7 +516,7 @@ uint32_t AP_Frsky_SPort_Passthrough::calc_velandyaw(void)
AP_AHRS &_ahrs = AP::ahrs();
WITH_SEMAPHORE(_ahrs.get_semaphore());
// horizontal velocity in dm/s (use airspeed if available and enabled - even if not used - otherwise use groundspeed)
const AP_Airspeed *aspeed = _ahrs.get_airspeed();
const AP_Airspeed *aspeed = AP::airspeed();
if (aspeed && aspeed->enabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

needs explicit nullptr check instead of non-null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's modifying a line which isn't modified already in this PR.

I've just changed the way the pointer is fetched. This pointer will never be nullptr more often that the existing code!

Similarly in the other telem backends.

Those are all probably wrong anyway - they should be asking the AHRS for the estimate, not the sensor.

@@ -121,7 +121,7 @@ void AP_LTM_Telem::send_Sframe(void)
const uint16_t amp = (uint16_t) roundf(current * 100.0f); // current sensor (expects value in hundredth of A)

// airspeed in m/s if available and enabled - even if not used - otherwise send 0
const AP_Airspeed *aspeed = AP::ahrs().get_airspeed();
const AP_Airspeed *aspeed = AP::airspeed();
uint8_t airspeed = 0; // airspeed sensor (m/s)
if (aspeed && aspeed->enabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

needs explicit nullptr chk instead of nonzero

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As elsewhere, unrelated to my PR. Not a bad change, just unrelated.

@@ -435,7 +435,7 @@ void AP_Spektrum_Telem::calc_airspeed()
AP_AHRS &ahrs = AP::ahrs();
WITH_SEMAPHORE(ahrs.get_semaphore());

const AP_Airspeed *airspeed = ahrs.get_airspeed();
const AP_Airspeed *airspeed = AP::airspeed();
float speed = 0.0f;
if (airspeed && airspeed->healthy()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

needs explicit nullptr chk instead of nonzero

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As elsewhere, unrelated to my PR. Not a bad change, just unrelated.

@@ -1049,14 +1049,14 @@ bool AP_AHRS_DCM::get_position(struct Location &loc) const
// return an airspeed estimate if available
bool AP_AHRS_DCM::airspeed_estimate(float &airspeed_ret) const
{
return airspeed_estimate(_airspeed?_airspeed->get_primary():0, airspeed_ret);
return airspeed_estimate(AP::airspeed()?AP::airspeed()->get_primary():0, airspeed_ret);
Copy link
Contributor

Choose a reason for hiding this comment

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

avoid the double call to singleton:
auto *airspeed = xxxxx;

@tridge tridge removed the DevCallEU label Nov 25, 2020
@peterbarker peterbarker force-pushed the pr/remove-ahrs-set-airspeed branch 2 times, most recently from d56148f to 018bbbc Compare November 28, 2020 22:25
@magicrub magicrub merged commit 5fae265 into ArduPilot:master Dec 7, 2020
@peterbarker peterbarker deleted the pr/remove-ahrs-set-airspeed branch December 8, 2020 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants