销 | 量 | 过 | 万 |
---|---|---|---|
TEEIS德国护膝夏天用薄款 优惠券 | 冠生园 百花蜂蜜428g 挤压瓶纯蜂蜜 | 巨奇严选 鞋子除臭剂360ml | 多芬身体磨砂膏280g |
健70%-75%酒精消毒棉片湿巾1418cm 80片/袋3袋大包清洁食品用消毒 优惠券 | AIMORNY52朵红玫瑰永生香皂花同城配送非鲜花七夕情人节生日礼物送女友 热卖 | 妙洁棉柔抹布10片装 厨房洗碗布家用神器 去油污强力吸水巾 | 宝宝馋了婴幼儿酸奶无添加白砂糖85g*10袋 儿童常温奶宝宝辅食饮品 |
素养生活 有机红花生400g 生花生米东北四粒红花生 凉拌 杂粮粗粮真空包装 | UG NX 12.0中文版从入门到精通ug nx建模曲面钣金装配工程图 有限元分析 机械设计 数控加工编程 autocad教程cad教材自学版完全自学宝典 | 乐品乐茶茶叶绿茶特级毛尖2025新茶明前春茶嫩芽散装自己喝京东自营 | 松鲜鲜松茸鲜调味料125g【0添加 松茸提鲜】代替盐鸡精味精煲汤炒菜调味 优惠券 |
本迪大号加厚洗脸盆2只装36cm洗脸盆洗菜盆洗脚盆学生塑料盆泡脚盆 | 公牛(BULL)开关插座 G12系列 十孔插座86型插座面板 G12Z423 白色 | 周十五益生菌蜂蜜露孕妇开塞蜜露待产包用产后哺乳期儿童蜂蜜栓礼品36支 | 小鹿蓝蓝婴幼儿肉蔬多维蝴蝶面600g含30小袋婴儿辅食果蔬营养面超值装 优惠券 |
ROS2(Robot Operating System 2) 是一个开源的机器人中间件框架,用于构建复杂的机器人系统。相比 ROS1,ROS2 在实时性、安全性、跨平台支持和分布式架构上进行了全面升级。
import rclpy
from rclpy.node import Node
class MyNode(Node):
def __init__(self):
super().__init__('my_node_name')
self.get_logger().info("Node started!")
def main():
rclpy.init()
node = MyNode()
rclpy.spin(node) # 保持节点运行
rclpy.shutdown()
# 发布者
from std_msgs.msg import String
publisher = self.create_publisher(String, 'chatter', 10)
publisher.publish(String(data="Hello ROS2"))
# 订阅者
def callback(msg):
self.get_logger().info(f"Received: {msg.data}")
self.create_subscription(String, 'chatter', callback, 10)
# 服务端
from example_interfaces.srv import AddTwoInts
def add(request, response):
response.sum = request.a + request.b
return response
self.srv = self.create_service(AddTwoInts, 'add_two_ints', add)
# 客户端
client = self.create_client(AddTwoInts, 'add_two_ints')
request = AddTwoInts.Request(a=2, b=3)
future = client.call_async(request)
# 声明参数
self.declare_parameter('speed', 0.5)
# 读取参数
speed = self.get_parameter('speed').value
ros2 pkg create my_py_pkg --build-type ament_python
保存到 my_py_pkg/my_py_pkg/node_script.py
在 setup.py
中添加:
entry_points={
'console_scripts': ['my_node = my_py_pkg.node_script:main'],
}
colcon build --packages-select my_py_pkg
source install/setup.bash
ros2 run my_py_pkg my_node
工具 | 命令示例 | 用途 |
---|---|---|
rqt | rqt |
图形化调试工具 |
ros2 topic | ros2 topic list |
查看活跃话题 |
ros2 node | ros2 node info /node_name |
节点信息 |
ros2 bag | ros2 bag record /topic |
数据录制与回放 |
ros2 --help
ros2 run demo_nodes_py talker/listener
提示:使用
colcon build
编译包,通过source install/setup.bash
激活环境。
ROS2 广泛应用于现代机器人系统的开发,涵盖以下领域:
# 机械臂关节控制示例
from control_msgs.action import FollowJointTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
class RobotArmController(Node):
def __init__(self):
super().__init__('arm_controller')
self._action_client = ActionClient(self, FollowJointTrajectory, '/arm_controller/follow_joint_trajectory')
def move_to_position(self, positions):
goal_msg = FollowJointTrajectory.Goal()
point = JointTrajectoryPoint()
point.positions = positions # [rad]
point.time_from_start = Duration(seconds=2).to_msg()
goal_msg.trajectory.points.append(point)
self._action_client.send_goal_async(goal_msg)
# 激光雷达数据处理
from sensor_msgs.msg import LaserScan
class ObstacleDetector(Node):
def __init__(self):
super().__init__('obstacle_detector')
self.subscription = self.create_subscription(
LaserScan,
'/scan',
self.scan_callback,
10)
def scan_callback(self, msg):
# 检测前方障碍物(简化示例)
front_scan = msg.ranges[len(msg.ranges)//4:3*len(msg.ranges)//4]
if min(front_scan) < 1.0: # 1米内有障碍物
self.get_logger().warn("Obstacle detected!")
# 自主导航到目标点
from nav2_msgs.action import NavigateToPose
from geometry_msgs.msg import PoseStamped
class DeliveryRobot(Node):
def send_to_location(self, x, y):
goal_pose = PoseStamped()
goal_pose.header.frame_id = 'map'
goal_pose.pose.position.x = x
goal_pose.pose.position.y = y
goal_msg = NavigateToPose.Goal()
goal_msg.pose = goal_pose
self.nav_client.wait_for_server()
self.nav_client.send_goal_async(goal_msg)
# 无人机航点飞行
from geographic_msgs.msg import GeoPoseStamped
from mavros_msgs.srv import CommandBool
class DroneController(Node):
async def fly_waypoints(self, coordinates):
# 解锁无人机
arm = self.create_client(CommandBool, '/mavros/cmd/arming')
await arm.call_async(CommandBool.Request(value=True))
# 发送航点
for lat, lon, alt in coordinates:
goal = GeoPoseStamped()
goal.position.latitude = lat
goal.position.longitude = lon
goal.position.altitude = alt
self.waypoint_publisher.publish(goal)
await asyncio.sleep(5) # 等待到达
# 手术器械精准控制
from sensor_msgs.msg import Joy
class SurgicalController(Node):
def __init__(self):
self.sub = self.create_subscription(Joy, '/surgical_joystick', self.joy_callback, 10)
self.tool_pub = self.create_publisher(Twist, '/surgical_tool/control', 10)
def joy_callback(self, msg):
# 将游戏手柄输入转换为器械控制
control = Twist()
control.linear.x = msg.axes[1] * 0.1 # 精度控制
control.linear.y = msg.axes[0] * 0.1
self.tool_pub.publish(control)
# warehouse_robot.py
import rclpy
from rclpy.action import ActionClient
from rclpy.node import Node
from nav2_msgs.action import NavigateToPose
from std_msgs.msg import String
from warehouse_interfaces.srv import ItemRequest
class WarehouseRobot(Node):
def __init__(self):
super().__init__('warehouse_robot')
# 导航客户端
self.nav_client = ActionClient(self, NavigateToPose, 'navigate_to_pose')
# 库存服务
self.inventory_client = self.create_client(ItemRequest, 'item_location')
# 任务订阅
self.create_subscription(String, 'delivery_tasks', self.task_callback, 10)
# 当前状态
self.current_task = None
async def task_callback(self, msg):
item_id = msg.data
self.get_logger().info(f"New task: Fetch {item_id}")
# 查询物品位置
item_loc = await self.get_item_location(item_id)
if not item_loc:
self.get_logger().error(f"Item {item_id} not found!")
return
# 导航到物品位置
await self.navigate_to(item_loc.x, item_loc.y)
# 模拟拾取操作
await asyncio.sleep(2)
# 导航到发货区
await self.navigate_to(5.0, 3.0)
self.get_logger().info(f"Item {item_id} delivered!")
async def get_item_location(self, item_id):
req = ItemRequest.Request()
req.item_id = item_id
future = self.inventory_client.call_async(req)
await future
return future.result().location
async def navigate_to(self, x, y):
goal_pose = PoseStamped()
goal_pose.header.frame_id = 'map'
goal_pose.pose.position.x = x
goal_pose.pose.position.y = y
goal_msg = NavigateToPose.Goal()
goal_msg.pose = goal_pose
self.nav_client.wait_for_server()
send_goal_future = self.nav_client.send_goal_async(goal_msg)
await send_goal_future
goal_handle = send_goal_future.result()
# 等待导航完成
result_future = goal_handle.get_result_async()
await result_future
return result_future.result().result
+-------------------+ +----------------+ +-----------------+
| 任务管理系统 | | 导航系统 | | 库存数据库 |
| (发布任务) |---->| (路径规划/避障)|<----| (物品位置查询) |
+-------------------+ +-------+--------+ +-----------------+
|
+-------v--------+
| 机器人执行器 |
| - 移动底盘 |
| - 机械臂 |
| - 传感器 |
+----------------+
组件化设计
# 独立导航组件
class NavigationComponent(Node):
def __init__(self):
super().__init__('navigation_component')
# ... 导航实现 ...
# 独立任务处理组件
class TaskHandlerComponent(Node):
def __init__(self):
super().__init__('task_handler')
# ... 任务处理 ...
QoS策略配置
from rclpy.qos import QoSProfile, QoSReliabilityPolicy
# 确保关键指令可靠传输
reliable_qos = QoSProfile(
depth=10,
reliability=QoSReliabilityPolicy.RELIABLE
)
self.cmd_pub = self.create_publisher(
Twist,
'/cmd_vel',
reliable_qos
)
生命周期管理
from rclpy.lifecycle import LifecycleNode
class SafetyMonitor(LifecycleNode):
def on_activate(self):
self.get_logger().info("Safety system activated")
# 启动监控线程
def on_deactivate(self):
self.get_logger().info("Safety system deactivated")
# 停止监控
实际开发建议:
- 使用
ros2 launch
管理复杂系统启动- 利用
ros2 bag
记录和回放传感器数据- 使用
ros2 doctor
定期检查系统健康状态- 通过
ros2 security
启用通信加密- 使用
ros2 tracing
进行性能分析
完整项目示例参考:ROS2 Warehouse Robot Demo
官方进阶教程:ROS2 Navigation Guide
书名 | 出版社 | 推荐 |
---|---|---|
Python编程 从入门到实践 第3版(图灵出品) | 人民邮电出版社 | ★★★★★ |
Python数据科学手册(第2版)(图灵出品) | 人民邮电出版社 | ★★★★★ |
图形引擎开发入门:基于Python语言 | 电子工业出版社 | ★★★★★ |
科研论文配图绘制指南 基于Python(异步图书出品) | 人民邮电出版社 | ★★★★★ |
Effective Python:编写好Python的90个有效方法(第2版 英文版) | 人民邮电出版社 | ★★★★★ |
Python人工智能与机器学习(套装全5册) | 清华大学出版社 | ★★★★★ |
书名 | 出版社 | 推荐 |
---|---|---|
Java核心技术 第12版:卷Ⅰ+卷Ⅱ | 机械工业出版社 | ★★★★★ |
Java核心技术 第11版 套装共2册 | 机械工业出版社 | ★★★★★ |
Java语言程序设计基础篇+进阶篇 原书第12版 套装共2册 | 机械工业出版社 | ★★★★★ |
Java 11官方参考手册(第11版) | 清华大学出版社 | ★★★★★ |
Offer来了:Java面试核心知识点精讲(第2版)(博文视点出品) | 电子工业出版社 | ★★★★★ |