完整的python package的目录结构如下:

  • source /opt/ros/dashing/setup.bash
  • cd ros2_ws/src && ros2 pkg create <package_name>
  • Delete CMakeLists.txt , create setup.py and setup.cfg and edit package.xml

    setup.py内容参考:
from setuptools import setup

package_name = 'ros2_demo_py'

setup(
name=package_name,
version='0.7.0',
packages=[package_name],
install_requires=['setuptools'],
zip_safe=True,
author='You',
author_email='you@youremail.com',
maintainer='YourFirstname Lastname',
maintainer_email='your@youremail.com',
keywords=['ROS'],
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python',
'Topic :: Software Development',
],
description='A simple ROS2 Python package',
license='Apache License, Version 2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'demo = ros2_demo_py.demo:main'
],
},
)

fbi warning!!!!!!!!!

'console_scripts': [

'demo = ros2_demo_py.demo:main'

],

告知ros2如何加载脚本.比如ros2 run ros2_demo_py demo实际上就是说要去执行ros2_demo_py/demo.py的main()函数. 更改setup.py后必须重新colcon build才会生效.

当我改成'console_scripts': [

'fuck_ = ros2_demo_py.what:main'

],时,相应的执行命令就变为ros2 run ros2_demo_py fuck_,执行的就是ros2_demo_py/what.py的main()函数

setup.cfg内容参考:

[develop]
script-dir=$base/lib/ros2_demo_py
[install]
install-scripts=$base/lib/ros2_demo_py

package.xml内容参考:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<name>ros2_demo_py</name>
<version>0.7.3</version>
<description>A simple ROS2 Python package</description> <maintainer email="sloretz@openrobotics.org">Shane Loretz</maintainer>
<license>Apache License 2.0</license> <exec_depend>rclpy</exec_depend>
<exec_depend>std_msgs</exec_depend> <!-- These test dependencies are optional
Their purpose is to make sure that the code passes the linters -->
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend> <export>
<build_type>ament_python</build_type>
</export>
</package>
  • 创建python代码

    demo.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 rclpy
from rclpy.node import Node from std_msgs.msg import String class MinimalPublisher(Node): def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(String, 'topic')
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0 def timer_callback(self):
msg = String()
msg.data = 'Hello World: %d' % self.i
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.i += 1 def main(args=None):
rclpy.init(args=args) minimal_publisher = MinimalPublisher() rclpy.spin(minimal_publisher) # Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
minimal_publisher.destroy_node()
rclpy.shutdown() if __name__ == '__main__':
main()
  • 回到workspace目录,编译

    user:~/ros2_ws$ colcon build --symlink-install

详细步骤参考:https://www.theconstructsim.com/ros2-tutorials-5-how-to-create-a-ros2-package-for-python-update/

ros2中创建一个python package的更多相关文章

  1. 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来

    /*4.创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来*/ #import <Foundation/Foundation.h>#defin ...

  2. Ionic 2 中创建一个照片倾斜浏览组件

    内容简介 今天介绍一个新的UI元素,就是当我们改变设备的方向时,我们可以看到照片的不同部分,有一种身临其境的感觉,类似于360全景视图在移动设备上的应用. 倾斜照片浏览 Ionic 2 实例开发 新增 ...

  3. iOS9中如何在日历App中创建一个任意时间之前开始的提醒(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 四.创建任意时间之前开始的提醒 现在我们找到了指定源中的指定日 ...

  4. 在C#/.NET应用程序开发中创建一个基于Topshelf的应用程序守护进程(服务)

    本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...

  5. Eclipse中创建一个新的SpringBoot项目

    在Eclipse中创建一个新的spring Boot项目: 1. 首先在Eclipse中安装STS插件:在Eclipse主窗口中点击 Help -> Eclipse Marketplace... ...

  6. 在存放源程序的文件夹中建立一个子文件夹 myPackage。例如,在“D:\java”文件夹之中创建一个与包同名的子文件夹 myPackage(D:\java\myPackage)。在 myPackage 包中创建一个YMD类,该类具有计算今年的年份、可以输出一个带有年月日的字符串的功能。设计程序SY31.java,给定某人姓名和出生日期,计算该人年龄,并输出该人姓名、年龄、出生日期。程序使用YM

    题目补充: 在存放源程序的文件夹中建立一个子文件夹 myPackage.例如,在“D:\java”文件夹之中创建一个与包同名的子文件夹 myPackage(D:\java\myPackage).在 m ...

  7. 父类是在子类创建对象时候 在子类中创建一个super内存空间

    父类是在子类创建对象时候 在子类中创建一个super内存空间

  8. iOS9中怎样在日历App中创建一个随意时间之前開始的提醒(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 四.创建随意时间之前開始的提醒 如今我们找到了指定源中的指定日 ...

  9. [Xcode 实际操作]九、实用进阶-(28)在iTunes Connect(苹果商店的管理后台)中创建一个新的新的APP

    目录:[Swift]Xcode实际操作 本文将演示如何在iTunes Connect(苹果商店的管理后台)中创建一个新的新的APP. 首先要做的是打开浏览器,并进入[iTunesConnect网站], ...

随机推荐

  1. Nginx代理缓存功能

    Nginx代理缓存功能      Nginx缓存主要是用于减轻后端服务器的负载,提高网站并发量,提升用户体验度. 注意:Nginx反向代理的缓存功能是由ngx_http_proxy_module提供, ...

  2. HttpRunner学习5--使用variables声明变量

    前言 在HttpRunner中,如果需要声明变量,可以通过关键字 variables 来完成,要引用声明的变量,则是通过 $+变量名 (如 $token )来实现.variables 可以在 conf ...

  3. Internet History,Technology,and Security - Technology: Internets and Packets (Week5)

    Week5 Technology: Internets and Packets Welcome to Week 5! This week, we’ll be covering internets an ...

  4. vue-cli4.0脚手架安装

    10月16日,官方发布消息称Vue-cli 4.0正式版发布,并且强烈建议升级;小编也是刚刚安装了最新版本的脚手架看看更新了什么 安装和vue-cli3.0的是一模一样的,对比了一下3.0的脚手架,除 ...

  5. [ASP.NET Core 3框架揭秘] 依赖注入[2]:IoC模式

    正如我们在<依赖注入:控制反转>提到过的,很多人将IoC理解为一种"面向对象的设计模式",实际上IoC不仅与面向对象没有必然的联系,它自身甚至算不上是一种设计模式.一般 ...

  6. 2.Android-sdk目录介绍、ADT使用介绍、创建helloworld

    1.android中常用名词介绍 ADT:  ADT为Eclipse的插件.为Eclipse和SDK之间起了一个桥梁的作用. SDK: 软件开发工具包(Soft Development Kit),它为 ...

  7. laravel上传至服务器上出现Whoops, looks like something went wrong.

    1.在本地能够很好运行的laravel,上传至服务器就出现了这个问题“Whoops, looks like something went wrong.”: 2.第一步把config/app.php文件 ...

  8. Builder模式的目的是解耦构建过程,为什么要用内部类?

    还没有看过Builder模式的作用,看过一篇介绍Builder模式的文章,这里是关于Builder模式的思考:思考是否有比新建一个内部类更好的方法,首先想到的是 package xyz.n490808 ...

  9. C# 打开文件/跳转链接

    mark一下~ 打开文件 1.打开文件夹: System.Diagnostics.Process.Start(FolderPath);-- 打开文件夹 System.Diagnostics.Proce ...

  10. C lang: The Command line

    Ax_command line h Ax_a command line describe The command line is in enviroment for DOS,to user opera ...