[ROS] 노드 통신 프로그래밍 2

happy_quokka·2023년 10월 16일
0

ROS

목록 보기
19/25

1:N 통신

1. msg_send/src 디렉토리로 이동

$ cd xycar_ws/src/msg_send/src/

2. teacher_int.py 생성

  • 생성 후 파일명이 녹색 -> 실행 권한 있음
$ cp teacher.py teacher_int.py
$ gedit 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 = count + 1
  rate.sleep()

3. student_int.py 파일 만들기

$ cp student.py student_int.py
$ gedit 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')

pub = rospy.Subscriber('my_topic', Int32, callback)

rospy.spin()

4. launch 디렉토리로 이동

$ cd ..
$ cd launch/

5. m_send_1n.launch 파일 생성

$ cp m_send.launch m_send_1n.launch
$ gedit 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>

6. 빌드

$ cm

7. roslaunch

$ roslaunch msg_send m_send_1n.launch

8. 확인

N:1 통신

1. launch 디렉토리로 이동

$ cs
$ cd msg_send/launch/

2. m_send_n1.launch 파일 생성

$ cp m_send_1n.launch m_send_n1.launch
$ gedit 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="student" output="screen"/>
</launch>

3. 실행

$ roslaunch msg_send m_send_n1.launch

4. 확인

$ rqt_graph

N:N 통신

1. launch 디렉토리로 이동

$ cs
$ cd msg_send/launch/

2. launch 파일 생성

$ cp m_send_n1.launch m_send_nn.launch
$ gedit 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>

3. roslaunch 실행

$ roslaunch msg_send m_send_nn.launch

4. 확인

0개의 댓글