-
_ROS1_custom_msgROS/ROS1 2023. 10. 4. 18:39
기존의 메시지를 사용할 수 있지만, 내가 직접 메시지를 만들 수 있다.
참고사이트
http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv
ROS/Tutorials/CreatingMsgAndSrv - ROS Wiki
Note: This tutorial assumes that you have completed the previous tutorials: using rosed. Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of you
wiki.ros.org
이번에는 다음과 같이 msg 폴더를 생성하고 커스텀 메시지를 만들고 사용해 볼 것이다.
그리고 CMakeList.txt와 package.xml에도 추가 작업이 필요하다.
Custom_Msg
$ roscd msg_send $ mkdir msg $ gedit my_msg.msg
my_msg.msg
string last_name int32 age int32 score string phone_num int32 id_number
package.xml에 추가할 내용.
<build_depend>message_generation</build_depend> <exec_depend>message_runtime</exec_depend>
CMakeLists.txt에 추가할 내용. (주석으로 된 부분은 풀면 된다.)
이후 catkin_make 명령어로 빌드를 진행한다.
다음 명령어로 my_msg를 확인할 수 있다.
$ rosmsg show my_msg # 나의 msg확인 $ ~/catkin_ws/devel/lib/python2.7/dist_packages/msg_send/msg # 해당 디렉토리에 생성된 msg확인이 가능하다.
만들어진 커스텀 메시지는 어디서든 사용가능하다. (다른 패키지에서도 사용할 수 있다.)
my_msg_sender.py
#!/usr/bin/env python import rospy from msg_send.msg import my_msg rospy.init_node('my_msg_sender', anonymous=True) pub = rospy.Publisher('my_topic', my_msg) msg = my_msg() msg.first_name = "chanic" msg.last_name = "Mok" msg.id_number = 20203209 msg.phone_num = "010-1234-5678" rate = rospy.Rate(1) while not rospy.is_shutdown(): pub.publish(msg) print("sending msg") rate.sleep()
my_msg_receiver.py
#!/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_num) rospy.init_node('my_msg_receiver', anonymous=True) sub = rospy.Subscriber('my_topic', my_msg, callback) rospy.spin()
원래 노드의 이름은 다른 이름과 겹치지않고 유일해야 한다.
노드를 생성할 때, anonymous=True는 이를 해제하는 역할을 하며, 복수의 노드를 실행할 수 있게 해준다.
my_msg.launch
<launch> <node pkg="msg_send" type="my_msg_sender.py" name="my_msg_sender"/> <node pkg="msg_send" type="my_msg_receiver.py" name="my_msg_reciver" output="screen"/> </launch>
실행 결과:
'ROS > ROS1' 카테고리의 다른 글
_ROS1_노드 실습(2) (1) 2023.10.17 _ROS1_노드 실습(1) (0) 2023.10.17 _ROS1_node_communication (0) 2023.10.04 _ROS1_roslaunch (0) 2023.10.03 _ROS1_package_pub&sub (0) 2023.10.02