机器人编程趣味实践07-信息交互(主题)

网友投稿 512 2022-05-29

在网络上,沟通交流的方式主要有文字、图片和视频。对于专业人士,通常是公式和代码。

在表述一个数学或物理规律所用字节或占用空间由小到大:

公式

文字

图片

视频

理解难度由简单到困难,如下:

视频

图片

文字

公式

果然,鱼与熊掌不可兼得。

旅程开启

那么如何构建一个简单的局域网聊天工具呢?

这里只给出最简陋版本的。

复习一下节点:

ros2 node list

猜一下,主题列表命令如下:

ros2 topic list

那么公司的每位小伙伴都要订阅这个主题,随时查看是否需要开会(恩……开会挺**********,此处省略)

ros2 topic pub /meeting -1 std_msgs/msg/String data:\ "下午1:30分,996福报会议室开短会"

window终端也一样:

当然也完全支持图片、视频等格式,并且可以用图形化界面哦,来看一下吧:

在线的员工,如下图所示:

当然也可以给每个员工一个正式工号,留作思考题。

机器人编程趣味实践07-信息交互(主题)

此外,信息也可以加密,未获授权的电脑无法观看,参考:

https://blog.csdn.net/ZhangRelay/article/details/101596837

公司或者实验室内部各成员之间交流通知,采用这种方式就不需要xx了,充分保护隐私哦。

如果做成图形化界面,就是聊天工具。更多内容,后续补充。现在回归本节内容:

信息交互(主题)

基本概念

在机器人操作系统中,将复杂的系统拆解为许多模块(松耦合)。主题是一个ROS图中的虚拟元素,用于展示节点之间消息传输。

一对一:

多对多:

到这里,节点和主题的概念,都已经清晰了。

更多内容参考:ROS 2主题-topics-

配置

下面,开启图形化示例(会议主题保留):

ros2 run turtlesim turtlesim_node

ros2 run turtlesim turtle_teleop_key

rqt_graph

使用rqt_graph,可以清晰可视化节点和主题的动态,当然也包括它们的连接情况。

消息流向由箭头表示:

关于主题的全部命令如下:

usage: ros2 topic [-h] [--include-hidden-topics] Call `ros2 topic -h` for more detailed usage. ...

Various topic related sub-commands

optional arguments:

-h, --help show this help message and exit

--include-hidden-topics

Consider hidden topics as well

Commands:

bw Display bandwidth used by topic

delay Display delay of topic from timestamp in header

echo Output messages from a topic

find Output a list of available topics of a given type

hz Print the average publishing rate to screen

info Print information about a topic

list Output a list of available topics

pub Publish a message to a topic

type Print a topic's type

Call `ros2 topic -h` for more detailed usage.

演示一些内容如下:

ros2 topic list

主题列表

主题列表中传递的消息类型:

ros2 topic list -t

在图形化rqt_graph中,要显示所有主题,注意hide!

消息可以传输各类信息,类似编程语言中的各种变量。后续讲解。

主题消息显示

格式如下:

ros2 topic echo

案例如下:

ros2 topic echo /turtle1/cmd_vel

默认不会有任何反应,如非遥控键盘。

此时,查看:

echo对应n____ros2cli_8418

主题信息

主题支持一对一、一对多、多对一、多对多。

分别查看meeting和/turtle1/cmd_vel。

ros2 topic info /turtle1/cmd_vel

主题消息接口类型

必须用规范的消息相互通信。

geometry_msgs/msg/Twist

使用如下命令查看:

ros2 interface show geometry_msgs/msg/Twist

对应具体实现:

主题发布

命令格式:

ros2 topic pub ''

以二维机器人速度发布为例:

ros2 topic pub --once /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

注意参数的含义:

--once    发布一次

--rate 1    以1Hz频率发布

命令要多尝试,才能掌握。

主题消息频率

ros2 topic hz

具体案例,机器人位置姿态发布的频率?

ros2 topic hz /turtle1/pose

退出

Ctrl+C

总结

节点通过主题发布信息,允许任意数量的其他节点订阅和访问此信息。在本实践中,使用rqt_graph和命令行工具查看了主题上多个节点之间的连接。现在应该对ROS2系统中数据的流动(通信)有了一个很好的理解。

附录

频率如何修改?

pose_pub_ = nh_->create_publisher(real_name + "/pose", qos);

auto p = std::make_unique();

p->x = pos_.x();

p->y = canvas_height - pos_.y();

p->theta = orient_;

p->linear_velocity = std::sqrt(lin_vel_x_ * lin_vel_x_ + lin_vel_y_ * lin_vel_y_);

p->angular_velocity = ang_vel_;

pose_pub_->publish(std::move(p));

会议演示代码?

#include

#include

#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/string.hpp"

class MinimalSubscriber : public rclcpp::Node

{

public:

MinimalSubscriber()

: Node("meeting")

{

// subscription_ = this->create_subscription(

// "meeting",

// 10,

// [this](std_msgs::msg::String::UniquePtr msg) {

// RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());

// });

subscription_ = this->create_subscription(

"meeting", 10, [this](std_msgs::msg::String::UniquePtr msg) {

RCLCPP_INFO(this->get_logger(), msg->data.c_str());

});

}

private:

rclcpp::Subscription::SharedPtr subscription_;

};

int main(int argc, char * argv[])

{

rclcpp::init(argc, argv);

rclcpp::spin(std::make_shared());

rclcpp::shutdown();

return 0;

}

机器人 视频

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:程序员到架构师需要的编程基础
下一篇:Spark3.0主要特性(1)—— Adaptive Query Execution
相关文章