Skip to content

Commit a3d471f

Browse files
committed
Lintering and style
Signed-off-by: Francisco Martín Rico <fmrico@gmail.com>
1 parent e309a86 commit a3d471f

3 files changed

Lines changed: 63 additions & 117 deletions

File tree

local_navigation/include/local_navigation/GridmapUpdaterNode.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class GridmapUpdaterNode : public rclcpp::Node
7575
void topic_callback_info(sensor_msgs::msg::CameraInfo::UniquePtr msg);
7676
void image_callback(sensor_msgs::msg::Image::UniquePtr msg);
7777

78-
std::tuple<float, int, int> get_point_color(pcl::PointXYZ point);
79-
8078
std::string map_frame_id_;
8179
std::string robot_frame_id_;
8280
std::string camera_frame_id_;
@@ -85,8 +83,6 @@ class GridmapUpdaterNode : public rclcpp::Node
8583
std::string lidar_topic_;
8684
std::string path_topic_;
8785

88-
bool camera_info_received_ {false};
89-
9086
double resolution_gridmap_ {0.2};
9187
double size_x_ {100.0};
9288
double size_y_ {100.0};

local_navigation/launch/local_navigation_perro.launch.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ def generate_launch_description():
3838
get_package_share_directory('go2_description'),
3939
'launch',
4040
'robot.launch.py')))
41-
42-
4341

