标签: 机器人
最近因学校老师的机器人项目,开始学习ROS,特意把ROS的基础教程放置与此,以便回顾 : )
一个节点其实只不过是ROS程序包中的一个可执行文件。ROS节点可以使用ROS客户库与其他节点通信。节点可以发布或接收一个话题。节点也可以提供或使用某种服务。
$ rosnode list #用于查看当前活跃的节点列表
$ rosnode info #用于查看的信息
$ rosrun [package_name] [node_name] #用法
$ rosrun turtlesim turtlesim_node #例子
节点与节点之间通过话题进行信息交流
rqt_graph能够创建一个显示当前系统运行情况的动态图形。rqt_graph是rqt程序包中的一部分。
$ rosrun rqt_graph rqt_graph #使用方法
$ rostopic -h #查看rostopic子命令
输出
rostopic bw display bandwidth used by topic
rostopic echo print messages to screen
rostopic hz display publishing rate of topic
rostopic list print information about active topics rostopic pub publish data to topic
rostopic type print topic type
例子
rostopic echo /turtle1/cmd_vel #适用ROS Hydro 及其之后的版本
话题之间的通信是通过在节点之间发送ROS消息实现的。对于发布器(turtle_teleop_key)和订阅器(turtulesim_node)之间的通信,发布器和订阅器之间必须发送和接收相同类型的消息。这意味着话题的类型是由发布在它上面的消息类型决定的。使用rostopic type命令可以查看发布在某个话题上的消息类型
终端输入
$ rosmsg -h
得到结果:
rosmsg is a command-line tool for displaying information about ROS Message types.
Commands:
rosmsg show Show message description
rosmsg info Alias for rosmsg show
rosmsg list List all messages
rosmsg md5 Display message md5sum
rosmsg package List messages in a package
rosmsg packages List packages that contain messages
Type rosmsg -h for more detailed usage
rostopic pub可以把数据发布到当前某个正在广播的话题上。
$ rostopic pub [topic] [msg_type] [args]
rostopic hz命令可以用来查看数据发布的频率。
用法:
rostopic hz [topic]
rqt_plot命令可以实时显示一个发布到某个话题上的数据变化图形。
服务(services)是节点之间通讯的另一种方式。服务允许节点发送请求(request) 并获得一个响应(response)
rosservice list 输出可用服务的信息
rosservice call 调用带参数的服务
rosservice type 输出服务类型
rosservice find 依据类型寻找服务
find services by service type rosservice uri 输出服务的ROSRPC uri
使用方法
$ rosservice type [service] #用法
$ rosservice type clear #例子
$ rosservice call clear #清除turtle运行的痕迹
rosparam使得我们能够存储并操作ROS 参数服务器(Parameter Server)上的数据。参数服务器能够存储整型、浮点、布尔、字符串、字典和列表等数据类型。rosparam使用YAML标记语言的语法。一般而言,YAML的表述很自然:1 是整型, 1.0是浮点型, one是字符串, true是布尔, [1, 2, 3]是整型列表, {a: b, c: d}是字典. rosparam有很多指令可以用来操作参数,如下所示:
rosparam set 设置参数
rosparam get 获取参数
rosparam load 从文件读取参数
rosparam dump 向文件中写入参数
rosparam delete 删除参数
rosparam list 列出参数名
使用方法
rosparam dump [file_name]
rosparam load [file_name] [namespace]
rqt_console属于ROS日志框架(logging framework)的一部分,用来显示节点的输出信息。
rqt_logger_level允许我们修改节点运行时输出信息的日志等级(logger levels)(包括 DEBUG、WARN、INFO和ERROR)。
rosrun rqt_console rqt_console #启动日志输出信息
rosrun rqt_logger_level rqt_logger_level #修改节点运行时输出信息的日志等级(logger levels)
$ roslaunch [package] [filename.launch] #用法
rosed [package_name] [filename]
在程序包/src文件夹中新建talker.cpp
粘贴以下代码:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include
/**
* This tutorial demonstrates simple sending of messages over the ROS system.
*/
int main(int argc, char **argv)
{
/**
* The ros::init() function needs to see argc and argv so that it can perform
* any ROS arguments and name remapping that were provided at the command line. For programmatic
* remappings you can use a different version of init() which takes remappings
* directly, but for most command-line programs, passing argc and argv is the easiest
* way to do it. The third argument to init() is the name of the node.
*
* You must call one of the versions of ros::init() before using any other
* part of the ROS system.
*/
ros::init(argc, argv, "talker"); //初始化 ROS 。它允许 ROS 通过命令行进行名称重映射
/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;
/**
* The advertise() function is how you tell ROS that you want to
* publish on a given topic name. This invokes a call to the ROS
* master node, which keeps a registry of who is publishing and who
* is subscribing. After this advertise() call is made, the master
* node will notify anyone who is trying to subscribe to this topic name,
* and they will in turn negotiate a peer-to-peer connection with this
* node. advertise() returns a Publisher object which allows you to
* publish messages on that topic through a call to publish(). Once
* all copies of the returned Publisher object are destroyed, the topic
* will be automatically unadvertised.
*
* The second parameter to advertise() is the size of the message queue
* used for publishing messages. If messages are published more quickly
* than we can send them, the number here specifies how many messages to
* buffer up before throwing some away.
*/
ros::Publisher chatter_pub = n.advertise("chatter", 1000);
ros::Rate loop_rate(10);
/**
* A count of how many messages we have sent. This is used to create
* a unique string for each message.
*/
int count = 0;
while (ros::ok())
{
/**
* This is a message object. You stuff it with data, and then publish it.
*/
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
/**
* The publish() function is how you send messages. The parameter
* is the message object. The type of this object must agree with the type
* given as a template parameter to the advertise<>() call, as was done
* in the constructor above.
*/
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}