/********************************************************************* * * Software License Agreement (BSD License) * * move_base2 — cài đặt plan_inspector. * * Author: DuongTD *********************************************************************/ #include #include #include #include #include #include #include #include namespace move_base2 { namespace { /// [m] Dưới ngưỡng này thì một đoạn plan coi như suy biến thành điểm. constexpr double kDegenerateSegment = 1e-9; /// @brief [m] Khoảng cách từ điểm (px,py) tới đoạn thẳng (ax,ay)-(bx,by). double pointToSegment(double px, double py, double ax, double ay, double bx, double by) { const double dx = bx - ax; const double dy = by - ay; const double len_sq = dx * dx + dy * dy; if (len_sq <= kDegenerateSegment) { return std::hypot(px - ax, py - ay); } double t = ((px - ax) * dx + (py - ay) * dy) / len_sq; t = std::max(0.0, std::min(1.0, t)); return std::hypot(px - (ax + t * dx), py - (ay + t * dy)); } } // namespace bool planarTransform(const tf3::BufferCore& tf, const std::string& from, const std::string& to, PlanarTransform& out) { out = PlanarTransform(); if (from.empty() || to.empty()) { return false; } if (from == to) { return true; // Identity — không cần chạm tới TF. } try { const tf3::TransformStampedMsg tf_msg = const_cast(tf).lookupTransform(to, from, tf3::Time()); const auto& q = tf_msg.transform.rotation; const double yaw = std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z)); // [rad] out.x = tf_msg.transform.translation.x; out.y = tf_msg.transform.translation.y; out.cos_yaw = std::cos(yaw); out.sin_yaw = std::sin(yaw); return true; } catch (const std::exception&) { // Không log ở đây: hàm này chạy theo nhịp monitor. Bên gọi quyết định có báo hay không. return false; } } std::size_t trackedNearestIndex(const std::vector& plan, const robot_geometry_msgs::PoseStamped& pose, std::size_t hint, double back_m, double ahead_m) { if (plan.size() < 2) { return 0; } const std::size_t last_seg = plan.size() - 2; hint = std::min(hint, last_seg); auto seg_len = [&](std::size_t i) { return std::hypot(plan[i + 1].pose.position.x - plan[i].pose.position.x, plan[i + 1].pose.position.y - plan[i].pose.position.y); }; std::size_t lo = hint; for (double walked = 0.0; lo > 0 && walked < back_m; --lo) { walked += seg_len(lo - 1); } std::size_t hi = hint; for (double walked = 0.0; hi < last_seg && walked < ahead_m; ++hi) { walked += seg_len(hi); } const double px = pose.pose.position.x; const double py = pose.pose.position.y; std::size_t best = hint; double best_distance = std::numeric_limits::max(); for (std::size_t i = lo; i <= hi; ++i) { const double distance = pointToSegment(px, py, plan[i].pose.position.x, plan[i].pose.position.y, plan[i + 1].pose.position.x, plan[i + 1].pose.position.y); if (distance < best_distance) { best_distance = distance; best = i; } } return best; } std::size_t nearestIndex(const std::vector& plan, const robot_geometry_msgs::PoseStamped& pose) { if (plan.size() < 2) { return 0; } const double px = pose.pose.position.x; const double py = pose.pose.position.y; std::size_t best = 0; double best_distance = std::numeric_limits::max(); for (std::size_t i = 0; i + 1 < plan.size(); ++i) { const double distance = pointToSegment(px, py, plan[i].pose.position.x, plan[i].pose.position.y, plan[i + 1].pose.position.x, plan[i + 1].pose.position.y); if (distance < best_distance) { best_distance = distance; best = i; } } return best; } double crossTrackError(const std::vector& plan, const robot_geometry_msgs::PoseStamped& pose) { if (plan.empty()) { return 0.0; } const double px = pose.pose.position.x; const double py = pose.pose.position.y; if (plan.size() == 1) { return std::hypot(px - plan.front().pose.position.x, py - plan.front().pose.position.y); } double best = std::numeric_limits::max(); for (std::size_t i = 0; i + 1 < plan.size(); ++i) { best = std::min(best, pointToSegment(px, py, plan[i].pose.position.x, plan[i].pose.position.y, plan[i + 1].pose.position.x, plan[i + 1].pose.position.y)); } return best; } } // namespace move_base2