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

happy_quokka·2023년 10월 16일
0

ROS

목록 보기
18/25

1. 패키지 생성

$ cd ~/xycar_ws/src/
또는
$ cs (alias로 미리 지정해놨기 때문)

$ catkin_create_pkg msg_send std_msgs rospy

2. launch 디렉토리 만들기

$ cd msg_send/
$ mkdir launch

3. 새로 만든 패키지 빌드

$ cm
또는
$ catkin_make

4. teacher.py와 student.py 작성

$ cd src/msg_send/src/
$ gedit teacher.py
  • 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')

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

rospy.spin()

5. 파이썬 파일 권한 부여

$ chmod +x *.py
또는
$ chmod +x teache.py student.py

6. launch 파일 작성 후 빌드

$ launch 디렉토리로 이동
$ gedit m_send.launch
$ cm
  • m_send.launch
<launch>
  <node pkg="msg_send" type="teacher.py" name="teacher"/>
  <node pkg="msg_send" type="student.py" name="student" output="screen"/>
</launch>

7. roslaunch 실행

$ roslaunch msg_send m_send.launch

8. 확인

$ rqt_graph

0개의 댓글