Skip to content

Commit 22c6ce0

Browse files
committed
AP_Follow: change to using position-from-origin internally
1 parent 8c1da9a commit 22c6ce0

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

Diff for: libraries/AP_Follow/AP_Follow.cpp

+66-17
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ void AP_Follow::clear_offsets_if_required()
167167

168168
// get target's estimated location
169169
bool AP_Follow::get_target_location_and_velocity(Location &loc, Vector3f &vel_ned) const
170+
{
171+
Vector3p pos_ned;
172+
Vector3f local_vel_ned; // so we either change both return parameters or neither
173+
if (!get_target_position_and_velocity(pos_ned, local_vel_ned)) {
174+
return false;
175+
}
176+
if (!AP::ahrs().get_location_from_origin_offset_NED(loc, pos_ned * 0.01)) {
177+
return false;
178+
}
179+
vel_ned = local_vel_ned;
180+
181+
if (_alt_type == AP_FOLLOW_ALTITUDE_TYPE_RELATIVE) {
182+
loc.change_alt_frame(Location::AltFrame::ABOVE_HOME);
183+
}
184+
185+
return true;
186+
}
187+
188+
// get target's estimated location and velocity, both NED SI from origin
189+
bool AP_Follow::get_target_position_and_velocity(Vector3p &pos_ned, Vector3f &vel_ned) const
170190
{
171191
// exit immediately if not enabled
172192
if (!_enabled) {
@@ -187,16 +207,14 @@ bool AP_Follow::get_target_location_and_velocity(Location &loc, Vector3f &vel_ne
187207
}
188208

189209
// project the vehicle position
190-
Location last_loc = _target_location;
191-
last_loc.offset(vel_ned.x * dt, vel_ned.y * dt);
192-
last_loc.alt -= vel_ned.z * 100.0f * dt; // convert m/s to cm/s, multiply by dt. minus because NED
210+
const Vector3p vel_ned_p { vel_ned.x, vel_ned.y, vel_ned.z };
211+
pos_ned = _target_position_ned + vel_ned_p * dt;
193212

194-
// return latest position estimate
195-
loc = last_loc;
196213
return true;
197214
}
198215

199216
// get distance vector to target (in meters) and target's velocity all in NED frame
217+
// FIXME: use the target_position_ned vector!
200218
bool AP_Follow::get_target_dist_and_vel_ned(Vector3f &dist_ned, Vector3f &dist_with_offs, Vector3f &vel_ned)
201219
{
202220
// get our location
@@ -341,6 +359,7 @@ bool AP_Follow::handle_global_position_int_message(const mavlink_message_t &msg)
341359
return false;
342360
}
343361

362+
Location _target_location;
344363
_target_location.lat = packet.lat;
345364
_target_location.lng = packet.lon;
346365

@@ -352,6 +371,10 @@ bool AP_Follow::handle_global_position_int_message(const mavlink_message_t &msg)
352371
// absolute altitude
353372
_target_location.set_alt_cm(packet.alt / 10, Location::AltFrame::ABSOLUTE);
354373
}
374+
if (!_target_location.get_vector_from_origin_NEU(_target_position_ned)) {
375+
return false;
376+
}
377+
_target_position_ned.z = -_target_position_ned.z; // NEU->NED
355378

356379
_target_velocity_ned.x = packet.vx * 0.01f; // velocity north
357380
_target_velocity_ned.y = packet.vy * 0.01f; // velocity east
@@ -386,18 +409,15 @@ bool AP_Follow::handle_follow_target_message(const mavlink_message_t &msg)
386409
return false;
387410
}
388411

389-
Location new_loc = _target_location;
412+
Location new_loc;
390413
new_loc.lat = packet.lat;
391414
new_loc.lng = packet.lon;
392415
new_loc.set_alt_m(packet.alt, Location::AltFrame::ABSOLUTE);
393416

