Skip to content

Commit 5d1fc6a

Browse files
authored
Merge pull request #3794 from JdeRobot/cpp-global-nav-update
Global nav Cpp update
2 parents d1c6378 + b1ab7ea commit 5d1fc6a

14 files changed

Lines changed: 385 additions & 407 deletions

File tree

exercises/global_navigation/cpp_lib/CMakeLists.txt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ set(ROS2_PACKAGES
1212
nav_msgs
1313
geometry_msgs
1414
sensor_msgs
15+
std_msgs
1516
cv_bridge
17+
nav_msgs
1618
)
1719

1820
foreach(pkg IN LISTS ROS2_PACKAGES)
@@ -26,29 +28,22 @@ find_package(OpenCV REQUIRED)
2628
set(COMMON_INTERFACES_INCLUDE "/home/ws/install/common_interfaces_cpp/include")
2729
set(COMMON_INTERFACES_LIB "/home/ws/install/common_interfaces_cpp/lib/libcommon_interfaces_cpp.so")
2830

29-
set(HAL_ROS_DEPS rclcpp geometry_msgs nav_msgs)
3031
set(WEBGUI_ROS_DEPS rclcpp nav_msgs geometry_msgs sensor_msgs cv_bridge)
31-
set(WEBGUI_SYS_DEPS Frequency Boost::system nlohmann_json::nlohmann_json ${OpenCV_LIBS})
32-
32+
set(WEBGUI_SYS_DEPS Frequency Boost::system nlohmann_json::nlohmann_json)
33+
set(HAL_ROS_DEPS rclcpp geometry_msgs sensor_msgs std_msgs cv_bridge nav_msgs)
3334

3435
# --- libFrequency.so ---
3536
add_library(Frequency SHARED src/Frequency.cpp)
3637
target_include_directories(Frequency PUBLIC include)
3738

38-
# --- libHAL.so ---
39-
add_library(HAL SHARED src/HAL.cpp)
40-
target_include_directories(HAL PUBLIC include ${COMMON_INTERFACES_INCLUDE})
41-
42-
ament_target_dependencies(HAL ${HAL_ROS_DEPS})
43-
target_link_libraries(HAL ${COMMON_INTERFACES_LIB})
44-
4539
# --- libWebGUI.so ---
4640
add_library(WebGUI SHARED src/WebGUI.cpp src/Map.cpp)
47-
target_include_directories(WebGUI PUBLIC
48-
include
49-
${COMMON_INTERFACES_INCLUDE}
50-
${OpenCV_INCLUDE_DIRS}
51-
)
52-
41+
target_include_directories(WebGUI PUBLIC include ${COMMON_INTERFACES_INCLUDE} ${OpenCV_INCLUDE_DIRS})
5342
ament_target_dependencies(WebGUI ${WEBGUI_ROS_DEPS})
54-
target_link_libraries(WebGUI ${WEBGUI_SYS_DEPS} ${COMMON_INTERFACES_LIB})
43+
target_link_libraries(WebGUI ${WEBGUI_SYS_DEPS} ${COMMON_INTERFACES_LIB} ${OpenCV_LIBS})
44+
45+
# --- libHAL.so ---
46+
add_library(HAL SHARED src/HAL.cpp)
47+
target_include_directories(HAL PUBLIC include ${COMMON_INTERFACES_INCLUDE} ${OpenCV_INCLUDE_DIRS})
48+
ament_target_dependencies(HAL ${HAL_ROS_DEPS})
49+
target_link_libraries(HAL ${COMMON_INTERFACES_LIB} ${OpenCV_LIBS})

exercises/global_navigation/cpp_lib/include/HAL.hpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@
33

44
#include <memory>
55
#include <thread>
6-
#include <chrono>
7-
#include "rclcpp/rclcpp.hpp"
8-
#include "common_interfaces_cpp/hal/motors.hpp"
96
#include "common_interfaces_cpp/hal/odometry.hpp"
107

