$ catkin_create_pkg msg_send std_msgs rospy
로 패키지 생성
$ mkdir launch
msg_send 아래에 런치디렉토리 생성
$ cm
으로 빌드
teacher.py 작성
#! /usr/bin/env python
import rospy
from std_msgs.msg import Int32
rospy.init_node("teacher")
pub = rospy.Publisher('my_topic', Int32)
rate = rospy.Rate(2)
count = 1
while not rospy.is_shutdown():
pub.publish(count)
count += 1
rate.sleep()
#! /usr/bin/env python
import rospy
from std_msgs.msg import Int32
def callback(msg):
print msg.data
rospy.init_node('student')
sub = rospy.Subscriber('my_topic', Int32, callback)
rospy.spin()
<launch> <node pkg="msg_send" type="teacher.py" name="teacher"/> <node pkg="msg_send" type="student.py" name="student" output="screen"/> </launch>
$ roslaunch msg_send m_send
로 실행$ rqt_graph
로 확인rospy.init_node('student', anonymous=True)
로 수정rospy.init_node('teacher', anonymous=True)
로 수정$ roscore
실행$ rosrun msg_send student.py
$ rosrun msg_send teacher.py
를 여러 번 실행<launch>
<node pkg="msg_send" type="teacher.py" name="teacher1"/>
<node pkg="msg_send" type="teacher.py" name="teacher2"/>
<node pkg="msg_send" type="teacher.py" name="teacher3"/>
<node pkg="msg_send" type="student.py" name="student1" output="screen"/>
<node pkg="msg_send" type="student.py" name="student2" output="screen"/>
<node pkg="msg_send" type="student.py" name="student3" output="screen"/>
</launch>