1. 패키지 생성
cd ~/${workspack_name}/src # cd ~/ros_ws/src
catkin_create_pkg ${package_name} roscpp rospy std_msgs # catkin_create_pkg driving_test roscpp rospy std_msgs
cd ../ && catkin_make
rospack find ${package_name} # rospack find driving_test
cd src/${package_name} # cd src/driving_test
mkdir launch
2. 파일 작성
src
에 turtle.py
를 생성한 후, 다음의 내용을 작성하고 chmod +x turtle.py
로 권한을 추가한다
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
def control(linear_x, angular_z):
global pub
msg = Twist()
msg.linear.x = linear_x
msg.linear.y = 0.0
msg.linear.z = 0.0
msg.angular.x = 0.0
msg.angular.y = 0.0
msg.angular.z = angular_z
pub.publish(msg)
def move():
rate = rospy.Rate(1)
speed = 1.0
velocity = 1.63
while not rospy.is_shutdown():
for i in range(3):
control(speed, velocity * -1)
rate.sleep()
for i in range(1):
control(speed, 0.0)
rate.sleep()
for i in range(3):
control(speed, velocity)
rate.sleep()
for i in range(1):
control(speed, 0.0)
rate.sleep()
if __name__ == "__main__":
try:
rospy.init_node('driving_node', anonymous=True)
pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
move()
except rospy.ROSInterruptException:
pass
launch
에 turtle.launch
를 생성한 후, 다음의 내용을 작성한다
<launch>
<node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node"/>
<node pkg="driving_test" type="turtle.py" name="control" output="screen"/>
</launch>
3. 노드 실행
roslaunch driving_test turtle.launch
로 노드를 실행한다