Develop/ROS

๐Ÿ”ฅ ์ž์œจ์ฃผํ–‰์„ ์œ„ํ•œ ROS ์ž…๋ฌธ(2) : Publisher ์™€ Subscriber

proggg 2024. 10. 5. 13:08
728x90

๋…ธ๋“œ๋“ค๊ฐ„ ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š”๊ฒƒ์ด ROS ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ด๋–ป๊ฒŒ ์ฃผ๊ณ  ๋ฐ›๋Š”์ง€์— ๋Œ€ํ•ด ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ๋ฐœํ–‰์ž( Publisher ) ์™€ ์ˆ˜์‹ ์ž ( Subscriber ) ๋ผ๋Š”๊ฒƒ์„ ์–ธ๊ธ‰ํ•˜์—ฌ ์•Œ๊ณ ์žˆ๋‹ค.

1. Publisher

publisher ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋ฐœํ–‰ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด๋•Œ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•  ์ ์€ Publisher ๊ฐ€ ๋ฐœํ–‰ํ•˜๋Š” ๋ฉ”์‹œ์ง€๋Š” ์ˆ˜์‹ ์ž๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด Publisher ํ˜ผ์ž ์žˆ๋‹ค๋ฉด ์•„๋ฌด๋„ ๋“ฃ๊ณ ์žˆ์ง€ ์•Š์ง€๋งŒ ํ˜ผ์ž ๋– ๋“ค๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ง€๋Š”๊ฒƒ์ด๋‹ค.

2. Subscriber

Publisher ์˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋“ค์–ด์ฃผ๋Š”๊ฒƒ์ด ๋ฐ”๋กœ Subscriber ์ด๋‹ค. Subscriber ๋Š” ๊ฐ™์€ ROS ๋„คํŠธ์›Œํฌ ์ƒ์— ์กด์žฌํ•˜๋Š” ํŠน์ • *ํ† ํ”ฝ ๋ช…์„ ๊ฐ€์ง„ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœํ–‰๋˜๋Š”๊ฒƒ์„ ๋ณด๊ณ  ์žˆ๊ณ  Subscriber ๊ฐ€ ์ฃผ์‹œํ•˜๊ณ  ์žˆ๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœํ–‰๋œ ๊ฒฝ์šฐ์— ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ†ตํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•œ๋‹ค .

์–ด๋–ป๊ฒŒ ๋งŒ๋“œ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

3. Make Publisher and Subscriber

atkin_ws
โ”œโ”€โ”€ build
โ”œโ”€โ”€ devel
โ””โ”€โ”€ src
    โ””โ”€โ”€ CMakeLists.txt

workspace ๋Š” ์œ„์™€ ๊ฐ™๋‹ค.

$ cd ~/catkin_ws/src/
$ roscreate-pkg pns_test
$ cd ../
$ catkin_make

Workspace/src ์•ˆ์— pns_test ๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

3-1. Publisher

๊ฐ„๋‹จํ•œ Publisher ๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Publisher ํด๋ž˜์Šค๋Š” rospy ์— ์ด๋ฏธ ์ •์˜๋˜์–ด์žˆ๋‹ค.

from rospy import Publisher, Rate, init_node

Rate ํด๋ž˜์Šค๋Š” ๋…ธ๋“œ์˜ ์‹คํ–‰ ์ฃผ๊ธฐ๋ฅผ ์กฐ์ ˆํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค์ด๊ณ , init_node ํ•จ์ˆ˜๋Š” rospy ์—์„œ ๋…ธ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

if __name__ == '__main__':
    init_node("publisher_test")

    publisher = Publisher()

 

์šฐ๋ฆฌ๋Š” ์ง€๊ธˆ ๋‹น์žฅ์€ ์šฐ์„  std_msg ๋ฅผ ์‚ฌ์šฉํ•˜์ž.

from std_msgs.msg import Int64
publsher = Publisher(name = "simple_pub", data_class=Int64, queue_size =1 )

์ด๊ฑธ๋กœ publisher_test ๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ์™€ ๊ทธ ๋…ธ๋“œ์—์„œ ๋ฐœํ–‰ํ•˜๋Š” 64bit Int ์™€ queue size 1 ์„ ๊ฐ€์ง€๋Š” simple_pub topic ์„ publisher ๋ฅผ ์ž‘์„ฑํ•œ๊ฒƒ์ด๋‹ค.

ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ๊ณ„์† ํ•ด์„œ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค˜์•ผ ํ•œ๋‹ค.

import rospy

rospy.spin() ## ๋ฌดํ•œ๋ฃจํ”„๋ผ๋Š” ๋œป.
$ roscore && rosrun pns_test app.py

## rosrun <pkg_name> <node_name>

Roscoe ๋Š” ROS Master Node ๋กœ ํ•œ๋ฒˆ์— ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ด€๋ฆฌ์ž Node ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ๋ช…๋ น์–ด์ž„.

