前言
本文目標,史上最全,入門最簡。工程鏈接:下載地址
這一節參考書上的例子,用gazobo仿真開源項目turtlebot3,我的環境是ubuntu16.04,kinetic版本ros。
(1)創建工作區以及包
創建wanderbot_ws工作區
mkdir -p ~/wanderbot_ws/src
cd ~/wanderbot_ws/src
catkin_init_workspace
創建wanderbot包
cd ~/wanderbot_ws/src
catkin_create_pkg wanderbot rospy geometry_msgs sensor_msgs
wanderbot:包名
rospy:ros的python支持庫
geometry_msgs sensor_msgs:依賴的消息
(2)安裝過程
安裝turtlebot3以及仿真環境
cd wanderbot_ws/src
git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
提示:如果下載速度太慢可以下載我的csdn包,然後把相對應的文件拖進去。
工程鏈接:下載地址
安裝gmapping包
gampping必裝不然後面slam自助導航會報錯,如果你不想實現slam可以跳過這一步。這裏給出兩種方法安裝。
從源碼安裝的方法:
cd wanderbot_ws/src
git clone https://github.com/ros-perception/openslam_gmapping.git
git clone https://github.com/ros-perception/slam_gmapping.git
git clone https://github.com/ros-planning/navigation.git
git clone https://github.com/ros/geometry2.git
git clone https://github.com/ros-planning/navigation_msgs.git
直接安裝的方法:
sudo apt-get install ros-kinetic-gmapping
這裏我選擇的是從源碼安裝,直接安裝的方法我電腦定位不了軟件包,然後更新了我的軟件庫之後有關ros的幾個源sudo apt-get update極慢,是超級超級超級超級爆炸慢,給我整吐了。
編譯
cd ~/wanderbot_ws
catkin_make
這裏如果你選擇安裝了gampping包而且是從源碼安裝的,不出意外編譯完了之後一片大紅,哈哈哈哈開心嗎。
不過問題不大,都是編譯器版本的小問題。
首先修改CMakeLists.txt:
vi ~/wanderbot_ws/src/CMakeLists.txt
添加這句話:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
如圖第六行
然後不出意外還有報錯【error】‘isnan’ was not declared in this scope。
我這裏是~/wanderbot_ws/src/turtlebot3_simulations/turtlebot3_fake/src下的turtlebot3_fake.cpp這個文件報錯,進去之後右鍵選擇gedit打開,ctrl+f查找isnan。然後在前面加上std::。
修改完後重新編譯
cd ~/wanderbot_ws
catkin_make
rosdep install --from-paths src -i -y
source ~/wanderbot_ws/devel/setup.bash
(3)仿真
準備
把模型拷貝到gazebo中:
下面兩個二選一:
仿真burger:
mkdir -p ~/.gazebo/models/
cp -r ~/wanderbot_ws/src/turtlebot3_simulations/turtlebot3_gazebo/models/turtlebot3_burger ~/.gazebo/models/
echo "export TURTLEBOT3_MODEL=burger" >> ~/.bashrc
echo "source ~/wanderbot_ws/devel/setup.bash" >>~/.bashrc
source ~/.bashrc
仿真waffle:
mkdir -p ~/.gazebo/models/
cp -r ~/wanderbot_ws/src/turtlebot3_simulations/turtlebot3_gazebo/models/turtlebot3_waffle ~/.gazebo/models/
echo "export TURTLEBOT3_MODEL=waffle" >> ~/.bashrc
echo "source ~/wanderbot_ws/devel/setup.bash" >>~/.bashrc
source ~/.bashrc
啓動仿真
啓動仿真環境:
roslaunch turtlebot3_gazebo turtlebot3_world.launch
(4)腳本控制
準備過程:
cd ~/wanderbot_ws/src/wanderbot/src
vi range_ahead.py
vi red_light_green_light.py
vi wander.py
chmod 777 range_ahead.py
chmod 777 red_light_green_light.py
chmod 777 wander.py
粘貼下面代碼進去。
紅燈倒車綠燈前進腳本:red_light_green_light.py
#!/usr/bin/env python
# coding = utf-8
import rospy
from geometry_msgs.msg import Twist
cmd_vel_pub=rospy.Publisher('cmd_vel',Twist,queue_size=1)
rospy.init_node('red_light_green_light')
red_light_twist=Twist()
green_light_twist=Twist()
deriving_forward=False
green_light_twist.linear.x=0.5 #綠燈前進
red_light_twist.linear.x=-0.5 #紅燈後退
light_change_time=rospy.Time.now()+rospy.Duration(3)
rate=rospy.Rate(10)
while not rospy.is_shutdown():
if deriving_forward :
cmd_vel_pub.publish(green_light_twist)
else:
cmd_vel_pub.publish(red_light_twist)
if light_change_time < rospy.Time.now():
deriving_forward= not deriving_forward
light_change_time= rospy.Time.now()+rospy.Duration(3)
rate.sleep()
顯示前方距離腳本:range_ahead.py
#!/usr/bin/env python
# coding = utf-8
import rospy
from sensor_msgs.msg import LaserScan
def scan_callback(msg):
range_ahead = msg.ranges[len(msg.ranges)/2]
print "range ahead: %0.1f" % range_ahead
rospy.init_node('range_ahead')
scan_sub = rospy.Subscriber('scan', LaserScan, scan_callback)
rospy.spin()
自動避障腳本:wander.py
#!/usr/bin/env python
# coding = utf-8
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
def scan_callback(msg):
global range_ahead
range_ahead = msg.ranges[len(msg.ranges)/2]
print(range_ahead)
range_ahead = 0
rospy.init_node('pub_speed')
scan_sub = rospy.Subscriber('scan',LaserScan,scan_callback)
cmd_vel_pub=rospy.Publisher('cmd_vel',Twist,queue_size=1)
speed = Twist()
rate = rospy.Rate(10)
mode = 0
while not rospy.is_shutdown():
if range_ahead>1:
speed.linear.x = -0.5
speed.angular.z = 0
cmd_vel_pub.publish(speed)
elif range_ahead<0.5:
speed.linear.x = 0.5
speed.angular.z = 0
cmd_vel_pub.publish(speed)
else:
speed.linear.x=0
speed.angular.z=0.5
cmd_vel_pub.publish(speed)
rate.sleep()
運行紅綠燈腳本:
rosrun wanderbot red_light_green_light.py
可以看到小車一前一後的運動。
檢查現在發佈的話題
rostopic list
可以看到由cmd_vel話題來控制小車的速度。
查看被聲明的話題
rostopic info cmd_vel
可以看到發佈這個話題的發佈者就是我們寫的控制腳本,接受者是gazebo仿真。
運行測距腳本:
rosrun wanderbot range_ahead.py
運行自動避障腳本:
rosrun wanderbot wander.py
他將根據雷達的距離值,自主避障。
(5)自主導航
ctrl c關閉之前的全部終端。
啓動環境地圖
roslaunch turtlebot3_gazebo turtlebot3_world.launch
啓動激光雷達自主避障
roslaunch turtlebot3_gazebo turtlebot3_simulation.launch
啓動rviz可視化
roslaunch turtlebot3_gazebo turtlebot3_gazebo_rviz.launch
(6)slam建圖導航
ctrl c關閉之前的全部終端。
啓動環境地圖
roslaunch turtlebot3_gazebo turtlebot3_world.launch
啓動鍵盤控制
roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
可以通過wxad控制前後前進速度,順時針逆時針旋轉速度,s停止
開啓激光雷達壁障
roslaunch turtlebot3_slam turtlebot3_slam.launch
啓動slam建圖
roslaunch turtlebot3_slam turtlebot3_slam.launch
這時候打開rviz和鍵盤控制的終端窗口,確保你的鼠標光標在鍵盤控制的終端裏。
然後通過wasdx這幾個按鍵控制把地圖建立完整
感覺整個地圖完整了即可。
保存地圖
rosrun map_server map_saver -f ~/map
這將地圖存在了~/目錄下面
圖中的map.yaml文件和map.pgm文件。
利用建好的地圖導航
除了gazebo關閉其他的終端然後打開rviz:
roslaunch turtlebot3_navigation turtlebot3_navigation.launch map_file:=$HOME/map.yaml
這時候可以看到雷達掃面的邊界和地圖的邊界有偏差。
利用2D pose Estimate修改位置
鼠標點擊地圖上的位置就是你的初始位置,箭頭指向就是你的機器人初始朝向。
修改成如下圖所示即可:
利用2D Nav Goal導航
(7)打開其他仿真場景
打開簡單的仿真器場景:
roslaunch turtlebot3_fake turtlebot3_fake.launch
啓動空世界地圖:
roslaunch turtlebot3_gazebo turtlebot3_empty_world.launch
啓動家庭環境地圖:
cp -r ~/catkin_ws/src/turtlebot3_simulations/turtlebot3_gazebo/models/turtlebot3_house ~/.gazebo/models/
roslaunch turtlebot3_gazebo turtlebot3_house.launch
第一次加載可能有點慢,如果長時間加載不出來可以試試這個方法:
在.gazebo文件下新建model文件夾,.gazebo文件隱藏在home下,ctrl+h可以看到,然後下載這個鏈接的包https://bitbucket.org/osrf/gazebo_models/downloads/,解壓到model下,然後就ok了,再打開gazebo
原文鏈接:https://blog.csdn.net/puqian13/article/details/90635885
總結
武漢疫情嚴重,畢設無法回校在硬件上做,所以選擇了仿真,希望能給大家提供一點幫助,記錄下自己的學習也好方便查閱。《ros機器人編程實踐》這本書挺適合入門的,但是他用的是ubuntu14.04好像,ros版本是indigo在許多使用上有很大的差異,而且這本書是外文翻譯過來的,裏面太多錯了。。。對着書上的步驟一步一步做是不可能實現的,這也是很不友好的地方。最後,武漢加油,中國加油!
參考
Ubuntu16.04+Ros Kinetic+TurtleBot3仿真搭建教程
在Ubuntu16.04 + ROS kinetic環境下安裝TurtleBot3
Ubuntu16.04+Ros Kinetic+TurtleBot3仿真搭建教程
參考書籍:《ros機器人編程實踐》
可能出現的問題以及解決方案
問題一
從我的csdn資源下載好工程後,在工作區下catkin_make報錯:
/home/xmy/wanderbot_ws/src/navigation/map_server/src/image_loader.cpp:43:27: fatal error: SDL/SDL_image.h: No such file or directory
compilation terminated.
解決方案:
百度查了下SDL是Ubuntu的庫,所以新裝的系統可能沒下載這個。
參考:參考鏈接
sudo apt-get install libsdl1.2-dev
安裝附加依賴:
sudo apt-get install libsdl-image1.2-dev
sudo apt-get install libsdl-mixer1.2-dev
sudo apt-get install libsdl-ttf2.0-dev
sudo apt-get install libsdl-gfx1.2-dev
問題二
如果最開始幾步git clone下載github東西速度太慢可以下載我的csdn的資源,然後把相對應的包拷貝進去。
發現一個很奇怪的問題,因爲昨天裝東西把電腦弄死機了,重啓ubuntu沒了ui界面所以重裝了ubuntu,然後正好根據自己寫的這篇博客全來過了一遍,下載自己的csdn包,然後在工作區catkin_make發現報了很多錯,說我的ros少了很多類似SDL/SDL_image.h的頭文件,然後我查看安裝ros時候給的安裝項目提示明明都有這些包呀。所以沒辦法只能再從git上下載來安裝過,然後github下載屬實太慢了幾k每秒。。。所以直接從csdn下載資源這就很快,然後把缺少的包拖了過來。