F1tenth - Lab1 , ROSwiki-Publishers and Subscribers

Hyun Ho An·2024년 1월 18일

F1tenth-Lecture 정리

목록 보기
2/2

Object in this lecture

  • 파일 구조와 ROS의 framework
  • Publisher와 Subscriber들이 어떻게 수행되는지
  • custom message기 어떻게 수행되는지
  • CMakeList.txt, package.xml 파일들
  • 의존성(dependencies)
  • launch file, Rviz, Bagfile

ROS wiki Publisher - (python)

[전체 코드]

 1 #!/usr/bin/env python
 2 # license removed for brevity
 3 import rospy
 4 from std_msgs.msg import String
 5 
 6 def talker():
 7     pub = rospy.Publisher('chatter', String, queue_size=10)
 8     rospy.init_node('talker', anonymous=True)
 9     rate = rospy.Rate(10) # 10hz
10     while not rospy.is_shutdown():
11         hello_str = "hello world %s" % rospy.get_time()
12         rospy.loginfo(hello_str)
13         pub.publish(hello_str)
14         rate.sleep()
15 
16 if __name__ == '__main__':
17     try:
18         talker()
19     except rospy.ROSInterruptException:
20         pass

[코드 설명]

#!/usr/bin/env python : 파이썬으로 사용할 것이라는 선언

import rospy : ros에서 사용하기 위한 라이브러리

from std_msgs.msg import String : 메세지의 타입을 String으로 설정하기위한 라이브러리

 pub = rospy.Publisher('chatter', String, queue_size=10)
 rospy.init_node('talker', anonymous=True)

위 코드는 Publisher의 topic은 /chatter이고, String 메세지 유형을 사용한다고 선언한다.
queue_size=10: NEW in Roshtdro(?), subcriber에서 메세지를 빨리 받지 못하면, 대기 중인 메세지의 양을 제한한다.

rospy.init_node(NAME,...) -> 노드의 이름 설정,
anonymous=True: 노드를 생성할 때 NAME의 끝에 난수를 추가하여 노드가 고유한 이름을 가지게 한다.

rate = rospy.Rate(10) # 10hz : loop에서 sleep과 같이 대기 시간이다.

while not rospy.is_shutdown():
    hello_str = "hello world %s" % rospy.get_time()
    rospy.loginfo(hello_str)
    pub.publish(hello_str)
    rate.sleep()

rospy.loginfo(hello_str): 화면에 표시하고, 노드 log파일에 기록하며, rosout에 표시된다.

pub.publish(hello_str): 이 작업은 pub.publish(hello_str)를 불러냄으로써 "chatter" topic을 publish한다.

rate.sleep(): rate time을 유지하기 위해 추가

ROS wiki Subscriber - (python)

[전체 코드]

 1 #!/usr/bin/env python
 2 import rospy
 3 from std_msgs.msg import String
 4 
 5 def callback(data):
 6     rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
 7     
 8 def listener():
 9 
10     # In ROS, nodes are uniquely named. If two nodes with the same
11     # name are launched, the previous one is kicked off. The
12     # anonymous=True flag means that rospy will choose a unique
13     # name for our 'listener' node so that multiple listeners can
14     # run simultaneously.
15     rospy.init_node('listener', anonymous=True)
16 
17     rospy.Subscriber("chatter", String, callback)
18 
19     # spin() simply keeps python from exiting until this node is stopped
20     rospy.spin()
21 
22 if __name__ == '__main__':
23     listener()

[코드 설명]

사실 위에서 설명한 publisher와 구조가 설명할 것 없이 같다....
간단히 설명하면,
callback: 공부할거리 + 1 설명을 안해준다...
rospy.spin(): 단순히 노드가 정지되지 않을 때까지 노드를 유지시켜주는 것이다.
cpp와 다르게 subscriber의 callback에 영향을 주지 않는다.

최종적으로,
talker(Node) -> /chatter(topic) -> listener(Node)
Message: "hello world %s" % rospy.get_time() (hello_str)

############################################################################
cpp도 추가예정 사실 코드보면 cpp를 진짜 많이 쓰긴함. python은 진짜 가끔 git에서 보이는거 같기도 하고

profile
한국해양대학교/인공지능공학부

0개의 댓글