点云发布(pointcloud_publisher)之ROS2案例

举报
zhangrelay 发表于 2021/10/10 23:41:40 2021/10/10
【摘要】 二维曲线绘制玩腻了,可以来搞一搞三维点云了,效果更棒哦。 官方示例给出了点云发布案例效果很好。 致敬Matlab 官方原版 源代码如下: # Copyright 2020 Evan Flynn## Licensed under the Apac...

二维曲线绘制玩腻了,可以来搞一搞三维点云了,效果更棒哦。


官方示例给出了点云发布案例效果很好。

致敬Matlab

官方原版

源代码如下:


  
  1. # Copyright 2020 Evan Flynn
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import numpy as np
  15. import rclpy
  16. from rclpy.node import Node
  17. from sensor_msgs.msg import PointCloud2
  18. from sensor_msgs.msg import PointField
  19. from sensor_msgs_py import point_cloud2
  20. from std_msgs.msg import Header
  21. class PointCloudPublisher(Node):
  22. rate = 30
  23. moving = True
  24. width = 100
  25. height = 100
  26. header = Header()
  27. header.frame_id = 'map'
  28. dtype = PointField.FLOAT32
  29. point_step = 16
  30. fields = [PointField(name='x', offset=0, datatype=dtype, count=1),
  31. PointField(name='y', offset=4, datatype=dtype, count=1),
  32. PointField(name='z', offset=8, datatype=dtype, count=1),
  33. PointField(name='intensity', offset=12, datatype=dtype, count=1)]
  34. def __init__(self):
  35. super().__init__('pc_publisher')
  36. self.publisher_ = self.create_publisher(PointCloud2, 'test_cloud', 10)
  37. timer_period = 1 / self.rate
  38. self.timer = self.create_timer(timer_period, self.timer_callback)
  39. self.counter = 0
  40. def timer_callback(self):
  41. self.header.stamp = self.get_clock().now().to_msg()
  42. x, y = np.meshgrid(np.linspace(-2, 2, self.width), np.linspace(-2, 2, self.height))
  43. z = 0.5 * np.sin(2*x-self.counter/10.0) * np.sin(2*y)
  44. points = np.array([x, y, z, z]).reshape(4, -1).T
  45. pc2_msg = point_cloud2.create_cloud(self.header, self.fields, points)
  46. self.publisher_.publish(pc2_msg)
  47. if self.moving:
  48. self.counter += 1
  49. def main(args=None):
  50. rclpy.init(args=args)
  51. pc_publisher = PointCloudPublisher()
  52. rclpy.spin(pc_publisher)
  53. pc_publisher.destroy_node()
  54. rclpy.shutdown()
  55. if __name__ == '__main__':
  56. main()

参考如上代码可以给出很多点云效果的。

z = np.sin(x)
 

 供参考


z = np.sqrt(8-x*x-y*y)
 

供参考


z = 0.2*y * np.sin(2*x-self.counter/10.0) + 0.2*x * np.cos(2*y+self.counter/10.0)
 

供参考 


 思考题:

        r = np.sqrt(4*x*x+4*y*y)
        z = np.sin(r)/r


致敬matlab


-Fin-


文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/120641613

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。