文章目录
- 写在前面的话
- 生成python工程包命令
- 运行python函数命令
- python工程包的目录结构
- 目录结构(细节)
- 报错 1( no module name '***')
- 错误示意 截图
- 终端输出
- 解决方法
- 报错 2( AttributeError: '***' object has no attribute '_default_callback_group')
- 终端输出
- 错误示意 截图
- 解决方法
- 成功运行
- 运行示意 截图
写在前面的话
在 ros2 新建依赖 python 的工程包(package)的命令如下,生成的文件结构如下图。在 setup.py 文件里面已经默认指定了运行的函数是 keyboard_control.py 的 main 函数,这篇文章解决的是需要调用另一个python文件的函数,遇到的报错问题限于我个人,可以参考…
生成python工程包命令
ros2 pkg create keyboard_control --build-type ament_python --dependencies rclpy --node-name keyboard_control
运行python函数命令
ros2 run keyboard_control keyboard_control
python工程包的目录结构
目录结构(细节)
终端打印目录结构需要用到 tree ,安装即可sudo apt install tree
xucg@xucg-vm17:~/xu_nav_ws/src/keyboard_control$ tree
.
├── keyboard_control
│ ├── can_control.py
│ ├── __init__.py
│ └── keyboard_control.py
├── package.xml
├── resource
│ └── keyboard_control
├── setup.cfg
├── setup.py
└── test├── test_copyright.py├── test_flake8.py└── test_pep257.py3 directories, 10 files
报错 1( no module name ‘***’)
错误示意 截图
终端输出
xucg@xucg-vm17:~/xu_nav_ws$ colcon build
Starting >>> keyboard_control
Finished <<< keyboard_control [3.91s] Summary: 1 package finished [4.59s]
xucg@xucg-vm17:~/xu_nav_ws$ ros2 run keyboard_control keyboard_control
Traceback (most recent call last):File "/home/xucg/xu_nav_ws/install/keyboard_control/lib/keyboard_control/keyboard_control", line 33, in <module>sys.exit(load_entry_point('keyboard-control==0.0.0', 'console_scripts', 'keyboard_control')())File "/home/xucg/xu_nav_ws/install/keyboard_control/lib/keyboard_control/keyboard_control", line 25, in importlib_load_entry_pointreturn next(matches).load()File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in loadmodule = import_module(match.group('module'))File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)File "<frozen importlib._bootstrap>", line 1050, in _gcd_importFile "<frozen importlib._bootstrap>", line 1027, in _find_and_loadFile "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlockedFile "<frozen importlib._bootstrap>", line 688, in _load_unlockedFile "<frozen importlib._bootstrap_external>", line 883, in exec_moduleFile "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removedFile "/home/xucg/xu_nav_ws/install/keyboard_control/lib/python3.10/site-packages/keyboard_control/keyboard_control.py", line 20, in <module>from can_control import can_controller
ModuleNotFoundError: No module named 'can_control'
[ros2run]: Process exited with failure 1
解决方法
需要指定到 python 文件的上级目录
from can_control import can_controller
>> from keyboard_control.can_control import can_controller
报错 2( AttributeError: ‘***’ object has no attribute ‘_default_callback_group’)
终端输出
xucg@xucg-vm17:~/xu_nav_ws$ colcon build
Starting >>> keyboard_control
Finished <<< keyboard_control [3.88s] Summary: 1 package finished [4.57s]
xucg@xucg-vm17:~/xu_nav_ws$ ros2 run keyboard_control keyboard_control
Traceback (most recent call last):File "/home/xucg/xu_nav_ws/install/keyboard_control/lib/keyboard_control/keyboard_control", line 33, in <module>sys.exit(load_entry_point('keyboard-control==0.0.0', 'console_scripts', 'keyboard_control')())File "/home/xucg/xu_nav_ws/install/keyboard_control/lib/python3.10/site-packages/keyboard_control/keyboard_control.py", line 145, in maincar_controller = can_controller()File "/home/xucg/xu_nav_ws/install/keyboard_control/lib/python3.10/site-packages/keyboard_control/can_control.py", line 221, in __init__self.sub_vel = self.create_subscription(Twist,'/keyboard_vel_msg', self.vel_callback, 1)File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/node.py", line 1347, in create_subscriptioncallback_group = callback_group or self.default_callback_groupFile "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/node.py", line 304, in default_callback_groupreturn self._default_callback_group
AttributeError: 'can_controller' object has no attribute '_default_callback_group'. Did you mean: 'default_callback_group'?
[ros2run]: Process exited with failure 1
错误示意 截图
解决方法
在调用的函数 __init__(self):
下面一行加上super().__init__('函数类名称')
即可
成功运行
xucg@xucg-vm17:~/xu_nav_ws$ colcon build
Starting >>> keyboard_control
Finished <<< keyboard_control [4.25s] Summary: 1 package finished [4.99s]
xucg@xucg-vm17:~/xu_nav_ws$ ros2 run keyboard_control keyboard_control
[17664, 5, 191, 35387, 15, 15, [b'\x03', b'\x1c', b'\x7f', b'\x15', b'\x04', b'\x00', b'\x01', b'\x00', b'\x11', b'\x13', b'\x1a', b'\x00', b'\x12', b'\x0f', b'\x17', b'\x16', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]Reading from the keyboard and Publishing to Twist!---------------------------注意:r是停止,角速度不能设置太大会导致转弯不稳车身摆动,Moving around:q w e a s d z canything else : stop,/. : increase/decrease max speed by 10%v/b : increase/decrease only linear speed by 10%n/m : increase/decrease only angular speed by 10%CTRL-C to quitcurrently: speed(0.0) turn(0.0)