ros2 初体验

  • 0.先贴官方的网站的指导
  • 1. 确保支持utf-8
  • 2.安装源
  • 3.安装ros2
  • 4.配置环境
  • 5.测试talker 和 listener
  • 6. cpp 和python demo代码
  • 7. ros1 和ros2 的区别,架构方面

0.先贴官方的网站的指导

我们通过 debian 包安装就是通过apt命令

传送门
网不好的话看看科学上网,或者换运营商试试

下面开始正文

1. 确保支持utf-8

安装的时候最好用英文环境吧

命令看下自己是不是utt-8

如果是就可以忽略下面的操作,不是请安心执行

sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8

2.安装源

安装需要用到的软件

sudo apt update && sudo apt install curl gnupg2 lsb-release

密钥认证GPG

curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

添加分支到源列表文件里
清华大学得源,速度快

sudo sh -c 'echo "deb http://mirror.tuna.tsinghua.edu.cn/ros2/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'

3.安装ros2


16.04 是xenial ,安装ardent版本

sudo apt-get update && sudo apt-get install ros-ardent-desktop

安装需要一定时间,这个包服务器是美国的,慢慢下载,失败就多试几次

4.配置环境

和ros1 类似,在.bashrc. 文件加一下

source /opt/ros/ardent/setup.bash

ros2 有个坑爹的地方就是自动补全有点不行,不想ros1 ,source 之后就会补全了

这里需要用到一个叫argcomplete 东东
安装:

sudo apt install python3-pip
sudo pip3 install argcomplete

激活:

sudo activate-global-python-argcomplet

这样就可以激活tab补全了,开心

5.测试talker 和 listener

ros2 run demo_nodes_cpp talker
ros2 run demo_nodes_py listener


不用起roscore,不用受制于master了,dds是个好东西

6. cpp 和python demo代码

ros2 可以用python3了,python2 马上EOX了

talker.cpp
官方写法

#include <chrono>
#include <memory>#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"using namespace std::chrono_literals;/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */class MinimalPublisher : public rclcpp::Node
{public:MinimalPublisher(): Node("minimal_publisher"), count_(0){publisher_ = this->create_publisher<std_msgs::msg::String>("chatter", 10);timer_ = this->create_wall_timer(500ms, std::bind(&MinimalPublisher::timer_callback, this));}private:void timer_callback(){auto message = std_msgs::msg::String();message.data = "Hello, world! " + std::to_string(count_++);RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());publisher_->publish(message);}rclcpp::TimerBase::SharedPtr timer_;rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;size_t count_;};int main(int argc, char * argv[]){rclcpp::init(argc, argv);rclcpp::spin(std::make_shared<MinimalPublisher>());rclcpp::shutdown();return 0;}

民间写法

#include <iostream>
#include <memory>#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"int main(int argc, char * argv[])
{//ros::init(argc, argv, "talker");rclcpp::init(argc, argv);//ros::NodeHandle n;auto node = rclcpp::Node::make_shared("talker");// 配置质量服务原则,ROS2针对以下几种应用提供了默认的配置:// publishers and subscriptions (rmw_qos_profile_default).// Services (rmw_qos_profile_services_default).// Sensor data (rmw_qos_profile_sensor_data).rmw_qos_profile_t custom_qos_profile = rmw_qos_profile_default;// 配置QoS中历史数据的缓存深度custom_qos_profile.depth = 7;//ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);auto chatter_pub = node->create_publisher<std_msgs::msg::String>("chatter", custom_qos_profile);//ros::Rate loop_rate(10);rclcpp::WallRate loop_rate(2);auto msg = std::make_shared<std_msgs::msg::String>();auto i = 1;//while (ros::ok())while (rclcpp::ok()) {msg->data = "Hello World: " + std::to_string(i++);std::cout << "Publishing: '" << msg->data << "'" << std::endl;//chatter_pub.publish(msg);chatter_pub->publish(msg);//ros::spinOnce();rclcpp::spin_some(node);//loop_rate.sleep();loop_rate.sleep();}return 0;
}

主要是用了automake_shared

listener.py

# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import sysimport rclpy
from rclpy.node import Nodefrom std_msgs.msg import Stringclass Listener(Node):def __init__(self):super().__init__('listener')self.sub = self.create_subscription(String, 'chatter', self.chatter_callback)def chatter_callback(self, msg):self.get_logger().info('I heard: [%s]' % msg.data)def main(args=None):if args is None:args = sys.argvrclpy.init(args=args)node = Listener()rclpy.spin(node)node.destroy_node()rclpy.shutdown()if __name__ == '__main__':main()

总结下,c++部分,库不一样了,不过思路一致,更c++化了 用的c++14

接口文档在这里http://docs.ros2.org/ardent/api/rclcpp/index.html

python的差别不大,以前是rospy,现在是rclpy了

7. ros1 和ros2 的区别,架构方面

ros1 ------------------------------------------------------------------------------ros2

先到这,洗洗睡了

