当前位置:   article > 正文

Webots Python 绘制机器人运动轨迹 无人车 无人机_运动轨迹图怎么弄webots

运动轨迹图怎么弄webots

前言

本代码由 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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

使用方法

  1. self.num_agents = 1 修改机器人数量;
  2. 需要在环境里将机器人 Define 别名: ROBOT0, ROBOT1, ROBOT2…,以对应代码 getFromDef(f"ROBOT{ind}")
  3. 一般你会在 supervisor.py 里创建一个全局的 class 继承 Supervisor,因此只需要将上面的代码融合进你的代码即可
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/1008181
推荐阅读
相关标签
  

闽ICP备14008679号