ROS

Ubuntu16.04 ROS Kinetic - 1:1 노드 통신

donie 2020. 12. 23. 01:30

1. 관련 실습

2020/12/23 - [ROS] - Ubuntu16.04 ROS Kinetic - 1:N 노드 통신

2020/12/23 - [ROS] - Ubuntu16.04 ROS Kinetic - N:1 노드 통신

2020/12/23 - [ROS] - Ubuntu16.04 ROS Kinetic - N:N 노드 통신

 

2. 패키지 생성

msg_send 라는 이름의 패키지 생성하였다.

2020/12/22 - [ROS] - Ubuntu16.04 ROS Kinetic - 패키지 생성

 

Ubuntu16.04 ROS Kinetic - 패키지 생성

패키지 생성 1) 패키지를 담을 디렉토리로 이동. cd ~/xycar_ws/src 2) 패키지 새로 만들기 catkin_create_pkg my_pkg1 std_msgs rospy my_pkg1에는 패키지 이름을 적고 뒤의 std_msgs와 rospy는 이 패키지가 의..

donie.tistory.com

 

3. 파이썬 파일 작성

teacher.py

#!/usr/bin/env python

import rospy
from std_msgs.msg import String

rospy.init_node('teacher')

pub = rospy.Publisher('my_topic', String)

rate = rospy.Rate(2)

while not rospy.is_shutdown():
	pub.publish('call me please')
	rate.sleep()

 

student.py

#!/usr/bin/env python

import rospy
from std_msgs.msg import String

def callback(msg):
	print msg.data

rospy.init_node('student')

sub = rospy.Subscriber('my_topic', String, callback)

rospy.spin()

 

실행권한을 부여하였다.

chmod +x teacher.py student.py

 

4. launch파일 작성

m_send.launch 이름의 launch파일을 생성하였다.

<launch>
	<node pkg="msg_send" type="teacher.py" name="teacher"/>
	<node pkg="msg_send" type="student.py" name="student" output="screen"/>
</launch>

 

5. 실행

roslaunch msg_send m_send.launch

위 명령어를 실행하여 전체 프로그램을 실행한다.

스크린에 call me please가 찍히는 것을 확인하면 0.5초마다 한번씩 찍히는 것을 확인할 수 있다.

 

rqt_graph를 실행하여 노드 관계를 확인하면 다음과 같다.