first commit
This commit is contained in:
118
include/sbpl_recovery/merge_grids.h
Normal file
118
include/sbpl_recovery/merge_grids.h
Normal file
@@ -0,0 +1,118 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* sbpl_recovery — gộp lưới costmap: global làm nền, cửa sổ local đè lên.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#ifndef SBPL_RECOVERY_MERGE_GRIDS_H_
|
||||
#define SBPL_RECOVERY_MERGE_GRIDS_H_
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <robot_costmap_2d/cost_values.h>
|
||||
#include <robot_costmap_2d/costmap_2d.h>
|
||||
|
||||
namespace sbpl_recovery
|
||||
{
|
||||
|
||||
/**
|
||||
* @struct PlanarTransform2D
|
||||
* @brief Phép biến đổi cứng 2D (frame local -> frame lưới đích), tra TF MỘT lần rồi áp hàng chục
|
||||
* nghìn cell — không tra TF trong vòng lặp.
|
||||
*/
|
||||
struct PlanarTransform2D
|
||||
{
|
||||
double x = 0.0; ///< [m] tịnh tiến
|
||||
double y = 0.0; ///< [m]
|
||||
double cos_yaw = 1.0;
|
||||
double sin_yaw = 0.0;
|
||||
|
||||
void apply(double in_x, double in_y, double& out_x, double& out_y) const
|
||||
{
|
||||
out_x = x + cos_yaw * in_x - sin_yaw * in_y;
|
||||
out_y = y + sin_yaw * in_x + cos_yaw * in_y;
|
||||
}
|
||||
|
||||
/// @brief Chiều ngược của @ref apply (frame đích -> frame nguồn). Phép quay là trực giao nên
|
||||
/// nghịch đảo chỉ là chuyển vị — không cần dựng transform thứ hai.
|
||||
void applyInverse(double in_x, double in_y, double& out_x, double& out_y) const
|
||||
{
|
||||
const double dx = in_x - x;
|
||||
const double dy = in_y - y;
|
||||
out_x = cos_yaw * dx + sin_yaw * dy;
|
||||
out_y = -sin_yaw * dx + cos_yaw * dy;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Cost một cell sau khi gộp hai nguồn: một bên báo chắn là chắn.
|
||||
*
|
||||
* Quy tắc (chốt 2026-08-05, xem DETOUR_MERGED_GRID_PLAN.md):
|
||||
* - local `NO_INFORMATION` -> giữ giá trị global (local chưa quét thì tin mắt rộng);
|
||||
* - global `NO_INFORMATION` nhưng local ĐÃ biết -> lấy local (255 là giá trị lớn nhất, `max` mù
|
||||
* sẽ biến "chưa biết" thành "chắn");
|
||||
* - cả hai đều biết -> `max`: local FREE (60) KHÔNG xoá tường global — global của hệ này chỉ chứa
|
||||
* tĩnh, tường không tự đi.
|
||||
*/
|
||||
inline unsigned char mergeCellCost(unsigned char global_cost, unsigned char local_cost)
|
||||
{
|
||||
if (local_cost == robot_costmap_2d::NO_INFORMATION)
|
||||
{
|
||||
return global_cost;
|
||||
}
|
||||
if (global_cost == robot_costmap_2d::NO_INFORMATION)
|
||||
{
|
||||
return local_cost;
|
||||
}
|
||||
return std::max(global_cost, local_cost);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Đè cửa sổ @p local lên @p out (đã là bản copy của lưới global, CÙNG frame với đích).
|
||||
*
|
||||
* Duyệt theo cell của local (nhỏ hơn nhiều lần lưới đích); tâm cell local được quy sang frame đích
|
||||
* bằng @p local_to_out rồi ghi theo @ref mergeCellCost. Cell local rơi ngoài lưới đích bị bỏ qua.
|
||||
* Cả hai lưới đều là BẢN CHỤP của bên gọi — hàm này không đụng costmap sống, không cần mutex.
|
||||
*/
|
||||
inline void overlayLocalGrid(const robot_costmap_2d::Costmap2D& local,
|
||||
const PlanarTransform2D& local_to_out,
|
||||
robot_costmap_2d::Costmap2D& out)
|
||||
{
|
||||
const unsigned int size_x = local.getSizeInCellsX();
|
||||
const unsigned int size_y = local.getSizeInCellsY();
|
||||
|
||||
for (unsigned int ly = 0; ly < size_y; ++ly)
|
||||
{
|
||||
for (unsigned int lx = 0; lx < size_x; ++lx)
|
||||
{
|
||||
const unsigned char local_cost = local.getCost(lx, ly);
|
||||
if (local_cost == robot_costmap_2d::NO_INFORMATION)
|
||||
{
|
||||
continue; // Không đóng góp gì — giữ nền global, khỏi tính transform.
|
||||
}
|
||||
|
||||
double wx = 0.0;
|
||||
double wy = 0.0;
|
||||
local.mapToWorld(lx, ly, wx, wy);
|
||||
|
||||
double ox = 0.0;
|
||||
double oy = 0.0;
|
||||
local_to_out.apply(wx, wy, ox, oy);
|
||||
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
if (!out.worldToMap(ox, oy, mx, my))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
out.setCost(mx, my, mergeCellCost(out.getCost(mx, my), local_cost));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sbpl_recovery
|
||||
|
||||
#endif // SBPL_RECOVERY_MERGE_GRIDS_H_
|
||||
Reference in New Issue
Block a user