1.常见命令汇总
小海龟的自由运动仿真
1.一个终端打开ros管理器
roscore
2.另一个终端打开ros的海龟节点,run一个新节点
rosrun turtlesim turtulesim_node
3添加一个控制命令输入
rosrun turtlesim turtle_teleop_key
4 查看总体运行的节点情况
rqt_graph
可以看到由2个node组成,由键盘的控制端teleop_turtle
通过topic
即/turtle/cmd_vel 发送先关控制指令到 turtlesim
来控制显示画面里面乌龟的游动.(左右控制专项,上下控制前进后退)
同时也记录一下常用的其他命令:
(1)rosnode
rosnode is a command-line tool for printing information about ROS Nodes.
打印节点信息
Commands:
rosnode ping test connectivity to node
rosnode list list active nodes
rosnode info print information about node
rosnode machine list nodes running on a particular machine or list machines
rosnode kill kill a running node
rosnode cleanup purge registration information of unreachable nodes
例如查看turtlesim这个节点的相关信息:
ztfmars@GL503:~$ rosnode info /turtlesim
--------------------------------------------------------------------------------
Node [/turtlesim]
Publications:
* /rosout [rosgraph_msgs/Log]
* /turtle1/color_sensor [turtlesim/Color]
* /turtle1/pose [turtlesim/Pose]
Subscriptions:
* /turtle1/cmd_vel [geometry_msgs/Twist]
Services:
* /clear
* /kill
* /reset
* /spawn
* /turtle1/set_pen
* /turtle1/teleport_absolute
* /turtle1/teleport_relative
* /turtlesim/get_loggers
* /turtlesim/set_logger_level
contacting node http://GL503:36561/ ...
Pid: 15418
Connections:
* topic: /rosout
* to: /rosout
* direction: outbound
* transport: TCPROS
* topic: /turtle1/cmd_vel
* to: /teleop_turtle (http://GL503:33687/)
* direction: inbound
* transport: TCPROS
(2)rostopic
rostopic is a command-line tool for printing information about ROS Topics.
可以用来打印和发布相关话题内容
Commands:
rostopic bw display bandwidth used by topic
rostopic delay display delay of topic from timestamp in header
rostopic echo print messages to screen
rostopic find find topics by type
rostopic hz display publishing rate of topic
rostopic info print information about active topic
rostopic list list active topics
rostopic pub publish data to topic
rostopic type print topic or field type
例如发布一条让海龟运动的指令(其实输入完了 …cmd_vel按下连续2次tab可以自动不全):
rostopic pub /turtle1/cmd_vel -r 10 geometry_msgs/Twist "linear:
x: 0.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0"
其中linear
表示线速度,angular
表示转动角度,-r
表示一直发布(不加的话,就是只发布一次)后面是频率即 10次/s. ros里面默认的速度都是 m/s, 角度都是 rad/s
(3) rosservice
ros启动的相关服务的显示和控制
Commands:
rosservice args print service arguments
rosservice call call the service with the provided args
rosservice find find services by service type
rosservice info print information about service
rosservice list list active services
rosservice type print service type
rosservice uri print service ROSRPC uri
Type rosservice <command> -h for more detailed usage, e.g. 'rosservice call -h'
例如在指定位置生成新海龟
rosservice call /spawn "x: 10.0
y: 8.0
theta: 0.0
name: 'turtule_new'"
(4)rosbag
Usage: rosbag <subcommand> [options] [args]
主要是用来储存和记录信息和操作数据
A bag is a file format in ROS for storing ROS message data. The rosbag command can record, replay and manipulate bags.
Available subcommands:
check Determine whether a bag is playable in the current system, or if it can be migrated.
compress Compress one or more bag files.
decompress Decompress one or more bag files.
filter Filter the contents of the bag.
fix Repair the messages in a bag file so that it can be played in the current system.
help
info Summarize the contents of one or more bag files.
play Play back the contents of one or more bag files in a time-synchronized fashion.
record Record a bag file with the contents of specified topics.
reindex Reindexes one or more bag files.
For additional information, see http://wiki.ros.org/rosbag
( 5 ) rosrun
Usage: rosrun [–prefix cmd] [–debug] PACKAGE EXECUTABLE [ARGS]
rosrun will locate PACKAGE and try to find
an executable named EXECUTABLE in the PACKAGE tree.
If it finds it, it will run it with ARGS.
用来启动一个安装包下面可执行文件的命令,如果找到的话,就会送入ARGS参数.
2.工作空间
- 结构:
src -> c++源码所在位置;
build-> 编译过程的中间文件所在位置;
devel-> 开发过程中的中间文件或者可执行文件所在位置;
install -> 最终发行的可执行文件所在的位置,可能会devel有重叠;
- 创建工作空间
注意:
catkin_init_workspace
:
初始化工作空间,是在src
之中执行的,执行后会生成源文件的cmakelist.txt;
catkin_make
:
编译命令是在根目录
文件下执行的,即~/catkin_ws
, 执行后会在根目录生成build
,devel
文件夹及其内部的编译文件;
catkin_make install
:
最后的生成执行文件过程的执行,也是在根目录
下,会生成install
文件夹的话;
source devel/setup.bash
:
让所有ROS环境变量生效
- 创建功能包
注意:
创建功能包的过程都是在src
文件夹之中,会在src
文件夹之中生成功能包的文件夹.里面也是包括include,src,cmakelist.txt,package.xml
的一整套;
catkin_make
编译过程都是在根目录
之中的.
另外,每一个功能包里面都是存在cmakelist.txt
(编译规则和内容定义) 和packages.xml
(编译依赖项,可以后期通过手动添加的)
3.publist话题发布
话题模型:
接上一章节内容:
(1)在建立工作空间之后,创建工作包.
注意添加上所使用的依赖项,例如本例子中需要使用ros的c++包,消息发送,定位信息,turtle节点信息
,所以就写上了roscpp, std_msg, geometry_msgs,turtlesim
(2)创建发布者代码
将相关代码命名为velocity_publisher.cpp
, 放置在ros文件夹根目录/src/learning_topic/src
之中
(3)配置cmaklist.txt
注意,此时的cmakelist.txt是新建的那个功能包里面的cmakelist.txt,即ros文件夹根目录/src/learning_topic/
在build表示的内容末尾增加如下2句:
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})
注意:编译完成之后,会在devel/lib
之下发现安装包的可执行文件的文件夹 learning_topic
,里面有编译生成的可执行文件velocity_publisher
(4)编译并且发布运行
注意:
编译一定是在你的ROS项目的根目录哦!如果你要是发现learning_topic话题没有生效的话,再去执行一下soruce devel/setup.bash
在本窗口,之后在本窗口执行运行发布命令的ros指令.
或者
直接将当前工程加入到shell环境里去
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
注意:如果使用python文件的话,就不用进行编译了,选择这个python文件-> 右键->属性->可作为执行文件(画上勾)
,直接运行rosrun learning_topic xxx.py
即可运行
4. 自定义话题的发布和接受过程
4.1 问题描述
第3节演示的是向系统已有的一个节点(turtle_sim
)系统里面根据已有的话题内容,发送固定内容.我们现在自定义2个节点(person_publisher / person_subscriber
),通过一个自定义的话题(/person_info
)来进行通信.
因为相关依赖包稍微有一点不同, 可以继续 第3节继续往下做. 或者重新新建一个功能包
case1: 完全重新做
(1) 首先建立ros空间,这个参照第2部分内容即可
(2) 复用前面的learning_tpic
功能包,只是修改一下相关的packge.xml
和里面的cmaklist.txt
;
并且在learning_tpic
文件夹内,新建msg文件夹
,并且在里面新建Person.msg文件
.具体内容参见如下:
(3) 进入功能包learning_tpic\src
文件夹,新建两个cpp文件person_publisher.cpp
, person_subscriber.cpp
person_publisher.cpp:
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* 该例程将发布/person_info话题,自定义消息类型learning_topic::Person
*/
#include <ros/ros.h>
#include "learning_topic/Person.h"
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "person_publisher");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10
ros::Publisher person_info_pub = n.advertise<learning_topic::Person>("/person_info", 10);
// 设置循环的频率
ros::Rate loop_rate(1);
int count = 0;
while (ros::ok())
{
// 初始化learning_topic::Person类型的消息
learning_topic::Person person_msg;
person_msg.name = "Tom";
person_msg.age = 18;
person_msg.sex = learning_topic::Person::male;
// 发布消息
person_info_pub.publish(person_msg);
ROS_INFO("Publish Person Info: name:%s age:%d sex:%d",
person_msg.name.c_str(), person_msg.age, person_msg.sex);
// 按照循环频率延时
loop_rate.sleep();
}
return 0;
}
person_subscriber.cpp:
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* 该例程将订阅/person_info话题,自定义消息类型learning_topic::Person
*/
#include <ros/ros.h>
#include "learning_topic/Person.h"
// 接收到订阅的消息后,会进入消息回调函数
void personInfoCallback(const learning_topic::Person::ConstPtr& msg)
{
// 将接收到的消息打印出来
ROS_INFO("Subcribe Person Info: name:%s age:%d sex:%d",
msg->name.c_str(), msg->age, msg->sex);
}
int main(int argc, char **argv)
{
// 初始化ROS节点
ros::init(argc, argv, "person_subscriber");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
ros::Subscriber person_info_sub = n.subscribe("/person_info", 10, personInfoCallback);
// 循环等待回调函数
ros::spin();
return 0;
}
(4) 在learning_tpic\cmakelist.txt
修改一下编译规则:
(5) 在项目的根文件,编译整体功能包:
catkin_make
可以在编译文件夹devel/lib/learning_tpic
中看到新生成的2个可执行文件, person_publisher
, person_subscriber
(6) 运行编译环境生效,并且启动窗口执行
source devel/setup.bash
重启3个窗口,分别执行如下命令,开始收发指令:
roscore
rosrun learning_tpic person_subscriber
rosrun learning_tpic person_publisher
case2: 继续第3节往下做
(1) 创建ros空间,这个部分参照第2部分
(2) 创新新的功能包, new_topic
catkin_create_pkg new_topic roscpp rospy std_msgs geometry_msgs turtlesim message_generation
注意依赖项,只是增加了一个message_generatio
(3) 创建好了之后,进入功能包new_topic\src
文件夹,新建两个cpp文件person_publisher.cpp
, person_subscriber.cpp
, 具体内容参照case1
(4) 在new_topic
文件夹同样新建msg
文件夹,在里面新建Persion.msg
,参照case2
(5) 修改package.xml
文件夹:
<build_export_depend>message_generation</build_export_depend>
<exec_depend>message_runtime</exec_depend>
(6) 修改new_topic\cmaklist.txt
################################################
## Declare ROS messages, services and actions ##
################################################
;;;;
#添加依赖项声明
add_message_files(FILES Person.msg)
generate_messages(DEPENDENCIES std_msgs)
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
# 去掉注释项目内容,修改如下
catkin_package(
# INCLUDE_DIRS include
LIBRARIES new_topic
CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim message_runtime
# DEPENDS system_lib
###########
## Build ##
###########
;;;;;
#添加独立话题(发送/接受方,话题是:person_info),设置编译的可执行文件,并且关联相关链接库/添加依赖项
add_executable(person_publisher_new src/person_publisher.cpp)
target_link_libraries(person_publisher_new ${catkin_LIBRARIES})
add_dependencies(person_publisher_new ${PROJECT_NAME}_generate_messages_cpp)
add_executable(person_subscriber_new src/person_subscriber.cpp)
target_link_libraries(person_subscriber_new ${catkin_LIBRARIES})
add_dependencies(person_subscriber_new ${PROJECT_NAME}_generate_messages_cpp)
(7) 在整体项目文件的根目录中执行编译命令:
catkin_make
可以在编译文件夹devel/lib/new_topic
中看到新生成的2个可执行文件, person_publisher_new
, person_subscriber_new
(8) 运行编译环境生效,并且启动窗口执行
source devel/setup.bash
重启3个窗口,分别执行如下命令,开始收发指令:
roscore
rosrun new_topic person_subscriber_new
rosrun new_topic person_publisher_new
可以看到具体通信过程和信息: