@@ -167,6 +167,26 @@ void AP_Follow::clear_offsets_if_required()
167
167
168
168
// get target's estimated location
169
169
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
170
190
{
171
191
// exit immediately if not enabled
172
192
if (!_enabled) {
@@ -187,16 +207,14 @@ bool AP_Follow::get_target_location_and_velocity(Location &loc, Vector3f &vel_ne
187
207
}
188
208
189
209
// 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;
193
212
194
- // return latest position estimate
195
- loc = last_loc;
196
213
return true ;
197
214
}
198
215
199
216
// get distance vector to target (in meters) and target's velocity all in NED frame
217
+ // FIXME: use the target_position_ned vector!
200
218
bool AP_Follow::get_target_dist_and_vel_ned (Vector3f &dist_ned, Vector3f &dist_with_offs, Vector3f &vel_ned)
201
219
{
202
220
// get our location
@@ -341,6 +359,7 @@ bool AP_Follow::handle_global_position_int_message(const mavlink_message_t &msg)
341
359
return false ;
342
360
}
343
361
362
+ Location _target_location;
344
363
_target_location.lat = packet.lat ;
345
364
_target_location.lng = packet.lon ;
346
365
@@ -352,6 +371,10 @@ bool AP_Follow::handle_global_position_int_message(const mavlink_message_t &msg)
352
371
// absolute altitude
353
372
_target_location.set_alt_cm (packet.alt / 10 , Location::AltFrame::ABSOLUTE);
354
373
}
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
355
378
356
379
_target_velocity_ned.x = packet.vx * 0 .01f ; // velocity north
357
380
_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)
386
409
return false ;
387
410
}
388
411
389
- Location new_loc = _target_location ;
412
+ Location new_loc;
390
413
new_loc.lat = packet.lat ;
391
414
new_loc.lng = packet.lon ;
392
415
new_loc.set_alt_m (packet.alt , Location::AltFrame::ABSOLUTE);
393
416
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)) {
398
418
return false ;
399
419
}
400
- _target_location = new_loc;
420
+ _target_position_ned. z = -_target_position_ned. z ; // NEU->NED
401
421
402
422
if (packet.est_capabilities & (1 <<1 )) {
403
423
_target_velocity_ned.x = packet.vel [0 ]; // velocity north
@@ -436,6 +456,12 @@ void AP_Follow::Log_Write_FOLL()
436
456
Vector3f vel_estimate;
437
457
UNUSED_RESULT (get_target_location_and_velocity (loc_estimate, vel_estimate));
438
458
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
+
439
465
// log lead's estimated vs reported position
440
466
// @LoggerMessage: FOLL
441
467
// @Description: Follow library diagnostic data
@@ -537,17 +563,40 @@ void AP_Follow::clear_dist_and_bearing_to_target()
537
563
_bearing_to_target = 0 .0f ;
538
564
}
539
565
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
+
540
584
// get target's estimated location and velocity (in NED), with offsets added
541
585
bool AP_Follow::get_target_location_and_velocity_ofs (Location &loc, Vector3f &vel_ned) const
542
586
{
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 )) {
546
590
return false ;
547
591
}
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;
551
600
return true ;
552
601
}
553
602
0 commit comments