티스토리 뷰

1. 관련 실습

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

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

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

 

2. 파이썬 파일 작성

위 실습에서 실습한 코드를 복사하여 약간씩 수정한다.

 

teacher_int.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()

 

student_int.py

#!/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()

 

3. 실행

하나의 코드로 여러 개의 노드를 연결하려면, 원래 각 노드의 이름을 다르게 해야 한다.

ex.

rosrun msg_send teacher_int-1.py
rosrun msg_send teacher_int-2.py
rosrun msg_send teacher_int-3.py

 

하지만 rospy.init_node('노드 이름', anonymous=True)로 anonymous속성을 True로 설정하면 노드 이름이 자동 설정되어 같은 노드를 여러번 실행해도 된다.

ex.

rosrun msg_send teacher_int.py
rosrun msg_send teacher_int.py
rosrun msg_send teacher_int.py

 

또, launch파일을 이용해서 roslaunch명령으로 여러 노드를 띄울 수 있다. ( 이 방법 이용

<launch>
	<node pkg="msg_send" type="teacher_int.py" name="teacher"/>
	<node pkg="msg_send" type="student_int.py" name="student1" output="screen"/>
	<node pkg="msg_send" type="student_int.py" name="student2" output="screen"/>
	<node pkg="msg_send" type="student_int.py" name="student3" output="screen"/>
</launch>
cm

 

roslaunch msg_send m_send_1n.launch

실행 결과는 아래와 같다.

1 1 1은 보이지 않지만, 2부터 계속 잘 찍히는 것을 확인할 수 있다. -> 나중에 설명.

3개의 subscriber노드가 각각 스크린에 출력하기 때문에 스크린에 3개씩 찍히는 것을 볼 수 있다.

 

rqt_graph로 노드 관계를 확인하면 아래와 같다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
글 보관함