@@ -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
470474bool 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
487490void MultirotorApiBase::setAngleLevelControllerGains (const vector<float >& kp, const vector<float >& ki, const vector<float >& kd)
0 commit comments