Skip to content

Commit e4d9a36

Browse files
authored
Fix reverse drive for slotcar (#150)
* Fix reverse drive for slotcar Signed-off-by: Michael X. Grey <greyxmike@gmail.com> * Fix logic for choosing between driving toward target vs facing requested direction Signed-off-by: Michael X. Grey <greyxmike@gmail.com> --------- Signed-off-by: Michael X. Grey <greyxmike@gmail.com>
1 parent ea8756c commit e4d9a36

1 file changed

Lines changed: 24 additions & 29 deletions

File tree

rmf_robot_sim_common/src/slotcar_common.cpp

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -539,28 +539,14 @@ SlotcarCommon::UpdateResult SlotcarCommon::update_diff_drive(
539539

540540
if (rotate_towards_next_target)
541541
{
542-
if (_traj_wp_idx+1 < trajectory.size())
543-
{
544-
const auto dpos_next =
545-
compute_dpos(trajectory.at(_traj_wp_idx+1).pose, _pose);
546-
547-
const auto goal_heading =
548-
compute_heading(trajectory.at(_traj_wp_idx+1).pose);
542+
const auto goal_heading =
543+
compute_heading(trajectory.at(_traj_wp_idx).pose);
549544

550-
double dir = 1.0;
551-
result.w = compute_change_in_rotation(
552-
current_heading, dpos_next, &goal_heading, &dir);
545+
result.w = compute_change_in_rotation(
546+
current_heading,
547+
Eigen::Vector3d::Zero(),
548+
&goal_heading);
553549

554-
if (dir < 0.0)
555-
current_heading *= -1.0;
556-
}
557-
else
558-
{
559-
const auto goal_heading =
560-
compute_heading(trajectory.at(_traj_wp_idx).pose);
561-
result.w = compute_change_in_rotation(
562-
current_heading, goal_heading);
563-
}
564550
result.target_linear_speed_now = 0.0;
565551
_current_mode.mode = RobotMode::MODE_PAUSED;
566552
}
@@ -588,9 +574,8 @@ SlotcarCommon::UpdateResult SlotcarCommon::update_diff_drive(
588574
if (!rotate_towards_next_target && _traj_wp_idx < trajectory.size())
589575
{
590576
const double d_yaw_tolerance = 5.0 * M_PI / 180.0;
591-
auto goal_heading = compute_heading(trajectory.at(_traj_wp_idx).pose);
592577
double dir = 1.0;
593-
result.w = compute_change_in_rotation(current_heading, dpos);
578+
result.w = compute_change_in_rotation(current_heading, dpos, nullptr, &dir);
594579

595580
// If d_yaw is less than a certain tolerance (i.e. we don't need to spin
596581
// too much), then we'll include the forward velocity. Otherwise, we will
@@ -609,7 +594,8 @@ SlotcarCommon::UpdateResult SlotcarCommon::update_diff_drive(
609594
const auto goal_heading = compute_heading(trajectory.back().pose);
610595
result.w = compute_change_in_rotation(
611596
current_heading,
612-
goal_heading);
597+
Eigen::Vector3d::Zero(),
598+
&goal_heading);
613599

614600
result.v = 0.0;
615601
}
@@ -887,7 +873,7 @@ std::string SlotcarCommon::get_level_name(const double z)
887873
double SlotcarCommon::compute_change_in_rotation(
888874
const Eigen::Vector3d& heading_vec,
889875
const Eigen::Vector3d& dpos,
890-
const Eigen::Vector3d* traj_vec,
876+
const Eigen::Vector3d* requested_heading,
891877
double* const dir) const
892878
{
893879
if (dpos.norm() < 1e-3)
@@ -898,12 +884,21 @@ double SlotcarCommon::compute_change_in_rotation(
898884
}
899885

900886
Eigen::Vector3d target = dpos;
901-
// If a traj_vec is provided and slotcar is reversible, of the two possible
902-
// headings (dpos/-dpos), choose the one closest to traj_vec
903-
if (traj_vec && _reversible)
887+
if (requested_heading)
888+
{
889+
// If requested_heading was provided, use that instead of dpos because requested_heading means
890+
// we are already close enough to the target
891+
target = *requested_heading;
892+
}
893+
else if (_reversible)
904894
{
905-
const double dot = traj_vec->dot(dpos);
906-
target = dot < 0 ? -dpos : dpos;
895+
// If no requested_heading is given then the robot needs to turn to face the
896+
// direction that it needs to move towards, i.e. dpos. If the robot is
897+
// reversible, then this dot product tells us if it should actually move
898+
// towards the goal in reverse.
899+
const auto dot = heading_vec.dot(target);
900+
901+
target = dot < 0 ? -target : target;
907902
// dir is negative if slotcar will need to reverse to go towards target
908903
if (dir)
909904
{

0 commit comments

Comments
 (0)