find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
ament_target_dependencies(composition_nodes rclcpp rclcpp_components std_msgs)
ament_package()
<buildtool_depend>ament_cmakebuildtool_depend>
<build_depend>rclcppbuild_depend>
<build_depend>rclcpp_componentsbuild_depend>
<build_depend>std_msgsbuild_depend>
<exec_depend>rclcppexec_depend>
<exec_depend>rclcpp_componentsexec_depend>
<exec_depend>std_msgsexec_depend>
<test_depend>ament_lint_autotest_depend>
<test_depend>ament_lint_commontest_depend>
<export>
<build_type>ament_cmakebuild_type>
export>
sudo apt install python3-colcon-common-extensions
find_package(sensor_msgs REQUIRED) # add dependence of sensor_msg
find_package(rosidl_default_generators REQUIRED) # add generator for generating msg source files
set(msg_files
"msg/MyInfo.msg"
"msg/Num.msg"
)
rosidl_generate_interfaces(${PROJECT_NAME}
${msg_files}
# ADD_LINTER_TESTS
DEPENDENCIES sensor_msgs
) # add generate source files like .h/.hpp etc
ament_export_dependencies(rosidl_default_runtime)
# export .so
<depend>sensor_msgsdepend>
<buildtool_depend>rosidl_default_generatorsbuildtool_depend>
<exec_depend>rosidl_default_runtimeexec_depend>
<member_of_group>rosidl_interface_packagesmember_of_group>
使用命令创建包(节点): ros2 pkg create my_node --build-type ament_python --dependencies rclpy
在上面生成的my_node/my_node 中添加python源文件
import rclpy
from rclpy.node import Node
from my_msg.msg import Num # CHANGE
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(Num, 'topic', 10) # CHANGE
timer_period = 0.5
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
def timer_callback(self):
msg = Num() # CHANGE
msg.num = self.i # CHANGE
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%d"' % msg.num) # CHANGE
self.i += 1
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
minimal_publisher.destroy_node()
rclpy.shutdown()
<build_depend>my_msgbuild_depend>
<exec_depend>my_msgexec_depend>
import rclpy
from rclpy.node import Node
from my_msg.msg import Num
class MinimalSubscriber(Node):
def __init__(self):
super().__init__('minimal_subscriber')
self.subscription = self.create_subscription(
Num,
'topic',
self.listener_callback,
10)
self.subscription # prevent unused variable warning
def listener_callback(self, msg):
self.get_logger().info('I heard: "%d"' % msg.num)
def main(args=None):
rclpy.init(args=args)
minimal_subscriber = MinimalSubscriber()
rclpy.spin(minimal_subscriber)
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
minimal_subscriber.destroy_node()
rclpy.shutdown()
#include
#include
#include
#include
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "util/msg/num.hpp"
using namespace std::chrono_literals;
/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */
class MinimalPublisher : public rclcpp::Node
{
public:
// MinimalPublisher()
// : Node("minimal_publisher"), count_(0)
// {
// publisher_ = this->create_publisher("topic", 10);
// timer_ = this->create_wall_timer(
// 500ms, std::bind(&MinimalPublisher::timer_callback, this));
// }
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher("/test/NumMsg", 10);
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalPublisher::timer_callback, this));
}
private:
// void timer_callback()
// {
// auto message = std_msgs::msg::String();
// message.data = "Hello, world! " + std::to_string(count_++);
// RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
// publisher_->publish(message);
// }
void timer_callback()
{
auto message = util::msg::Num();
message.num_test = 3;
// RCLCPP_INFO(this->get_logger(), "Publishing: '%f'", message.num_test.c_str());
std::cout << "running talker" << std::endl;
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
// rclcpp::Publisher::SharedPtr publisher_;
rclcpp::Publisher::SharedPtr publisher_;
size_t count_;
};
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared());
rclcpp::shutdown();
return 0;
}
#include
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "util/msg/num.hpp"
using std::placeholders::_1;
class MinimalSubscriber : public rclcpp::Node
{
// public:
// MinimalSubscriber()
// : Node("minimal_subscriber")
// {
// subscription_ = this->create_subscription(
// "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
// }
// private:
// void topic_callback(const std_msgs::msg::String & msg) const
// {
// RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
// }
// rclcpp::Subscription::SharedPtr subscription_;
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
subscription_ = this->create_subscription(
"/test/NumMsg", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}
private:
void topic_callback(const util::msg::Num &msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%ld'", msg.num_test);
}
rclcpp::Subscription::SharedPtr subscription_;
};
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared());
rclcpp::shutdown();
return 0;
}