Skip to content

Commit 7b6a842

Browse files
committed
fix configuration and test bugs
Signed-off-by: lotusymt <[email protected]>
1 parent 50148b7 commit 7b6a842

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

nav2_collision_monitor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ add_library(${detector_library_name} SHARED
7272
src/scan.cpp
7373
src/pointcloud.cpp
7474
src/polygon_source.cpp
75+
src/costmap.cpp
7576
src/range.cpp
7677
src/kinematics.cpp
7778
)

nav2_collision_monitor/include/nav2_collision_monitor/costmap.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ class CostmapSource : public Source
103103
*/
104104
void getParameters(std::string & source_topic);
105105

106+
/**
107+
* @brief Check if costmap data has been received.
108+
* @return true if data has been received, false otherwise.
109+
*/
110+
bool hasData() const {return data_ != nullptr;}
111+
106112
private:
107113
/**
108114
* @brief Subscription callback to store the latest costmap message.

nav2_collision_monitor/launch/collision_monitor_node.launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Unless required by applicable law or agreed to in writing, software
1212
# distributed under the License is distributed on an "AS IS" BASIS,
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific languazge governing permissions and
14+
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

1717
import os

nav2_collision_monitor/test/collision_detector_node_test.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -508,44 +508,37 @@ void Tester::publishCostmap(const double dist, const rclcpp::Time & stamp)
508508
std::unique_ptr<nav2_msgs::msg::Costmap> msg =
509509
std::make_unique<nav2_msgs::msg::Costmap>();
510510

511-
msg->header.frame_id = SOURCE_FRAME_ID;
511+
// Costmap in odom frame (more typical for Nav2 costmaps)
512+
msg->header.frame_id = ODOM_FRAME_ID;
512513
msg->header.stamp = stamp;
513514

514-
// Set up metadata for a 20x20 costmap with 0.1m resolution
515+
// Metadata: 20x20 grid, 0.1 m resolution
515516
msg->metadata.map_load_time = stamp;
516517
msg->metadata.update_time = stamp;
517518
msg->metadata.layer = "test_layer";
518-
msg->metadata.resolution = 0.1; // 10cm resolution
519+
msg->metadata.resolution = 0.1;
519520
msg->metadata.size_x = 20;
520521
msg->metadata.size_y = 20;
521-
// Place origin so that obstacle at distance 'dist' is in the costmap
522-
msg->metadata.origin.position.x = -1.0;
523-
msg->metadata.origin.position.y = dist - 1.0;
522+
523+
// Choose origin so that cell (0,0) center is exactly at (0, dist)
524+
msg->metadata.origin.position.x = -0.05; // 0.05 = 0.5 * resolution
525+
msg->metadata.origin.position.y = dist - 0.05; // dist - 0.5 * resolution
524526
msg->metadata.origin.position.z = 0.0;
525527
msg->metadata.origin.orientation.w = 1.0;
526528

527-
// Create a costmap with an obstacle at distance 'dist'
528-
msg->data.resize(400, 0); // 20x20 = 400 cells
529+
// Initialize all cells as free
530+
msg->data.assign(msg->metadata.size_x * msg->metadata.size_y, 0);
529531

530-
// Calculate cell index for obstacle at (0, dist) in costmap coordinates
531-
// Costmap origin is at (-1.0, dist-1.0), so (0, dist) is at cell (10, 10)
532-
const int obstacle_x = 10;
533-
const int obstacle_y = 10;
532+
// Put one lethal obstacle at cell (0,0)
533+
const int obstacle_x = 0;
534+
const int obstacle_y = 0;
534535
const int obstacle_idx = obstacle_y * msg->metadata.size_x + obstacle_x;
535-
536-
// Set obstacle cells to lethal cost
537536
msg->data[obstacle_idx] = nav2_costmap_2d::LETHAL_OBSTACLE;
538-
// Set surrounding cells as well for better detection
539-
if (obstacle_idx + 1 < 400) {
540-
msg->data[obstacle_idx + 1] = nav2_costmap_2d::LETHAL_OBSTACLE;
541-
}
542-
if (obstacle_idx + msg->metadata.size_x < 400) {
543-
msg->data[obstacle_idx + msg->metadata.size_x] = nav2_costmap_2d::LETHAL_OBSTACLE;
544-
}
545537

546538
costmap_pub_->publish(std::move(msg));
547539
}
548540

541+
549542
bool Tester::waitData(
550543
const double expected_dist,
551544
const std::chrono::nanoseconds & timeout,

nav2_collision_monitor/test/sources_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ class CostmapWrapper : public nav2_collision_monitor::CostmapSource
412412

413413
bool dataReceived() const
414414
{
415-
return data_ != nullptr;
415+
return hasData();
416416
}
417417
}; // CostmapWrapper
418418

0 commit comments

Comments
 (0)