ros调试工具foxglove使用指南三:在3d空间写写画画(Panel->3D ->Scene entity)

目录

前言:

一、安装foxglove消息包(ros1)

[二、Scene entity 介绍](#二、Scene entity 介绍)

三、SceneUpdate介绍

四、实际的应用

五、代码解释

六、实验结果


前言:

如果你想要在3d空间空间里面显示一些东西,比如文字,比如一些形状,而且想控制这些东西的显示时间,那么可以使用本文提到的方法。

一、安装foxglove消息包(ros1)

参考链接:Introduction | Foxglove Docs

复制代码
sudo apt install ros-noetic-foxglove-msgs

二、Scene entity 介绍

参考链接:3D | Foxglove Docs

一组基本形状(立方体、球体、文本、网格、线条等),用于显示从基本边界框到复杂的3D决策树或道路网络的任何内容。

Scene entity必须包装在SceneUpdate消息中。下面文档中Scene entity介绍的具体位置。

三、SceneUpdate介绍

参考连接: SceneUpdate | Foxglove Docs

SceneUpdate的数据结构如下, 里面有两个属性,entities和deletions,其中entities是用来显示物体的,deletions使用来删除物体的。这里重点介绍entities,是一个SceneEntity的列表,代表SceneUpdate可以一下子显示多个物体。

四、实际的应用

参考连接:SceneEntity | Foxglove Docs

TextPrimitive | Foxglove Docs

我想使用ros1在3d空间里面的某一个位置,显示一些文字。Scene entity的数据结构如下,显示一定的时间,我需要利用的是frame_id, life_time,texts。texts的数据结构如下面的第二幅截图。

五、代码解释

python 复制代码
#!/usr/bin/env python
import rospy
from foxglove_msgs.msg import SceneUpdate, SceneEntity
from foxglove_msgs.msg import TextPrimitive  # 导入 TextPrimitive
from rospy import Duration  # 导入 Duration 用于设置生命周期
import time


def publish_text(text, publisher):
    """
    发布包含指定文本的 SceneUpdate 消息
    :param text: 要发布的文本信息
    :param publisher: 消息发布者对象
    """
    msg = SceneUpdate()
    entity = SceneEntity()
    text_primitive = TextPrimitive()
    text_primitive.text = text
    text_primitive.font_size = 1.0  # 设置字体大小
    text_primitive.color.r = 1.0
    text_primitive.color.g = 0.0
    text_primitive.color.b = 0.0
    text_primitive.color.a = 1.0  # 设置透明度
    text_primitive.pose.position.x = 0.0
    text_primitive.pose.position.y = 0.0
    text_primitive.pose.position.z = 2.0  # 文本在 z 轴方向上保持不变
    entity.texts.append(text_primitive)
    entity.id = "text_entity"
    entity.frame_id = "map"
    if text:
        # 设置文本显示 2 秒
        entity.lifetime = Duration(2)
    else:
        # 清空文本时生命周期设为 0
        entity.lifetime = Duration(0)
    msg.entities.append(entity)
    publisher.publish(msg)
    # if text:
    #     rospy.loginfo(f'发布文本信息: {text}')
    # else:
    #     rospy.loginfo('清空文本信息')


def main():
    rospy.init_node('text_scene_update_publisher', anonymous=True)
    # 创建发布者,发布 SceneUpdate 消息到 'text_scene_update_topic' 话题
    publisher = rospy.Publisher('text_scene_update_topic', SceneUpdate, queue_size=10)
    rate = rospy.Rate(0.5)  # 每 2 秒执行一次循环

    text_to_display = "您撞到墙上了!"
    display_duration = 10
    while not rospy.is_shutdown():
        # 发布显示文本的消息
        publish_text(text_to_display, publisher)
        # 等待 2 秒
        time.sleep(display_duration)
        # 发布清空文本的消息
        rate.sleep()


if __name__ == '__main__':
    try:
        main()
    except rospy.ROSInterruptException:
        pass

六、实验结果

​​

相关推荐
江华森10 分钟前
Python 实现高德地图找房(一):环境搭建与数据爬虫
开发语言·爬虫·python
VIP_CQCRE41 分钟前
用 Ace Data Cloud 快速接入 OpenAI Chat Completions:对话、流式、多轮和多模态一篇打通
python·ai·openai·api·教程
心中有国也有家1 小时前
AtomGit Flutter 鸿蒙客户端:呼吸练习的完整生命周期
学习·flutter·华为·harmonyos
Zeeland1 小时前
构建护城河:自学习 Agent
学习
老迟到的茉莉1 小时前
Hermes 是谁?跟 Claude Code 差在哪
开发语言·python
brave_zhao2 小时前
nginx的进程架构
java·学习·nginx
枫零NET2 小时前
跟着OpenCode学习Pi Coding Agent-05-Agent的类型系统
人工智能·学习
Day(AKA Elin)2 小时前
【Day】MTP(Multi Token Prediction)技术学习
python·深度学习·学习·llama
Waay2 小时前
Linux 三个核心环境变量配置文件、作用域、生效方式完整梳理
linux·运维·学习·云原生·容器
枫零NET2 小时前
跟着OpenCode学习Pi Coding Agent-06-Agent循环
学习