ubuntu  选择Hong Kong 源

1. ROS1安装

添加 sources.list(设置你的电脑可以从 packages.ros.org 接收软件.)

sudo sh -c '. /etc/lsb-release && echo "deb [arch=amd64] http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'

添加 keys

sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

安装ROS1桌面完整版

sudo apt update
sudo apt install ros-noetic-desktop-full

环境测试

source /opt/ros/noetic/setup.bash

补全rosdep

sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
sudo apt install python3-rosdep
sudo rosdep init
rosdep update

2. ROS2安装

确保系统要支持 UTF-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

设置软件源

sudo apt update
sudo apt install curl gnupg2 lsb-release # 下面这条语句,我的输出错误: gpg: no valid OpenPGP data found
# curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
# 解决上面的问题,可以换成下面这条语句:
$ curl http://repo.ros2.org/repos.key | sudo apt-key add - # 之后再添加源:
sudo sh -c '. /etc/lsb-release && echo "deb
[arch=amd64]http://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'

安装ROS2桌面完整版

sudo apt update
sudo apt install ros-foxy-desktop
sudo apt install python3-colcon-common-extensions

环境测试

source /opt/ros/foxy/setup.bash

自动补全工具

sudo apt update
sudo apt install python3-argcomplete
sudo apt update
sudo apt install ros-foxy-rmw-connext-cpp

安装ros1转换bridge

sudo apt update
sudo apt install ros-foxy-ros1-bridge

ROS1和ROS2混合配置

sudo gedit ~/.bashrc

打开.bashrc文本文件后在末尾加上如下代码,完成环境变量设置。

#source /opt/ros/noetic/setup.bash
echo "ros noetic(1) or ros2 foxy(2)?"
read edition
if [ "$edition" -eq "1" ];then
source /opt/ros/noetic/setup.bash
else
source /opt/ros/foxy/setup.bash
fi

ROS1和ROS2差异

编译

catkin_make --> colcon build

catkin_make -DCATKIN_WHITELIST_PACKAGES="" --> colcon build --packages-select  PACKAGE_NAME

bashrc存入

echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc --> echo "source ~/colcon_ws/install/setup.bash" >> ~/.bashrc

terminal 命令

rosrun --> ros2 run
rosnode --> ros2 node
roslaunch --> ros2 launch
rosparam --> ros2 param
rospkg --> ros2 pkg
rosservice --> ros2 service
rossrv --> ros2 srv
rostopic --> ros2 topic
rosaction --> ros2 action

ROS2写法

有一说一,ROS2的class管理还是比较好的,ROS2相比ROS1在语法上有了很大的改变,在编程思路还是一样。这里给出最简单的topic通讯。

#include <chrono>
#include <functional>
#include <memory>
#include <string> #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>("topic", 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 <chrono>
#include <functional>
#include <memory>
#include <string> #include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

命名空间

using namespace std::chrono_literals;

使用面向对象中的继承,类名为MinimalPublisher继承rclcpp::Node这个类,继承方式为public;

class MinimalPublisher : public rclcpp::Node

私有成员有回调函数timer_callback,计时器对象timer_,发布者对象publisher_,统计数量的count_;
该timer_callback函数是设置消息数据和实际发布消息的地方。该RCLCPP_INFO宏确保每个发布的消息都打印到控制台。还有计时器、发布者和计数器字段的声明。

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_;

公有成员:类的构造函数用于对私有成员进行初始化;
公共构造函数命名节点minimal_publisher并初始化count_为 0。在构造函数内部,发布者使用String消息类型、主题名称topic和所需的队列大小进行初始化,以在发生备份时限制消息。接下来,timer_被初始化,这会导致timer_callback函数每秒执行两次。this指的是该节点

public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalPublisher::timer_callback, this));
}

主函数部分
rclcpp::init初始化 ROS 2,并rclcpp::spin开始处理来自节点的数据,包括来自计时器的回调。

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}

创建订阅者

#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1; class MinimalSubscriber : public rclcpp::Node
{
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
subscription_ = this->create_subscription<std_msgs::msg::String>(
"topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
} private:
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
}; int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriber>());
rclcpp::shutdown();
return 0;
}

修改CMakeLists.txt文件,将下面内容复制到文件中并保存

add_executable(talker src/publisher_member_function.cpp)
ament_target_dependencies(talker rclcpp std_msgs) add_executable(listener src/subscriber_member_function.cpp)
ament_target_dependencies(listener rclcpp std_msgs) install(TARGETS
talker
listener
DESTINATION lib/${PROJECT_NAME})