11-
class HAL : public rclcpp::Node
8+
// Forward declarations to speed up compilation by avoiding heavy ROS 2 includes.
9+
// - MotorsNode links to "common_interfaces_cpp/hal/motors.hpp"
10+
// - OdometryNode links to "common_interfaces_cpp/hal/odometry.hpp"
11+
class MotorsNode;
12+
class OdometryNode;
13+
namespace rclcpp::executors { class SingleThreadedExecutor; }
14+
15+
class HAL
1216
{
1317
public:
14-
HAL();
15-
static void set_v(const float speed);
16-
static void set_w(const float speed);
18+
// Prevent instantiation. HAL acts as a global static utility.
19+
HAL() = delete;
20+
21+
static void set_v(const float velocity);
22+
static void set_w(const float velocity);
1723
static Pose3d get_pose3d();
1824

1925
private:
26+
static void init();
27+
friend class SystemBootstrapper;
28+
29+
// Hidden internal state. Not accessible to the user.
2030
static std::shared_ptr<MotorsNode> motors_node_;
2131
static std::shared_ptr<OdometryNode> odometry_node_;
22-
std::thread spin_thread_;
32+
static std::shared_ptr<rclcpp::executors::SingleThreadedExecutor> executor_;
33+
static std::thread spin_thread_;
2334
};
2435

2536
#endif

exercises/global_navigation/cpp_lib/include/Map.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55
#include <string>
66
#include <functional>
7+
#include <mutex>
78
#include <opencv2/opencv.hpp>
89
#include "common_interfaces_cpp/hal/odometry.hpp"
910

@@ -44,6 +45,7 @@ class Map
4445
private:
4546
std::function<Pose3d()> pose_callback_;
4647
cv::Mat grid_;
48+
std::mutex grid_mtx_;
4749
};
4850

4951
#endif
Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,34 @@
11
#ifndef INCLUDE_WEBGUI_HPP_
22
#define INCLUDE_WEBGUI_HPP_
33

4-
#include <boost/beast/core.hpp>
5-
#include <boost/beast/websocket.hpp>
6-
#include <boost/asio/connect.hpp>
7-
#include <boost/asio/ip/tcp.hpp>
8-
#include <boost/asio/strand.hpp>
9-
#include <nlohmann/json.hpp>
104
#include <opencv2/opencv.hpp>
11-
#include <mutex>
5+
#include <vector>
126
#include <memory>
137
#include <string>
14-
#include <vector>
15-
#include "rclcpp/rclcpp.hpp"
16-
#include "geometry_msgs/msg/point.hpp"
17-
#include "nav_msgs/msg/path.hpp"
18-
#include "sensor_msgs/msg/image.hpp"
19-
#include "common_interfaces_cpp/hal/odometry.hpp"
20-
#include "Map.hpp"
8+
#include <thread>
219

22-
namespace beast = boost::beast;
23-
namespace websocket = beast::websocket;
24-
namespace net = boost::asio;
25-
using tcp = net::ip::tcp;
26-
using json = nlohmann::json;
10+
class WebGUINode;
11+
namespace rclcpp::executors { class MultiThreadedExecutor; }
2712

2813
class WebGUI
2914
{
3015
public:
31-
WebGUI();
32-
33-
static void showNumpy(const cv::Mat& image);
34-
static void showPath(const std::vector<std::vector<int>>& array);
35-
static std::vector<double> getTargetPose();
36-
static cv::Mat getMap(const std::string& url);
37-
static std::vector<int> rowColumn(const std::vector<double>& pose);
38-
static std::vector<int> worldToGrid(const std::vector<double>& pose);
39-
static std::vector<double> gridToWorld(const std::vector<int>& cell);
40-
static void reset_gui();
41-
static std::string payloadImage();
42-
43-
static std::vector<double> worldXY;
44-
static std::string array_str;
45-
46-
private:
47-
static cv::Mat image_to_be_shown;
48-
static bool image_to_be_shown_updated;
49-
static std::mutex image_show_lock;
50-
static std::mutex array_lock;
51-
};
16+
WebGUI() = delete;
5217

