Skip to content

Commit 356d58b

Browse files
authored
Merge pull request #2516 from songxiaocheng/PR/rotate_to_yaw
Fix bug: rotateToYaw not working
2 parents 1f1188a + 4115e0a commit 356d58b

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

AirLib/include/vehicles/multirotor/api/MultirotorApiBase.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ class MultirotorApiBase : public VehicleApiBase {
339339
//TODO: make this configurable?
340340
float landing_vel_ = 0.2f; //velocity to use for landing
341341
float approx_zero_vel_ = 0.05f;
342+
float approx_zero_angular_vel_ = 0.01f;
342343
};
343344

344345
}} //namespace

AirLib/src/vehicles/multirotor/api/MultirotorApiBase.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -446,25 +446,29 @@ bool MultirotorApiBase::rotateToYaw(float yaw, float timeout_sec, float margin)
446446
{
447447
SingleTaskCall lock(this);
448448

449-
const YawMode yaw_mode(false, VectorMath::normalizeAngle(yaw));
450-
Waiter waiter(getCommandPeriod(), timeout_sec, getCancelToken());
451-
452-
float estimated_pitch, estimated_roll, estimated_yaw;
449+
if (timeout_sec <= 0)
450+
return true;
453451

454452
auto start_pos = getPosition();
455-
do {
456-
auto kinematics = getKinematicsEstimated();
457-
VectorMath::toEulerianAngle(kinematics.pose.orientation,
458-
estimated_pitch, estimated_roll, estimated_yaw);
459-
460-
if (isYawWithinMargin(estimated_yaw, margin))
461-
return true;
453+
float yaw_target = VectorMath::normalizeAngle(yaw);
454+
YawMode move_yaw_mode(false, yaw_target);
455+
YawMode stop_yaw_mode(true, 0);
462456

463-
//change yaw by moving to same position but constant yaw mode
464-
moveToPositionInternal(start_pos, yaw_mode);
465-
} while (waiter.sleep());
457+
return waitForFunction([&]() {
458+
if (isYawWithinMargin(yaw_target, margin)) { // yaw is within margin, then trying to stop rotation
459+
moveToPositionInternal(start_pos, stop_yaw_mode); // let yaw rate be zero
460+
auto yaw_rate = getKinematicsEstimated().twist.angular.z();
461+
if (abs(yaw_rate) <= approx_zero_angular_vel_) { // already sopped
462+
return true; //stop all for stably achieving the goal
463+
}
464+
}
465+
else { // yaw is not within margin, go on rotation
466+
moveToPositionInternal(start_pos, move_yaw_mode);
467+
}
466468

467-
return false; //we are not exiting because we reached yaw
469+
// yaw is not within margin
470+
return false; //keep moving until timeout
471+
}, timeout_sec).isComplete();
468472
}
469473

470474
bool MultirotorApiBase::rotateByYawRate(float yaw_rate, float duration)
@@ -476,12 +480,11 @@ bool MultirotorApiBase::rotateByYawRate(float yaw_rate, float duration)
476480

477481
auto start_pos = getPosition();
478482
YawMode yaw_mode(true, yaw_rate);
479-
Waiter waiter(getCommandPeriod(), duration, getCancelToken());
480-
do {
483+
484+
return waitForFunction([&]() {
481485
moveToPositionInternal(start_pos, yaw_mode);
482-
} while (waiter.sleep());
483-
484-
return waiter.isTimeout();
486+
return false; //keep moving until timeout
487+
}, duration).isTimeout();
485488
}
486489

487490
void MultirotorApiBase::setAngleLevelControllerGains(const vector<float>& kp, const vector<float>& ki, const vector<float>& kd)

0 commit comments

Comments
 (0)