๋…ธ๋“œ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด 
rqt_graph
๋ฅผ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋…ธ๋“œ์™€ ํ† ํ”ฝ๊ฐ„์˜ ์—ฐ๊ฒฐ๊ด€๊ณ„๋ฅผ ์‹œ๊ฐํ™”ํ•ด์„œ ๋ณด์—ฌ์ค€๋‹ค.

๋˜ํ•œ rostopic ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ROS ๋„คํŠธ์›Œํฌ์ƒ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋ฉ”์‹œ์ง€ ํ† ํ”ฝ์„ ํ™•์ธํ• ์ˆ˜์žˆ์Œ.

$ rostopic list -v

Published topics:
 * /rosout [rosgraph_msgs/Log] 1 publisher
 * /simple_pub [std_msgs/Int64] 1 publisher
 * /rosout_agg [rosgraph_msgs/Log] 1 publisher

Subscribed topics:
 * /rosout [rosgraph_msgs/Log] 1 subscriber

์„ฑ๊ณต์ ์œผ๋กœ, std_msgs.msg.Int64 ํƒ€์ž…์œผ๋กœ, simple_pub์ด๋ผ๋Š” ํ† ํ”ฝ๋ช…์˜ Publisher๊ฐ€ ๋“ฑ๋ก๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ, ์ •์˜ํ•œ Publisher๋ฅผ ์ด์šฉํ•˜์—ฌ, ์‹ค์ œ๋กœ ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐœํ–‰ํ•ด๋ด…์‹œ๋‹ค. ๋ฉ”์„ธ์ง€๋Š” Publisher ๊ฐ์ฒด์˜ publish ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐœํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

random ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ, 0๋ถ€ํ„ฐ 255๊นŒ์ง€์˜ ๋žœ๋คํ•œ ๊ฐ’์„ 1์ดˆ ์ฃผ๊ธฐ๋กœ ๋ฐœํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

#!/usr/bin/python

import rospy
from random import randint
from std_msgs.msg import Int64

if __name__ == '__main__':
    rospy.init_node("publisher_test")

    publisher = rospy.Publisher(
        name="simple_pub", data_class=Int64, queue_size=1)

    r = rospy.Rate(1)
    while not rospy.is_shutdown():

        msg = Int64()
        msg.data = randint(0, 255)

        rospy.loginfo(msg)

        publisher.publish(msg)

        r.sleep()

์œ„์™€ ๊ฐ™์€ ๋…ธ๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

$ rosrun pns_test app.py
[INFO] [1651948710.958126]: data: 186
[INFO] [1651948711.959265]: data: 174
[INFO] [1651948712.959405]: data: 8
[INFO] [1651948713.959293]: data: 106
[INFO] [1651948714.959313]: data: 56
[INFO] [1651948715.959301]: data: 188
[INFO] [1651948716.959352]: data: 167
[INFO] [1651948717.959304]: data: 27
[INFO] [1651948718.959255]: data: 139
[INFO] [1651948719.959348]: data: 11
[INFO] [1651948720.959383]: data: 46
[INFO] [1651948721.959318]: data: 71
[INFO] [1651948722.959257]: data: 87
...

๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฅผ rostopic์„ ์ด์šฉํ•˜์—ฌ ํ™•์ธํ•ด๋ณธ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

$ rostopic echo /simple_pub
data: 174
---
data: 8
---
data: 106
---
data: 56
---
data: 188
---
data: 167
---
data: 27
---
data: 139
---
data: 11
---
data: 46
---
data: 71
---
data: 87
---
...

์ •์ƒ์ ์œผ๋กœ ์ •์ˆ˜ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐœํ–‰ํ•˜๋Š” Publisher๊ฐ€ ์ž‘์„ฑ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€์˜ ๊ฒฐ๊ณผ๋ฅผ rqt๋กœ ํ™•์ธํ•ด๋ณด๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

publisher_test๋ผ๋Š” ๋…ธ๋“œ์—์„œ, simple_pub์ด๋ผ๋Š” ํ† ํ”ฝ์ด ๋ฐœํ–‰๋˜์–ด, rostopic_... ๋ผ๋Š” ๋…ธ๋“œ์—์„œ ์ด๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ rostopic_... ๋…ธ๋“œ๋Š” rostopic echo์ž…๋‹ˆ๋‹ค.

3-1. Subscriber

์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐœํ–‰ํ•˜๋Š” ๊ฒƒ์— ์„ฑ๊ณตํ•˜์˜€๊ณ , ๋‹ค์Œ์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๋ฐœํ–‰๋œ ๋ฉ”์„ธ์ง€๋ฅผ ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ด…๋‹ˆ๋‹ค.

์šฐ์„ , ๊ธฐ์กด์— ์ž‘์„ฑํ•œ ํŒŒ์ผ์˜ ์ด๋ฆ„์„ pub.py๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , ์ƒˆ๋กœ sub.py ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

