์•„์ฃผ ๊ฐ„๋‹จํ•œ ros publisher๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž!

rospy ์„ค๋ช…

rospy๋Š” ๋‹ค๋ฅธ C++๋กœ๋œ ํŒจํ‚ค์ง€์™€๋Š” ๋‹ฌ๋ฆฌ CMakeList.txt๋‚˜ package.xml์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ์–ด๋Š ๊ฒฝ๋กœ์—์„œ๋“  ROS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ include ํ•  ์ˆ˜ ์žˆ๊ณ , ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์ฃผ์ž

bashํŒŒ์ผ ์ˆ˜์ •

gedit์œผ๋กœ bashํŒŒ์ผ์„ ์˜คํ”ˆํ•ด์ค€๋‹ค

$ gedit ~/.bashrc

์•„๋ž˜ ์ค„์„ ์ ๊ณ  ์ €์žฅ ํ›„ ์ฐฝ์„ ๋‹ซ๋Š”๋‹ค

source /opt/ros/melodic/setup.bash

์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ์ˆ˜์ •๋œ bashํŒŒ์ผ์„ ์ ์šฉํ•ด์ค€๋‹ค

$ source ~/.bashrc


Simple ROS Publisher

1. ํด๋” ์ƒ์„ฑ

ํŒŒ์ผ์„ ๋งŒ๋“ค ํด๋”๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  ๊ทธ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜์ž

$ mkdir ~/rospy_tutorial
$ cd ~/rospy_tutorial

2. Publisher.py ํŒŒ์ผ ์ƒ์„ฑ

publisher๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค

$ gedit pub.py

3. ์ „์ฒด์ ์ธ ์ฝ”๋“œ

์ „์ฒด์ ์ธ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ํ•˜๋‚˜ํ•˜๋‚˜ ๋‚˜๋ˆ ์„œ ๋ณด์ž

In pub.py,

#! /usr/bin/env python
import rospy
from std_msgs.msg import String

rospy.init_node('topic_pub_node')

pub = rospy.Publisher('bigbigpark', String, queue_size=1)
msg = String()
r = rospy.Rate(1)

if __name__ == '__main__':
  msg = 'Hi! My name is bigbigpark'
  
  while not rospy.is_shutdown():
    pub.publish(msg)
    r.sleep()

์—ฌ๊ธฐ๊นŒ์ง€๋งŒ ๋ณด๊ณ ๋„ ์ฝ”๋“œ์˜ ๊ตฌ์กฐ๊ฐ€ ์ดํ•ด๋˜๋Š” ์‚ฌ๋žŒ์€ ๋ฐ”๋กœ 5๋ฒˆ์œผ๋กœ ๋„˜์–ด๊ฐ€์ž

4. ์ฝ”๋“œ ๋‚˜๋ˆ ์„œ ๋ณด๊ธฐ

์ € ๋ฌธ์žฅ์„ ์…”๋ฑ…์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ ์ž์„ธํ•œ ์„ค๋ช…์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜์ž

#! /usr/bin/env python


rospy ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค
std_msgs๋ผ๋Š” ๋ฉ”์„ธ์ง€ ํŒจํ‚ค์ง€์—์„œ String์ด๋ผ๋Š” ํ—ค๋” ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค

import rospy
from std_msgs.msg import String


rospy์— ๊ด€ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— ์ œ์ผ ๋จผ์ € ์ˆ˜ํ–‰๋˜์–ด์•ผํ•˜๋Š” init_node์ด๋‹ค
์ด๋ ‡๊ฒŒ ์„ ์–ธํ•˜๋ฉด topic_pub_node๋ผ๋Š” ์ด๋ฆ„์˜ ๋…ธ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ๋œป์ด๋‹ค

rospy.init_node('topic_pub_node')


Publisher๋ฅผ ์„ ์–ธํ•ด์ค€๋‹ค
์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ์ธ์ž๋Š” ์•„๋ž˜์ชฝ์— ๋‚˜์™€์žˆ๋Š” ์ˆœ์„œ๋Œ€๋กœ ํ† ํ”ฝ๋ช…, ํƒ€์ž…, ํ ์‚ฌ์ด์ฆˆ ์ด๋‹ค
๋‹ค์‹œ ๋งํ•ด์„œ bigbigpark์ด๋ผ๋Š” ์ด๋ฆ„์˜ String ํƒ€์ž…์˜ ํ† ํ”ฝ์„ publishํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์ด๊ณ , ๊ทธ ๋•Œ ํ ์‚ฌ์ด์ฆˆ๋Š” 1๋กœ ํ•˜๊ฒ ๋‹ค๋Š” ๋œป์ด๋‹ค
์ด ๊ธ€์—์„œ๋Š” queue_size์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค๋ฃจ์ง€ ์•Š๊ฒ ๋‹ค

pub = rospy.Publisher('bigbigpark', String, queue_size=1)


Publisher๋ฅผ ์„ ์–ธํ–ˆ์œผ๋ฉด ๊ฑฐ๊ธฐ ์•ˆ์— ๋‹ด์•„์ค„ ๋ฉ”์„ธ์ง€๋ฅผ ์„ ์–ธํ•ด์ค˜์•ผ ํ•œ๋‹ค
์ด๋ฒˆ ์˜ˆ์ œ์—์„œ๋Š” String ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์ชฝ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•ด์ค€๋‹ค
์ฐธ๊ณ ๋กœ String ๊ฐ์ฒด์— ์–ด๋–ค ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋Š๋ƒ๋Š” std_msgs/String Message ์—ฌ๊ธฐ์„œ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , ํ„ฐ๋ฏธ๋„ ์ฐฝ์—์„œ rosmsg info std_msgs/String ์œผ๋กœ๋„ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค
์ถ”๊ฐ€๋กœ ๋ฉ”์„ธ์ง€ ๋ฐœํ–‰ ์ฃผ๊ธฐ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ 1 Hz๋กœ ์„ค์ •ํ•ด์ค€๋‹ค

msg = String()
r = rospy.Rate(1)


main๋ฌธ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑ์„ ํ–ˆ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค

  1. msg์— ์ž„์˜์˜ ๋ฌธ์ž์—ด์„ ์ €์žฅ
  2. msg๋ฅผ publish
  3. 1 Hz ๋งˆ๋‹ค ๋ฃจํ”„๊ฐ€ ๋™์ž‘

rospy.is_shutdown()์€ ๋…ธ๋“œ ๋‚ด๋ถ€์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋– ์„œ ๋น„์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๊ฐ€ ๋˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ Ctrl+C๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•œ๋‹ค

if __name__ == '__main__':
  msg = 'Hi! My name is bigbigpark'
  
  while not rospy.is_shutdown():
    pub.publish(msg)
    r.sleep()


5. ์‹คํ–‰ํ•˜๊ธฐ

ํŒŒ์ผ์€ ์ €์žฅํ•˜๊ณ  ํ„ฐ๋ฏธ๋„ ์ฐฝ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•ด๋ณด์ž

$ roscore
$ python pub.py


6. ๊ฒฐ๊ณผ

์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ํ˜„์žฌ ๋ฐœํ–‰์ค‘์ธ topic์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

$ rostopic list

์•„๋ž˜์ชฝ๊ณผ ๊ฐ™์ด /bigbigpark์ด๋ผ๋Š” ํ† ํ”ฝ๋ช…์ด ์ถœ๋ ฅ๋œ๋‹ค

/bigbigpark
/rosout
/rosout_agg

echo๋ช…๋ น์–ด๋กœ ๋ณผ ๋•Œ๋„ ์ž˜ ๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค

$ rostopic echo /bigbigpark

Leave a comment