4442
local_navigation_cmd = Node(package='local_navigation',
4543
executable='local_navigation_program',

local_navigation/src/local_navigation/GridmapUpdaterNode.cpp

Lines changed: 63 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ GridmapUpdaterNode::GridmapUpdaterNode(const rclcpp::NodeOptions & options)
4545
tf_buffer_(this->get_clock()),
4646
tf_listener_(tf_buffer_)
4747
{
48-
4948
get_params();
5049

5150
init_colors();
@@ -82,7 +81,7 @@ GridmapUpdaterNode::get_params()
8281
path_topic_ = this->get_parameter("path_topic").as_string();
8382
map_frame_id_ = this->get_parameter("map_frame").as_string();
8483
robot_frame_id_ = this->get_parameter("robot_frame").as_string();
85-
camera_frame_id_ = this->get_parameter("camera_frame").as_string();
84+
camera_frame_id_ = this->get_parameter("camera_frame").as_string();
8685
}
8786

8887
void
@@ -97,7 +96,7 @@ GridmapUpdaterNode::init_gridmap()
9796

9897
// Adding layers
9998
gridmap_->add("elevation");
100-
gridmap_->add("RGB");
99+
gridmap_->add("RGB");
101100
gridmap_->add("transversality");
102101

103102
reset_gridmap();
@@ -125,32 +124,68 @@ GridmapUpdaterNode::reset_gridmap()
125124

126125
// Set init values in matrixes
127126
for (auto i = 0; i < gridmap_->getSize()(0); i++) {
128-
for (auto j = 0; j < gridmap_->getSize()(1); j++) {
129-
em_(i, j) = NAN;
130-
tm_(i, j) = color_unknown_;
131-
cm_(i, j) = 0;
132-
}
127+
for (auto j = 0; j < gridmap_->getSize()(1); j++) {
128+
em_(i, j) = NAN;
129+
tm_(i, j) = color_unknown_;
130+
cm_(i, j) = 0;
131+
}
133132
}
134133

135134
// Dump matrixes to gridmap layers
136135
for (auto i = 0; i < gridmap_->getSize()(0); i++) {
137-
for (auto j = 0; j < gridmap_->getSize()(1); j++) {
138-
grid_map::Index map_index(i, j);
139-
gridmap_->at("elevation", map_index) = em_(i, j);
140-
gridmap_->at("transversality", map_index) = tm_(i, j);
141-
gridmap_->at("RGB", map_index) = cm_(i, j);
136+
for (auto j = 0; j < gridmap_->getSize()(1); j++) {
137+
grid_map::Index map_index(i, j);
138+
gridmap_->at("elevation", map_index) = em_(i, j);
139+
gridmap_->at("transversality", map_index) = tm_(i, j);
140+
gridmap_->at("RGB", map_index) = cm_(i, j);
141+
}
142+
}
143+
}
144+
145+
std::tuple<float, int, int>
146+
get_point_color(
147+
const pcl::PointXYZ & point, const image_geometry::PinholeCameraModel & camera_model,
148+
const cv::Mat & image_rgb_raw)
149+
{
150+
cv::Mat world_point_fromCamera = (cv::Mat_<double>(3, 1) << point.x, point.y, point.z);
151+
cv::Point2d point_2d = camera_model.project3dToPixel(cv::Point3d(point.x, point.y, point.z));
152+
153+
double point_x = point_2d.x;
154+
double point_y = point_2d.y;
155+
156+
if (point_x > 0 && point_x < image_rgb_raw.cols && point_y > 0 && point_y < image_rgb_raw.rows) {
157+
if (image_rgb_raw.type() == CV_8UC3) {
158+
cv::Vec3b color = image_rgb_raw.at<cv::Vec3b>(static_cast<int>(point_y),
159+
static_cast<int>(point_x));
160+
Eigen::Vector3i color_eigen(color[0], color[1], color[2]);
161+
float color_value;
162+
grid_map::colorVectorToValue(color_eigen, color_value);
163+
return {color_value, point_x, point_y};
164+
} else if (image_rgb_raw.type() == CV_8UC1) {
165+
float color_value = image_rgb_raw.at<uint8_t>(static_cast<int>(point_y),
166+
static_cast<int>(point_x));
167+
return {color_value, point_x, point_y};
168+
} else if (image_rgb_raw.type() == CV_8UC4) {
169+
cv::Vec4b color = image_rgb_raw.at<cv::Vec4b>(static_cast<int>(point_y),
170+
static_cast<int>(point_x));
171+
Eigen::Vector3i color_eigen(color[2], color[1], color[0]);
172+
float color_value;
173+
grid_map::colorVectorToValue(color_eigen, color_value);
174+
return {color_value, point_x, point_y};
175+
} else {
176+
return {-1.0, -1.0, -1.0};
142177
}
178+
} else {
179+
return {-1.0, -1.0, -1.0};
143180
}
144181
}
145182

146183
void
147184
GridmapUpdaterNode::update_gridmap(
148185
const pcl::PointCloud<pcl::PointXYZ> & pc_map,
149186
const pcl::PointCloud<pcl::PointXYZ> & pc_robot,
150-
const pcl::PointCloud<pcl::PointXYZ> & pc_camera
151-
)
187+
const pcl::PointCloud<pcl::PointXYZ> & pc_camera)
152188
{
153-
154189
// RCLCPP_INFO(get_logger(), "Updating gridmap");
155190
for (size_t i = 0; i < pc_map.size(); i++) {
156191
const auto & point = pc_map[i];
@@ -203,30 +238,18 @@ GridmapUpdaterNode::update_gridmap(
203238

204239
gridmap_->at("elevation", idx) = em_(idx(0), idx(1));
205240

206-
207-
208-
if (point_camera.z > 0) // Prevent to proyect points behind the camera
241+
if (camera_model_ != nullptr && !image_rgb_raw_.empty() &&
242+
point_camera.z > 0) // Prevent to proyect points behind the camera
209243
{
210-
auto [color, p_x, p_y] = get_point_color(point_camera);
211-
if (color > 0)
212-
{
244+
auto [color, p_x, p_y] = get_point_color(point_camera, *camera_model_, image_rgb_raw_);
245+
if (color > 0) {
213246
cm_(idx(0), idx(1)) = color;
214247
gridmap_->at("RGB", idx) = color;
215-
216-
// // Debug
217-
// image_rgb_raw_.at<cv::Vec3b>(p_y, p_x) = color;
218248
}
219-
}
220-
else
221-
{
249+
} else {
222250
gridmap_->at("RGB", idx) = cm_(idx(0), idx(1));
223251
}
224252
}
225-
// // DEBUG Proyected points
226-
// cv_bridge::CvImage cv_image;
227-
// cv_image.image = image_rgb_raw_;
228-
// cv_image.encoding = sensor_msgs::image_encodings::RGB8;
229-
// img_pub_->publish(*cv_image.toImageMsg().get());
230253
}
231254

232255
void
@@ -269,8 +292,6 @@ transform_cloud(
269292
void
270293
GridmapUpdaterNode::pc_callback(sensor_msgs::msg::PointCloud2::UniquePtr pc_in)
271294
{
272-
// RCLCPP_INFO(get_logger(), "PointCloud received");
273-
274295
pcl::PointCloud<pcl::PointXYZ>::Ptr pcl_cloud(new pcl::PointCloud<pcl::PointXYZ>);
275296
pcl::fromROSMsg(*pc_in, *pcl_cloud);
276297

@@ -326,103 +347,34 @@ GridmapUpdaterNode::path_callback(nav_msgs::msg::Path::UniquePtr path_in)
326347
void
327348
GridmapUpdaterNode::topic_callback_info(sensor_msgs::msg::CameraInfo::UniquePtr msg)
328349
{
329-
// RCLCPP_INFO(get_logger(), "Camera info received");
330-
331350
camera_model_ = std::make_shared<image_geometry::PinholeCameraModel>();
332351
camera_model_->fromCameraInfo(*msg);
333352

334-
camera_info_received_ = true;
335-
336353
subscription_info_ = nullptr;
337354
}
338355

339356
void
340357
GridmapUpdaterNode::image_callback(sensor_msgs::msg::Image::UniquePtr msg)
341358
{
342-
// RCLCPP_INFO(get_logger(), "Img received");
343-
344359
cv_bridge::CvImagePtr image_rgb_ptr;
345360

346-
try
347-
{
348-
if (msg->encoding == sensor_msgs::image_encodings::RGB8)
349-
{
361+
try {
362+
if (msg->encoding == sensor_msgs::image_encodings::RGB8) {
350363
image_rgb_ptr = cv_bridge::toCvCopy(*msg, sensor_msgs::image_encodings::RGB8);
351-
}
352-
else if (msg->encoding == sensor_msgs::image_encodings::MONO8)
353-
{
364+
} else if (msg->encoding == sensor_msgs::image_encodings::MONO8) {
354365
image_rgb_ptr = cv_bridge::toCvCopy(*msg, sensor_msgs::image_encodings::MONO8);
355-
}
356-
else if (msg->encoding == sensor_msgs::image_encodings::BGRA8)
357-
{
366+
} else if (msg->encoding == sensor_msgs::image_encodings::BGRA8) {
358367
image_rgb_ptr = cv_bridge::toCvCopy(*msg, sensor_msgs::image_encodings::BGRA8);
359-
}
360-
else
361-
{
368+
} else {
362369
RCLCPP_ERROR(get_logger(), "Unsupported encoding %s", msg->encoding.c_str());
363370
return;
364371
}
365-
}
366-
catch (cv_bridge::Exception & e)
367-
{
372+
} catch (cv_bridge::Exception & e) {
368373
RCLCPP_ERROR(get_logger(), "cv_bridge exception: %s", e.what());
369374
return;
370375
}
371-
image_rgb_raw_ = image_rgb_ptr->image;
372-
}
373-
374-
std::tuple<float, int, int>
375-
GridmapUpdaterNode::get_point_color(pcl::PointXYZ point)
376-
{
377-
378-
if(!camera_info_received_)
379-
{
380-
RCLCPP_ERROR(get_logger(), "Error: Camera info not received");
381-
return {-1.0, -1.0, -1.0};
382-
}
383-
384-
cv::Mat world_point_fromCamera = (cv::Mat_<double>(3, 1) << point.x, point.y, point.z);
385-
386-
cv::Mat intrinsic_matrix = cv::Mat(camera_model_->intrinsicMatrix());
387376

388-
cv::Point2d point_2d = camera_model_->project3dToPixel(cv::Point3d(point.x, point.y, point.z));
389-
390-
double point_x = point_2d.x;
391-
double point_y = point_2d.y;
392-
393-
if (point_x > 0 && point_x < image_rgb_raw_.cols && point_y > 0 && point_y < image_rgb_raw_.rows)
394-
{
395-
if (image_rgb_raw_.type() == CV_8UC3)
396-
{
397-
cv::Vec3b color = image_rgb_raw_.at<cv::Vec3b>(int(point_y), int(point_x));
398-
Eigen::Vector3i color_eigen(color[0], color[1], color[2]);
399-
float color_value;
400-
grid_map::colorVectorToValue(color_eigen, color_value);
401-
return {color_value, point_x, point_y};
402-
}
403-
else if (image_rgb_raw_.type() == CV_8UC1)
404-
{
405-
float color_value = image_rgb_raw_.at<uint8_t>(int(point_y), int(point_x));
406-
return {color_value, point_x, point_y};
407-
}
408-
else if (image_rgb_raw_.type() == CV_8UC4)
409-
{
410-
cv::Vec4b color = image_rgb_raw_.at<cv::Vec4b>(int(point_y), int(point_x));
411-
Eigen::Vector3i color_eigen(color[2], color[1], color[0]);
412-
float color_value;
413-
grid_map::colorVectorToValue(color_eigen, color_value);
414-
return {color_value, point_x, point_y};
415-
}
416-
else
417-
{
418-
RCLCPP_ERROR(get_logger(), "Error: Image encoding not supported");
419-
return {-1.0, -1.0, -1.0};
420-
}
421-
}
422-
else
423-
{
424-
return {-1.0, -1.0, -1.0};
425-
}
377+
image_rgb_raw_ = image_rgb_ptr->image;
426378
}
427379

428380
} // namespace local_navigation

0 commit comments

Comments
 (0)