小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
ROS2 运行课程4.2.1节代码时问题
-
-
问题描述:
(1) 编译后运行ros2 topic list
时无法显示自定义的话题/sexy_girl
等
(2) 执行colcon build
会有WARNING
提示:colcon.colcon_core.verb:Some selected packages are already built in one or more underlay workspaces
: -
源码:
#town_ws/src/village_li/village_li/li4.py #!/usr/bin/env python3 import rclpy from rclpy.node import Node # 1. 导入消息类型 from std_msgs.msg import String, UInt32 class WriterNode(Node): """ 创建一个作家节点,并在初始化时输出一个话 """ def __init__(self,name): super().__init__(name) self.get_logger().info("大家好,我是%s,我是一名作家!" % name) self.pub_novel = self.create_publisher(String,"sexy_girl",10) self.count = 1 #创建定时器 self.timer_period = 5 #启动一个定时装置 self.timer = self.create_timer(self.timer_period, self.timer_callback) self.account = 80 # 2.创建并初始化订阅者 submoney self.submoney = self.create_subscription(UInt32, "sexy_girl_money", self.recv_money_callback, 10) def timer_callback(self): msg = String() #对 String 类进行实例化 msg.data = '第%d回: 潋滟湖 %d 次偶遇胡艳娘' % (self.count, self.count) self.pub_novel.publish(msg) #将小说内容发布出去 self.get_logger().info("李四:我发布了艳娘传奇:%s" % msg.data) self.count += 1 # 3.编写发布逻辑发布数据 def recv_money_callback(self,money): self.account += money.data self.get_logger().info("李四: 收到%d的稿费, 账户余额为%d" % (money.data,self.account)) def main(args=None): rclpy.init(args=args) node = WriterNode("li4") rclpy.spin(node) rclpy.shutdown()
#town_ws/src/village_li/setup.py from setuptools import setup package_name = 'village_li' 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='jing', maintainer_email='jing@todo.todo', description='TODO: Package description', license='TODO: License declaration', tests_require=['pytest'], entry_points={ 'console_scripts': [ "li4_node = village_li.li4:main" ], }, )
// town_ws/.vscode/c_cpp_properties.json { "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/opt/ros/foxy/include/" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "gnu17", "cppStandard": "gnu++14", "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 }
- 运行及打印
jing@ubuntu:~/town_ws$ colcon build
[0.191s] WARNING:colcon.colcon_core.verb:Some selected packages are already built in one or more underlay workspaces:
'village_li' is in: /home/jing/town_ws/install/village_li
'village_wang' is in: /home/jing/town_ws/install/village_wang
If a package in a merged underlay workspace is overridden and it installs headers, then all packages in the overlay must sort their include directories by workspace order. Failure to do so may result in build failures or undefined behavior at run time.
If the overridden package is used by another package in any underlay, then the overriding package in the overlay must be API and ABI compatible or undefined behavior at run time may occur.
If you understand the risks and want to override a package anyways, add the following to the command line:
--allow-overriding village_li village_wangThis may be promoted to an error in a future release of colcon-core.
Starting >>> village_li
Starting >>> village_wang
Finished <<< village_wang [0.62s]
Finished <<< village_li [1.44s]Summary: 2 packages finished [1.54s]
jing@ubuntu:~/town_ws$ source install/setup.bash
jing@ubuntu:~/town_ws$ ros2 topic list
/parameter_events
/rosout -
-
@1071904412 是否调用了ros2 run指令
-
@小鱼
不好意思,遗漏了调用ros2 run指令的输出,以下是完成的运行及打印:jing@ubuntu:~/town_ws$ colcon build Starting >>> village_li Starting >>> village_wang Finished <<< village_li [1.07s] Finished <<< village_wang [5.25s] Summary: 2 packages finished [5.43s] jing@ubuntu:~/town_ws$ source install/setup.bash jing@ubuntu:~/town_ws$ ros2 topic list /parameter_events /rosout jing@ubuntu:~/town_ws$ ros2 run village_li li4_node [INFO] [1650080203.400523699] [li4]: 大家好,我是li4,我是一名作家! [INFO] [1650080208.406564149] [li4]: 李四:我发布了艳娘传奇:第1回: 潋滟湖 1 次偶遇胡艳娘 [INFO] [1650080213.406630184] [li4]: 李四:我发布了艳娘传奇:第2回: 潋滟湖 2 次偶遇胡艳娘 ^CTraceback (most recent call last): File "/home/jing/town_ws/install/village_li/lib/village_li/li4_node", line 11, in <module> load_entry_point('village-li==0.0.0', 'console_scripts', 'li4_node')() File "/home/jing/town_ws/install/village_li/lib/python3.8/site-packages/village_li/li4.py", line 52, in main rclpy.spin(node) # 保持节点运行,检测是否收到退出指令(Ctrl+C) File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/__init__.py", line 191, in spin executor.spin_once() File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 706, in spin_once handler, entity, node = self.wait_for_ready_callbacks(timeout_sec=timeout_sec) File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 692, in wait_for_ready_callbacks return next(self._cb_iter) File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 589, in _wait_for_ready_callbacks _rclpy.rclpy_wait(wait_set, timeout_nsec) KeyboardInterrupt jing@ubuntu:~/town_ws$ colcon build [0.170s] WARNING:colcon.colcon_core.verb:Some selected packages are already built in one or more underlay workspaces: 'village_li' is in: /home/jing/town_ws/install/village_li 'village_wang' is in: /home/jing/town_ws/install/village_wang If a package in a merged underlay workspace is overridden and it installs headers, then all packages in the overlay must sort their include directories by workspace order. Failure to do so may result in build failures or undefined behavior at run time. If the overridden package is used by another package in any underlay, then the overriding package in the overlay must be API and ABI compatible or undefined behavior at run time may occur. If you understand the risks and want to override a package anyways, add the following to the command line: --allow-overriding village_li village_wang This may be promoted to an error in a future release of colcon-core. Starting >>> village_li Starting >>> village_wang Finished <<< village_wang [0.45s] Finished <<< village_li [0.88s] Summary: 2 packages finished [0.97s] jing@ubuntu:~/town_ws$ source install/setup.bash jing@ubuntu:~/town_ws$ ros2 topic list /parameter_events /rosout
-
此回复已被删除! -
@小鱼 在执行了 4.2.2-2节前的代码后,问题(1) 编译后运行ros2 topic list时无法显示自定义的话题 /sexy_girl 就解决了,但不清楚原因是什么
-
@1071904412 大概知道你的问题了,节点只有在运行中才能被检测到,不要打断再topic list
-
-
@小鱼 好的~谢谢!