正常导入包依赖命令:
cd src
catkin_create_pkg 自定义ROS包名 roscpp rospy std_msgs
这个是在创建功能包的同时导入包依赖。
请问下如何分开呢,先创建自定义功能包在按项目需求导入包依赖。
比如一个项目 我创建了功能包导入了roscpp 但是我突然又想用到rospy了怎么办?
项目是公司找的别人开发的ros项目
第一步
执行上面命令编译出现下图错误
百度
https://blog.csdn.net/qq_39779233/article/details/119384745
使用这个教程安装不起ros-melodic-catkin
按照此教程正常无报错运行
https://fishros.com/d2lros2/#/chapt10/10.5%E9%85%8D%E7%BD%AEFishbot%E8%BF%9B%E8%A1%8C%E5%BB%BA%E5%9B%BE
但是控制机器人走路并未出现5.3当中的建图路线
正常教程路线
我创建的未展示路线
@271658536 在 CMake编译错误 中说:
ModuleNotFoundError: No module named 'catkin_pkg'
已经解决,解决方法原文如下:
https://blog.csdn.net/qq_27865227/article/details/122609055
前置操作
安装carotgrapher
sudo apt install ros-foxy-cartographer
sudo apt install ros-foxy-cartographer-ros
然后在fishbot_ws工作空间创建fishbot_cartographer功能包
ros2 pkg create fishbot_cartographer
执行编译命令
colcon build --packages-select fishbot_cartographer
CMakeLists.txt文件内容如下
cmake_minimum_required(VERSION 3.5)
project(fishbot_cartographer)
install(
DIRECTORY config launch rviz
DESTINATION share/${PROJECT_NAME}
)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
编译错误内容如下
Starting >>> fishbot_cartographer
--- stderr: fishbot_cartographer
Traceback (most recent call last):
File "/opt/ros/foxy/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 21, in <module>
from catkin_pkg.package import parse_package_string
ModuleNotFoundError: No module named 'catkin_pkg'
CMake Error at /opt/ros/foxy/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:94 (message):
execute_process(/home/ros2/miniconda3/bin/python3
/opt/ros/foxy/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py
/home/ros2/fishbot_ws/src/fishbot_cartographer/package.xml
/home/ros2/fishbot_ws/build/fishbot_cartographer/ament_cmake_core/package.cmake)
returned error code 1
Call Stack (most recent call first):
/opt/ros/foxy/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:49 (_ament_package_xml)
/opt/ros/foxy/share/ament_lint_auto/cmake/ament_lint_auto_find_test_dependencies.cmake:31 (ament_package_xml)
CMakeLists.txt:37 (ament_lint_auto_find_test_dependencies)
---
Failed <<< fishbot_cartographer [0.63s, exited with code 1]
Summary: 0 packages finished [0.85s]
1 package failed: fishbot_cartographer
1 package had stderr output: fishbot_cartographer
新建功能包无任何操作,编译也是这个错误。
urdf文件
<?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- | This document was autogenerated by xacro from test03_my_car.xacro | -->
<!-- | EDITING THIS FILE BY HAND IS NOT RECOMMENDED | -->
<!-- =================================================================================== -->
<!-- 组合小车底盘与摄像头与雷达 -->
<robot name="my_car">
<!-- 圆柱惯性矩阵 -->
<!-- 立方体惯性矩阵 -->
<!-- 宏:黑色设置 -->
<material name="black">
<color rgba="0.0 0.0 0.0 1.0"/>
</material>
<!-- 质量 -->
<!-- 底盘 -->
<link name="base_footprint">
<visual>
<geometry>
<sphere radius="0.001"/>
</geometry>
</visual>
</link>
<link name="base_link">
<visual>
<geometry>
<cylinder length="0.08" radius="0.1"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0"/>
<material name="yellow">
<color rgba="0.5 0.3 0.0 0.5"/>
</material>
</visual>
<collision>
<geometry>
<cylinder length="0.08" radius="0.1"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0"/>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.001516666666666667" ixy="0" ixz="0" iyy="0.001516666666666667" iyz="0" izz="0.0025000000000000005"/>
</inertial>
</link>
<joint name="base_link2base_footprint" type="fixed">
<parent link="base_footprint"/>
<child link="base_link"/>
<origin xyz="0 0 0.055"/>
</joint>
<gazebo reference="base_link">
<material>Gazebo/Yellow</material>
</gazebo>
<!-- 质量 -->
<link name="left_wheel">
<visual>
<geometry>
<cylinder length="0.015" radius="0.0325"/>
</geometry>
<origin rpy="1.5707963 0.0 0.0" xyz="0.0 0.0 0.0"/>
<material name="black"/>
</visual>
<collision>
<geometry>
<cylinder length="0.015" radius="0.0325"/>
</geometry>
<origin rpy="1.5707963 0.0 0.0" xyz="0.0 0.0 0.0"/>
</collision>
<inertial>
<mass value="0.05"/>
<inertia ixx="1.4140625000000002e-05" ixy="0" ixz="0" iyy="1.4140625000000002e-05" iyz="0" izz="2.6406250000000005e-05"/>
</inertial>
</link>
<joint name="left_wheel2base_link" type="continuous">
<parent link="base_link"/>
<child link="left_wheel"/>
<origin xyz="0 0.1 -0.0225"/>
<axis xyz="0 1 0"/>
</joint>
<gazebo reference="left_wheel">
<material>Gazebo/Red</material>
</gazebo>
<link name="right_wheel">
<visual>
<geometry>
<cylinder length="0.015" radius="0.0325"/>
</geometry>
<origin rpy="1.5707963 0.0 0.0" xyz="0.0 0.0 0.0"/>
<material name="black"/>
</visual>
<collision>
<geometry>
<cylinder length="0.015" radius="0.0325"/>
</geometry>
<origin rpy="1.5707963 0.0 0.0" xyz="0.0 0.0 0.0"/>
</collision>
<inertial>
<mass value="0.05"/>
<inertia ixx="1.4140625000000002e-05" ixy="0" ixz="0" iyy="1.4140625000000002e-05" iyz="0" izz="2.6406250000000005e-05"/>
</inertial>
</link>
<joint name="right_wheel2base_link" type="continuous">
<parent link="base_link"/>
<child link="right_wheel"/>
<origin xyz="0 -0.1 -0.0225"/>
<axis xyz="0 1 0"/>
</joint>
<gazebo reference="right_wheel">
<material>Gazebo/Red</material>
</gazebo>
<!-- 质量 -->
<link name="front_wheel">
<visual>
<geometry>
<sphere radius="0.0075"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0"/>
<material name="black"/>
</visual>
<collision>
<geometry>
<sphere radius="0.0075"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0"/>
</collision>
<inertial>
<mass value="0.03"/>
<inertia ixx="6.75e-07" ixy="0" ixz="0" iyy="6.75e-07" iyz="0" izz="6.75e-07"/>
</inertial>
</link>
<joint name="front_wheel2base_link" type="continuous">
<parent link="base_link"/>
<child link="front_wheel"/>
<origin xyz="0.0925 0 -0.0475"/>
<axis xyz="1 1 1"/>
</joint>
<gazebo reference="front_wheel">
<material>Gazebo/Red</material>
</gazebo>
<link name="back_wheel">
<visual>
<geometry>
<sphere radius="0.0075"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0"/>
<material name="black"/>
</visual>
<collision>
<geometry>
<sphere radius="0.0075"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0"/>
</collision>
<inertial>
<mass value="0.03"/>
<inertia ixx="6.75e-07" ixy="0" ixz="0" iyy="6.75e-07" iyz="0" izz="6.75e-07"/>
</inertial>
</link>
<joint name="back_wheel2base_link" type="continuous">
<parent link="base_link"/>
<child link="back_wheel"/>
<origin xyz="-0.0925 0 -0.0475"/>
<axis xyz="1 1 1"/>
</joint>
<gazebo reference="back_wheel">
<material>Gazebo/Red</material>
</gazebo>
<!-- 摄像头安装的z坐标:底盘高度 / 2 + 摄像头高度 / 2 -->
<!-- 摄像头质量 -->
<!-- 摄像头关节以及link -->
<link name="camera">
<visual>
<geometry>
<box size="0.01 0.025 0.025"/>
</geometry>
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.0"/>
<material name="black"/>
</visual>
<collision>
<geometry>
<box size="0.01 0.025 0.025"/>
</geometry>
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.0"/>
</collision>
<inertial>
<mass value="0.01"/>
<inertia ixx="6.041666666666668e-07" ixy="0" ixz="0" iyy="6.041666666666668e-07" iyz="0" izz="1.041666666666667e-06"/>
</inertial>
</link>
<joint name="camera2base_link" type="fixed">
<parent link="base_link"/>
<child link="camera"/>
<origin xyz="0.08 0.0 0.052500000000000005"/>
</joint>
<gazebo reference="camera">
<material>Gazebo/Blue</material>
</gazebo>
<!-- 支架安装的z坐标:底盘高度 / 2 + 支架高度 / 2 -->
<!-- 支架质量 -->
<link name="support">
<visual>
<geometry>
<cylinder length="0.15" radius="0.01"/>
</geometry>
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.0"/>
<material name="red">
<color rgba="0.8 0.2 0.0 0.8"/>
</material>
</visual>
<collision>
<geometry>
<cylinder length="0.15" radius="0.01"/>
</geometry>
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.0"/>
</collision>
<inertial>
<mass value="0.02"/>
<inertia ixx="3.8e-05" ixy="0" ixz="0" iyy="3.8e-05" iyz="0" izz="1.0000000000000002e-06"/>
</inertial>
</link>
<joint name="support2base_link" type="fixed">
<parent link="base_link"/>
<child link="support"/>
<origin xyz="0.0 0.0 0.11499999999999999"/>
</joint>
<gazebo reference="support">
<material>Gazebo/White</material>
</gazebo>
<!-- 雷达安装的z坐标:支架高度 / 2 + 雷达高度 / 2 -->
<!-- 雷达质量 -->
<!-- 雷达关节以及link -->
<link name="laser">
<visual>
<geometry>
<cylinder length="0.05" radius="0.03"/>
</geometry>
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.0"/>
<material name="black"/>
</visual>
<collision>
<geometry>
<cylinder length="0.05" radius="0.03"/>
</geometry>
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.0"/>
</collision>
<inertial>
<mass value="0.1"/>
<inertia ixx="4.333333333333333e-05" ixy="0" ixz="0" iyy="4.333333333333333e-05" iyz="0" izz="4.4999999999999996e-05"/>
</inertial>
</link>
<joint name="laser2support" type="fixed">
<parent link="support"/>
<child link="laser"/>
<origin xyz="0.0 0.0 0.1"/>
</joint>
<gazebo reference="laser">
<material>Gazebo/Black</material>
</gazebo>
<!-- 控制器 -->
<gazebo>
<plugin name='diff_drive' filename='libgazebo_ros_diff_drive.so'>
<ros>
<namespace>/</namespace>
<remapping>cmd_vel:=cmd_vel</remapping>
<remapping>odom:=odom</remapping>
</ros>
<update_rate>30</update_rate>
<left_joint>left_wheel2base_link</left_joint>
<right_joint>right_wheel2base_link</right_joint>
<!-- kinematics -->
<wheel_separation>0.2</wheel_separation>
<wheel_diameter>0.065</wheel_diameter>
<!-- limits -->
<max_wheel_torque>20</max_wheel_torque>
<max_wheel_acceleration>1.0</max_wheel_acceleration>
<!-- output -->
<publish_odom>true</publish_odom>
<publish_odom_tf>true</publish_odom_tf>
<publish_wheel_tf>false</publish_wheel_tf>
<odometry_frame>odom</odometry_frame>
<robot_base_frame>base_footprint</robot_base_frame>
</plugin>
<plugin name="fishbot_joint_state" filename="libgazebo_ros_joint_state_publisher.so">
<ros>
<remapping>~/out:=joint_states</remapping>
</ros>
<update_rate>30</update_rate>
<joint_name>left_wheel2base_link</joint_name>
<joint_name>right_wheel2base_link</joint_name>
</plugin>
</gazebo>
</robot>
launch代码
import os
from launch import LaunchDescription
from launch.actions import ExecuteProcess
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare
def generate_launch_description():
package_name = 'fishbot_description'
urdf_name = "test04.urdf"
rviz_name = "demo01.rviz"
robot_name_in_model = 'fishbot'
ld = LaunchDescription()
pkg_share = FindPackageShare(package=package_name).find(package_name)
urdf_model_path = os.path.join(pkg_share, f'urdf/{urdf_name}')
rviz_path_ = os.path.join(pkg_share, f'config/{rviz_name}')
gazebo_world_path = os.path.join(pkg_share, 'world/box_house.world')
# Start Gazebo server
start_gazebo_cmd = ExecuteProcess(
cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so',gazebo_world_path],
output='screen')
# Launch the robot
spawn_entity_cmd = Node(
package='gazebo_ros',
executable='spawn_entity.py',
arguments=['-entity', robot_name_in_model, '-file', urdf_model_path ], output='screen')
# 添加机器人状态发布节点
robot_state_publisher_node = Node(
package='robot_state_publisher',
executable='robot_state_publisher',
name='robot_state_publisher',
arguments=[urdf_model_path]
)
# 可选:用于控制关节运动的节点
joint_state_publisher_gui_node = Node(
package='joint_state_publisher_gui',
executable='joint_state_publisher_gui',
name='joint_state_publisher_gui',
arguments=[urdf_model_path]
)
rviz2_node = Node(
package='rviz2',
executable='rviz2',
name='rviz2',
output='screen',
arguments=['-d', rviz_path_]
# arguments=['-d', rviz_path]
)
ld.add_action(start_gazebo_cmd)
ld.add_action(spawn_entity_cmd)
ld.add_action(robot_state_publisher_node)
# ld.add_action(joint_state_publisher_gui_node)
ld.add_action(rviz2_node)
return ld
运行launch文件后会出现警告是怎么回事呢
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "back_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-4] Warning: Invalid frame ID "front_wheel" passed to canTransform argument source_frame - frame does not exist
[rviz2-4] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
https://fishros.com/d2lros2/#/chapt4/4.10服务实现(C++)?id=_2服务端(王二)实现
上面是c++实现,我想改造成python。
代码如下
import rclpy
from rclpy.node import Node
# 1. 导入消息类型
from std_msgs.msg import String,UInt32
#从村庄接口服务类中导入卖小说服务
from village_interfaces.srv import SellNovel
#创建队列
from queue import Queue,LifoQueue,PriorityQueue
from rclpy.executors import MultiThreadedExecutor
from rclpy.callback_groups import MutuallyExclusiveCallbackGroup
from rclpy.callback_groups import CallbackGroup
class ThrottledCallbackGroup(CallbackGroup):
def __init__(self, node):
super().__init__()
# 声明服务端
node.get_logger().info('声明服务端----------------------------------')
self.sell_service = node.create_service(SellNovel,"sell_novel",node.sell_novel_callback)
self.bucket = 10
self.bucket_max = 10
def can_execute(self, entity):
"""
Ask group if this entity could be executed.
:param entity: A timer, subscriber, client, or service instance
:rtype bool: true if a callback can be executed
"""
return self.bucket > 0
def beginning_execution(self, entity):
"""
Get permission from the group to execute a callback for an entity.
:param entity: A timer, subscriber, client, or service instance
:rtype bool: true if the executor has permission to execute it
"""
with self.lock:
if self.bucket > 0:
# Take a token
self.bucket -= 1
return True
# The bucket has no tokens
return False
def ending_execution(self, entity):
"""
Notify group that a callback finished executing.
:param entity: A timer, subscriber, client, or service instance
"""
pass
def timer_callback(self):
"""Replenish the tokens in the bucket at a steady rate."""
with self.lock:
# If there is room in the bucket, add a token to it.
if self.bucket < self.bucket_max:
self.bucket += 1
class createNode(Node):
"""
对象编写
"""
def __init__(self,name):
super().__init__(name)
self.get_logger().info("大家好,我是%s,我是一名订阅者!" % name)
# 创建队列
self.queue_list = Queue(maxsize=0)
self.msg = ""
# 2.订阅李四发布的文章
self.sub_wangwu = self.create_subscription(String,"sexy_girl", self.recv_msg_callback, 10)
#3. 创建金钱发布者
self.pub_money = self.create_publisher(UInt32,"sexy_girl_money", 10)
# 声明回调组
# self.sell_novel_callback_group = ThrottledCallbackGroup(self)
self.sell_novel_callback_group = MutuallyExclusiveCallbackGroup()
self.sell_service = self.create_service(SellNovel,"sell_novel",self.sell_novel_callback,callback_group=self.sell_novel_callback_group)
def recv_msg_callback(self,msg):
"""
3. 获取订阅老四的数据
"""
self.msg = msg.data
self.get_logger().info('穷汉王五:我已经收到了李四的稿子,%s' % self.msg)
"""
4.发布金钱消息给李四
"""
money = UInt32()
money.data = 10
self.pub_money.publish(money) #将金钱发给李四
self.queue_list.put(self.msg)
# 买书请求回调函数
def sell_novel_callback(self,request, response):
# 判断当前书的数量章节够不够,不够就要攒书,在返回
# 等待queue_list里面书够 等待就会让当前线程阻塞
self.get_logger().info('收到一个买书的请求,一共给了%d 元' % request.money)
# 应给小说数量,一块钱一章
novelsNum = request.money*1
# 判断当前书库里书的数量是否满足张三要买的数量,不够则进入等待函数
if self.queue_list._qsize() < novelsNum:
self.get_logger().info('当前艳娘传奇章节存量为%d:不能满足需求,开始等待' % self.queue_list._qsize())
# 设置rate周期为1s,代表1s检查一次
rate = self.create_rate(1)
while (self.queue_list._qsize()<novelsNum):
self.get_logger().info('设置rate周期为1s,代表1s检查一次 %s' % rclpy.ok())
if rclpy.ok() == False:
self.get_logger().info('程序被终止了')
return
self.get_logger().info('等待中,目前已有%d章,还差%d章' % (self.queue_list._qsize(),novelsNum-self.queue_list._qsize()))
rclpy.spin_once(self)
rate.sleep()
self.get_logger().info('当前艳娘传奇章节存量为%d:已经满足需求' % self.queue_list._qsize())
for novel in self.queue_list:
print ('当前小说: %s'% novel)
response.novels.append(self.queue_list.get())
return response
def main(args=None):
"""
王五订阅小说给李四稿费
"""
rclpy.init(args=args) # 初始化rclpy
node = createNode("wangwu") # 新建一个节点
#rclpy.Executor.add_node(node).spin() rclcpp::executors::MultiThreadedExecutor exector;
executor = MultiThreadedExecutor()
#executor = PriorityExecutor()
executor.add_node(node)
executor.spin()
#rclpy.spin(node) # 保持节点运行,检测是否收到退出指令(Ctrl+C)
rclpy.shutdown() # 关闭rclpy
执行wangwu命令:
colcon build --packages-select village_li
source install/setup.bash
os2 run village_li wangwu_node
执行购买小说命令:
ros2 service call /sell_novel village_interfaces/srv/SellNovel "{money: 5}"
在执行作家李四命令写小说
各位大佬 卡在这个地方不动了 是代码哪儿写错了呢