自定义消息
1.定义msg文件
功能包下新建 msg 目录,添加文件 Person.msg
1 2 3 4
| string name uint16 age float64 height
|
2.编辑配置文件
package.xml中添加编译依赖与执行依赖
1 2
| <build_depend>message_generation</build_depend> <exec_depend>message_runtime</exec_depend>
|
CMakeLists.txt编辑 msg 相关配置
1 2 3 4 5 6 7 8
| find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation ) # 需要加入 message_generation,必须有 std_msgs
|
1 2 3 4 5 6
| ## 配置 msg 源文件 add_message_files( FILES Person.msg )
|
1 2 3 4 5 6
| # 生成消息时依赖于 std_msgs generate_messages( DEPENDENCIES std_msgs )
|
1 2 3 4 5 6 7 8
| #执行时依赖 catkin_package( # INCLUDE_DIRS include # LIBRARIES demo02_talker_listener CATKIN_DEPENDS roscpp rospy std_msgs message_runtime # DEPENDS system_lib )
|
C++ 需要调用的中间文件(…/工作空间/devel/include/包名/xxx.h)
Python 需要调用的中间文件(…/工作空间/devel/lib/python3/dist-packages/包名/msg)
后续调用相关 msg 时,是从这些中间文件调用的
cpp实现
vscode配置
将前面生成的 head 文件路径配置进 c_cpp_properties.json 的 includepath属性:
1 2 3 4 5
| "includePath": [ "/opt/ros/noetic/include/**", "/usr/include/**", "/xxx/yyy工作空间/devel/include/**" //配置 head 文件的路径 ],
|
发布方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| /* 需求: 循环发布人的信息
*/
#include "ros/ros.h" #include "demo02_talker_listener/Person.h"
int main(int argc, char *argv[]) { setlocale(LC_ALL,"");
//1.初始化 ROS 节点 ros::init(argc,argv,"talker_person");
//2.创建 ROS 句柄 ros::NodeHandle nh;
//3.创建发布者对象 ros::Publisher pub = nh.advertise<demo02_talker_listener::Person>("chatter_person",1000);
//4.组织被发布的消息,编写发布逻辑并发布消息 demo02_talker_listener::Person p; p.name = "sunwukong"; p.age = 2000; p.height = 1.45;
ros::Rate r(1); while (ros::ok()) { pub.publish(p); p.age += 1; ROS_INFO("我叫:%s,今年%d岁,高%.2f米", p.name.c_str(), p.age, p.height);
r.sleep(); ros::spinOnce(); }
return 0; }
|
订阅方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| /* 需求: 订阅人的信息
*/
#include "ros/ros.h" #include "demo02_talker_listener/Person.h"
void doPerson(const demo02_talker_listener::Person::ConstPtr& person_p){ ROS_INFO("订阅的人信息:%s, %d, %.2f", person_p->name.c_str(), person_p->age, person_p->height); }
int main(int argc, char *argv[]) { setlocale(LC_ALL,"");
//1.初始化 ROS 节点 ros::init(argc,argv,"listener_person"); //2.创建 ROS 句柄 ros::NodeHandle nh; //3.创建订阅对象 ros::Subscriber sub = nh.subscribe<demo02_talker_listener::Person>("chatter_person",10,doPerson);
//4.回调函数中处理 person
//5.ros::spin(); ros::spin(); return 0; }
|
配置 CMakeLists.txt
需要添加 add_dependencies 用以设置所依赖的消息相关的中间文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| add_executable(person_talker src/person_talker.cpp) add_executable(person_listener src/person_listener.cpp)
add_dependencies(person_talker ${PROJECT_NAME}_generate_messages_cpp) add_dependencies(person_listener ${PROJECT_NAME}_generate_messages_cpp)
target_link_libraries(person_talker ${catkin_LIBRARIES} ) target_link_libraries(person_listener ${catkin_LIBRARIES} )
|
python实现
vscode配置
为了方便代码提示以及误抛异常,需要先配置 vscode,将前面生成的 python 文件路径配置进 settings.json
1 2 3 4 5 6 7
| { "python.autoComplete.extraPaths": [ "/opt/ros/noetic/lib/python3/dist-packages", "/xxx/yyy工作空间/devel/lib/python3/dist-packages" ] }
|
发布方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #! /usr/bin/env python """ 发布方: 循环发送消息
""" import rospy from demo02_talker_listener.msg import Person
if __name__ == "__main__": #1.初始化 ROS 节点 rospy.init_node("talker_person_p") #2.创建发布者对象 pub = rospy.Publisher("chatter_person",Person,queue_size=10) #3.组织消息 p = Person() p.name = "葫芦瓦" p.age = 18 p.height = 0.75
#4.编写消息发布逻辑 rate = rospy.Rate(1) while not rospy.is_shutdown(): pub.publish(p) #发布消息 rate.sleep() #休眠 rospy.loginfo("姓名:%s, 年龄:%d, 身高:%.2f",p.name, p.age, p.height)
|
订阅方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #! /usr/bin/env python """ 订阅方: 订阅消息
""" import rospy from demo02_talker_listener.msg import Person
def doPerson(p): rospy.loginfo("接收到的人的信息:%s, %d, %.2f",p.name, p.age, p.height)
if __name__ == "__main__": #1.初始化节点 rospy.init_node("listener_person_p") #2.创建订阅者对象 sub = rospy.Subscriber("chatter_person",Person,doPerson,queue_size=10) rospy.spin() #4.循环
|
终端下进入 scripts 执行:chmod +x *.py
配置 CMakeLists.txt
1 2 3 4 5 6 7 8
| catkin_install_python(PROGRAMS scripts/talker_p.py scripts/listener_p.py scripts/person_talker.py scripts/person_listener.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} )
|