자율주행을 위한 ROS 시작하기(2)

signer do·2021년 6월 17일
1

자율주행

목록 보기
2/2

ROS programming

1. publisher node 만들기(python)

출처 : http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29

1) 선언하기

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

첫번째 줄은 Python script를 실행가능하게 해준다.
두번째 줄은 Ros node를 사용하기 위함이고
그 다음은 publishing할 때 std_msgs/String의 message type을 사용하기 위함이다.

2) 함수

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()
  • ropsy의 Publisher는 1)'chatter'라는 topic의 name으로, 2)'String'이라는 message type으로 3) subscriber가 빨리 받지 않을 때 queue할 message 양을 의미한다.
    이렇게 pub라는 변수가 publisher가 되었다.

  • ropsy.init_node( ) 함수는 rospy가 정보를 가질 때까지 rospy에게 node의 이름을 말해주는 중요한 역할이다. 이것은 ROS Master와 communication을 시작하게 해준다.
    이제 node의 이름은 talker 이다. 이 때 node이름은 "/" slash가 포함한 것이 아닌 그저 base_name을 넣어줌. anoynous가 True라는 것은 node가 임의의 랜덤 number가 NAME에 추가되어 unique한 name을 갖게 해준다.

  • rospy.Rate() 함수는 10hz의 속도로 publish 하는 것이다. 이렇게 rospy에 해당 publisher가 등록되게 된다.

  • rospy가 shutdown되기 전까지 hello_str에 data를 pub이라는 Publisher가 내보내게 된다. rate.sleep은 그만큼 기다린다는 뜻이다.

3) main함수

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

2. subsriber node 만들기(python)

출처 : http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29

1) 선언하기

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

첫번째 줄은 Python script를 실행가능하게 해준다.
두번째 줄은 Ros node를 사용하기 위함이고
그 다음은 subscribing할 때 std_msgs/String의 message type을 사용하기 위함이다.

2) 함수

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
    
def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # name are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber("chatter", String, callback)

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

publisher와 다르게 init을 한 후 -> rospy.Subscriber 만들기

  • ropsy.init_node( ) 함수는 rospy가 정보를 가질 때까지 rospy에게 node의 이름을 말해주는 중요한 역할이다. 이것은 ROS Master와 communication을 시작하게 해준다.
    이제 node의 이름은 'chatter' 이다.
    anoynous가 True라는 것은 node가 임의의 랜덤 number가 NAME에 추가되어 unique한 name을 갖게 해준다. 이로써 이름이 같은 두 node가 launch됐을 때, 이전 것이 kicked off된다. unique한 name을 사용하면 여러 개의 똑같은 이름의 listener가 동시에 작동할 수 있다.

  • ropsy의 Subsriber는 1)'chatter'라는 topic의 name으로, 2)'String'이라는 message type으로 3) callback함수는 message를 받으면 호출할 함수이다.

여기서는 listener() 함수 자체가 subsriber가 되었다.

3) main함수

if __name__ == '__main__':
    listener()

2. code compile하기 위한

CMakeList.txt에서 catkin_install_python()함수를 호출해야한다.
즉 아래의 코드를 추가하여 python코드를 catkin_make할 수 있도록 해준다.
이때 scripts/talker.py와 scripts/listener.py 두 node 파이썬 file을 인자로 넣어준다.

catkin_install_python(PROGRAMS scripts/talker.py scripts/listener.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
profile
Don't hesitate!

0개의 댓글