394-
// FOLLOW_TARGET is always AMSL, change the provided alt to
395-
// above home if we are configured for relative alt
396-
if (_alt_type == AP_FOLLOW_ALTITUDE_TYPE_RELATIVE &&
397-
!new_loc.change_alt_frame(Location::AltFrame::ABOVE_HOME)) {
417+
if (!new_loc.get_vector_from_origin_NEU(_target_position_ned)) {
398418
return false;
399419
}
400-
_target_location = new_loc;
420+
_target_position_ned.z = -_target_position_ned.z; // NEU->NED
401421

402422
if (packet.est_capabilities & (1<<1)) {
403423
_target_velocity_ned.x = packet.vel[0]; // velocity north
@@ -436,6 +456,12 @@ void AP_Follow::Log_Write_FOLL()
436456
Vector3f vel_estimate;
437457
UNUSED_RESULT(get_target_location_and_velocity(loc_estimate, vel_estimate));
438458

459+
Location _target_location;
460+
UNUSED_RESULT(AP::ahrs().get_location_from_origin_offset_NED(_target_location, _target_position_ned * 0.01));
461+
if (_alt_type == AP_FOLLOW_ALTITUDE_TYPE_RELATIVE) {
462+
_target_location.change_alt_frame(Location::AltFrame::ABOVE_HOME);
463+
}
464+
439465
// log lead's estimated vs reported position
440466
// @LoggerMessage: FOLL
441467
// @Description: Follow library diagnostic data
@@ -537,17 +563,40 @@ void AP_Follow::clear_dist_and_bearing_to_target()
537563
_bearing_to_target = 0.0f;
538564
}
539565

566+
// get target's estimated origin-relative-position and velocity (both
567+
// in SI NED), with offsets added
568+
bool AP_Follow::get_target_position_and_velocity_ofs(Vector3p &pos_ned, Vector3f &vel_ned) const
569+
{
570+
Vector3f ofs_ned;
571+
if (!get_offsets_ned(ofs_ned)) {
572+
return false;
573+
}
574+
if (!get_target_position_and_velocity_ofs(pos_ned, vel_ned)) {
575+
return false;
576+
}
577+
// can't add Vector3p and Vector3f directly:
578+
pos_ned.x += ofs_ned.x;
579+
pos_ned.y += ofs_ned.y;
580+
pos_ned.z += ofs_ned.z;
581+
return true;
582+
}
583+
540584
// get target's estimated location and velocity (in NED), with offsets added
541585
bool AP_Follow::get_target_location_and_velocity_ofs(Location &loc, Vector3f &vel_ned) const
542586
{
543-
Vector3f ofs;
544-
if (!get_offsets_ned(ofs) ||
545-
!get_target_location_and_velocity(loc, vel_ned)) {
587+
Vector3p pos_ned;
588+
Vector3f local_vel_ned; // so we either change both return parameters or neither
589+
if (!get_target_position_and_velocity_ofs(pos_ned, local_vel_ned)) {
546590
return false;
547591
}
548-
// apply offsets
549-
loc.offset(ofs.x, ofs.y);
550-
loc.alt -= ofs.z*100;
592+
if (!AP::ahrs().get_location_from_origin_offset_NED(loc, pos_ned * 0.01)) {
593+
return false;
594+
}
595+
if (_alt_type == AP_FOLLOW_ALTITUDE_TYPE_RELATIVE) {
596+
loc.change_alt_frame(Location::AltFrame::ABOVE_HOME);
597+
}
598+
599+
vel_ned = local_vel_ned;
551600
return true;
552601
}
553602

Diff for: libraries/AP_Follow/AP_Follow.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,20 @@ class AP_Follow
6868
// true if we have a valid target location estimate
6969
bool have_target() const;
7070

71-
// get target's estimated location and velocity (in NED)
71+
// get target's estimated position (NED-from-origin) and velocity (in NED)
72+
// from position-from-origin. metres and metres/second
73+
bool get_target_position_and_velocity(Vector3p &pos_ned, Vector3f &vel_ned) const;
74+
75+
// get target's estimated position (NED-from-origin) and velocity (in NED)
76+
// from position-from-origin with offsets added. metres and metres/second
77+
bool get_target_position_and_velocity_ofs(Vector3p &pos_ned, Vector3f &vel_ned) const;
78+
79+
// get target's estimated location and velocity (in NED). Derived
80+
// from position-from-origin.
7281
bool get_target_location_and_velocity(Location &loc, Vector3f &vel_ned) const;
7382

74-
// get target's estimated location and velocity (in NED), with offsets added
83+
// get target's estimated location and velocity (in NED), with
84+
// offsets added. Derived from position-from-origin.
7585
bool get_target_location_and_velocity_ofs(Location &loc, Vector3f &vel_ned) const;
7686

7787
// get distance vector to target (in meters), target plus offsets, and target's velocity all in NED frame
@@ -156,7 +166,7 @@ class AP_Follow
156166

157167
// local variables
158168
uint32_t _last_location_update_ms; // system time of last position update
159-
Location _target_location; // last known location of target
169+
Vector3p _target_position_ned; // last known position of target
160170
Vector3f _target_velocity_ned; // last known velocity of target in NED frame in m/s
161171
Vector3f _target_accel_ned; // last known acceleration of target in NED frame in m/s/s
162172
uint32_t _last_heading_update_ms; // system time of last heading update

0 commit comments

Comments
 (0)