## 工作环境搭建
查看环境变量的配置:
wangsan@wangsan-Y50:~$ export | grep ROS declare -x ROSLISP_PACKAGE_DIRECTORIES="" declare -x ROS_DISTRO="kinetic" declare -x ROS_ETC_DIR="/opt/ros/kinetic/etc/ros" declare -x ROS_MASTER_URI="http://localhost:11311" declare -x ROS_PACKAGE_PATH="/opt/ros/kinetic/share" declare -x ROS_ROOT="/opt/ros/kinetic/share/ros"
apt安装后,配置命令:
wangsan@wangsan-Y50:~$ source /opt/ros/kinetic/setup.bash
创建catkin工作空间:
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
编译工作空间里的所有文件:
$ cd ~/catkin_ws/
$ catkin_make
会生成build、devel,加src,一共三个文件夹。
将当前工作空间设置在ROS工作环境的最顶层:
wangsan@wangsan-Y50:~/catkin_ws$ source devel/setup.bash
查看环境变量ROS_PACKAGE_PATH是否包含我们的工作空间目录:
wangsan@wangsan-Y50:~/catkin_ws$ echo $ROS_PACKAGE_PATH
/home/wangsan/catkin_ws/src:/opt/ros/kinetic/share
ok,以上让我们的工作环境搭建完成。
## 文件系统命令
安装教程中需要的ros-tutorials程序包:
$ sudo apt-get install ros-kinetic-ros-tutorials
获得软件包的路径信息:
wangsan@wangsan-Y50:~/catkin_ws$ rospack find roscpp
/opt/ros/kinetic/share/roscpp
直接切换到软件包的路径下:
wangsan@wangsan-Y50:~/catkin_ws$ roscd roscpp wangsan@wangsan-Y50:/opt/ros/kinetic/share/roscpp$ pwd /opt/ros/kinetic/share/roscpp
roscd只能切换到环境变量ROS_PACKAGE_PATH中包含的ROS软件包的路径:
wangsan@wangsan-Y50:/opt/ros/kinetic/share/roscpp$ echo $ROS_PACKAGE_PATH /home/wangsan/catkin_ws/src:/opt/ros/kinetic/share
不同路径间用冒号分隔开来。
直接切换进软件包的子目录中:
wangsan@wangsan-Y50:~$ roscd roscpp/cmake wangsan@wangsan-Y50:/opt/ros/kinetic/share/roscpp/cmake$ pwd /opt/ros/kinetic/share/roscpp/cmake
切换到ROS保存日记文件的目录下,但没执行过任何ROS程序时,系统报错说该目录不存在:
wangsan@wangsan-Y50:/opt/ros/kinetic/share/roscpp/cmake$ roscd log
No active roscore
按软件包的名称罗列目录,而不是按绝对路径:
wangsan@wangsan-Y50:~$ rosls roscpp_tutorials
cmake launch package.xml srv
Tab自动完成输入:
wangsan@wangsan-Y50:~$ roscd roscpp_tut<<< 现在请按TAB键 >>>
## 创建/编译ROS程序包
在catkin工作空间内创建一个catkin程序包beginner_tutorials,该程序包依赖程序包std_msgs rospy roscpp:
wangsan@wangsan-Y50:~/catkin_ws/src$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
查看beginner_tutorials程序包的一级依赖:
wangsan@wangsan-Y50:~/catkin_ws/src$ rospack depends1 beginner_tutorials
roscpp
rospy
std_msgs
也可以进入程序包路径,查看package.xml:
$ roscd beginner_tutorials
$ cat package.xml
<package> ... <buildtool_depend>catkin</buildtool_depend> <build_depend>roscpp</build_depend> <build_depend>rospy</build_depend> <build_depend>std_msgs</build_depend> ... </package>
递归检测出所有的依赖包:
wangsan@wangsan-Y50:~/catkin_ws/src$ rospack depends beginner_tutorials
cpp_common
rostime
roscpp_traits
roscpp_serialization
catkin
genmsg
genpy
message_runtime
gencpp
geneus
gennodejs
genlisp
message_generation
rosbuild
rosconsole
std_msgs
rosgraph_msgs
xmlrpcpp
roscpp
rosgraph
rospack
roslib
rospy
自定义package.xml,包含
- 描述标签
- 维护者标签
- 许可标签
- 依赖项标签
wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials$ cat package.xml <?xml version="1.0"?> <package> <name>beginner_tutorials</name> <version>0.1.0</version> <description>The beginner_tutorials package</description> <maintainer email="sdgmwzh@foxmail.com">wangsan</maintainer> <license>TODO</license> <!-- <url type="website">http://wiki.ros.org/beginner_tutorials</url> --> <!-- <author email="sdgmwzh@foxmail.com">wangsan</author> --> <buildtool_depend>catkin</buildtool_depend> <build_depend>roscpp</build_depend> <build_depend>rospy</build_depend> <build_depend>std_msgs</build_depend> <run_depend>roscpp</run_depend> <run_depend>rospy</run_depend> <run_depend>std_msgs</run_depend> </package>
编译我们的软件包:
wangsan@wangsan-Y50:~/catkin_ws$ catkin_make
## ROS节点
开启ROS核心服务,运行所有ROS程序前首先要运行的命令:
$ roscore
列出运行的节点:
$ rosnode list
返回一个特定节点的信息(rosout用于收集和记录节点调试输出信息):
$ rosnode info /rosout
运行软件包内的某个节点,而不需要知道绝对路径:
$ rosrun turtlesim turtlesim_node
出现一直小王八,关掉,再重启,会出现另一个小王八。。
改变节点名称:
$ rosrun turtlesim turtlesim_node __name:=my_turtle
测试节点:
$ rosnode ping my_turtle
## ROS话题
键盘来控制turtle的运动,运行turtlesim中的另一个节点:
$ rosrun turtlesim turtle_teleop_key
rqt_graph能够创建一个显示当前系统运行情况的动态图形,首先安装rpt程序包:
$ sudo apt-get install ros-kinetic-rqt $ sudo apt-get install ros-kinetic-rqt-common-plugins
打开当前系统运行情况的动态图形:
$ rosrun rqt_graph rqt_graph
使用帮助选项查看rostopic的子命令:
$ rostopic -h
显示某个话题上发布的数据:
$ rostopic echo /turtle1/cmd_vel
查看list子命令需要的参数:
$ rostopic list -h
显示出发布和订阅的话题及其类型的详细信息:
$ rostopic list -v
查看所发布话题的消息类型:
$ rostopic type /turtle1/cmd_vel
geometry_msgs/Twist
进而查看消息的详细情况:
$ rosmsg show geometry_msgs/Twist
知道了数据类型,就可亦把数据发布到当前正在广播的话题上了,我们来让小王八按命令移动:
$ rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
-1:发布完消息后马上退出
/turtle1/cmd_vel:话题名称
geometry_msgs/Twist:消息类型
--:告诉解析器,后面的不是命令的参数选项了
让小王八一直游,游啊游:
$ rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
查看数据发布的频率:
$ rostopic hz /turtle1/pose
绘制某话题上的数据变化图形:
$ rosrun rqt_plot rqt_plot
## ROS服务和参数
显示节点提供的服务:
$ rosservice list
查看clear服务的类型:
$ rosservice type clear
调用服务:
rosservice call [service] [args]
$ rosservice call clear
查看spawn服务的类型并带參数调用:
$ rosservice type spawn| rossrv show $ rosservice call spawn 2 2 0.2 ""
查看参数服务器上有哪些参数:
$ rosparam list
修改背景颜色的红色通道并清除服务器使修改生效:
$ rosparam set background_r 150 $ rosservice call clear
显示参数服务器上的所有内容:
$ rosparam get /
将所有的参数写入params.yaml文件:
$ rosparam dump params.yaml
将yaml文件重载入新的命名空间,比如说copy空间:
$ rosparam load params.yaml copy $ rosparam get copy/background_b
使用rqt_console和rqt_logger_level 跳过
## rosed编辑文件
编辑roscpp package里的Logger.msg文件:
$ rosed roscpp Logger.msg
使用Tab键补全文件名
$ rosed [package_name] <tab>
wangsan@wangsan-Y50:~$ cd ~/catkin_ws/src/beginner_tutorials/ wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials$ ls CMakeLists.txt include launch package.xml src wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials$ mkdir msg wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials$ ls CMakeLists.txt include launch msg package.xml src wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials$ echo "int64 num" > msg/Num.msg wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials$ cd msg/ wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials/msg$ ls Num.msg wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials/msg$ echo -e "int64 num string first_name string last_name uint8 age uint32 source" > Num.msg wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials/msg$ cat Nun.msg int64 num string first_name string last_name uint8 age uint32 source
wangsan@wangsan-Y50:~/catkin_ws/devel$ rosmsg show beginner_tutorials/Num int64 num string first_name string last_name uint8 age uint32 score wangsan@wangsan-Y50:~/catkin_ws/devel$ rosmsg show Num [beginner_tutorials/Num]: int64 num string first_name string last_name uint8 age uint32 score
!
CMakelists.txt修改时,
genenrate_messages()添加需要在函数catkin_packages()之前,否则catkin_make时会报错;
!
rosmsg show beginner_tutorials/Num出一堆乱码,提示找不到我刚刚做的msg文件,echo $ROS_PACKAGE_PATH发现没有当前工作空间,难道每次开机都要source一下~/catkin_ws/devel路径下的setup.bash?
wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials$ rossrv show beginner_tutorials/AddTwoInts int64 a int64 b --- int64 sum wangsan@wangsan-Y50:~/catkin_ws/src/beginner_tutorials$ rossrv show AddTwoInts [beginner_tutorials/AddTwoInts]: int64 a int64 b --- int64 sum [rospy_tutorials/AddTwoInts]: int64 a int64 b --- int64 sum