5 #include <gtest/gtest.h> 10 #if ROS_VERSION_MINIMUM(1,14,0) 38 const std::string& baseLinkTransformName,
41 #if ROS_VERSION_MINIMUM(1,14,0) 43 tfListener_(tfBuffer_)
54 privateNodeHandle.
setParam(name +
"/robot_base_frame", baseLinkTransformName);
55 privateNodeHandle.
setParam(name +
"/origin_x", origin.x());
56 privateNodeHandle.
setParam(name +
"/origin_y", origin.y());
57 privateNodeHandle.
setParam(name +
"/width", width);
58 privateNodeHandle.
setParam(name +
"/height", height);
59 privateNodeHandle.
setParam(name +
"/plugins", std::vector<std::string>());
60 privateNodeHandle.
setParam(name +
"/resolution", 0.5);
61 privateNodeHandle.
setParam(name +
"/robot_radius", 0.03);
62 #if ROS_VERSION_MINIMUM(1,14,0) 63 costmap_ = std::make_shared<ROSCostmap>(name, tfBuffer_);
65 costmap_ = std::make_shared<ROSCostmap>(name, tfListener_);
68 for (
unsigned int index = 0; index < costmap_->getCostmap()->getSizeInCellsY(); ++index ) {
69 unsigned int dimension = costmap_->getCostmap()->getSizeInCellsX();
72 for (
unsigned int fill_index = 0; fill_index < dimension - 2; ++fill_index )
74 double fraction =
static_cast<double>(fill_index + 1) / static_cast<double>(costmap_->getCostmap()->getSizeInCellsX());
102 transforms_.insert(std::pair<std::string, tf::Transform>(name, transform));
111 #if ROS_VERSION_MINIMUM(1,14,0) 117 for (std::pair<std::string, tf::Transform> p :
transforms_) {
119 #if ROS_VERSION_MINIMUM(1,14,0) 120 geometry_msgs::TransformStamped transformMsg;
139 boost::lock_guard<costmap_2d::Costmap2D::mutex_t> lock(*(ros_costmap.
getCostmap()->
getMutex()));
152 boost::lock_guard<costmap_2d::Costmap2D::mutex_t> lock(*(ros_costmap.
getCostmap()->
getMutex()));
166 TEST(Costmap2DROSConversion, full_window)
168 std::cout << std::endl;
169 ROS_INFO(
"***********************************************************");
171 ROS_INFO(
"***********************************************************");
173 std::string layer_name =
"obstacle_costs";
181 ros_costmap_5x5.
getROSCostmap()->getCostmap()->getSizeInCellsX()
182 * ros_costmap_5x5.
getROSCostmap()->getCostmap()->getResolution());
185 ros_costmap_5x5.
getROSCostmap()->getCostmap()->getSizeInCellsY()
186 * ros_costmap_5x5.
getROSCostmap()->getCostmap()->getResolution());
187 ASSERT_EQ(grid_map_5x5.
getSize()[0], ros_costmap_5x5.
getROSCostmap()->getCostmap()->getSizeInCellsX());
188 ASSERT_EQ(grid_map_5x5.
getSize()[1], ros_costmap_5x5.
getROSCostmap()->getCostmap()->getSizeInCellsY());
190 ASSERT_EQ(position.x(), ros_costmap_5x5.
getROSCostmap()->getCostmap()->getOriginX());
191 ASSERT_EQ(position.y(), ros_costmap_5x5.
getROSCostmap()->getCostmap()->getOriginY());
194 for (
unsigned int i = 0; i < 5; ++i) {
195 for (
unsigned int j = 0; j < 5; ++j) {
196 std::cout << static_cast<int>(ros_costmap_5x5.
getROSCostmap()->getCostmap()->getCost(i, j))
199 std::cout << std::endl;
201 for (
unsigned int i = 0; i < 5; ++i) {
202 for (
unsigned int j = 0; j < 5; ++j) {
203 std::cout << static_cast<int>(grid_map_5x5.
at(layer_name,
grid_map::Index(i, j))) <<
" ";
205 std::cout << std::endl;
209 std::cout <<
"Original cost: " <<
static_cast<int>(ros_costmap_5x5.
getROSCostmap()->getCostmap()->getCost(8,0)) << std::endl;
210 std::cout <<
"New Cost: " << grid_map_5x5.
at(layer_name,
grid_map::Index(1,9)) << std::endl;
211 std::vector<float> cost_translation_table;
213 unsigned char cost = ros_costmap_5x5.
getROSCostmap()->getCostmap()->getCost(8,0);
214 ASSERT_EQ(grid_map_5x5.
at(layer_name,
grid_map::Index(1,9)), cost_translation_table[cost]);
215 std::cout << std::endl;
218 TEST(Costmap2DROSConversion, cost_map_centres)
220 std::cout << std::endl;
221 ROS_INFO(
"***********************************************************");
222 ROS_INFO(
" Check Subwindow Centres");
223 ROS_INFO(
"***********************************************************");
224 ROS_INFO(
"Subwindows are centred as closely as possible to the robot");
225 ROS_INFO(
"pose, though not exactly. They still need to align with");
226 ROS_INFO(
"the underlying ros costmap so that they don't introduce a");
227 ROS_INFO(
"new kind of error. As a result, the centre is shifted from");
228 ROS_INFO(
"the robot pose to the nearest appropriate point which aligns");
229 ROS_INFO(
"the new cost map exactly on top of the original ros costmap.");
230 std::cout << std::endl;
231 std::string layer_name =
"obstacle_costs";
235 grid_map::GridMap grid_map_5x5_3x3_offset, grid_map_5x5_3x3_centre, grid_map_5x5_2_5x2_5_offset;
244 ASSERT_EQ(-3.5, grid_map_5x5_3x3_offset.
getPosition().x());
245 ASSERT_EQ(2.5, grid_map_5x5_3x3_offset.
getPosition().y());
246 ASSERT_EQ(-3.5, grid_map_5x5_3x3_centre.
getPosition().x());
247 ASSERT_EQ(-3.5, grid_map_5x5_3x3_centre.
getPosition().y());
248 ASSERT_EQ(-9.75, grid_map_5x5_2_5x2_5_offset.
getPosition().x());
249 ASSERT_EQ(2.25, grid_map_5x5_2_5x2_5_offset.
getPosition().y());
250 std::cout << std::endl;
257 int main(
int argc,
char **argv)
259 ros::init(argc, argv,
"test_from_ros_costmaps");
264 testing::InitGoogleTest(&argc, argv);
265 int result = RUN_ALL_TESTS();
const Length & getLength() const
Convert Costmap2DRos objects into cost or grid maps.
bool fromCostmap2DROS(costmap_2d::Costmap2DROS &ros_costmap, const std::string &layer_name, grid_map::GridMap &grid_map)
bool getPosition(const Index &index, Position &position) const
void initializeFromCostmap2D(costmap_2d::Costmap2DROS &costmap2d, MapType &outputMap)
const std::string & getFrameId() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool fromCostmap2DROSAtRobotPose(costmap_2d::Costmap2DROS &ros_costmap, const grid_map::Length &geometry, const std::string &layer_name, grid_map::GridMap &grid_map)
bool addLayerFromCostmap2DAtRobotPose(costmap_2d::Costmap2DROS &costmap2d, const std::string &layer, MapType &outputMap)
void broadcastCostmap2DROSTestSuiteTransforms(TransformBroadcaster &broadcaster)
ROSCostmapServer(const std::string &name, const std::string &baseLinkTransformName, const grid_map::Position &origin, const double &width, const double &height)
ROSCostmapPtr getROSCostmap()
static const unsigned char INSCRIBED_INFLATED_OBSTACLE
bool initializeFromCostmap2DAtRobotPose(costmap_2d::Costmap2DROS &costmap2d, const Length &length, MapType &outputMap)
float & at(const std::string &layer, const Index &index)
static void create(std::vector< DataType > &costTranslationTable)
bool addLayerFromCostmap2D(const costmap_2d::Costmap2D &costmap2d, const std::string &layer, MapType &outputMap)
#define ROS_INFO_STREAM(args)
static const unsigned char LETHAL_OBSTACLE
static const unsigned char NO_INFORMATION
TEST(Costmap2DROSConversion, full_window)
static void transformStampedTFToMsg(const StampedTransform &bt, geometry_msgs::TransformStamped &msg)
int main(int argc, char **argv)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
const Size & getSize() const