ROS

ROS - 1:N, N:1, N:N 통신

스테디음 2022. 3. 1. 22:11

다양한 통신 시나리오

사진 출처: 프로그래머스 자율주행 데브코스 강의자료

 

노드를 여러 개 띄울 때

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

만약 노드의 이름이 모두 같다면? 이전에 실행된 노드가 저절로 꺼진다.

 

그렇다면

$ rosrun msg_send teacher_1.py
$ rosrun msg_send teacher_2.py
$ rosrun msg_send teacher_3.py

 

이렇게 실행시킬 때마다 이름을 바꾸어주어야 하는가? → 그럼 너무 귀찮다!

 

 

그럼 어떻게?

노드의 init 함수에서 anonymous = True 값을 넣어주면 실행시킬 때 노드의 이름을 바꿔주지 않아도 노드 이름이 자동으로 설정된다. (anonymous 기본값은 false이기 때문에 저 설정을 꼭 해주어야 한다!)

rospy.init_node('student', anonymous=True)
$ rosrun msg_send student.py
$ rosrun msg_send student.py
$ rosrun msg_send student.py

 

이렇게 해주면 똑같은 파일을 여러 번 띄워도 노드 이름이 자동으로 각각 다르게 설정된다.

 

 

Launch 파일을 이용해서 roslaunch 명령으로 여러 노드를 띄울 수 있다.

노드 설정에서 name="OOO" 부분을 다르게 설정해주면 된다.

<node pkg="msg_send" type="teacher_int.py" name="teacher1"/>
<node pkg="msg_send" type="teacher_int.py" name="teacher2"/>
<node pkg="msg_send" type="teacher_int.py" name="teacher3"/>

 

 

1:N 통신

launch 파일만 바꿔서

<launch>
	<node pkg="msg_send" type="teacher.py" name="teacher"/>
	<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>

 

 

N:1 통신

<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="student" output="screen"/>
</launch>

 

 

N:N 통신

<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>

'ROS' 카테고리의 다른 글

ROS - Custom Message  (0) 2022.03.01
ROS - 노드 통신을 위한 패키지 만들기  (0) 2022.03.01
ROS - roslaunch  (0) 2022.03.01
ROS - turtlesim, publisher, subscriber  (0) 2022.03.01
ROS 설치 및 환경설정  (0) 2022.02.28