安装 ubuntu16.04 ROS2 超过5分钟你打我 后带 测试talker listener demo相关推荐

  1. VMware虚拟机下安装Ubuntu16.04镜像完整教程

    目录 1)安装前准备 2)安装Ubuntu 16.04镜像 3)One More Thing 1)安装前准备 PC电脑操作系统是WIN7,已正确安装虚拟机VMware 12. 2)安装Ubuntu 1 ...

  2. Python3.7.3安装(Ubuntu16.04)

    Python3.7.3安装(Ubuntu16.04) 前面的文章已经介绍了在Windows上安装Python2和Python3了,现在介绍Linux系统上的安装.Ubuntu16.04上默认安装了Py ...

  3. Python2.7.16安装(Ubuntu16.04)

    Python2.7.16安装(Ubuntu16.04) 前面的文章已经介绍了在Windows上安装Python2和Python3了,现在介绍Linux系统上的安装.Ubuntu16.04上默认安装了P ...

  4. 记一次解决Intel 9462无线网卡的笔记本安装Ubuntu16.04后无法连接WIFI问题的艰难历程

    文章目录 前言 安装环境 解决方案 安装过程 初始环境 安装Ubuntu16.04.2 硬件禁止问题 激活无线网卡 更新软件列表附加驱动 更新内核1 安装Ubuntu16.04.7 查找网卡版本 更新 ...

  5. 利用UItraISO软碟通制作U盘启动盘安装Ubuntu16.04系统

    为了方便以后学习回顾,开始自己的电子笔记之旅!第一次写博客还有点小激动,嘿嘿!直接切入主题:如何利用UItraISO软碟通制作U盘启动盘安装Ubuntu16.04系统?(本博客是在Windows10下 ...

  6. U盘启动盘成功安装Ubuntu16.04(解决安装卡logo等问题)

    写在前面: 我的笔记本是华硕FX503VD.重装系统后,需要安装Ubuntu16.04来完成毕业设计,本来自己之前已经成功安装过,但是这次不知道为什么又卡在这里了,主要的问题还是U盘启动后,选择UEF ...

  7. 在Vmware虚拟机中安装Ubuntu16.04(server版)操作系统

    一.工具 window操作系统 Vmware虚拟机软件      Vmware下载地址 Ubuntu操作系统镜像    Ubuntu镜像文件下载地址 二.步骤 先安装好Vmware虚拟机软件,不会安装 ...

  8. 树莓派装linux ros,树莓派安装Ubuntu16.04 MATE系统以及ROS(kinetic)的安装

    安装ubuntu16.04 MATE系统 1.安装Ubuntu16.04镜像 从官网下载并按照树莓派刷写系统的流程将ubuntu16.04 MATE烧写到树莓派的SD卡上 2.设置WiFi 将树莓派外 ...

  9. VirtraulBox中安装Ubuntu16.04

    描述:在win10系统下的Virtural Box6.1中安装Ubuntu16.04的常见问题:下载版本选择 .Virtural Box中的ubuntu界面显示不全.共享剪切板.共享文件夹.处理器数量 ...

最新文章

  1. KNN学习之图像分类与KNN原理
  2. JZOJ 3786. 【NOI2015模拟8.19】图
  3. Boost:验证atomic <>没有对void指针提供算术运算
  4. c语言1 2 3 10000,在网上看到一个求2的10000次方的方法,有个地方看不懂,求大佬...
  5. HashMap 学习笔记
  6. 我要去三清山国家公园。。。
  7. 虎牙AI基础技术部招聘深度学习/计算机视觉实习生
  8. 实现安卓中TextView,EditText中数字的数码管字体显示
  9. npm下载和使用(超详细)
  10. 传智播客黑马程序员_新程序员的最佳播客,以及聆听他们的最佳工具
  11. tensorflow-ckpt2npy
  12. Got permission denied while trying to connect to the Docker daemon socket
  13. 如何使用Epicor Functions(一)
  14. php架构师培训,php架构师培训效果怎么样
  15. php编写六十甲子纳音表_六十甲子顺序表
  16. 计算机组成原理 第四版 总线的主模块 总线的从模块,总线的基本概念
  17. Arduino实验三:继电器实验
  18. 自控力读书笔记 第七章 出售未来:及时享乐的经济学
  19. 经纬坐标(BLH)数据创建.kml文件小工具设计 Java版
  20. 记录AOSP源码编译刷机(pixel 4a)

热门文章

  1. 【单镜头反光相机】影调、反差、光比、宽容度;光质(硬光、软光)、硬调、软调、高调、低调、中间调...
  2. 免费资源-成语答题小程序源码+教程
  3. C/C++中argv[ ]与argc内容解读//(涉及运用cmd)
  4. python模拟点击屏幕ios_python模拟点击在ios中实现的实例讲解
  5. Memory Technology Device (MTD) 设备分析
  6. VMtools的安装教程
  7. C# unix时间戳 秒、毫秒、微秒。日期 字符串 格式
  8. 计算机专业迎新晚会主题海报,如何制作迎新晚会宣传海报?推荐使用迎新海报素材!...
  9. 如何快速将图片中的文字提取出来
  10. matlab受力曲线导入adams中,如何在adams中导入dxf曲线