Skip to content

Commit a334c1b

Browse files
committed
Added example rosbridge publisher node
1 parent 6c7b2a1 commit a334c1b

File tree

7 files changed

+165
-1
lines changed

7 files changed

+165
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(rosbridge_publisher)
3+
4+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
5+
add_compile_options(-Wall -Wextra -Wpedantic)
6+
endif()
7+
8+
# find dependencies
9+
find_package(ament_cmake REQUIRED)
10+
find_package(rclcpp REQUIRED)
11+
find_package(std_msgs REQUIRED)
12+
find_package(sample_msgs REQUIRED)
13+
# uncomment the following section in order to fill in
14+
# further dependencies manually.
15+
# find_package(<dependency> REQUIRED)
16+
17+
add_library(rosbridge_publisher_lib src/rosbridge_publisher_core.cpp)
18+
19+
target_include_directories(rosbridge_publisher_lib PUBLIC include)
20+
21+
add_executable(rosbridge_publisher_node src/rosbridge_publisher_node.cpp)
22+
ament_target_dependencies(rosbridge_publisher_lib rclcpp std_msgs sample_msgs)
23+
target_link_libraries(rosbridge_publisher_node rosbridge_publisher_lib)
24+
target_include_directories(rosbridge_publisher_node PUBLIC
25+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
26+
$<INSTALL_INTERFACE:include>)
27+
target_compile_features(rosbridge_publisher_node PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
28+
29+
install(TARGETS rosbridge_publisher_node
30+
DESTINATION lib/${PROJECT_NAME})
31+
32+
if(BUILD_TESTING)
33+
find_package(ament_lint_auto REQUIRED)
34+
# the following line skips the linter which checks for copyrights
35+
# comment the line when a copyright and license is added to all source files
36+
set(ament_cmake_copyright_FOUND TRUE)
37+
# the following line skips cpplint (only works in a git repo)
38+
# comment the line when this package is in a git repo and when
39+
# a copyright and license is added to all source files
40+
set(ament_cmake_cpplint_FOUND TRUE)
41+
ament_lint_auto_find_test_dependencies()
42+
endif()
43+
44+
ament_package()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef ROSBRIDGE_PUBLISHER_CORE_HPP_
2+
#define ROSBRIDGE_PUBLISHER_CORE_HPP_
3+
4+
#include "rclcpp/rclcpp.hpp"
5+
6+
#include "sample_msgs/msg/hand_pose.hpp"
7+
8+
class ROSbridgePublisherCore {
9+
public:
10+
11+
explicit ROSbridgePublisherCore();
12+
13+
std::string hand_pose_to_string(const sample_msgs::msg::HandPose pose);
14+
15+
private:
16+
};
17+
18+
#endif // AGGREGATOR_CORE_HPP_
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef ROSBRIDGE_PUBLISHER_NODE_HPP_
2+
#define ROSBRIDGE_PUBLISHER_NODE_HPP_
3+
4+
#include "rclcpp/rclcpp.hpp"
5+
6+
#include "sample_msgs/msg/hand_pose.hpp"
7+
8+
#include "rosbridge_publisher_core.hpp"
9+
10+
/**
11+
* Implementation of a ROS2 node that listens to the "unfiltered" and "filtered"
12+
* topics and echoes the operating frequency of the topic to the console.
13+
*/
14+
class ROSbridgePublisherNode: public rclcpp::Node {
15+
public:
16+
17+
ROSbridgePublisherNode();
18+
19+
private:
20+
21+
void timer_callback();
22+
23+
rclcpp::TimerBase::SharedPtr timer_;
24+
25+
rclcpp::Publisher<sample_msgs::msg::HandPose>::SharedPtr hand_pose_pub_;
26+
27+
ROSbridgePublisherCore rosbridge_publisher_;
28+
29+
};
30+
31+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>rosbridge_publisher</name>
5+
<version>0.0.0</version>
6+
<description>TODO: Package description</description>
7+
<maintainer email="yichen.liu@hotmail.com">yichen</maintainer>
8+
<license>TODO: License declaration</license>
9+
10+
<buildtool_depend>ament_cmake</buildtool_depend>
11+
12+
<depend>rclcpp</depend>
13+
<depend>sample_msgs</depend>
14+
<depend>std_msgs</depend>
15+
16+
<test_depend>ament_lint_auto</test_depend>
17+
<test_depend>ament_lint_common</test_depend>
18+
19+
<export>
20+
<build_type>ament_cmake</build_type>
21+
</export>
22+
</package>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <chrono>
2+
#include <cmath>
3+
4+
#include "rosbridge_publisher_core.hpp"
5+
6+
ROSbridgePublisherCore::ROSbridgePublisherCore(){}
7+
8+
std::string ROSbridgePublisherCore::hand_pose_to_string(const sample_msgs::msg::HandPose pose)
9+
{
10+
std::string s;
11+
std::vector<float> hand_pose = pose.data;
12+
s += "[";
13+
for(int i=0; i < hand_pose.size(); i++){
14+
s += std::to_string(hand_pose[i]);
15+
s += ",";
16+
}
17+
s += "]";
18+
return s;
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "rosbridge_publisher_node.hpp"
2+
3+
#include <chrono>
4+
#include <memory>
5+
6+
using namespace std::chrono_literals;
7+
8+
ROSbridgePublisherNode::ROSbridgePublisherNode() : Node("rosbridge_publisher"), rosbridge_publisher_(ROSbridgePublisherCore())
9+
{
10+
11+
hand_pose_pub_ = this->create_publisher<sample_msgs::msg::HandPose>("teleop", 10);
12+
timer_ = this->create_wall_timer(
13+
500ms, std::bind(&ROSbridgePublisherNode::timer_callback, this)
14+
);
15+
}
16+
17+
void ROSbridgePublisherNode::timer_callback(){
18+
auto message = sample_msgs::msg::HandPose();
19+
message.data = std::vector<float>(17);
20+
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", rosbridge_publisher_.hand_pose_to_string(message).c_str());
21+
hand_pose_pub_->publish(message);
22+
}
23+
24+
int main(int argc, char **argv) {
25+
rclcpp::init(argc, argv);
26+
rclcpp::spin(std::make_shared<ROSbridgePublisherNode>());
27+
rclcpp::shutdown();
28+
return 0;
29+
}

autonomy/wato_msgs/sample_msgs/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ set(msg_files
1717
msg/Filtered.msg
1818
msg/FilteredArray.msg
1919
msg/Unfiltered.msg
20-
msg/Metadata.msg)
20+
msg/Metadata.msg
21+
msg/HandPose.msg)
2122
rosidl_generate_interfaces(sample_msgs
2223
${msg_files}
2324
DEPENDENCIES std_msgs)

0 commit comments

Comments
 (0)