1. 程式人生 > >通過TurtleBot學navigation與ROS的筆記

通過TurtleBot學navigation與ROS的筆記

TurtleBot執行的幾條關鍵語句

#啟動模擬環境
roslaunch turtlebot_gazebo tuetlebot_world.launch
#鍵盤控制
roslaunch turtlebot_teleop keyboard_teleop.launch
#rviz
roslaunch turtlebot_rviz_launchers view_robot.launch

測試Kinect(模擬環境下不需要Kinnect,日後再補充)

http://learn.turtlebot.com/2015/02/01/8/

echo $TURTLEBOT_3D_SENSOR
# Output: kinect

如果輸出不是kinect,則需要更改該變數

echo "export TURTLEBOT_3D_SENSOR=kinect" >> .bashrc

顯示深度圖和RGB圖

rosrun image_view image_view image:=/camera/depth/image_raw

此處的:=意為覆蓋賦值。其他的還有
?=若左邊變數未被賦值則賦值
+=在左邊變數後面加上新字串

簡單例子 Draw a square

#!/usr/bin/env python

'''
Copyright (c) 2015, Mark Silliman
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'''
# An example of TurtleBot 2 drawing a 0.4 meter square. # Written for indigo import rospy from geometry_msgs.msg import Twist from math import radians class DrawASquare(): def __init__(self): # initiliaze rospy.init_node('drawasquare', anonymous=False) # What to do you ctrl + c
rospy.on_shutdown(self.shutdown) self.cmd_vel = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10) # 5 HZ r = rospy.Rate(5); # create two different Twist() variables. One for moving forward. One for turning 45 degrees. # let's go forward at 0.2 m/s move_cmd = Twist() move_cmd.linear.x = 0.2 # by default angular.z is 0 so setting this isn't required #let's turn at 45 deg/s turn_cmd = Twist() turn_cmd.linear.x = 0 turn_cmd.angular.z = radians(45); #45 deg/s in radians/s #two keep drawing squares. Go forward for 2 seconds (10 x 5 HZ) then turn for 2 second count = 0 while not rospy.is_shutdown(): # go forward 0.4 m (2 seconds * 0.2 m / seconds) rospy.loginfo("Going Straight") for x in range(0,10): self.cmd_vel.publish(move_cmd) r.sleep() # turn 90 degrees rospy.loginfo("Turning") for x in range(0,10): self.cmd_vel.publish(turn_cmd) r.sleep() count = count + 1 if(count == 4): count = 0 if(count == 0): rospy.loginfo("TurtleBot should be close to the original starting position (but it's probably way off)") def shutdown(self): # stop turtlebot rospy.loginfo("Stop Drawing Squares") self.cmd_vel.publish(Twist()) rospy.sleep(1) if __name__ == '__main__': try: DrawASquare() except: rospy.loginfo("node terminated.")

模擬環境下可以實現較為精確的速度控制。此例程中需要注意的

#執行一次r.sleep,ROS會控制睡眠時間為上次呼叫到這次呼叫剛好為0.2s。
        r = rospy.Rate(5);
        r.sleep()

# What to do you ctrl + c    
        rospy.on_shutdown(self.shutdown)

建立地圖

http://learn.turtlebot.com/2015/02/03/8/

roslaunch turtlebot_gazebo turtlebot_world.launch

roslaunch turtlebot_gazebo gmapping_demo.launch

roslaunch turtlebot_rviz_launchers view_navigation.launch