概述
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;
}
主要是用了auto
和 make_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 sys
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class 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.argv
rclpy.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的全部内容,希望文章能够帮你解决安装 ubuntu16.04 ROS2 超过5分钟你打我 后带 测试talker listener demo所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复