받은 토픽을 가공해서 보내기

image

코드 작성

  • remote_teacher.py
#!/usr/bin/env python

import rospy
from msg_send.msg import my_msg
from std_msgs.msg import String

import time

def call_back(data):
	st_name = data.last_name + ' ' + data.first_name
	curr_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
	st_name2 = "Good morning, " + st_name + " " + curr_time

	
	pub.publish(st_name2)


rospy.init_node("remote_teacher",anonymous = True)

pub = rospy.Publisher("msg_from_xycar", String, queue_size =1)
sub = rospy.Subscriber("msg_to_xycar", my_msg, call_back)

rospy.spin()
  • remote_student.py
#!/usr/bin/env python

import rospy
from msg_send.msg import my_msg
from std_msgs.msg import String

done = False
def call_back(data):
    print(data.data)
    done = True

rospy.init_node('remote_student', anonymous = True)
pub = rospy.Publisher('msg_to_xycar', my_msg, queue_size = 1)
rospy.Subscriber('msg_from_xycar', String, call_back)
rate = rospy.Rate(1)

msg = my_msg()
msg.first_name = "Sanghun"
msg.last_name = "Park"
msg.age = 26
msg.score = 100
msg.id_number = 12345678
msg.phone_number = "010-0000-0000"

while not rospy.is_shutdown() and not done:
    pub.publish(msg)
    print("sending message...")
    rate.sleep()
  • remote.launch
<launch>
	<node pkg ="msg_send" type="remote_teacher.py" name="teacher" output="screen"/>
	<node pkg ="msg_send" type="remote_student.py" name="student" output="screen"/>
</launch>

실행결과

  • $ rqt_graph

image

  • $ rostopic echo /msg_to_xycar을 입력해 msg_to_xycar 토픽의 내용을 확인함
  • $ rostopic echo /msg_from_xycar을 입력해 msg_from_xycar 토픽의 내용을 확인함
  • student에서 보낸 토픽을 가공해서 teacher에서 보내는 것을 확인할 수 있음

image