53-
class WebGUINode : public rclcpp::Node
54-
{
55-
public:
56-
WebGUINode();
57-
58-
static std::shared_ptr<Map> map_;
59-
static std::shared_ptr<OdometryNode> pose3d_node_;
60-
61-
static void publish_target(double x, double y);
18+
static void show_image(const cv::Mat& image);
19+
static void show_path(const std::vector<std::vector<int>>& path);
20+
static std::vector<double> get_target_pose();
21+
static cv::Mat get_map(const std::string& url);
22+
static std::vector<int> world_to_grid(const std::vector<double>& pose);
23+
static std::vector<double> grid_to_world(const std::vector<double>& cell);
6224

6325
private:
64-
void path_callback(nav_msgs::msg::Path::UniquePtr msg);
65-
void image_callback(sensor_msgs::msg::Image::UniquePtr msg);
66-
67-
rclcpp::Subscription<nav_msgs::msg::Path>::SharedPtr path_sub_;
68-
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr image_sub_;
26+
static void init();
27+
friend class SystemBootstrapper;
6928

70-
static std::shared_ptr<rclcpp::Publisher<geometry_msgs::msg::Point>> target_pub_;
29+
static std::shared_ptr<WebGUINode> gui_node_;
30+
static std::shared_ptr<rclcpp::executors::MultiThreadedExecutor> executor_;
31+
static std::thread spin_thread_;
7132
};
7233

7334
#endif
Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
#include "HAL.hpp"
2+
#include "common_interfaces_cpp/hal/motors.hpp"
3+
#include "common_interfaces_cpp/hal/odometry.hpp"
4+
#include "rclcpp/rclcpp.hpp"
5+
#include <chrono>
26

37
using namespace std::chrono_literals;
48

59
std::shared_ptr<MotorsNode> HAL::motors_node_ = nullptr;
610
std::shared_ptr<OdometryNode> HAL::odometry_node_ = nullptr;
11+
std::shared_ptr<rclcpp::executors::SingleThreadedExecutor> HAL::executor_ = nullptr;
12+
std::thread HAL::spin_thread_;
713

8-
HAL::HAL() : Node("hal_node")
14+
void HAL::init()
915
{
10-
motors_node_ = std::make_shared<MotorsNode>("/cmd_vel", 4.0, 0.3);
11-
odometry_node_ = std::make_shared<OdometryNode>("/odom", "hal_odometry_node");
12-
13-
spin_thread_ = std::thread([]() {
14-
rclcpp::executors::SingleThreadedExecutor executor;
15-
executor.add_node(HAL::motors_node_);
16-
executor.add_node(HAL::odometry_node_);
17-
18-
while (rclcpp::ok()) {
19-
executor.spin_some();
20-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
21-
}
22-
});
23-
spin_thread_.detach();
16+
if (!motors_node_) {
17+
motors_node_ = std::make_shared<MotorsNode>("/cmd_vel", 4.0, 0.3, "hal_motors");
18+
odometry_node_ = std::make_shared<OdometryNode>("/odom", "hal_odometry_node");
19+
20+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
21+
executor_->add_node(motors_node_);
22+
executor_->add_node(odometry_node_);
23+
24+
spin_thread_ = std::thread([]() {
25+
executor_->spin();
26+
});
27+
spin_thread_.detach();
28+
}
2429
}
2530

26-
void HAL::set_v(const float speed)
31+
void HAL::set_v(const float velocity)
2732
{
28-
if (motors_node_) {
29-
motors_node_->sendV(static_cast<double>(speed));
30-
}
33+
if (motors_node_) motors_node_->sendV(static_cast<double>(velocity));
3134
}
3235

33-
void HAL::set_w(const float speed)
36+
void HAL::set_w(const float velocity)
3437
{
35-
if (motors_node_) {
36-
motors_node_->sendW(static_cast<double>(speed));
37-
}
38+
if (motors_node_) motors_node_->sendW(static_cast<double>(velocity));
3839
}
3940

4041
Pose3d HAL::get_pose3d()
4142
{
42-
if (odometry_node_) {
43-
return odometry_node_->getPose3d();
44-
}
43+
if (odometry_node_) return odometry_node_->getPose3d();
4544
return Pose3d();
4645
}

exercises/global_navigation/cpp_lib/src/Map.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ cv::Mat Map::RTFormula()
7575

7676
double Map::getGridVal(int x, int y)
7777
{
78+
std::lock_guard<std::mutex> lock(grid_mtx_);
7879
return grid_.at<double>(y, x);
7980
}
8081

8182
void Map::setGridVal(int x, int y, double val)
8283
{
84+
std::lock_guard<std::mutex> lock(grid_mtx_);
8385
grid_.at<double>(y, x) = val;
8486
}
8587

0 commit comments

Comments
 (0)