ROS1/2机器人之从命令调用到程序编写
Posted zhangrelay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS1/2机器人之从命令调用到程序编写相关的知识,希望对你有一定的参考价值。
难度级别:
容易☞命令调用
困难☞程序编写
命令调用简单案例
ROS1:
rosrun package-name executable-name
*ROS1必须要保证主节点运行,才能单独使用rosrun。
(roscore)
ROS2:
ros2 run package-name executable-name
比如启动键盘遥控turtlesim
ROS1:
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
大多数 ROS1 节点在启动时连接到节点管理器上,如果运行中连接中断,则不会尝试重新连接。因此,如果 roscore 被终止,当前运行的其他节点将无法建立新的连接,即使 稍后重启 roscore 也无济于事。
于是,引入DDS!(当然这只是其中一个重要原因)
ROS2:
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
ROS 2建立在DDS / RTPS之上,将其作为中间件,提供发现、序列化和传输等功能。 总述一下,DDS是一 个端到端的中间件,它提供了ROS系统相关的功能,例如分布式发现(并不是ROS 1采用的集中式,如roscore)和控制传输不同 “服务质量”的选项。
DDS是一个行业标准,然后由一系列供应商实施,如:RTI 的实现版本-Connext或ADLink的实现版本-OpenSplice RTPS(又 名DDSI-RTPS)是DDS用于通过网络进行通信的有线协议,虽 然有些实现并不能满足完整的DDS API,但可以为ROS 2提供足够的功能,例如eProsima的实现版本-快速RTPS。 ROS 2支持多种DDS / RTPS实现,因此在选择供应商/实现时,并不是“一刀切”。在选择中间件实现时,可能会考虑许多因 素:许可、技术、平台可用性或计算占用空间等因素。供应商可能会提供多个针对满足不同需求的DDS或RTPS实现版本。例如,RTI有一些Connext实现的变化,其目的各不相同,例如专门 针对微控制器而另一个针对需要特殊安全认证的应用(目前ROS 2仅支持其标准桌面版本)。为 了 使 用 DDS / RTPS 实 现 与 ROS 2 , ROS 中 间 接 口 “ROS Middleware interface, RMW”(又名rmw接口或rmw)封装需要创建一个使用DDS实现或RTPS实现的API和工具抽象ROS中 间件接口。实现和维护RMW包用于支持DDS开发需要做很多工作,但至少支持一些实现对于确保ROS 2代码库不依赖于任何一 个特定实现非常重要,因为用户依据具体项目的需求,可能希望根据需要切换实现版本。
程序编写简单案例
为何package-name是turtlesim?
这是功能包的名字?
cmake_minimum_required(VERSION 3.5)
project(turtlesim)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
find_package(ament_cmake REQUIRED)
find_package(ament_index_cpp REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Widgets)
find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(std_msgs REQUIRED)
find_package(std_srvs REQUIRED)
include_directories(include $Qt5Widgets_INCLUDE_DIRS)
rosidl_generate_interfaces($PROJECT_NAME
"action/RotateAbsolute.action"
"msg/Color.msg"
"msg/Pose.msg"
"srv/Kill.srv"
"srv/SetPen.srv"
"srv/Spawn.srv"
"srv/TeleportAbsolute.srv"
"srv/TeleportRelative.srv")
set(dependencies "ament_index_cpp" "geometry_msgs" "rclcpp" "rclcpp_action" "std_msgs" "std_srvs")
set(turtlesim_node_SRCS
src/turtlesim.cpp
src/turtle.cpp
src/turtle_frame.cpp
)
set(turtlesim_node_HDRS
include/turtlesim/turtle_frame.h
)
qt5_wrap_cpp(turtlesim_node_MOCS $turtlesim_node_HDRS)
add_executable(turtlesim_node $turtlesim_node_SRCS $turtlesim_node_MOCS)
target_link_libraries(turtlesim_node Qt5::Widgets)
ament_target_dependencies(turtlesim_node $dependencies)
rosidl_target_interfaces(turtlesim_node $PROJECT_NAME "rosidl_typesupport_cpp")
add_executable(turtle_teleop_key tutorials/teleop_turtle_key.cpp)
ament_target_dependencies(turtle_teleop_key $dependencies)
rosidl_target_interfaces(turtle_teleop_key $PROJECT_NAME "rosidl_typesupport_cpp")
add_executable(draw_square tutorials/draw_square.cpp)
ament_target_dependencies(draw_square $dependencies)
rosidl_target_interfaces(draw_square $PROJECT_NAME "rosidl_typesupport_cpp")
add_executable(mimic tutorials/mimic.cpp)
ament_target_dependencies(mimic $dependencies)
rosidl_target_interfaces(mimic $PROJECT_NAME "rosidl_typesupport_cpp")
install(TARGETS turtlesim_node turtle_teleop_key draw_square mimic
DESTINATION lib/$PROJECT_NAME)
install(DIRECTORY images
DESTINATION share/$PROJECT_NAME
FILES_MATCHING PATTERN "*.png" PATTERN "*.svg")
install(DIRECTORY launch
DESTINATION share/$PROJECT_NAME)
ament_package()
功能包名字参考:project(turtlesim)
与此类似,节点名字如下:
add_executable(turtlesim_node $turtlesim_node_SRCS $turtlesim_node_MOCS)
target_link_libraries(turtlesim_node Qt5::Widgets)
ament_target_dependencies(turtlesim_node $dependencies)
rosidl_target_interfaces(turtlesim_node $PROJECT_NAME "rosidl_typesupport_cpp")
add_executable(turtle_teleop_key tutorials/teleop_turtle_key.cpp)
ament_target_dependencies(turtle_teleop_key $dependencies)
rosidl_target_interfaces(turtle_teleop_key $PROJECT_NAME "rosidl_typesupport_cpp")
命令调用,通俗不严谨说类似API;
编写程序,通俗不严谨说类似APP。
调用API难,还是开发APP难?不言而喻呢。
只使用命令调用(受制于程序):
如果程序编写则可以(自由飞翔的舞台):
#include "turtlesim/turtle_frame.h"
#include <QPointF>
#include <cstdlib>
#include <ctime>
#define DEFAULT_BG_R 0x65
#define DEFAULT_BG_G 0x65
#define DEFAULT_BG_B 0x65
namespace turtlesim
TurtleFrame::TurtleFrame(rclcpp::Node::SharedPtr& node_handle, QWidget* parent, Qt::WindowFlags f)
: QFrame(parent, f)
, path_image_(888, 888, QImage::Format_ARGB32)
, path_painter_(&path_image_)
, frame_count_(0)
, id_counter_(0)
setFixedSize(888, 888);
setWindowTitle("TurtleSim奇妙曲线");
srand(time(NULL));
update_timer_ = new QTimer(this);
update_timer_->setInterval(16);
update_timer_->start();
connect(update_timer_, SIGNAL(timeout()), this, SLOT(onUpdate()));
nh_ = node_handle;
rcl_interfaces::msg::IntegerRange range;
range.from_value = 0;
range.step = 1;
range.to_value = 255;
rcl_interfaces::msg::ParameterDescriptor background_r_descriptor;
background_r_descriptor.description = "Red channel of the background color";
background_r_descriptor.integer_range.push_back(range);
rcl_interfaces::msg::ParameterDescriptor background_g_descriptor;
background_g_descriptor.description = "Green channel of the background color";
background_g_descriptor.integer_range.push_back(range);
rcl_interfaces::msg::ParameterDescriptor background_b_descriptor;
background_b_descriptor.description = "Blue channel of the background color";
background_b_descriptor.integer_range.push_back(range);
nh_->declare_parameter("background_r", rclcpp::ParameterValue(DEFAULT_BG_R), background_r_descriptor);
nh_->declare_parameter("background_g", rclcpp::ParameterValue(DEFAULT_BG_G), background_g_descriptor);
nh_->declare_parameter("background_b", rclcpp::ParameterValue(DEFAULT_BG_B), background_b_descriptor);
以上是关于ROS1/2机器人之从命令调用到程序编写的主要内容,如果未能解决你的问题,请参考以下文章