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
Use AP::airspeed() to get airspeed sensor, rather than going via AP_AHRS #15108
Conversation
@@ -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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()
.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
d6d9d13
to
26f52c1
Compare
e579eb1
to
7e2843d
Compare
7e2843d
to
cd10d2c
Compare
cd10d2c
to
00b09f1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs nullptr checking
libraries/AP_AHRS/AP_AHRS.h
Outdated
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary whitespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
libraries/AP_AHRS/AP_AHRS.h
Outdated
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary whitespace
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
libraries/AP_AHRS/AP_AHRS_DCM.cpp
Outdated
@@ -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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
libraries/AP_AHRS/AP_AHRS_DCM.cpp
Outdated
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
libraries/AP_AHRS/AP_AHRS_NavEKF.cpp
Outdated
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary whitespace
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
libraries/AP_AHRS/AP_AHRS_DCM.cpp
Outdated
@@ -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); |
There was a problem hiding this comment.
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;
d56148f
to
018bbbc
Compare
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.