~/xycar_ws/src/msg_send/msg/my_msg.msg 에 다음과 같이 작성
string first_name string last_name int32 age int32 score string phone_number int32 id_number
... <exec_depend>rospy</exec_depend> <exec_depend>std_msgs</exec_depend> # 아래 두 줄 추가 <build_depend>message_generation</build_depend> <exec_depend>message_runtime</exec_depend> ...
... find_package(catkin REQUIRED COMPONENTS rospy std_msgs message_generation # 이부분 추가 ) ... # 주석 해제후 my_msg.msg 추가 add_message_files( FILES my_msg.msg ) ... # 주석 해제 generate_messages( DEPENDENCIES std_msgs ) ... catkin_package( CATKIN_DEPENDS message_runtime # 이부분 추가 # INCLUDE_DIRS include # LIBRARIES msg_send # CATKIN_DEPENDS rospy std_msgs # DEPENDS system_lib )
$ catkin_make
후 $rosmsg show my_msg
확인jsg@jsg-ubuntu:~/xycar_ws$ rosmsg show my_msg [msg_send/my_msg]: string first_name string last_name int32 age int32 score string phone_number int32 id_number
#! /usr/bin/env python import rospy from msg_send.msg import my_msg rospy.init_node('msg_sender', anonymous=True) pub = rospy.Publisher('msg_to_xycar', my_msg) msg = my_msg() msg.first_name = "gildon" msg.last_name = "Hong" msg.id_number = 20041003 msg.phone_number = "010-8990-3003" rate = rospy.Rate(1) while not rospy.is_shutdown(): pub.publish(msg) print("sending message") rate.sleep()
#! /usr/bin/env python import rospy from msg_send.msg import my_msg def callback(msg): print("1. Name : ", msg.last_name + msg.first_name) print("2. ID : ", msg.id_number) print("3. Phone Number : ", msg.phone_number) rospy.init_node('msg_receiver', anonymous=True) sub = rospy.Subscriber('msg_to_xycar', my_msg, callback) rospy.spin()