🔋 驱动器(Actuator)#

驱动器 (Actuator) 是控制机器人关节运动的核心组件,MotrixSim 支持多种类型的驱动器。

每个驱动器都可以配置不同的类型和参数,以适应不同的应用场景。 驱动器可以是马达、位置控制器、速度控制器或通用控制器等。它们通过设置目标位置、速度或其他参数来实现对机器人的精确控制。

现支持的驱动器类型有:

类型

解释

马达(Motor)

用于驱动机器人的关节,提供基本的运动能力。

位置(Position)

精确控制关节的角度或位置。

速度(Velocity)

控制关节的运动速度。

通用(General)

提供更灵活的控制方式,可以根据需要自定义控制策略。

设置可兼容 MuJoCo actuator 的参数。

General 部分属性未支持,参照 支持列表

驱动器示例#

首先加载模型 model 并通过 model.get_actuator 方法获取指定的 Actuator,参数可以是驱动器的名称或索引。 然后通过 actuator.set_ctrl 方法设置控制目标值。

索引与文件中定义顺序一致,可通过 model.actuator_names 方法获取所有驱动器的名称列表。

以下是涵盖上述所有内容的完整代码脚本:

# Copyright (C) 2020-2025 Motphys Technology Co., Ltd. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import time

from motrixsim import SceneData, load_model, step
from motrixsim.render import RenderApp

# Mouse controls:
# - Press and hold left button then drag to rotate the camera/view
# - Press and hold right button then drag to pan/translate the view


def main():
    # Create render window for visualization
    render = RenderApp()
    # The scene description file
    path = "examples/assets/actuator.xml"
    # Load the scene model
    model = load_model(path)
    # Create the render instance of the model
    render.launch(model)
    # Create the physics data of the model
    data = SceneData(model)

    # ----------Try to access actuators----------
    # How many actuators are in the model?
    num_actuators = model.num_actuators
    print(f"num_actuators is : {num_actuators}")
    # The name list of actuators
    actuator_names = model.actuator_names
    print(f"actuator_names is : {actuator_names}")
    # The control range of actuators
    actuator_ctrl_limits = model.actuator_ctrl_limits
    print(f"actuator_ctrl_limits is : {actuator_ctrl_limits}")

    # Get actuator_A index in model
    actuator_A_index = model.get_actuator_index("actuator_A")
    # Get actuator_A by the way with index
    actuator_A = model.get_actuator(actuator_A_index)
    print(f"actuator_A by index name is : {actuator_A.name}")
    # Get actuator_A by the way with name
    actuator_A = model.get_actuator("actuator_A")
    # Get other actuators
    actuator_B = model.get_actuator("actuator_B")
    actuator_C = model.get_actuator("actuator_C")

    # Look at the actuators' index
    print(
        f"actuator_A index is :{actuator_A.index}, actuator_B index is :{actuator_B.index}, "
        f"actuator_C index is : {actuator_C.index}"
    )

    # Look at the actuators' control range
    actuator_A_ctrl_range = actuator_A.ctrl_range
    actuator_B_ctrl_range = actuator_B.ctrl_range
    actuator_C_ctrl_range = actuator_C.ctrl_range
    print(f"actuator_A_ctrl_range is : {actuator_A_ctrl_range}")
    print(f"actuator_B_ctrl_range is : {actuator_B_ctrl_range}")
    print(f"actuator_C_ctrl_range is : {actuator_C_ctrl_range}")

    start = time.time()
    flip = False
    print_count = 0

    while True:
        # Control the step interval to prevent too fast simulation
        time.sleep(0.02)

        if time.time() - start > 1.5:
            actuator_A.set_ctrl(data, 1.0 if flip else -1.0)
            actuator_C.set_ctrl(data, 0.5 if flip else -0.5)
            flip = not flip
            start = time.time()

        if print_count < 200:
            ctrls = model.get_actuator_ctrls(data)
            print(f"actuator_ctrls is :{ctrls}")
            print_count += 1

        # Physics world step
        step(model, data)
        # Sync render objects from physic world
        render.sync(data)


if __name__ == "__main__":
    main()