本文目标,史上最全,入门最简。工程链接:下载地址
这一节参考书上的例子,用gazobo仿真开源项目turtlebot3,我的环境是ubuntu16.04,kinetic版本ros。
mkdir -p ~/wanderbot_ws/src
cd ~/wanderbot_ws/src
catkin_init_workspace
cd ~/wanderbot_ws/src
catkin_create_pkg wanderbot rospy geometry_msgs sensor_msgs
wanderbot:包名
rospy:ros的python支持库
geometry_msgs sensor_msgs:依赖的消息
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包,然后把相对应的文件拖进去。
工程链接:下载地址
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
把模型拷贝到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
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
rostopic info cmd_vel
可以看到发布这个话题的发布者就是我们写的控制脚本,接受者是gazebo仿真。
rosrun wanderbot range_ahead.py
rosrun wanderbot wander.py
他将根据雷达的距离值,自主避障。
ctrl c关闭之前的全部终端。
roslaunch turtlebot3_gazebo turtlebot3_world.launch
roslaunch turtlebot3_gazebo turtlebot3_simulation.launch
roslaunch turtlebot3_gazebo turtlebot3_gazebo_rviz.launch
ctrl c关闭之前的全部终端。
roslaunch turtlebot3_gazebo turtlebot3_world.launch
roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
可以通过wxad控制前后前进速度,顺时针逆时针旋转速度,s停止
roslaunch turtlebot3_slam turtlebot3_slam.launch
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
鼠标点击地图上的位置就是你的初始位置,箭头指向就是你的机器人初始朝向。
修改成如下图所示即可:
打开简单的仿真器场景:
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下载资源这就很快,然后把缺少的包拖了过来。