Skip to content

Commit 02b2bf7

Browse files
authored
Merge pull request #3791 from JdeRobot/obstacle-avoidance-cpp-update
obstacle avoidance cpp update
2 parents cd3f58d + 0a6457a commit 02b2bf7

17 files changed

Lines changed: 587 additions & 578 deletions

File tree

exercises/obstacle_avoidance/cpp_lib/CMakeLists.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,31 @@ endforeach()
2121

2222
find_package(Boost REQUIRED COMPONENTS system)
2323
find_package(nlohmann_json REQUIRED)
24+
find_package(OpenCV REQUIRED)
2425

2526
set(COMMON_INTERFACES_INCLUDE "/home/ws/install/common_interfaces_cpp/include")
2627
set(COMMON_INTERFACES_LIB "/home/ws/install/common_interfaces_cpp/lib/libcommon_interfaces_cpp.so")
2728

2829
set(WEBGUI_ROS_DEPS rclcpp geometry_msgs std_msgs nav_msgs sensor_msgs)
29-
set(WEBGUI_SYS_DEPS Frequency Boost::system nlohmann_json::nlohmann_json)
30+
set(WEBGUI_SYS_DEPS Frequency Boost::system nlohmann_json::nlohmann_json ${OpenCV_LIBS})
3031
set(HAL_ROS_DEPS rclcpp geometry_msgs sensor_msgs nav_msgs)
31-
32+
set(HAL_SYS_DEPS ${OpenCV_LIBS})
3233

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

37-
3838
# --- libWebGUI.so ---
3939
add_library(WebGUI SHARED
4040
src/WebGUI.cpp
4141
src/Map.cpp
42-
src/Lap.cpp
4342
)
44-
target_include_directories(WebGUI PUBLIC include ${COMMON_INTERFACES_INCLUDE})
43+
target_include_directories(WebGUI PUBLIC include ${COMMON_INTERFACES_INCLUDE} ${OpenCV_INCLUDE_DIRS})
4544
ament_target_dependencies(WebGUI ${WEBGUI_ROS_DEPS})
4645
target_link_libraries(WebGUI ${WEBGUI_SYS_DEPS} ${COMMON_INTERFACES_LIB})
4746

48-
4947
# --- libHAL.so ---
5048
add_library(HAL SHARED src/HAL.cpp)
51-
target_include_directories(HAL PUBLIC include ${COMMON_INTERFACES_INCLUDE})
49+
target_include_directories(HAL PUBLIC include ${COMMON_INTERFACES_INCLUDE} ${OpenCV_INCLUDE_DIRS})
5250
ament_target_dependencies(HAL ${HAL_ROS_DEPS})
53-
target_link_libraries(HAL ${COMMON_INTERFACES_LIB})
51+
target_link_libraries(HAL ${HAL_SYS_DEPS} ${COMMON_INTERFACES_LIB})
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
#ifndef INCLUDE_HAL_HPP_
22
#define INCLUDE_HAL_HPP_
33

4-
#include <vector>
54
#include <memory>
65
#include <thread>
7-
#include <chrono>
8-
#include "rclcpp/rclcpp.hpp"
9-
#include "common_interfaces_cpp/hal/motors.hpp"
10-
#include "common_interfaces_cpp/hal/laser.hpp"
11-
#include "common_interfaces_cpp/hal/odometry.hpp"
6+
#include <vector>
7+
#include <array>
8+
9+
class MotorsNode;
10+
class OdometryNode;
11+
class LaserNode;
12+
namespace rclcpp::executors { class MultiThreadedExecutor; }
1213

13-
class HAL : public rclcpp::Node
14+
class HAL
1415
{
1516
public:
16-
HAL();
17-
static void set_v(const double velocity);
18-
static void set_w(const double velocity);
19-
static Pose3d get_pose3d();
20-
static const LaserData *get_laser_data();
17+
HAL() = delete;
18+
19+
static void set_v(const float velocity);
20+
static void set_w(const float velocity);
21+
static std::array<double, 3> get_pose3d();
22+
static std::vector<float> get_laser_data();
2123

2224
private:
23-
static std::shared_ptr<OdometryNode> pose3d_node_;
25+
static void init();
26+
friend class SystemBootstrapper;
27+
2428
static std::shared_ptr<MotorsNode> motors_node_;
29+
static std::shared_ptr<OdometryNode> odometry_node_;
2530
static std::shared_ptr<LaserNode> laser_node_;
26-
std::thread spin_thread_;
31+
static std::shared_ptr<rclcpp::executors::MultiThreadedExecutor> executor_;
32+
static std::thread spin_thread_;
2733
};
2834

2935
#endif

exercises/obstacle_avoidance/cpp_lib/include/Lap.hpp

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,72 @@
1-
#ifndef INCLUDE_MAP_HPP_
2-
#define INCLUDE_MAP_HPP_
1+
#ifndef MAP_HPP_
2+
#define MAP_HPP_
33

4-
#include <string>
5-
#include <vector>
6-
#include <memory>
7-
#include <functional>
8-
#include <nlohmann/json.hpp>
94
#include "common_interfaces_cpp/hal/odometry.hpp"
105
#include "common_interfaces_cpp/hal/laser.hpp"
6+
#include <memory>
7+
#include <vector>
8+
#include <string>
9+
#include <mutex>
10+
#include <array>
11+
#include <thread>
12+
#include <atomic>
13+
#include <nlohmann/json.hpp>
1114

