ROS之配置setup.py以在devel空间下运行python节点

ROS之配置setup.py以在devel空间下运行python节点

截图


实操要点

步骤:
CMakeLists.txt中

#找到如下部分取消catkin_python_setup()的注释
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
catkin_python_setup()
#添加
#......
install(PROGRAMS
   scripts/talker.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )
 install(PROGRAMS
   scripts/talker2.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )
#......

根据自己包的目录写一个py文件。

from distutils.core import setup

setup(
    version='0.0.0',
    scripts=['scripts/talker.py','scripts/talker2.py'],
    packages=['zzzserialtest'],
    package_dir={'': 'scripts'}
)


这个py文件应该放到包的根目录,而不是工作空间的根目录。

catkin_make 


catkin_make后会在devel/bin/出现python节点。

rosrun zzzserialtest talker.py


如果正常运行,那么说明你已经成功了!


编译错误示例:
缺少setup.py时编译报错。

CMake Error at C:/opt/ros/melodic/x64/share/catkin/cmake/catkin_python_setup.cmake:31 (message):
  catkin_python_setup() called without 'setup.py' in project folder '
  D:/project/ros/rosws2/src/zzzserialtest'
Call Stack (most recent call first):
  zzzserialtest/CMakeLists.txt:23 (catkin_python_setup)


-- Configuring incomplete, errors occurred!
See also "D:/project/ros/rosws2/build/CMakeFiles/CMakeOutput.log".
See also "D:/project/ros/rosws2/build/CMakeFiles/CMakeError.log".
NMAKE : fatal error U1077: “C:\opt\python27amd64\Lib\site-packages\cmake\data\bin\cmake.exe”: 返回代码“0x1”
Stop.
Invoking "nmake" failed


注意版本号问题
在pakage.xml里

<version>0.0.0</version>

py文件的版本号应该与其对应否则catkin_make编译不过。
报错如下

CMake Error at C:/opt/ros/melodic/x64/share/catkin/cmake/catkin_python_setup.cmake:79 (message):
  catkin_python_setup() version in setup.py (...) differs from version in
  package.xml (0.0.0)
Call Stack (most recent call first):
  zzzserialtest/CMakeLists.txt:23 (catkin_python_setup)


-- Configuring incomplete, errors occurred!
See also "D:/project/ros/rosws2/build/CMakeFiles/CMakeOutput.log".
See also "D:/project/ros/rosws2/build/CMakeFiles/CMakeError.log".
NMAKE : fatal error U1077: ??C:\opt\python27amd64\Lib\site-packages\cmake\data\bin\cmake.exe??: ???????0x1??
Stop.
Invoking "nmake cmake_check_build_system" failed

官方说明
Handling of setup.py
http://docs.ros.org/api/catkin/html/user_guide/setup_dot_py.html


简译

setup.py的处理 

如果您的ROS包包含Python模块和要安装的脚本,则需要定义安装过程和一种使脚本能够在devel空间中访问的方法。python生态系统在distutils或setuputils库中定义安装标准。使用这些库,在项目(package)的根目录中名为setup.py的文件中定义安装文件。py文件使用Python脚本来描述相关的一些要安装的Python内容。


We recommend to prefer distutils package over setuptools/distribute, because with distutils we can avoid the creation of egg-info folders in the project source folder. The setup.cfg file of distutils2 is not supported by catkin.

Catkin allows you to specify the installation of your python files in this setup.py and reuse some of the information in your CMakeLists.txt.

You can do so by including the line:

我们建议使用distutils包而不是setuptools/distribute,因为使用distutils可以避免在项目源文件夹中创建egg-info文件夹。catkin不支持distutils2的setup.cfg文件。
Catkin允许您在setup.py中指定要安装的python文件,并复用CMakeLists.txt中的一些信息。

在项目(package)CMakeLists.txt中可以用下面的语句:

catkin_python_setup()

catkin会使用distutils的hot-patched 版本执行setup.py来读取设置devel空间的参数,并使用CMAKE_INSTALL_PREFIX下的适用参数执行setup.py来安装到catkin install空间。

这意味着,如果手动使用以下命令执行setup.py:

不要这样安装

# DO NOT USE
# python setup.py install

that would install to a different location, and you would have multiple installed versions. Python will decide at runtime which one to use based on your PYTHONPATH environment variable, and it may pick the one you expect it to pick, but we recommend against having multiple installed versions in the first place. Using setup.py to create a pypi package of your catkin package currently has no support for ROS messages and services, and core ROS libraries (e.g. rospy) are not available on pypi, so this using setup.py for pypi is not very useful for ROS nodes.

它将安装到不同的位置,并且您将有多个已安装的版本。Python将在运行时根据PYTHONPATH环境变量决定使用哪个版本,它可能会选择您希望的版本(也可能不是),我们建议先前不要安装多个版本。使用setup.py创建catkin package的pypi包目前不支持ROS消息和服务,并且核心ROS库(例如rospy)在pypi上不可用,因此对ROS节点使用pypi 的setup.py不是很有用。(有点绕。。。,请理解原文)


