0
点赞
收藏
分享

微信扫一扫

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


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

  • ​​ROS2官方教程14 (dingtalk.com)ROS/ROS2机器人命令(cli)和基础编程(rclpy)的高效学习方法_机器人操作系统​​https://h5.dingtalk.com/group-live-share/index.htm?encCid=41b6de7bfdba9689f8fc78dd4aafa8c2&liveUuid=b6aecc5b-b447-4593-b9c4-fbd25a805a50#/​​​

官方-主题教程:

  • 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- 


举报

相关推荐

0 条评论