完整的python package的目录结构如下:
- source /opt/ros/dashing/setup.bash
- cd ros2_ws/src && ros2 pkg create
- 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='[email protected]',
maintainer='YourFirstname Lastname',
maintainer_email='[email protected]',
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'
],
},
)
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="[email protected]">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/
原文地址:https://www.cnblogs.com/sdu20112013/p/11864846.html
时间: 2024-10-11 15:07:34