mkdir -p 02_ros_ws/src
进入terminal,查看文件夹
2. 创建功能包ros2 pkg create tom --build-type ament_python --dependencies rclpy
cd 进入src文件夹后,执行上述命令。其中 ros2 pkg create
cd 进入工作空间,可以看到
使用tree命令,得到如下文件结构
3. 编写python节点代码import rclpy from rclpy.node import Node def main(args=None): # init the client lib rclpy.init(args=args) # create node instance node_tom_say = Node(node_name="tom_say") # get logger node_tom_say.get_logger().info("Hello, Jerry. I am comming.") # spin the node rclpy.spin(node=node_tom_say) # shutdown the node rclpy.shutdown()4. 修改配置文件
打开配置文件,src/tom/setup.py
from setuptools import setup package_name = 'tom' setup( name=package_name, version='0.0.0', packages=[package_name], data_files=[ ('share/ament_index/resource_index/packages', ['resource/' + package_name]), ('share/' + package_name, ['package.xml']), ], install_requires=['setuptools'], zip_safe=True, maintainer='franklin', maintainer_email='franklin@todo.todo', description='TODO: Package description', license='TODO: License declaration', tests_require=['pytest'], entry_points={ 'console_scripts': [ ], }, )
在 entry_points={'console_scripts': [],}中添加节点位置,修改后如下所示
from setuptools import setup package_name = 'tom' setup( name=package_name, version='0.0.0', packages=[package_name], data_files=[ ('share/ament_index/resource_index/packages', ['resource/' + package_name]), ('share/' + package_name, ['package.xml']), ], install_requires=['setuptools'], zip_safe=True, maintainer='franklin', maintainer_email='franklin@todo.todo', description='TODO: Package description', license='TODO: License declaration', tests_require=['pytest'], entry_points={ 'console_scripts': [ "node_tom_say=tom.tom_say:main" ], }, )5. 编译节点
cd 02_ros_ws, 编译节点 colcon build ,编译后如下如所示
source 更新一下
source install/setup.bash
查看功能包是否存在
ros2 pkg list | grep tom
查看如下信息
6. 运行节点查看节点
7. 代码优化
import rclpy from rclpy.node import Node class TomNode(Node): def __init__(self, name): super().__init__(name) self.get_logger().info(f"Hello, Jerry. {name} is comming again.") def main(args=None): # init the client lib rclpy.init(args=args) # create node instance node_tom_say_again = TomNode(name="tom_say_again") # spin the node rclpy.spin(node=node_tom_say_again) # shutdown the node rclpy.shutdown()
修改配置文件setup.py. 添加 "node_tom_say_again=tom.tom_say_code_updated:main"
from setuptools import setup package_name = 'tom' setup( name=package_name, version='0.0.0', packages=[package_name], data_files=[ ('share/ament_index/resource_index/packages', ['resource/' + package_name]), ('share/' + package_name, ['package.xml']), ], install_requires=['setuptools'], zip_safe=True, maintainer='franklin', maintainer_email='franklin@todo.todo', description='TODO: Package description', license='TODO: License declaration', tests_require=['pytest'], entry_points={ 'console_scripts': [ "node_tom_say=tom.tom_say:main", "node_tom_say_again=tom.tom_say_code_updated:main" ], }, )
重新编译:
参考连接:
【鱼香ROS】动手学ROS2|ROS2基础入门到实践教程|小鱼带你手把手学习ROS2_哔哩哔哩_bilibili
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)