1215
class Target {
1316
public:
14-
Target(const std::string& id, const Pose3d& pose, bool active = false, bool reached = false);
15-
std::string getId() const;
16-
Pose3d getPose() const;
17-
bool isReached() const;
18-
void setReached(bool value);
19-
20-
std::string id;
21-
Pose3d pose;
22-
bool reached;
23-
bool active;
17+
Target(std::string id, Pose3d pose, bool active = false, bool reached = false);
18+
19+
std::string get_id() const;
20+
Pose3d get_pose() const;
21+
bool is_reached() const;
22+
void set_reached(bool value);
23+
bool is_active() const;
24+
void set_active(bool value);
25+
26+
private:
27+
std::string id_;
28+
Pose3d pose_;
29+
bool active_;
30+
bool reached_;
2431
};
2532

2633
class Map {
2734
public:
28-
Map(std::function<LaserData()> laser_cb, std::function<Pose3d()> pose_cb);
29-
30-
void setCar(double x, double y);
31-
void setObs(double x, double y);
32-
void setAvg(double x, double y);
33-
void setTargetPos(double x, double y);
34-
35-
std::string get_json_data();
36-
std::shared_ptr<Target> getNextTarget();
35+
Map(std::shared_ptr<LaserNode> laser, std::shared_ptr<OdometryNode> odom);
36+
~Map();
37+
38+
void set_car(double newx, double newy);
39+
void set_obs(double newx, double newy);
40+
void set_avg(double newx, double newy);
41+
void set_target_pos(double newx, double newy);
42+
void set_target_x(double x);
43+
void set_target_y(double y);
44+
45+
std::array<double, 2> get_next_target();
46+
void mark_current_target_reached();
3747
void reset();
3848

39-
double targetx, targety;
49+
nlohmann::json get_json_data();
4050

4151
private:
42-
std::vector<double> setPose(const Pose3d& pose);
43-
std::pair<nlohmann::json, double> setLaserValues();
52+
void load_targets();
53+
void laser_poll_loop();
54+
55+
double carx_, cary_;
56+
double obsx_, obsy_;
57+
double avgx_, avgy_;
58+
double targetx_, targety_;
4459

45-
double carx, cary, obsx, obsy, avgx, avgy;
4660
std::vector<std::shared_ptr<Target>> targets_;
47-
nlohmann::json payload_;
61+
std::shared_ptr<LaserNode> laser_;
62+
std::shared_ptr<OdometryNode> odom_;
63+
64+
LaserData cached_laser_;
65+
std::mutex laser_mtx_;
66+
std::thread laser_thread_;
67+
std::atomic<bool> laser_running_;
4868

49-
std::function<LaserData()> laser_callback_;
50-
std::function<Pose3d()> pose_callback_;
69+
std::mutex map_mtx_;
5170
};
5271

5372
#endif
Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,32 @@
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>
10-
#include "rclcpp/rclcpp.hpp"
11-
#include "geometry_msgs/msg/point.hpp"
12-
#include "std_msgs/msg/bool.hpp"
13-
#include "common_interfaces_cpp/hal/odometry.hpp"
14-
#include "common_interfaces_cpp/hal/laser.hpp"
15-
#include "Map.hpp"
16-
#include "Lap.hpp"
4+
#include <memory>
5+
#include <thread>
6+
#include <array>
177

18-
namespace beast = boost::beast;
19-
namespace websocket = beast::websocket;
20-
namespace net = boost::asio;
21-
using tcp = net::ip::tcp;
22-
using json = nlohmann::json;
8+
class WebGUINode;
9+
namespace rclcpp::executors { class MultiThreadedExecutor; }
2310

24-
class WebGUI {
11+
class WebGUI
12+
{
2513
public:
26-
WebGUI();
27-
static void showForces(const std::vector<double>& v1, const std::vector<double>& v2, const std::vector<double>& v3);
28-
static void showLocalTarget(const std::vector<double>& v);
29-
static std::shared_ptr<Target> getNextTarget();
30-
static void setTargetx(double x);
31-
static void setTargety(double y);
32-
};
33-
34-
class WebGUINode : public rclcpp::Node {
35-
public:
36-
WebGUINode();
37-
static std::shared_ptr<Map> map_;
38-
static std::shared_ptr<Lap> lap_;
39-
40-
static std::shared_ptr<OdometryNode> pose3d_node_;
41-
static std::shared_ptr<LaserNode> laser_node_;
14+
WebGUI() = delete;
4215

43-
void publish_current_target();
16+
static void show_forces(const std::array<double, 2>& vec1, const std::array<double, 2>& vec2, const std::array<double, 2>& vec3);
17+
static void show_local_target(const std::array<double, 2>& new_vec);
18+
static std::array<double, 2> get_next_target();
19+
static void set_target_x(double x);
20+
static void set_target_y(double y);
21+
static void mark_target_reached();
4422

4523
private:
46-
void target_reached_callback(std_msgs::msg::Bool::UniquePtr msg);
47-
48-
rclcpp::Subscription<geometry_msgs::msg::Point>::SharedPtr sub_car_;
49-
rclcpp::Subscription<geometry_msgs::msg::Point>::SharedPtr sub_obs_;
50-
rclcpp::Subscription<geometry_msgs::msg::Point>::SharedPtr sub_avg_;
51-
rclcpp::Subscription<geometry_msgs::msg::Point>::SharedPtr sub_target_;
52-
rclcpp::Subscription<std_msgs::msg::Bool>::SharedPtr sub_reached_;
53-
rclcpp::Publisher<geometry_msgs::msg::Point>::SharedPtr pub_current_target_;
24+
static void init();
25+
friend class SystemBootstrapper;
5426

55-
std::shared_ptr<Target> current_target_obj_;
27+
static std::shared_ptr<WebGUINode> gui_node_;
28+
static std::shared_ptr<rclcpp::executors::MultiThreadedExecutor> executor_;
29+
static std::thread spin_thread_;
5630
};
5731

5832
#endif

0 commit comments

Comments
 (0)