dora-rs / dora-lerobot

Lerobot boosted with dora
Apache License 2.0
63 stars 6 forks source link

Improve documentation and use ZeroCopy StructArray #19

Closed Hennzau closed 3 months ago

Hennzau commented 3 months ago

Hi, for this short PR of few comits I imroved a bit the documentation (in fact I just made corrections), and I managed to used ZeroCopy StructArray instead of the old way of creating dictionary etc...

Editable node-hub, development mode

PWM Position Control

  - id: lcr-to-lcr
    env:
      LEADER_CONTROL: ../configs/leader.left.json
      FOLLOWER_CONTROL: ../configs/follower.left.json

You load it and you can transform pwm/logical values

    with open(
        os.environ.get("LEADER_CONTROL")
    ) as file:
        leader_control = json.load(file)
        load_control_table_from_json_conversion_tables(leader_control, leader_control)
leader_logical_position = pwm_to_logical_arrow(leader_pwm_position, leader_control)
follower_pwm_goal = logical_to_pwm_with_offset_arrow(follower_pwm_position, leader_logical_goal, follower_control)

Note: The PWM Position Control library also enables zero risk of breaking something: if a communication error occurs and the pwm_goal is not in the right range, it wraps the value so the servo doesn't have to jump one turn

ZeroCopy StructArray

def wrap_joints_and_values(
    joints: Union[list[str], np.array, pa.Array],
    values: Union[int, list[int], np.array, pa.Array],
) -> pa.StructArray:
    return pa.StructArray.from_arrays(
        arrays=[joints, values], names=["joints", "values"]
    )

Use this function to wrap joints and values (from a json configuration file for example)

goal_current = wrap_joints_and_values(joints, [np.uint32(config[joint]["goal_current"]) for joint in joints])

Then you can directly write this command to the dynamixel bus, or send it to the dataflow

self.bus.write_goal_current(self.config["goal_current"])
node.send_output(
    "goal_current",
    self.config["goal_current"]
)
haixuanTao commented 3 months ago

Looks great!

Thanks!

I guess logical_to_pwm_with_offset_arrow and pwm_to_logical_arrow and wrap_joints_and_values can somehow be wrap within fastformat right? so that we can have one logic to do this everytime we will need to do this in the future :)

haixuanTao commented 3 months ago

I think that zero-copy struct look great for things that have the same length, which is the case in this PR.

Hennzau commented 3 months ago

Looks great!

Thanks!

I guess logical_to_pwm_with_offset_arrow and pwm_to_logical_arrow and wrap_joints_and_values can somehow be wrap within fastformat right? so that we can have one logic to do this everytime we will need to do this in the future :)

Yup! When fastformat will be ready I will make those changes! :)