Skip to content

Commit a4668bf

Browse files
committed
change pkg structure and add hal interfaces for cpp
1 parent dc90661 commit a4668bf

18 files changed

Lines changed: 817 additions & 7 deletions

File tree

common_interfaces_cpp/CMakeLists.txt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,50 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
55
add_compile_options(-Wall -Wextra -Wpedantic)
66
endif()
77

8-
# Define a list of dependencies for easy modularity and future additions
8+
# Define a list of ROS 2 dependencies for easy modularity
99
set(ros2_dependencies
1010
rclcpp
1111
nav_msgs
12+
sensor_msgs
13+
geometry_msgs
14+
gazebo_msgs
15+
rosgraph_msgs
16+
cv_bridge
1217
)
1318

1419
# Find ament_cmake (required for the build system itself)
1520
find_package(ament_cmake REQUIRED)
1621

17-
# Loop through the list to find all specified packages automatically
22+
# Loop through the list to find all specified ROS 2 packages automatically
1823
foreach(dependency IN LISTS ros2_dependencies)
1924
find_package(${dependency} REQUIRED)
2025
endforeach()
2126

22-
# Create the shared library from the source files
27+
# Find OpenCV explicitly (Required by Camera and Classnet for cv::Mat and cv::dnn)
28+
find_package(OpenCV REQUIRED)
29+
30+
# Create the shared library from the source files matching the new structure
2331
add_library(${PROJECT_NAME} SHARED
24-
src/odometry.cpp
32+
src/hal/bumper.cpp
33+
src/hal/camera.cpp
34+
src/hal/classnet.cpp
35+
src/hal/laser.cpp
36+
src/hal/lidar.cpp
37+
src/hal/motors.cpp
38+
src/hal/odometry.cpp
39+
src/hal/sim_time.cpp
2540
)
2641

2742
# Specify the include directories so the compiler finds the .hpp files
2843
target_include_directories(${PROJECT_NAME} PUBLIC
2944
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
3045
$<INSTALL_INTERFACE:include>
46+
${OpenCV_INCLUDE_DIRS}
3147
)
3248

33-
# Link all listed dependencies to the library
49+
# Link all listed ROS 2 dependencies and OpenCV to the library
3450
ament_target_dependencies(${PROJECT_NAME} ${ros2_dependencies})
51+
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})
3552

