ROS环境安装与配置
熟悉ROS基本命令,并运行小海龟demo例子,通过键盘控制小海龟运动。同时用话题查看器查看两个node之间的消息传递;用C++或python写一个程序,控制小海龟走一个圆形。
一、ROS介绍
机器人操作系统(Robot Operating System, ROS)是一个应用于机器人上的操作系统,它操作方便、功能强大,特别适用于机器人这种多节点多任务的复杂场景。 因此自ROS诞生以来,受到了学术界和工业界的欢迎,如今已经广泛应用于机械臂、移动底盘、无人机、无人车等许多种类的机器人上。
二、版本选择
Ubuntu版本 | ROS版本 |
---|---|
20.04 | Noetic Ninjemys |
18.04 | Melodic Morenia |
16.04 | Kinetic Kame |
三、安装步骤
1.添加ROS软件源
sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.ustc.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main" > /etc/apt/sources.list.d/ros-latest.list'
2.添加密钥
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654
3.安装ROS
sudo apt update
sudo apt-get install ros-melodic-desktop-full
4.初始化rosdep
sudo rosdep init
rosdep update
(1)使用sudo rodep init出现sudo rosdep:找不到命令提示的错误
解决方法:
sudo apt install python-rosdep
出现ERROR: cannot download default sources list from: https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list Website may be down.的错误
cd /etc
sudo gedit hosts
在文本后面添加(ip是通过https://site.ip138.com进行输入raw.githubusercontent.com查询获取得到,任意选择一个)
ip raw.githubusercontent.com
不过这里存在网络问题,如果使用的是移动网通过上面方法也无法解决问题,最终用电信网解决。
(3)出现ERROR: error loading sources list: <urlopen error <urlopen error timed out (https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml)
尝试很多次也没有成功,最终选择采用离线安装
本地安装的方式
①下载相关文件
Github下载地址:https://github.com/ros/rosdistro
②解压文件
unzip rosdistro-master.zip -d rosdistro
③修改20-default.list文件
sudo nano /etc/ros/rosdep/sources.list.d/20-default.list
文件内容修改如下:
# os-specific listings first
yaml file:///home/xxx/rosdistro/rosdep/osx-homebrew.yaml osx
# generic
yaml file:///home/xxx/rosdistro/rosdep/base.yaml
yaml file:///home/xxx/rosdistro/rosdep/python.yaml
yaml file:///home/xxx/rosdistro/rosdep/ruby.yaml
gbpdistro file:///home/xxx/rosdistro/releases/fuerte.yaml fuerte
# newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
注意:上述 xxx 需替换为自己的用户名,如本人的用户名为: hxs
修改 sources_list.py 文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/sources_list.py
注释原 DEFAULT_SOURCES_LIST_URL 一行,然后在其后添加如下一行,注意:xxx替换
DEFAULT_SOURCES_LIST_URL = 'file:///home/xxx/rosdistro/rosdep/sources.list.d/20-default.list'
修改 rep3.py 文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/rep3.py
注释原 REP3_TARGETS_URL 一行,然后在其后添加如下一行,注意:xxx替换
REP3_TARGETS_URL = 'file:///home/xxx/rosdistro/releases/targets.yaml'
修改 init.py 文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdistro/__init__.py
注释原 DEFAULT_INDEX_URL 一行,然后在其后添加如下一行,注意:xxx替换
DEFAULT_INDEX_URL = 'file:///home/xxx/rosdistro/index-v4.yaml'
重新 rosdep update,如果结果如下图所示,则说明该步骤已经成功了
⑥ 添加 ROS 环境变量
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
⑦ 安装 rosinstall
sudo apt install python-rosinstall python-rosinstall-generator python-wstool build-essential
⑧ 第一次启动ROS时,需要进行环境变量配置,执行如下命令
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
四、ROS第一个例程——小海龟
先开启三个终端,然后分别依次输入如下命令:
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
然后将鼠标聚焦到输入最后一个命令的窗口,通过键盘的上下左右键,分别控制小王八的前进,后退,左转,右转。
五、同时用话题查看器查看两个node之间的消息传递
①话题获取:
②消息获取:
③获取消息格式:
乌龟线速度取值只有x和角速度z
因为只有前后左右四个键位控制运动
可以看到只有线速度x和角速度y
因为只有上下左右四个键
上下控制线速度x,左右控制角速度z
六、C++编码实现小海龟圆周运动.
1.创建ROS工作空间
①创建工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
②编译工作空间
cd ~/catkin_ws
catkin_make
③编译工作空间
source devel/setup.bash
④检查环境变量
echo $ROS_PACKAGE_PATH
2.创建ROS功能包
①创建功能包
cd ~/catkin_ws/src
catkin_create_pkg learning_topic std_msgs rospy roscpp geometry_msgs
②编译功能包
cd ~/catkin_ws
catkin_make
source devel/setup.bash
3.C++代码
①.创建publisher.cpp文件
在位置~/catkin_ws/src/learning_topic/src/下创建publisher.cpp文件.
②.发布话题的逻辑:
1.初始化ROS节点;
2.向ROS Master注册节点信息,包括发布的话题名和话题中的消息类型;
3.创建消息数据;
4.按照一定频率循环发布消息。
③.C++代码:
#include <sstream>
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "geometry_msgs/Twist.h"
int main(int argc, char **argv)
{
//初始化节点
ros::init(argc, argv, "publisher");
//创建节点句柄
ros::NodeHandle n;
//创建publisher,发布话题[/turtle1/cmd_vel],消息类型[geometry_msgs::Twist]
ros::Publisher chatter_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 1000);
//设置循环频率
ros::Rate loop_rate(100);
while (ros::ok())
{
//定义发布的数据
geometry_msgs::Twist msg;
//定义linear数据
geometry_msgs::Vector3 linear;
linear.x=1;
linear.y=0;
linear.z=0;
//定义angular数据
geometry_msgs::Vector3 angular;
angular.x=0;
angular.y=0;
angular.z=1;
msg.linear=linear;
msg.angular=angular;
//发布msg数据
chatter_pub.publish(msg);
//循环等待回调函数
ros::spinOnce();
//按照循环频率延时
loop_rate.sleep();
}
return 0;
}
4.添加编译选项
Python在ubuntu是可以直接执行的脚本语言,而C++是编译语言,在执行之前需要经过编译过程,因此在编译工作空间之前需要配置文件。
cd ~/catkin_ws/src/learning_topic
gedit CMakeLists.txt
在最后加上以下代码
add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${catkin_LIBRARIES})
保存退出,再进行编译命令。
cd ~/catkin_ws
catkin_make
如果也显示100% 就接着下一步,启动publisher节点控制小海龟运动.
5.启动小海龟并实现控制
roscore
#打开新终端
rosrun turtlesim turtlesim_node
#打开新终端
cd ~/catkin_ws
#下面这一步是为了保证rosrun命令能够找到相应的功能包,有可以省去这一步骤的方法,各位可以自行查找
source ~/catkin_ws/devel/setup.bash
rosrun learning_topic publisher
完成命令后可以看到小海龟以圆周运动.
七、总结
这次实验是继用键盘控制小海龟移动后的进一步实验步骤,熟悉了ROS的消息和话题的收发机制.直观上也了解了小海龟的运动机制,了解了Turtlesim以线速度和角速度为基础运动,并且可以在终端直接进行赋值从而使得小海龟以我们想要的方式运动,并且最后我们实现了编程方式控制小海龟圆周运动,对ROS也有了更进一步的了解.
八、资料参考
https://blog.csdn.net/qq_43279579/article/details/114291635
更多推荐
所有评论(0)