一、安装ROS-melodic
sudo apt-get update
sudo apt-get install ros-melodic-desktop-full
sudo apt-get install ros-melodic-rqt*
在第三步的时候可能会报错:
ming@ming:~$ rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
ERROR: unable to process source [https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml]:
......
ERROR: error loading sources list:
<urlopen error <urlopen error [Errno 111] Connection refused> (https://raw.githubusercontent.com/ros/rosdistro/master/ardent/distribution.yaml)>
解决方案:
sudo apt-get update
rosdep update
二、小海龟测试
分别打开三个终端窗口输入指令:
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
三、创建Ros工作空间和功能包:
3.1 创建工作空间
1、创建工作环境
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace
2、编译工作空间
$ cd ~/catkin_ws/
$ catkin_mak
3、设置环境变量
$ source devel/setup.bash
4、检查环境变量
$ echo $ROS_PACKAGE_PATH
3.1 创建功能包
$ catkin_create_pkg [depend1] [depend2] [depend3 ]
1、创建功能包
$ cd ~/catkin_ws/src
$ catkin_create_pkg test_pkg std_msgs rospy roscpp
2、编译功能包
$ cd ~/catkin_ws
$ catkin_make
$ source ~/catkin_ws/devel/setup.bash
注:同一个工作空间下,不允许存在同名功能包;不同工作空间下,允许存在同名功能包
四、C++实现海龟运动的基本操作
4.1设置C++可执行文件链接库
使用C++编程控制小海龟时,要对CMakeLists.txt中的编译规则进行设置。这里的CMakeList.txt是在任务功能包文件下的。
1、设置需要编译的代码和生产的可执行文件。
2、设置链接库:
4.2、编译和允许可执行文件
$ cd ~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun [功能包名] [程序文件名]
4.3、环境变量设置小tips
在catkin_nake后,要设置环境变量:source devel/setup.bash
为了防止每次忘记设置环境变量,可以先将这段指令,复制在主目录下的隐藏文件:.bashrc中。方法:进入主目录,Ctrl+H,显示隐藏文件,找到并双击打开/.bashrc 拉到最后一行,添加:source /home/用户名/catkin_ws(工作空间目录)/devel/setup.bash ,保存并重新启动终端。
这样在后续执行一些需要打开多个终端窗口执行不同命令时,不用每个都去手动添加环境变量,减少了操作上可能忽略的错误。