3653
# Install the include directory so other packages can do #include
3754
install(DIRECTORY include/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef COMMON_INTERFACES_CPP__BUMPER_HPP_
2+
#define COMMON_INTERFACES_CPP__BUMPER_HPP_
3+
4+
#include <vector>
5+
#include <string>
6+
#include "rclcpp/rclcpp.hpp"
7+
#include "gazebo_msgs/msg/contacts_state.hpp"
8+
9+
/* ### AUXILIARY FUNCTIONS ### */
10+
11+
const int RIGHT_BUMPER = 0;
12+
const int CENTER_BUMPER = 1;
13+
const int LEFT_BUMPER = 2;
14+
15+
class BumperData {
16+
public:
17+
BumperData();
18+
int state;
19+
int bumper;
20+
21+
std::string to_string() const;
22+
};
23+
24+
BumperData contactsToBumperData(const std::vector<gazebo_msgs::msg::ContactsState>& contacts);
25+
26+
/* ### HAL INTERFACE ### */
27+
class BumperNode : public rclcpp::Node {
28+
public:
29+
BumperNode(const std::vector<std::string>& topics);
30+
BumperData getBumperData() const;
31+
32+
private:
33+
void right_callback(const gazebo_msgs::msg::ContactsState::SharedPtr contact);
34+
void center_callback(const gazebo_msgs::msg::ContactsState::SharedPtr contact);
35+
void left_callback(const gazebo_msgs::msg::ContactsState::SharedPtr contact);
36+
37+
std::vector<std::string> topics;
38+
std::vector<rclcpp::Subscription<gazebo_msgs::msg::ContactsState>::SharedPtr> subs_;
39+
std::vector<gazebo_msgs::msg::ContactsState> contact_states_;
40+
};
41+
42+
#endif
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#ifndef COMMON_INTERFACES_CPP__CAMERA_HPP_
2+
#define COMMON_INTERFACES_CPP__CAMERA_HPP_
3+
4+
#include <string>
5+
#include <memory>
6+
#include "rclcpp/rclcpp.hpp"
7+
#include "sensor_msgs/msg/image.hpp"
8+
#include <opencv2/opencv.hpp>
9+
#include <cv_bridge/cv_bridge.h>
10+
11+
/* ### AUXILIARY FUNCTIONS ### */
12+
13+
const int MAXRANGE = 8; // max length received from imageD
14+
const int MINRANGE = 0;
15+
16+
/* Represents a camera image with metadata and pixel data. */
17+
class Image {
18+
public:
19+
Image();
20+
int height; // Image height [pixels]
21+
int width; // Image width [pixels]
22+
double timeStamp; // Time stamp [s] */
23+
std::string format; // Image format string (RGB8, BGR,...)
24+
cv::Mat data; // The image data itself
25+
26+
std::string to_string() const;
27+
};
28+
29+
// Declaración de función auxiliar implícita en tu código Python
30+
cv::Mat depthToRGB8(const cv::Mat& gray_img_buff, const std::string& encoding);
31+
32+
/*
33+
* Convert a ROS Image message to a JdeRobot Image object.
34+
*
35+
* Args:
36+
* img: ROS sensor_msgs/Image message.
37+
*
38+
* Returns:
39+
* Image object with BGR data, or None if the message is empty.
40+
*/
41+
std::shared_ptr<Image> imageMsg2Image(const sensor_msgs::msg::Image& img);
42+
43+
/* ### HAL INTERFACE ### */
44+
45+
/* ROS2 node that subscribes to a camera topic and stores the latest image. */
46+
class CameraNode : public rclcpp::Node {
47+
public:
48+
CameraNode(const std::string& topic);
49+
50+
/*
51+
* Return the latest camera image.
52+
*
53+
* Returns:
54+
* Image object with BGR data, or None if no image has been received.
55+
*/
56+
std::shared_ptr<Image> getImage() const;
57+
58+
private:
59+
/* Store the latest image message received from the topic. */
60+
void listener_callback(const sensor_msgs::msg::Image::SharedPtr msg);
61+
62+
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr sub_;
63+
sensor_msgs::msg::Image last_img_;
64+
};
65+
66+
#endif
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef COMMON_INTERFACES_CPP__CLASSNET_HPP_
2+
#define COMMON_INTERFACES_CPP__CLASSNET_HPP_
3+
4+
#include <string>
5+
#include <vector>
6+
#include <map>
7+
#include <opencv2/opencv.hpp>
8+
#include <opencv2/dnn.hpp>
9+
10+
extern const std::string FROZEN_GRAPH;
11+
extern const std::string PB_TXT;
12+
extern const int SIZE;
13+
14+
extern std::map<int, std::string> LABEL_MAP;
15+
16+
class BoundingBox {
17+
public:
18+
BoundingBox(int identifier, const std::string& class_id_str, float score, float xmin, float ymin, float xmax, float ymax);
19+
20+
int id;
21+
std::string class_id;
22+
float score;
23+
float xmin;
24+
float ymin;
25+
float xmax;
26+
float ymax;
27+
28+
std::string to_string() const;
29+
};
30+
31+
class NeuralNetwork {
32+
public:
33+
NeuralNetwork();
34+
cv::Mat detect(const cv::Mat& img);
35+
36+
// Get bounding boxes function
37+
std::vector<BoundingBox> getBoundingBoxes(const cv::Mat& img);
38+
39+
private:
40+
cv::dnn::Net net;
41+
};
42+
43+
#endif
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef COMMON_INTERFACES_CPP__LASER_HPP_
2+
#define COMMON_INTERFACES_CPP__LASER_HPP_
3+
4+
#include <vector>
5+
#include <string>
6+
#include "rclcpp/rclcpp.hpp"
7+
#include "sensor_msgs/msg/laser_scan.hpp"
8+
9+
/* ### AUXILIARY FUNCTIONS */
10+
11+
class LaserData {
12+
public:
13+
LaserData();
14+
std::vector<float> values; // meters
15+
double minAngle; // Angle of first value (rads)
16+
double maxAngle; // Angle of last value (rads)
17+
double minRange; // Min Range possible (meters)
18+
double maxRange; // Max Range possible (meters)
19+
double timeStamp; // seconds
20+
21+
std::string to_string() const;
22+
};
23+
24+
/*
25+
* Translates from ROS LaserScan to JderobotTypes LaserData.
26+
* @param scan: ROS LaserScan to translate
27+
* @type scan: LaserScan
28+
* @return a LaserData translated from scan
29+
*/
30+
LaserData laserScan2LaserData(const sensor_msgs::msg::LaserScan& scan);
31+
32+
33+
/* ### HAL INTERFACE ### */
34+
class LaserNode : public rclcpp::Node {
35+
public:
36+
LaserNode(const std::string& topic);
37+
LaserData getLaserData() const;
38+
39+
private:
40+
void listener_callback(const sensor_msgs::msg::LaserScan::SharedPtr scan);
41+
42+
rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr sub_;
43+
sensor_msgs::msg::LaserScan last_scan_;
44+
};
45+
46+
#endif
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef COMMON_INTERFACES_CPP__LIDAR_HPP_
2+
#define COMMON_INTERFACES_CPP__LIDAR_HPP_
3+
4+
#include <vector>
5+
#include <string>
6+
#include <memory>
7+
#include <mutex>
8+
#include <array>
9+
#include "rclcpp/rclcpp.hpp"
10+
#include "sensor_msgs/msg/point_cloud2.hpp"
11+
12+
class LidarData {
13+
public:
14+
LidarData();
15+
std::vector<std::array<float, 3>> points;
16+
std::vector<float> intensities;
17+
double timeStamp;
18+
double min_range;
19+
double max_range;
20+
std::pair<double, double> field_of_view;
21+
bool is_dense;
22+
23+
std::string to_string() const;
24+
};
25+
26+
LidarData pointCloud2LidarData(const sensor_msgs::msg::PointCloud2::SharedPtr cloud);
27+
28+
class LidarNode : public rclcpp::Node {
29+
public:
30+
LidarNode(const std::string& topic);
31+
LidarData getLidarData();
32+
sensor_msgs::msg::PointCloud2::SharedPtr get_point_cloud();
33+
34+
private:
35+
void pointcloud_callback(const sensor_msgs::msg::PointCloud2::SharedPtr cloud);
36+
37+
std::mutex lock_;
38+
sensor_msgs::msg::PointCloud2::SharedPtr last_cloud_;
39+
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr sub_;
40+
};
41+
42+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef COMMON_INTERFACES_CPP__MOTORS_HPP_
2+
#define COMMON_INTERFACES_CPP__MOTORS_HPP_
3+
4+
#include <string>
5+
#include "rclcpp/rclcpp.hpp"
6+
#include "geometry_msgs/msg/twist.hpp"
7+
8+
/* ### HAL INTERFACE ### */
9+
10+
/* ROS2 node that publishes linear and angular velocity commands to a robot. */
11+
class MotorsNode : public rclcpp::Node {
12+
public:
13+
MotorsNode(const std::string& topic, double maxV, double maxW);
14+
15+
/* Publish a linear velocity command.
16+
*
17+
* Args:
18+
* v (float): Linear velocity in m/s.
19+
*/
20+
void sendV(double v);
21+
22+
/* Publish an angular velocity command.
23+
*
24+
* Args:
25+
* w (float): Angular velocity in rad/s.
26+
*/
27+
void sendW(double w);
28+
29+
private:
30+
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr pub;
31+
geometry_msgs::msg::Twist last_twist;
32+
};
33+
34+
#endif

common_interfaces_cpp/include/common_interfaces_cpp/odometry.hpp renamed to common_interfaces_cpp/include/common_interfaces_cpp/hal/odometry.hpp

File renamed without changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef COMMON_INTERFACES_CPP__SIM_TIME_HPP_
2+
#define COMMON_INTERFACES_CPP__SIM_TIME_HPP_
3+
4+
#include <string>
5+
#include "rclcpp/rclcpp.hpp"
6+
#include "rosgraph_msgs/msg/clock.hpp"
7+
8+
/* ### AUXILIARY FUNCTIONS */
9+
10+
class SimTimeData {
11+
public:
12+
SimTimeData();
13+
int seconds;
14+
int nanoseconds;
15+
16+
std::string to_string() const;
17+
};
18+
19+
/*
20+
* Translates from ROS Clock to JderobotTypes SimTimeData.
21+
* @param clock: ROS Clock to translate
22+
* @type clock: Clock
23+
* @return a SimTimeData translated from clock
24+
*/
25+
SimTimeData simTime2SimTimeData(const rosgraph_msgs::msg::Clock& clock);
26+
27+
28+
/* ### HAL INTERFACE ### */
29+
30+
class SimTimeNode : public rclcpp::Node {
31+
public:
32+
SimTimeNode();
33+
SimTimeData getSimTime() const;
34+
35+
private:
36+
void listener_callback(const rosgraph_msgs::msg::Clock::SharedPtr msg);
37+
38+
rclcpp::Subscription<rosgraph_msgs::msg::Clock>::SharedPtr sub;
39+
rosgraph_msgs::msg::Clock last_sim_time_;
40+
};
41+
42+
#endif

0 commit comments

Comments
 (0)