1、自定义消息类型Student
1.1 创建base_interfaces_demo包
1.2 创建Student.msg文件
string name
int32 age
float64 height
1.2 在cmakeLists.txt中增加如下语句
#增加自定义消息类型的依赖
find_package(rosidl_default_generators REQUIRED)
# 为接口文件生成源代码
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Student.msg"
)
2、发布方实现
#导入所需的包
import rclpy
from rclpy.node import Node
from base_interfaces_demo.msg import Student
#自定义节点类
class Talker(Node):
def __init__(self):
super().__init__("talker_stu_node_py")
self.count=0
self.get_logger().info("发布方已创建!(python)")
self.publisher=self.create_publisher(Student,"chatter_stu",10)
self.timer=self.create_timer(0.1,self.on_timer)
def on_timer(self):
message=Student()
mes