1. my_msg 형식
string first_name
string last_name
int32 age
int32 score
string phone_number
int32 id_number
2. 파일 작성
remote_teacher.py
import rospy
import time
from std_msgs.msg import String
from msg_send.msg import my_msg
name = "teacher"
pub_topic = "msg_from_teacher"
sub_topic = "msg_to_teacher"
def callback(msg):
student_name = msg.last_name + ' ' + msg.first_name
curr_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
result_str = "Good morning, "+student_name+' '+curr_time
pub.publish(result_str)
rospy.init_node(name)
sub = rospy.Subscriber(sub_topic, my_msg, callback)
pub = rospy.Publisher(pub_topic, String, queue_size=1)
rospy.spin()
remote_student.py
import rospy
from std_msgs.msg import String
from msg_send.msg import my_msg
name = "student"
pub_topic = "msg_to_teacher"
sub_topic = "msg_from_teacher"
def callback(msg):
print(msg.data)
rospy.init_node(name)
pub = rospy.Publisher(pub_topic, my_msg, queue_size=1)
sub = rospy.Subscriber(sub_topic, String, callback)
rate = rospy.Rate(1)
student_data = my_msg()
student_data.first_name = "Gil-Dong"
student_data.last_name = "Hong"
student_data.age = 20
student_data.score = 100
student_data.id_number = 12345678
student_data.phone_number = "010-1234-5678"
while not rospy.is_shutdown():
pub.publish(student_data)
print("sending message...")
rate.sleep()
sr_remote.launch
<launch>
<node name="teacher" pkg="msg_send" type="remote_teacher.py" output="screen" />
<node name="student" pkg="msg_send" type="remote_student.py" output="screen" />
</launch>
3. chmod

4. 실행
$ roslaunch msg_send sr_remote.launch
$ rostopic echo msg_to_teacher
$ rostopic echo msg_from_teacher
$ rqt_graph



