新建消息类型
catkin_create_pkg car_interfaces roscpp rospy std_msgs message_generation message_runtime
书写自定义的msg:
比如我写一个GlobalPathPlanningInterface.msg:
float64 timestamp #时间戳
float32[] startpoint #起点位置,先经后纬
float32[] endpoint #终点位置,先经后纬
float32[] routedata #路径集合(所有途径点的集合)
float32 process_time # 进程处理时间
接着再处理编译文件CMakeLists.txt:
add_message_files(FILESGlobalPathPlanningInterface.msg# Message2.msg
)generate_messages(DEPENDENCIESstd_msgs
)catkin_package(
# INCLUDE_DIRS include
# LIBRARIES car_interfacesCATKIN_DEPENDS message_generation message_runtime roscpp rospy std_msgs
# DEPENDS system_lib
)
修改package.xml文件:
添加上:
<build_depend>message_runtime</build_depend><exec_depend>message_generation</exec_depend>
这样就定义好了。
rosmsg show msg包/msg类型
新建一个功能包
catkin_create_pkg planning roscpp rospy std_msgs
catkin_make # 编译一次即可
接着就能直接新建py写了,比cpp简单太多了。
global_path_planning.py文件:
#!/usr/bin/env python3
#coding=utf-8import rospy
from car_interfaces.msg import GlobalPathPlanningInterfaceif __name__ == "__main__":rospy.init_node("chao_node") # 节点名称rospy.logwarn("我的枪去而复返")pub = rospy.Publisher("global_path_planning_data", GlobalPathPlanningInterface, queue_size = 10) # 话题名称rate = rospy.Rate(10)while not rospy.is_shutdown():rospy.loginfo("我要开始刷屏了") #发一次显示一个,不是实际的消息,只是一个infomsg = GlobalPathPlanningInterface()msg.timestamp = 2.7msg.startoint = [1.0, 2.0, 3.0]msg.endpoint = []msg.routedata = []msg.process_time = 3.1pub.publish(msg)rate.sleep()
注意:
(这样的话其实也可以直接再创建的时候添加,就不用改这两个文件了)
catkin_create_pkg self_start roscpp rospy std_msgs car_interfaces
如果没有动car_interfaces,那就添加就好了
修改CMakeLists.txt:
find_package:
增加car_interfaces
修改package.xml:
<build_depend>car_interfaces</build_depend>
<exec_depend>car_interfaces</exec_depend>
测试效果还可以