For the develspace, 
为了devel空间(中能运行python节点)
the following setup.py arguments to setup() will be used by catkin:
以下setup.py文件setup()中的参数 将被catkin使用:

from distutils.core import setup

setup(
    version='...',
    scripts=['bin/myscript'],
    packages=['mypkg'],
    package_dir={'': 'src'}
)

This creates relays 
//【relay】(理解为中继)
//v.接转,转送,转发(信息、消息等);播放,转播(电视或广播讯号)
//n.接力赛;接班的人(或动物);轮换者;中继设备

for all scripts listed in scripts to a folder in devel space 
这 为scripts(数组)中所有列出的脚本 创建中继到devel空间中的一个文件夹。
where they can be found and executed, 
在那里能被找到和执行
and also relay packages for any package listed in packages.
还为packages(数组)中列出的包创建中继包。

 
这将为在scripts中列出的所有脚本创建中继到devel空间中的一个文件夹,在该文件夹中可以找到并执行它们,还将为packages中列出的包创建中继包。中继包是一个文件夹,其中只有一个__init__.py的文件夹,而没有其他东东。

Importing this folder in python will execute the contents of __init__.py, which will in turn import the original python modules in the folder in the sourcespace using the python exec() function.
在python中导入此文件夹将执行__init__.py的内容,然后用python exec()函数依次导入在源空间文件夹中的原始python模块

The version will be compared to that declared in package.xml, and raise an error on mismatch.
version (变量)将与package.xml中声明的版本信息进行比较,并在不匹配时引发错误。

注意------------
如果您以前编写过非ROS Python包,那么您可能在distuils的setup函数中使用了requires字段。然而,这个字段在ROS中“没有意义”。

所有Python依赖项都应在package.xml中指定,例如
<run_depend>Python numpy</run_depend>(对于package.xml的旧版本1)

<exec_depend>Python numpy</exec_depend>(若使用package.xml的格式2)。

并非所有Python或pip包都映射到ROS依赖项。如果要添加对mypackage 的Python包依赖项,快速检查,请尝试运行rosdep resolve python-mypackage或rosdep resolve python-mypackage-pip。如果这些调用返回错误,您可能需要在rosdep中的python.yaml文件中搜索类似的名称。如果找不到请求的包,可以考虑创建一个Pull请求来添加它。
--------------


在setup.py中使用package.xml

Writing a setup.py file without duplicating information contained in the package.xml is possible using a catkin_pkg convenience function like this:
在不复制package.xml中包含的信息的情况下,编写setup.py文件,能使用catkin_pkg便捷的功能,示例:

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup(
    packages=['mypkg'],
    scripts=['bin/myscript'],
    package_dir={'': 'src'}
)

setup(**d)

这将解析package.xml并格式化字段,
such that multiple authors with emails will be set nicely for setup.py, in case one distributes to pypi.
这样就可以为setup.py方便的设置多个带有电子邮件的作者,以备其中一个分发到pypi。

注意--------------

ROS用户通常不使用scripts参数,因为在ROS中,可执行文件应该使用rosrun执行,而不是安装到全局bin文件夹中。安装此类python脚本的一种方法是将以下内容添加到CMakeLists.txt:

catkin_install_python(PROGRAMS scripts/myscript
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

#catkin_安装_python(程序  源scripts/myscript.py
# 目的地  ${CATKIN_PACKAGE_BIN_DESTINATION})

注意--------------
请参阅上一节中,关于其他有用字段requires的注释,该注释说明通常是distutils设置的一部分。Do not use it 不要在ROS中使用。
-------------------


开发空间限制

对于devel空间,catkin当前不支持以下任何distutils参数:

  • py_modules
  • data_files
  • 任何扩展模块功能

在setuptools中,devel空间不支持以下参数:

  • zip-safe
  • entry_points

在distribute中,devel空间不支持以下参数:

  • include_package_data
  • exclude_package_data
  • zip_safe
  • entry_points
  • setup_requires
  • namespace_packages
  • use_2to3

这些功能只能在install空间中正常工作。

genmsg交互

genmsg是一个外部catkin包,为ROS消息提供语言绑定。当使用genmsg宏时,存在排序约束 ordering constraints exist,在这种情况下,您必须按以下顺序调用宏:

project(...)
...
find_package(catkin ...)
...
catkin_python_setup()
...
generate_messages()
...
catkin_package()


ROS与Python入门教程-制作Makefile文件
https://www.ncnynl.com/archives/201611/1063.html

ros 编译 Python 文件
https://blog.csdn.net/light_jiang2016/article/details/55505627


 

发布了78 篇原创文章 · 获赞 76 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/qq_38288618/article/details/103398428