/home/***/catkin_ws/src/pns_test/
โ”œโ”€โ”€ CMakeLists.txt
โ”œโ”€โ”€ Makefile
โ”œโ”€โ”€ mainpage.dox
โ”œโ”€โ”€ manifest.xml
โ””โ”€โ”€ src
    โ”œโ”€โ”€ pub.py
    โ””โ”€โ”€ sub.py

sub.py ํŒŒ์ผ์—๋Š” pub.py๊ณผ ๊ฐ™์€ ๋ผˆ๋Œ€๋ฅผ ๊ณต์œ ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

#!/usr/bin/python

import rospy
from std_msgs.msg import Int64

if __name__ == '__main__':
    rospy.init_node("sublisher_test")

    r = rospy.Rate(1)
    while not rospy.is_shutdown():
        r.sleep()

์ดํ›„, ๋ฉ”์ธ ํ•จ์ˆ˜์— Subscriber ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ด์ค๋‹ˆ๋‹ค.

subscriber = rospy.Subscriber()

Subscriber ํด๋ž˜์Šค๋Š” ์ƒ์„ฑ์ž๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.

name, data_class, callback, callback_args, queue_size, buff_size, tcp_nodelay

Publisher์™€ ์œ ์‚ฌํ•˜๊ฒŒ, name์€ ๊ตฌ๋…ํ•  ํ† ํ”ฝ๋ช…, data_class๋Š” ํ•ด๋‹น ํ† ํ”ฝ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , Publisher์™€ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์„ ๋ณด์ด๋Š” ๋ถ€๋ถ„์€ ๋ฐ”๋กœ callback์ž…๋‹ˆ๋‹ค. callback ๋ณ€์ˆ˜๋Š”, ํ† ํ”ฝ์ด ๋ฐœํ–‰๋˜๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๋•Œ, ์ž‘๋™ํ•  ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ํ•จ์ˆ˜๋ฅผ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ํ˜•ํƒœ๋กœ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ argument๋Š” ๊ตฌ๋…ํ•œ ๋ฉ”์„ธ์ง€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

data = None

def callbackFunction(msg):
    global data
    data = msg

    rospy.loginfo(data)

Subscriber ๊ฐ์ฒด๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๊ณ  ์‹คํ–‰์‹œํ‚จ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

subscriber = rospy.Subscriber(
        name="simple_pub", data_class=Int64, callback=callbackFunction)
$ roscore && rosrun pns_test pub.py && rosrun pns_test sub.py

[INFO] [1651949917.660658]: data: 7
[INFO] [1651949918.663203]: data: 27
[INFO] [1651949919.663436]: data: 68
[INFO] [1651949920.658246]: data: 30
[INFO] [1651949921.658043]: data: 172
[INFO] [1651949922.658009]: data: 193
[INFO] [1651949923.658300]: data: 166
...

์„ฑ๊ณต์ ์œผ๋กœ Subscriber๊ฐ€ ๋™์ž‘ํ•จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ, ์›ํ•˜๋Š” callback ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํ›„์ฒ˜๋ฆฌ ๋“ฑ์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. ๋.

์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•˜์—ฌ, python์„ ์ด์šฉํ•˜์—ฌ, ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ์˜ Publisher์™€ Subscriber์„ ์ •์˜, ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐœํ–‰ํ•˜๊ณ , ๊ตฌ๋…ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

ROS๋ฅผ ์ด์šฉํ•œ ๋กœ๋ด‡ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ผ ํ•˜๋Š” ๊ฒƒ์€, ๊ฒฐ๊ตญ ์ด๋Ÿฐ ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐœํ–‰ํ•˜๊ณ  ๊ตฌ๋…ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์ตœ์†Œ ๋‹จ์œ„๋ณ„๋กœ ๋‚˜๋ˆ ์„œ ํ•˜๋‚˜์˜ ๊ฑฐ๋Œ€ํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ด๋ฃจ๋„๋ก ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

[๋ฐœ์ทŒ]

https://h17.notion.site/ROS-2-Publisher-Subscriber-b99a13eb14d34dea8f25b604f541d540

 

์ž์œจ์ฃผํ–‰์„ ์œ„ํ•œ ROS ์ž…๋ฌธ(2) : Publisher ์™€ Subscriber | Notion

๋…ธ๋“œ๋“ค๊ฐ„ ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š”๊ฒƒ์ด ROS ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ด๋–ป๊ฒŒ ์ฃผ๊ณ  ๋ฐ›๋Š”์ง€์— ๋Œ€ํ•ด ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ๋ฐœํ–‰์ž( Publisher ) ์™€ ์ˆ˜์‹ ์ž ( Subscriber ) ๋ผ๋Š”๊ฒƒ์„ ์–ธ๊ธ‰ํ•˜์—ฌ ์•Œ๊ณ ์žˆ๋‹ค.

h17.notion.site

 

728x90