小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
ros2运行报错
-
在看4-6-2的时候跟着鱼哥使用ros2 run village_li li4_node命令时,系统报错ImportError: cannot import name 'BorrowMoney' from 'village_interfaces.srv'
据体文件和错误为以下
import rclpy
from rclpy.node import Node
#1.导入消息类型
from std_msgs.msg import String,UInt32
#1.导入服务接口
from village_interfaces.srv import BorrowMoneyclass WriterNode(Node):
def init(self,name):
super().init(name)
self.get_logger().info("大家好,我是作家li4%s." % name)
#2.声明并创建发布者
self.pub_novel = self.create_publisher(String,"sexy_girl",10)self.count = 0 self.timer_period=5 self.timer=self.create_timer(self.timer_period,self.timer_callback) self.account = 80 self.sub_money = self.create_subscription(UInt32,"sexy_girl_money",self.recv_money_callback,10) #3.声明并创建服务端 self.borrow_server = self.create_service(BorrowMoney,"brrow_money",self.borrow_money_callback) #2.创建服务端回调函数 def borrow_money_callback(self,request,response): pass self.get_logger().info("收到来自:%s 的借钱请求,账户目前有:%d" % (request.name,self.account)) if request.money <= self.account*0.1: response.success = True response.money = request.money self.account = self.account - request.money self.get_logger().info("借钱成功,借出%d,目前还剩%d" %(response.money,self.account)) else: response.success = False response.money = 0 self.get_logger.info("对不起,大兄弟,现在手头紧,不能借给你!") return response def timer_callback(self): msg=String() msg.data = "第%d回:淩艳湖 %d 次偶遇胡艳娘" % (self.count,self.count) self.pub_novel.publish(msg) #让发布者发布消息 self.get_logger().info("发布了一个章节的小说,内容是:%s" % msg.data) self.count += 1 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)
li4_node = WriterNode("li4")
rclpy.spin(li4_node)
rclpy.shutdown()www@www-virtual-machine:~/town_ws$ ros2 run village_li li4_node
Traceback (most recent call last):
File "/home/www/town_ws/install/village_li/lib/village_li/li4_node", line 33, in <module>
sys.exit(load_entry_point('village-li==0.0.0', 'console_scripts', 'li4_node')())
File "/home/www/town_ws/install/village_li/lib/village_li/li4_node", line 25, in importlib_load_entry_point
return next(matches).load()
File "/usr/lib/python3.10/importlib/metadata/init.py", line 171, in load
module = import_module(match.group('module'))
File "/usr/lib/python3.10/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/www/town_ws/install/village_li/lib/python3.10/site-packages/village_li/li4.py", line 6, in <module>
from village_interfaces.srv import BorrowMoney
ImportError: cannot import name 'BorrowMoney' from 'village_interfaces.srv' (/home/www/town_ws/install/village_interfaces/local/lib/python3.10/dist-packages/village_interfaces/srv/init.py)
[ros2run]: Process exited with failure 1 -
检查编译后的install/village_interfaces/local/lib/python3.10/dist-packages/village_interfaces/srv 看看有没有BorrowMoney.py