Webots Python 绘制机器人运动轨迹 无人车 无人机

前言

本代码由 Webots supervisor_draw_trail.wbt 示例 world 里的 supervisor_draw_trail.c 改写而来,并添加了对多智能体(多机器人)运动轨迹绘制的支持

在自定义环境里的运行效果:

在这里插入图片描述

代码

from controller import Supervisor


class PathDrawer(Supervisor):
    def __init__(self):
        super().__init__()
        self.MAXIMUM_NUMBER_OF_COORDINATES = 20000
        self.REFRESH_FACTOR = 10
        self.index = 0
        self.first_step = True
        
        # ---- 修改为你的机器人数量 ----
        self.num_agents = 1      
        # ------------------------------

        for ind in range(self.num_agents):
            self.path_str = ""
            self.create_trail_shape(ind)
        self.get_nodes_and_fileds()

        # Get the target object node to track
        self.target_node = []
        for ind in range(self.num_agents):
            # ---- 需要在环境里将机器人 Define 别名: ROBOT0, ROBOT1, ROBOT2... -
            self.target_node.append(self.getFromDef(f"ROBOT{ind}"))  
            # -----------------------------------------------------------------

    def strcat(self, content):
        self.path_str += content

    def create_trail_shape(self, agent_ind):
        # If TRAIL exists in the world then silently remove it.
        existing_trail = self.getFromDef(f"TRAIL{agent_ind}")
        if (existing_trail):
            existing_trail.remove()

        # Create the TRAIL Shape.
        tmp_str = "{\n"
        self.strcat(f"DEF TRAIL{agent_ind} Shape {tmp_str}")
        self.strcat("  appearance Appearance {\n")
        self.strcat("    material Material {\n")
        self.strcat("      ambientIntensity 1\n")
        self.strcat("      diffuseColor 0 1 0\n")
        self.strcat("      emissiveColor 0 1 0\n")
        self.strcat("    }\n")
        self.strcat("  }\n")
        self.strcat(f"  geometry DEF TRAIL_LINE_SET{agent_ind} IndexedLineSet {tmp_str}")
        self.strcat("    coord Coordinate {\n")
        self.strcat("      point [\n")
        for _ in range(self.MAXIMUM_NUMBER_OF_COORDINATES):
            self.strcat("      0 0 0\n")
        self.strcat("      ]\n")
        self.strcat("    }\n")
        self.strcat("    coordIndex [\n")
        for _ in range(self.MAXIMUM_NUMBER_OF_COORDINATES):
            self.strcat("      0 0 -1\n")
        self.strcat("    ]\n")
        self.strcat("  }\n")
        self.strcat("}\n")

        # Import TRAIL and append it as the world root nodes.
        root_children_field = self.getRoot().getField("children")
        root_children_field.importMFNodeFromString(-1, self.path_str)
        print(f"已添加新路径节点{agent_ind}")

    def get_nodes_and_fileds(self):
        self.point_field_all = []
        self.coord_index_field_all = []
        for ind in range(self.num_agents):
            self.trail_line_set_node = self.getFromDef(f"TRAIL_LINE_SET{ind}")
            self.coordinates_node = self.trail_line_set_node.getField("coord").getSFNode()
            self.point_field = self.coordinates_node.getField("point")
            self.point_field_all.append(self.point_field)
            self.coord_index_field = self.trail_line_set_node.getField("coordIndex")
            self.coord_index_field_all.append(self.coord_index_field)

    def update_trail(self):
        for ind in range(self.num_agents):
            # Get the current target translation.
            target_translation = self.target_node[ind].getPosition()
            print(target_translation)
            # Add the new target translation in the line set.
            self.point_field_all[ind].setMFVec3f(self.index, target_translation)
            # Update the line set indices.
            if self.index > 0:
                # Link successive indices.
                self.coord_index_field_all[ind].setMFInt32(3 * (self.index - 1), self.index - 1)
                self.coord_index_field_all[ind].setMFInt32(3 * (self.index - 1) + 1, self.index)
            elif (self.index == 0 and self.first_step == False):
                # Link the first and the last indices.
                self.coord_index_field_all[ind].setMFInt32(3 * (self.MAXIMUM_NUMBER_OF_COORDINATES - 1), 0)
                self.coord_index_field_all[ind].setMFInt32(3 * (self.MAXIMUM_NUMBER_OF_COORDINATES - 1) + 1,
                                                self.MAXIMUM_NUMBER_OF_COORDINATES - 1)
            # Unset the next indices.
            self.coord_index_field_all[ind].setMFInt32(3 * self.index, self.index)
            self.coord_index_field_all[ind].setMFInt32(3 * self.index + 1, self.index)

        self.index += 1
        self.index = self.index % self.MAXIMUM_NUMBER_OF_COORDINATES
        self.first_step = False


if __name__ == "__main__":
    supervisor = PathDrawer()
    while supervisor.step(32 * 10) != -1:
        supervisor.update_trail()

使用方法

  1. self.num_agents = 1 修改机器人数量;
  2. 需要在环境里将机器人 Define 别名: ROBOT0, ROBOT1, ROBOT2…,以对应代码 getFromDef(f"ROBOT{ind}")
  3. 一般你会在 supervisor.py 里创建一个全局的 class 继承 Supervisor,因此只需要将上面的代码融合进你的代码即可
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值