Skip to content

Commit 57c80cd

Browse files
committed
AP_Common: rearrange get_vector_from_origin_NEU to be less destructive
this change means that if the origin call failes we don't half-update the object before returning false
1 parent 3968652 commit 57c80cd

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

libraries/AP_Common/Location.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ bool Location::get_alt_m(AltFrame desired_frame, float &ret_alt) const
222222
}
223223

224224
#if AP_AHRS_ENABLED
225+
// converts location to a vector from origin; if this method returns
226+
// false then vec_ne is unmodified
225227
bool Location::get_vector_xy_from_origin_NE(Vector2f &vec_ne) const
226228
{
227229
Location ekf_origin;
@@ -233,18 +235,21 @@ bool Location::get_vector_xy_from_origin_NE(Vector2f &vec_ne) const
233235
return true;
234236
}
235237

238+
// converts location to a vector from origin; if this method returns
239+
// false then vec_neu is unmodified
236240
bool Location::get_vector_from_origin_NEU(Vector3f &vec_neu) const
237241
{
238-
// convert lat, lon
239-
if (!get_vector_xy_from_origin_NE(vec_neu.xy())) {
240-
return false;
241-
}
242-
243242
// convert altitude
244243
int32_t alt_above_origin_cm = 0;
245244
if (!get_alt_cm(AltFrame::ABOVE_ORIGIN, alt_above_origin_cm)) {
246245
return false;
247246
}
247+
248+
// convert lat, lon
249+
if (!get_vector_xy_from_origin_NE(vec_neu.xy())) {
250+
return false;
251+
}
252+
248253
vec_neu.z = alt_above_origin_cm;
249254

250255
return true;

libraries/AP_Common/Location.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ class Location
5555
// - above-origin and origin is not set
5656
bool change_alt_frame(AltFrame desired_frame);
5757

58-
// get position as a vector (in cm) from origin (x,y only or x,y,z)
59-
// return false on failure to get the vector which can only
60-
// happen if the EKF origin has not been set yet
61-
// x, y and z are in centimetres
58+
// get position as a vector (in cm) from origin (x,y only or
59+
// x,y,z) return false on failure to get the vector which can only
60+
// happen if the EKF origin has not been set yet x, y and z are in
61+
// centimetres. If this method returns false then vec_ne is
62+
// unmodified.
6263
bool get_vector_xy_from_origin_NE(Vector2f &vec_ne) const WARN_IF_UNUSED;
64+
// converts location to a vector from origin; if this method returns
65+
// false then vec_neu is unmodified
6366
bool get_vector_from_origin_NEU(Vector3f &vec_neu) const WARN_IF_UNUSED;
6467

6568
// return horizontal distance in meters between two locations

libraries/AP_Common/tests/test_location.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ TEST(Location, Tests)
280280
EXPECT_EQ(0, test_location4.loiter_xtrack);
281281
EXPECT_TRUE(test_location4.initialised());
282282

283-
const Location test_location_empty{test_vect, Location::AltFrame::ABOVE_HOME};
284-
EXPECT_FALSE(test_location_empty.get_vector_from_origin_NEU(test_vec3));
283+
// can't create a Location using a vector here as there's no origin for the vector to be relative to:
284+
// const Location test_location_empty{test_vect, Location::AltFrame::ABOVE_HOME};
285+
// EXPECT_FALSE(test_location_empty.get_vector_from_origin_NEU(test_vec3));
285286
}
286287

287288
TEST(Location, Distance)

0 commit comments

Comments
 (0)