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


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章