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

更新时间:2023-07-20 00:40:31 阅读: 评论:0

ROSROS2机器⼈命令(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):
发布源码:
1import rclpy
de import Node
3
耶鲁大学公开课下载
4from std_msgs.msg import String
5
6
7class MinimalPublisher(Node):
8
9    def __init__(lf):
10        super().__init__('minimal_publisher')
11        lf.publisher_ = lf.create_publisher(String, 'topic', 10)
12        timer_period = 0.5  # conds
13        lf.timer = lf.create_timer(timer_period, lf.timer_callback)
14        lf.i = 0
15
16    def timer_callback(lf):
17        msg = String()
18        msg.data = 'Hello World: %d' % lf.i
19        lf.publisher_.publish(msg)
20        lf.get_logger().info('Publishing: "%s"' % msg.data)
21        lf.i += 1
22
23
24def main(args=None):
初二英语单词表25    rclpy.init(args=args)
26
27    minimal_publisher = MinimalPublisher()
28
29    rclpy.spin(minimal_publisher)
30
英语文章阅读31    # Destroy the node explicitly
32    # (optional - otherwi it will be done automatically
33    # when the garbage collector destroys the node object)
34    minimal_publisher.destroy_node()
35    rclpy.shutdown()
36
37
38if __name__ == '__main__':
39    main()
订阅源码:
1import rclpy
de import Node
3
4from std_msgs.msg import String
5
6
7class MinimalSubscriber(Node):
8
9    def __init__(lf):
10        super().__init__('minimal_subscriber')
11        lf.subscription = lf.create_subscription(
12            String,
13            'topic',
教育部考试中心托福网考网上报名
pumpkin14            lf.listener_callback,
四级听力在线练习15            10)
16        lf.subscription  # prevent unud variable warning
17
18    def listener_callback(lf, msg):
19        lf.get_logger().info('I heard: "%s"' % msg.data)
20
21
22def main(args=None):
23    rclpy.init(args=args)
24
25    minimal_subscriber = MinimalSubscriber()
26
27    rclpy.spin(minimal_subscriber)
28
29    # Destroy the node explicitly
30    # (optional - otherwi it will be done automatically
31    # when the garbage collector destroys the node object)
32    minimal_subscriber.destroy_node()
33    rclpy.shutdown()英语聊天室
34
35
36if __name__ == '__main__':
37    main()
通过3年多ROS2教学发现,学⽣掌握这些⾮常轻松,但是知识迁移的能⼒⽋缺,此处说明。
⽐如使⽤主题发布字符串如何实现:
参考python代码思考
from std_msgs.msg import String
msg.data = 'Hello World: %d' % lf.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代码实现
1import rclpy
de import Node
3
4# from std_msgs.msg import String
5from geometry_msgs.msg import Twist
6
7
8
9class MinimalPublisher(Node):
10
11    def __init__(lf):
12        super().__init__('my_pub')
13        lf.publisher_ = lf.create_publisher(Twist, 'turtle1/cmd_vel', 10)
14        timer_period = 1  # conds
15        lf.timer = lf.create_timer(timer_period, lf.timer_callback)
16        lf.i = 0
17
小学生课堂游戏18    def timer_callback(lf):
19        vel = Twist()
20#        vel.data = 'My Hello World: %d' % lf.i
21        vel.linear.x=1.0
22        vel.angular.z=0.01*lf.i
23        lf.publisher_.publish(vel)
24        lf.get_logger().info('Publishing: x"%f"' % vel.linear.x)
25        lf.get_logger().info('Publishing: z"%f"' % vel.angular.z)
26        lf.i += 1
27
28
29def main(args=None):
30    rclpy.init(args=args)中国教育考试院官网
31
32    minimal_publisher = MinimalPublisher()
33
34    rclpy.spin(minimal_publisher)
35
36    # Destroy the node explicitly
37    # (optional - otherwi it will be done automatically
38    # when the garbage collector destroys the node object)
39    minimal_publisher.destroy_node()
40    rclpy.shutdown()
41
42
43if __name__ == '__main__':
44    main()
这种思考和研究的⽅法,是通⽤的,掌握了学习官⽅⽂档的速度和效率都会快很多。-End-

本文发布于:2023-07-20 00:40:31,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/182707.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:发布   字符串   消息
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图