통신방식
1: N
N:1
N:N
##노드를 여러개 띄울 때
#1. 하나의 코드로 여러개의 노드를 연결하려면 각 노드의 이름을 달리해야함.
but
1) 노드의 init 함수에서 anonymous=True값을 넣어주면 노드이름이 자동 설정됨
2) eg. rospy.init_node("student", anonymous=True) =>default is False
노드1
$ rosrun msg_send teacher_int-1.py
$ rosrun msg_send teacher_int-2.py
$ rosrun msg_send teacher_int-3.py
노드2
$ rosrun msg_send student_int.py
$ rosrun msg_send student_int.py
$ rosrun msg_send student_int.py
init_node 인자에서 anonymouse 를 True라 하면 하나의 코드로 여러개의 노드를 연결하려면, 코드들의 이름이 달라야하는데 anonymouse를 True라하면 코드이름 변경할 필요없이 자동으로 ROS에서 노드이름을 변경시켜준다. 그래서 소스코드들의 이름을 바꿀 필요가없다.
#2 Launch 파일 이용해서 roslaunch 명령으로 여러 노드를 띄울 수 있음.
1) 노드 설정에서 name="ooo"부분을 다르게 설정
2) eg) <node pkg="msg_send" type="teacher_int.py" name="teacher1"/>
파일이 동일하면 , 소스코드파일 안에 anonymouse True설정 할 필요없이 launch 파일 내에서 name으로 변경할 수 있다.
(작업이 수월)
<launch>
<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"/>
<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>
#3. 1:N 통신
1)Launch 파일만 바꿔서 (m_send_1n.launch)
<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>
실습
teacher 를 teacher_int 로 복사해서 teacher_int 를 하기와 같이 수정
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
rospy.init_node("teacher")
pub = rospy.Publisher("msg_to_student",Int32)
rate = rospy.Rate(2)
count = 1
while not rospy.is_shutdown():
pub.publish(count)
count+=1
rate.sleep()
2) Student도 student_int 파일로 복사해서 아래와 같이 코드 작성
#!/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("msg_to_student", INT32, callback)
rospy.spin()
3) 1:n 의미하는 launch_1n.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>
빌드 하고 실행하자
실행하면 아래와 같이 나온다.
$ roslaunch msg_send m_send_1n.launch
참고 이전의 코드들 보려면 history 명령어
!번호 입력 시 명령어 입력됨
또는 !! 만 명령어 입력 시 이전 실행했던거 재실행.
rqt_graph 로 보자
## N:1 통신
#1. m_send_n1.launch 파일 생성 후 아래와 같이 코드 수정
<launch>
<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"/>
<node pkg="msg_send" type="student_int.py" name="student1" output="screen"/>
</launch>
#2 실행하기
roslaunch msg_send m_send_n1.launch
##N:N 통신
Launch 파일만 바꾸자 (m_send_nn.launch)
<launch>
<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"/>
<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>
실습)
해당 경로 가서
코드
<launch>
<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"/>
<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>
실행
$ roslaunch msg_send m_send_nn.launch
rqt_graph 열면
N:N 통신완료
'프로그래머스 > ROS' 카테고리의 다른 글
ros노드 통신프로그래밍은 이걸로 대체하기 (나만의 메시지 자료구조 ) // 핵심// 이걸로 기록지 만들기 (0) | 2022.11.09 |
---|---|
ROS 노드 통신 프로그래밍3(나만의 메시지 정리) (0) | 2022.11.08 |
ROS 노드 통신 프로그래밍(1) (0) | 2022.11.08 |
week2-2 코드분석 (과제2) (0) | 2022.11.08 |
week2-2 8자주행 (과제1) (0) | 2022.11.08 |