1、定义
在功能包的下新建msg目录 新建Person.msg文件
#Person.msg
string name
uint8 sex
uint8 age
uint8 unknown=0
uint8 male=1
uint8 female=2
image.png
保证添加这以下两行
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
image.png
CMakeLists.txt
中修改
...
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
...
find_package中添加 message_generation
catkin_package(
INCLUDE_DIRS include
LIBRARIES learning2
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
DEPENDS system_lib
)
catkin_package 添加 message_runtime
add_message_files(
FILES
Person.msg
)
add_message_files 添加 Person.msg 即创建的文件
generate_messages(
DEPENDENCIES
std_msgs
)
完成后编译
catkin_make
不能导出生成的类时 catkin_make install
https://answers.ros.org/question/105711/rospy-custom-message-importerror-no-module-named-msg/
rosmsg show Person
2、使用
#!/usr/bin/env python
# license removed for brevity
# talker.py
import rospy
from std_msgs.msg import String
from learning2.msg import Person # learning2功能包的名称
def talker():
pub = rospy.Publisher('chatter', Person, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
pub.publish(Person("xx",3,3))
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
网友评论