ROS/ROS2机器人命令(cli)和基础编程(rclpy)的高效学习方法

Posted zhangrelay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS/ROS2机器人命令(cli)和基础编程(rclpy)的高效学习方法相关的知识,希望对你有一定的参考价值。


啰啰嗦嗦讲满整整51分钟的直播回放如下:


官方-主题教程:

  • ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

官方-发布和订阅(Python):

发布源码:

import rclpy
from rclpy.node import Node

from std_msgs.msg import String


class MinimalPublisher(Node):

    def __init__(self):
        super().__init__('minimal_publisher')
        self.publisher_ = self.create_publisher(String, 'topic', 10)
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        msg = String()
        msg.data = 'Hello World: %d' % self.i
        self.publisher_.publish(msg)
        self.get_logger().info('Publishing: "%s"' % msg.data)
        self.i += 1


def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

订阅源码:

import rclpy
from rclpy.node import Node

from std_msgs.msg import String


class MinimalSubscriber(Node):

    def __init__(self):
        super().__init__('minimal_subscriber')
        self.subscription = self.create_subscription(
            String,
            'topic',
            self.listener_callback,
            10)
        self.subscription  # prevent unused variable warning

    def listener_callback(self, msg):
        self.get_logger().info('I heard: "%s"' % msg.data)


def main(args=None):
    rclpy.init(args=args)

    minimal_subscriber = MinimalSubscriber()

    rclpy.spin(minimal_subscriber)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_subscriber.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

通过3年多ROS2教学发现,学生掌握这些非常轻松,但是知识迁移的能力欠缺,此处说明。

比如使用主题发布字符串如何实现:

  • 参考python代码思考

from std_msgs.msg import String

msg.data = 'Hello World: %d' % self.i

ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

改为:

字符串消息发布
ros2 topic pub -r 1 /py_msg std_msgs/msg/String "{ data: My Hello World }"

字符串消息订阅
ros2 topic echo /py_msg

同样类比一下:

geometry_msgs/msg/Twist

from geometry_msgs.msg import Twist

速度消息发布如何使用python代码实现

import rclpy
from rclpy.node import Node

# from std_msgs.msg import String
from geometry_msgs.msg import Twist



class MinimalPublisher(Node):

    def __init__(self):
        super().__init__('my_pub')
        self.publisher_ = self.create_publisher(Twist, 'turtle1/cmd_vel', 10)
        timer_period = 1  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        vel = Twist()
#        vel.data = 'My Hello World: %d' % self.i
        vel.linear.x=1.0
        vel.angular.z=0.01*self.i
        self.publisher_.publish(vel)
        self.get_logger().info('Publishing: x"%f"' % vel.linear.x)
        self.get_logger().info('Publishing: z"%f"' % vel.angular.z)
        self.i += 1


def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

这种思考和研究的方法,是通用的,掌握了学习官方文档的速度和效率都会快很多。


-End- 


以上是关于ROS/ROS2机器人命令(cli)和基础编程(rclpy)的高效学习方法的主要内容,如果未能解决你的问题,请参考以下文章

ROS1/ROS2机器人编程光速入门指南(2021)

ROS1/ROS2机器人编程光速入门指南(2021)

-Launchers .3.3

-Launchers .3.3

ROS/ROS2机器人操作系统行业应用情况(引用,含教育)

如何才能克服学习和研究ROS1/ROS2机器人中遇到的困难?