ubuntu 20.04 安装 ros1 和ros2的更多相关文章

  1. Ubuntu 20.04安装Docker

    Docker学习系列文章 入门必备:十本你不容错过的Docker入门到精通书籍推荐 day1.全面的Docker快速入门教程 day2.CentOS 8.4安装Docker day3.Windows1 ...

  2. ubuntu 20.04 安装 vim8.2

    由于ubuntu 20.04自带的vim版本比较老了,有些新装的插件适配不上,所以需要安装最新版本的vim.在网上找了很久也没有比较官方的安装教程所以记录一下. 安装依赖库 sudo apt inst ...

  3. Ubuntu 20.04 安装和编译poco 1.10.1

    1.首先安装其openssl其它依赖库,打开终端,使用root账户(sudo su),完成以下库的安装 //安装odbc相关库 apt-get install unixodbc apt-get ins ...

  4. Ubuntu 20.04 安装kodi播放器

    打开终端,执行命令在线安装 sudo apt-get install software-properties-common sudo add-apt-repository ppa:team-xbmc/ ...

  5. Ubuntu 20.04安装mysql后用mysql root无法登录

    刚安装mysql后,执行 mysql -u root -p 提示无法执行 解决方案: sudo mysql -u root -p 使用root权限不用密码就能进入mysql 然后 >ALTER ...

  6. Ubuntu 20.04下源码编译安装ROS 2 Foxy Fitzroy

    ROS 2 Foxy Fitzroy(以下简称Foxy)于2020年6月5日正式发布了,是LTS版本,支持到2023年5月.本文主要根据官方的编译安装教程[1]完成,并记录编译过程中遇到的问题. 1. ...

  7. Ubuntu 20.04.1 安装软件和系统配置脚本

    #!/bin/bash # https://launchpad.net/ubuntu # https://www.easyicon.net # https://download-chromium.ap ...

  8. Ubuntu 20.04上通过Wine 安装微信

    没有想过会在一个手机软件上花这么多心思,好在今天总算安装成功,觉得可以记录下这个过程,方便他人方便自己. 首先介绍下我使用过的其他方法,希望可以节省大家一些时间: Rambox Pro:因为原理是网页 ...

  9. 树莓派安装 Ubuntu 20.04 LTS 碰壁指南

    树莓派安装 Ubuntu 20.04 LTS 碰壁指南 设备 Raspberry 4B 4+32G 系统 Ubuntu 20.04 LTS 1.镜像下载与烧录 镜像下载地址:https://cdima ...

随机推荐

  1. URLEncoder和URLDecoder转码

    目前看是为了解决网络传输的中文乱码问题 import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import  ...

  2. C#早期绑定&后期绑定

    早期绑定(early binding),又可以称为静态绑定(static binding).在使用某个程序集前,已知该程序集里封装的方法.属性等.则创建该类的实例后可以直接调用类中封装的方法. 后期绑 ...

  3. Bagging与随机森林

    Bagging Bagging是并行式集成学习算法最著名的代表,基于自助采样法(bootstrap sampling). 给定m个样本的数据集,选取m次,每次选1个样本,构成一个新的样本集,其中有的样 ...

  4. Qt:QWidget

    0.说明 QWidget类是所有用户界面对象的基类. QWidget是用户界面的原子类.它接收鼠标.键盘和来自系统的其他事件,并在屏幕上将它们绘制出来.每个Widget都是矩形的,并按照Z-order ...

  5. Pandas:to_excel时如何不覆盖之前的Excel表、ExcelWriter类

    如果只是想把一个DataFrame保存为单独的一个Excel文件,那么直接写: data.to_excel('xxx.excel','sheet1',index=False) 但是这样做,只会保存为单 ...

  6. 任意文件夹打开CMD命令窗口

    1:打开任意文件夹 2:按住Shift键,鼠标右键单击 3:该文件夹下的命令窗口已打开,方便快捷

  7. 微服务入门二:SpringCloud(版本Hoxton SR6)

    一.什么是SpringCloud 1.官方定义 1)官方定义:springcloud为开发人员提供了在分布式系统中快速构建一些通用模式的工具(例如配置管理.服务发现.断路器.智能路由.微代理.控制总线 ...

  8. css3渐变色字体

    \3cspan id="mce_marker" data-mce-type="bookmark">\3c/span>\3cspan id=" ...

  9. 二进制部署1.23.4版本k8s集群-6-部署Node节点服务

    本例中Master节点和Node节点部署在同一台主机上. 1 部署kubelet 1.1 集群规划 主机名 角色 IP CFZX55-21.host.com kubelet 10.211.55.21 ...

  10. C#/Vsto中CustomTaskPanes和Ribbon的使用方法

    在工作中有一个需求,需要添加工作区选项卡,Excel中CustomTaskPanes面板很适合这样的场景,而非集中处理在Excel的Ribbon面板中,毕竟在大型项目中表现层已经过于复杂了.首先写一个 ...