ROS环境安装与配置、
一、ROS的安装
本人使用的是Ubuntu18.04版本,下载的ROS系统为Melodic。而不同的Ubuntu版本要安装不同的ROS系统,否则会报错。
1、添加ROS 源
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
2、添加密钥
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654
3、安装
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
(2)出现ERROR: error loading sources list: <urlopen error <urlopen error timed out (https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml)
解决办法:
1、在GitHub上下载文件: https://github.com/ros/rosdistro
2、解压
unzip rosdistro-master.zip -d rosdistro
3、修改20-default.list文件
sudo nano /etc/ros/rosdep/sources.list.d/20-default.list
将以下内容修改
#os-specific listings first
yaml file:///home/tc/ros/rosdistro/rosdep/osx-homebrew.yaml osx
#generic
yaml file:///home/tc/ros/rosdistro/rosdep/base.yaml
yaml file:///home/tc/ros/rosdistro/rosdep/python.yaml
yaml file:///home/tc/ros/rosdistro/rosdep/ruby.yaml
gbpdistro file:///home/tc/ros/rosdistro/releases/fuerte.yaml fuerte
#newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
将上面的 tc换成自己的ID。
4、再修改source_list.py文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/sources_list.py
然后修改以下内容将文本中内容替换:
DEFAULT_SOURCES_LIST_URL = 'file:///home/lyy/ros/rosdistro/rosdep/sources.list.d/20-default.list'
5、然后修改rep3.py文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/rep3.py
替换一下内容:
REP3_TARGETS_URL = 'file:///home/lyy/ros/rosdistro/releases/targets.yaml'
6、修改__init__.py文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdistro/__init__.py
替换一下内容:
DEFAULT_INDEX_URL = 'file:///home/lyy/ros/rosdistro/index-v4.yaml'
最后
运行
rosdep update
7、设置环境变量
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
8、安装rosinstall
sudo apt-get install python-rosinstall python-rosinstall-generator python-wstool build-essential
二、小海龟例程
1、在主目录下建立一个新的文件夹命名为rounddr
2、然后打开,再建立一个文件夹,命名为src
3、打开,点击打开终端,
4、输入
catkin_init_workspace
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
5、编译环境
在rounddr文件中打开终端,输入
catkin_make
$ cd ~/catkin_ws/
$ catkin_make
6、编写程序
①.打开终端,在catkin_ws/src路径下新建my_turtle_package包。
cd catkin_ws/src
catkin_create_pkg my_turtle_package rospy roscpp
②.在my_turtle_package/src 中创建cpp文件
cd my_turtle_package/src/
touch draw_circle.cpp
③.打开draw_circle.cpp,编写画圆代码。
gedit draw_circle.cpp
将下面的画圆程序复制到draw_circle.cpp中。
#include "ros/ros.h"
#include<geometry_msgs/Twist.h> //运动速度结构体类型 geometry_msgs::Twist的定义文件
int main(int argc, char *argv[])
{
ros::init(argc, argv, "vel_ctrl"); //对该节点进行初始化操作
ros::NodeHandle n; //申明一个NodeHandle对象n,并用n生成一个广播对象vel_pub
ros::Publisher vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
//vel_pub会在主题"/cmd_vel"(机器人速度控制主题)里广播geometry_msgs::Twist类型的数据
ROS_INFO("draw_circle start...");//输出显示信息
while(ros::ok())
{
geometry_msgs::Twist vel_cmd; //声明一个geometry_msgs::Twist 类型的对象vel_cmd,并将速度的值赋值到这个对象里面
vel_cmd.linear.x = 2.0;//前后(+-) m/s
vel_cmd.linear.y = 0.0; //左右(+-) m/s
vel_cmd.linear.z = 0.0;
vel_cmd.angular.x = 0;
vel_cmd.angular.y = 0;
vel_cmd.angular.z = 1.8; //机器人的自转速度,+左转,-右转,单位是rad/s
vel_pub.publish(vel_cmd);
ros::spinOnce();//调用此函数给其他回调函数得以执行(比例程未使用回调函数)
}
return 0;
}
④.终端(terminal)中继续输入下面的命令:
cd ~/catkin_ws/src/my_turtle_package
gedit CMakeLists.txt
⑤.在CMakeLists.txt文件中找到##Declare a C++ executable,在这一行的前面添加如下内容:
add_executable(draw_circle src/draw_circle.cpp)
target_link_libraries(draw_circle ${catkin_LIBRARIES})
⑥.保存并退出CMakeLists.txt文件。然后在terminal中继续输入如下命令进行编译:
cd ~/catkin_ws/
catkin_make
编译成功
最后结果如下: