Chapter 2. ROS 创建和编译功能包

2023-05-16

1. 创建ROS功能包

使用catkin_create_pkg命令来创建一个新的catkin程序包。
首先切换到之前通过创建catkin工作空间教程创建的catkin工作空间中的src目录下:

~/ros_workspace$ cd src

接着使用catkin_create_pkg命令来创建一个名为’beginner_tutorials’的新程序包,这个程序包依赖于std_msgs、roscpp和rospy:

~/ros_workspace/src$ catkin_create_pkg my_demo roscpp rospy std_msgs

创建成功后,提示如下:

Created file my_demo/package.xml
Created file my_demo/CMakeLists.txt
Created folder my_demo/include/my_demo
Created folder my_demo/src
Successfully created files in /home/nic/ros_workspace/src/my_demo. Please adjust the values in package.xml.

这将会创建一个名为my_demo的文件夹,这个文件夹里面包含一个package.xml文件和一个CMakeLists.txt文件,这两个文件都已经自动包含了部分你在执行catkin_create_pkg命令时提供的信息。

catkin_create_pkg命令会要求你输入package_name,如果有需要你还可以在后面添加一些需要依赖的其它程序包:

# This is an example, do not try to run this
# catkin_create_pkg <package_name> [depend1] [depend2] [depend3]

catkin_create_pkg命令也有更多的高级功能,这些功能在catkin/commands/catkin_create_pkg中有描述。

2. 编译ROS功能包

采用CLion开发环境进行编译

CLion的安装及破解请参照此文

(1)创建节点 my_demo
~/ros_workspace/src/my_demo/src$ gedit my_demo.cpp

将一下示例代码复制到my_demo.cpp中:

/*
 * Copyright (C) 2008, Morgan Quigley and Willow Garage, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
// %Tag(FULLTEXT)%
// %Tag(ROS_HEADER)%
#include "ros/ros.h"
// %EndTag(ROS_HEADER)%
// %Tag(MSG_HEADER)%
#include "std_msgs/String.h"
// %EndTag(MSG_HEADER)%

#include <sstream>

/**
 * This tutorial demonstrates simple sending of messages over the ROS system.
 */
int main(int argc, char **argv)
{
    /**
     * The ros::init() function needs to see argc and argv so that it can perform
     * any ROS arguments and name remapping that were provided at the command line.
     * For programmatic remappings you can use a different version of init() which takes
     * remappings directly, but for most command-line programs, passing argc and argv is
     * the easiest way to do it.  The third argument to init() is the name of the node.
     *
     * You must call one of the versions of ros::init() before using any other
     * part of the ROS system.
     */
// %Tag(INIT)%
    ros::init(argc, argv, "talker");
// %EndTag(INIT)%

    /**
     * NodeHandle is the main access point to communications with the ROS system.
     * The first NodeHandle constructed will fully initialize this node, and the last
     * NodeHandle destructed will close down the node.
     */
// %Tag(NODEHANDLE)%
    ros::NodeHandle n;
// %EndTag(NODEHANDLE)%

    /**
     * The advertise() function is how you tell ROS that you want to
     * publish on a given topic name. This invokes a call to the ROS
     * master node, which keeps a registry of who is publishing and who
     * is subscribing. After this advertise() call is made, the master
     * node will notify anyone who is trying to subscribe to this topic name,
     * and they will in turn negotiate a peer-to-peer connection with this
     * node.  advertise() returns a Publisher object which allows you to
     * publish messages on that topic through a call to publish().  Once
     * all copies of the returned Publisher object are destroyed, the topic
     * will be automatically unadvertised.
     *
     * The second parameter to advertise() is the size of the message queue
     * used for publishing messages.  If messages are published more quickly
     * than we can send them, the number here specifies how many messages to
     * buffer up before throwing some away.
     */
// %Tag(PUBLISHER)%
    ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
// %EndTag(PUBLISHER)%

// %Tag(LOOP_RATE)%
    ros::Rate loop_rate(10);
// %EndTag(LOOP_RATE)%

    /**
     * A count of how many messages we have sent. This is used to create
     * a unique string for each message.
     */
// %Tag(ROS_OK)%
    int count = 0;
    while (ros::ok())
    {
// %EndTag(ROS_OK)%
        /**
         * This is a message object. You stuff it with data, and then publish it.
         */
// %Tag(FILL_MESSAGE)%
        std_msgs::String msg;

        std::stringstream ss;
        ss << "hello world " << count;
        msg.data = ss.str();
// %EndTag(FILL_MESSAGE)%

// %Tag(ROSCONSOLE)%
        ROS_INFO("%s", msg.data.c_str());
// %EndTag(ROSCONSOLE)%

        /**
         * The publish() function is how you send messages. The parameter
         * is the message object. The type of this object must agree with the type
         * given as a template parameter to the advertise<>() call, as was done
         * in the constructor above.
         */
// %Tag(PUBLISH)%
        chatter_pub.publish(msg);
// %EndTag(PUBLISH)%

// %Tag(SPINONCE)%
        ros::spinOnce();
// %EndTag(SPINONCE)%

// %Tag(RATE_SLEEP)%
        loop_rate.sleep();
// %EndTag(RATE_SLEEP)%
        ++count;
    }


    return 0;
}
// %EndTag(FULLTEXT)%
(2)配置CLion快捷方式

进入CLion快捷方式所在的目录:

~$ cd /usr/share/applications

打开并编辑快捷方式:

/usr/share/applications$ sudo gedit jetbrains-clion.desktop

按照如下所示修改:

Exec=bash -i -c "/opt/clion-2018.1.6/bin/clion.sh" %f

重新注销系统使之生效。

(3)加载功能包

运行CLion,打开功能包下的CMakeList.txt文件,并作为工程打开,如图:
这里写图片描述

(4)编译节点并运行

打开功能包工程如图,编辑CMakeList.txt文件:

add_executable(my_demo src/my_demo.cpp)
target_link_libraries(my_demo ${catkin_LIBRARIES})

这里写图片描述

点击调试按钮,提示如下:
这里写图片描述

打开新的终端,输入:

~$ roscore

这里写图片描述

在CLion环境中,显示如下:
这里写图片描述

新建一个终端查看执行结果:
这里写图片描述

3. 创建ROS功能包并利用CLion进行编译,至此成功完成!

补充说明

如果采用标准版的QtCreator进行编译,请按以下步骤进行:

a. 参照前面,创建节点 demo_node
b. 设置QtCreator能够在启动时候添加ros环境

查找QtCreator的快捷方式

修改QtCreator的快捷方式

在Exec=处加上 bash -i -c
这里写图片描述
重启QtCreator。

c. 打开QtCreator,通过OpenProject打开工作空间下的CMakeLists.txt文件

这里写图片描述

d. 配置项目

这里写图片描述

e. 修改package中CMakeLists.txt的内容

这里写图片描述

d. 运行结果

这里写图片描述
这里写图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Chapter 2. ROS 创建和编译功能包 的相关文章

  • emWin - Movie篇

    STemWin版本 xff1a 544 xff08 ST购买了emWin的license xff0c 可以在ST芯片中使用emWin工具包 xff0c 就叫STemWin xff09 emWin开发工具包可以转换JPG BMP GIF等各种
  • 芯片端子的多路复用

    嵌入式软件的开发 xff0c 经常要和芯片打交道 xff0c 和个人电脑的通用平台的CPU使用X86或X64架构不同 xff0c 嵌入式电子产品使用的主控芯片是各种各样的 xff0c 从8051单片机 xff0c 到ARM Cortex M
  • 小说三要素和六要素

    小说是以刻画人物为中心 xff0c 通过完整的故事情节和具体的环境描写反映社会生活的一种文学体裁 或通过情节的展开和环境的渲染反映社会生活的一种文学体裁 小说有三个要素 xff1a 人物形象 故事情节 典型环境 xff08 自然环境和社会环
  • POV写作手法

    POV xff08 Point of View xff09 xff0c 一种写作手法 xff0c 即 视点人物写作手法 xff0c 在叙述同一件事可以自由选取最丰厚的角度 xff0c 大大加强了叙述的灵活性 xff0c 在讲述故事的同时作者
  • 工具说明书 - 搜索引擎推荐

    Yandex 俄罗斯人用的多 Yet Another Indexer 34 xff08 另一个索引 xff09 把定位改到美国 xff0c 再把搜索设置为无限制 www yandex com CC Search 查一些版权免费的图片和音视频
  • 嵌入式 - 在嵌入式软件开发中使用stdint.h头文件

    在嵌入式系统软件开发上 xff0c 对不同的平台 xff0c 其每个机器字长都可能不同 在这个硬件平台上使用int xff0c 可能是4个字节 xff0c 你做一个很大整数的运算也没问题 但换个硬件平台 xff0c int可能变成了2个字节
  • 编程参考 - C语言中将字符串转换为整数

    C语言 xff0c 主要有两种方法可以将字符串转为整数 xff1a atoi 和 strtol 1 xff0c atoi Syntax int atoi const char str 包含的头文件是 xff1a stdlib h 给定字符串
  • 工具及方法 - Excel插件XLTools

    立即开始使用XLTools XLTools是一个功能强大的Excel插件 xff0c 专为商业用户设计 它将帮助您在Excel中更快 更容易地准备数据 跟踪变化和实现任务自动化 无论您是刚刚开始免费试用还是购买了终身许可证 xff0c 本介
  • Linux - Ubuntu里安装Python的包

    在Ubuntu中 xff0c apt install python xff0c 默认是安装python2 要安装python3 要使用apt install python3 安装后运行python python2 xff0c 调用的都是py
  • CURL访问HTTPS证书登录

    curl cacert test crt https abc com test crt指服务端公钥 若服务端要求客户端认证 xff0c 需要将pfx证书转换成pem格式 openssl pkcs12 clcerts nokeys in ce
  • 司空见惯 - 体彩中奖交多少税

    体彩中奖交多少税 彩票只有大盘彩如双色球 大乐透等才在中奖1万元以上的情况下交20 的意外所得税 xff0c 小盘彩如3D 排列三等无论中多少都是不用交税的 对于中奖金额在1万元以下的 xff0c 是可以享受免征个人所得税福利政策的 对于中
  • 司空见惯 - 一树春风

    一树春风有两般 南枝向暖北枝寒 现前一段西来意 一片西飞一片东 xff08 一片东来一片西 xff09 了元 一树春风 了元是谁 xff1f 了元 xff0c 字觉老 xff0c 号佛印 xff0c 浮梁王氏子 北宋著名诗僧 xff0c 禅
  • 知识点滴 - 世界化工企业百强

    2022年7月25日 xff0c 美国 化学与工程新闻 xff08 C amp EN xff09 发布2022年全球化工企业50强名单 2022 09 21日附近 xff0c 国际石化市场信息服务商安迅思ICIS发布了最新世界化工企业100
  • 工具及方法 - 安装播放器pot player

    官网下载 xff1a potplayer daum net 可能国内访问有问题 xff0c 还有一个网站 xff1a Global Potplayer 或者为了纯净安全些 xff0c 找下国外可下载网站 xff1a PotPlayer 23
  • cpprestsdk应用实例

    RESTful REST全称是Representational State Transfer xff0c 通常译为表述性状态转移 xff0c 是一种网络应用程序的设计风格和开发方式 它首次出现在2000年Roy Fielding的博士论文中
  • VS2013中如何安装NuGet插件

    1 点击菜单栏的工具 gt 拓展和更新 2 点击左边的联机 xff0c 在右边的输入框中输入NuGet Package Manager xff0c 在中间栏中显示结果 xff0c 点击安装按钮即可完成安装 我这里已经安装过了 xff0c 所
  • vs2013中如何安装OpencvSharp并使用

    1 使用本方法前请确保你的vs已经安装了NuGet插件 xff0c 如果没有安装可以根据下面链接方法进行安装 VS2013中如何安装NuGet插件 2 右击你的项目 xff0c 选择 管理NuGet工具包 3 在弹出的窗口左边点击 联机 x
  • I2C 基础原理详解

    今天来学习下I2C通信 I2C Inter Intergrated Circuit 指的是 IC Intergrated Circuit 之间的 Inter 通信方式 如上图所以有很多的周边设备都是用I2C通信方式进行通信的 I2C Int
  • 简单一招破解网站内容不能复制+图片不能右击(naver blog有效)

    很多网站为了保护权利设置了内容不可复制 xff0c 并且图片右击无法获取图片链接或者是下载图片 xff0c 网上看了很多方法 xff0c 获取针对别的网站有效 xff0c 但是韩国的naver blog不行 今天一大早又谷歌了一下 xff0
  • RPLIDAR思岚雷达学习记录--3--rplidar_ros-master修改保存csv格式数据

    rplidar ros master 下载rplidar ros master包源后在ros工作空间内打开 xff0c 并编译 xff0c 详细过程及下载地址见学习记录 1 本文介绍的过于繁琐 xff0c 考虑到还有一些新接触到ros的同学

随机推荐