王虚左Ros2课程通信机制实操(期中大作业)Python实现

在王虚左的Ros2课程通信机制实操(期中大作业)课程及源代码中,仅提供了C++版本的代码及讲解视频,但是对于目前我的需求来说,Python、rclpy更适合目前的开发任务,于是我着手进行了话题通信案例、服务通信案例、动作通信案例、参数通信案例,原本我以为最难的应该是动作通信案例,但其实由于Ros2资料较少,每一种都不好实现。(由于逻辑的讲解在视频里都有,我就不详细说了,仅提供我python的代码)

在这之中我也遇到了不少的问题,可以参考我的下述文章

Ros2参数练习中This might be a ROS 1 message type but it should be a ROS 2 message type.问题

Ros2中goal_handle状态SUCCEED及ACCEPTED及CANCLED在rclpy中的死循环(彻底解决版本)

Ros2中goal_handle状态SUCCEED及ACCEPTED在rclpy中的死循环(未彻底解决,可参考我的另一篇彻底解决篇)

Ros2中动作通信的goal_handle类型在不同回调函数中的区别

话题通信案例(乌龟同步镜像运动):

  1. 创建两个turtlesim_node节点,且需要具有不同的节点名称、话题名称,通过为turtlesim_node设置namespace解决;

  2. 乌龟掉头通过调用turtlesim_node内置的action功能来实现乌龟航向的设置;

  3. 订阅乌龟1的速度并生成发布控制乌龟2运动的速度指令,需要订阅乌龟1的位姿相关话题来获取乌龟1的速度,并且按照“镜像运动”的需求生成乌龟2的速度指令,并且该节点需要在掉头完毕后启动。

  4. 最后,通过launch文件集成节点。

$ ros2 pkg create py07_exercise --build-type ament_python --dependencies rclpy turtlesim base_interfaces_demo geometry_msgs rclpy_action --node-name exer01_pub_sub

launch添加(在setup.py中)

    data_files=[
        ("share/ament_index/resource_index/packages", ["resource/" + package_name]),
        ("share/" + package_name, ["package.xml"]),
        # 下面三种都可以,第二种比较省事
        # ('share/' + package_name, ['launch/exe01_pub_sub.launch.py'])
        (
            os.path.join("share/", package_name, "launch"),
            glob(os.path.join("launch", "*.launch.py")),
        # 或
        '''
        ('share/' + package_name, glob("launch/py/*.launch.py")),
        ('share/' + package_name, glob("launch/xml/*.launch.xml")),
        ('share/' + package_name, glob("launch/yaml/*.launch.yaml")) 
        '''
        ),
    ],

若出现不正确问题,一定是launch文件中添加错误,如launch.py写成launch,py等

在Vscode编译launch文件,出现错误,没关系,在terminal中启动就行,Vscode某些时候权限可能不够,在terminal中构建就行

$ colcon build --packages-select py07_exercise

启动两个turtlesim_node节点,节点2中的乌龟自动调头180°,我们可以通过键盘控制节点1中的乌龟运动,但是不能控制节点2的乌龟,需要自实现功能:可以根据乌龟1的速度生成并发布控制乌龟2运动的速度指令,最终两只乌龟做镜像运动。

exer01_pub_sub.launch.py

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import ExecuteProcess, RegisterEventHandler
from launch.event_handlers import OnProcessExit

def generate_launch_description():
    t1 = Node(package="turtlesim", executable="turtlesim_node")
    t2 = Node(package="turtlesim", executable="turtlesim_node", namespace="t2")

    rotate = ExecuteProcess(
        cmd=[
            "ros2 action send_goal /t2/turtle1/rotate_absolute turtlesim/action/RotateAbsolute \"{'theta': 3.14}\""
        ],
        output="both",
        shell=True,
    )
    exer01 = Node(package="py07_exercise", executable="exer01_pub_sub")
    register_rotate_exit_event = RegisterEventHandler(
        event_handler=OnProcessExit(target_action=rotate, on_exit=exer01)
    )

    return LaunchDescription([t1, t2, rotate, register_rotate_exit_event])

exer01_pub_sub.py

import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist
from turtlesim.msg import Pose

class Exer01PubSub(Node):
    def __init__(self):
        super().__init__("exer01pubsub_node_py")
        self.get_logger().info("Exer01PubSub create!")
        self.twist_pub = self.create_publisher(Twist, "/t2/turtle1/cmd_vel", 10)
        self.pose_sub = self.create_subscription(
            Pose, "/turtle1/pose", self.pose_cb, 10
        )

    def pose_cb(self, pose):
        geo_tw = Twist()
        geo_tw.linear.x = pose.linear_velocity
        geo_tw.angular.z = -pose.angular_velocity
        self.twist_pub.publish(geo_tw)

def main():
    rclpy.init()
    rclp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值