完整的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/