이전 예제의 launch file을 다시 살펴볼까요??
<?xml version="1.0" encoding="UTF-8"?>
<launch>
<!-- launch first exmaple -->
<node pkg="topic_tutorial" type="cmdvel_pub.py" name="drive_forward" output="screen">
</node>
</launch>
이제 우리는, 패키지, node, topic, launch file에 대해 알고 있기 때문에 위 내용을 이해할 수 있게 되었습니다.
그럼, 드디어 코딩으로 넘어가서, example1_publisher.py를 파헤쳐볼까요?
#! /usr/bin/env python
"""
Twist Publisher example
referenced from wiki.ros.org
url: <http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29>
"""
import time
import rospy
from geometry_msgs.msg import Twist
rospy.init_node("drive_forward")
pub = rospy.Publisher("/cmd_vel", Twist, queue_size=1)
r = rospy.Rate(1) # 1 Hz
forward = Twist()
stop = Twist()
forward.linear.x = 0.5
stop.linear.x = 0.0
start_time = time.time()
rospy.loginfo("==== DriveForward node Started, move forward during 5 seconds ====\\n")
while not rospy.is_shutdown():
if time.time() - start_time < 5.0:
pub.publish(forward)
else:
rospy.logwarn(" 5 seconds left, Stop!! ")
pub.publish(stop)
break
r.sleep()
github 파일에 주석은 제외하였습니다.
#! /usr/bin/env python
The #! magic, details about the shebang/hash-bang mechanism on various Unix flavours