Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code to communicate with Hiwonder motors #391

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

jess-moss
Copy link
Collaborator

@jess-moss jess-moss commented Aug 28, 2024

What this does

This PR adds hiwonder.py which enables communication (read/write) with the Hiwonder motors, which will be used in a the next robot, the Moss V1.1. Note: In future iterations, more functions may need to be added to this class.

How it was tested

Tested various setups such as the one below in which I read and write motor positions for two serial bus servos daisy chained together.

from lerobot.common.robot_devices.motors.hiwonder import HiwonderMotorsBus

def main():
    # Only connected to two motors currently.
    motors = {"motor1": (1, 'model_x'), "motor2": (2, 'model_y')}
    hw_bus = HiwonderMotorsBus("/dev/ttyUSB0", motors)
    hw_bus.connect()
    positions = hw_bus.read()

    try:
        while True:
            positions = hw_bus.read()

            # Get current positions for both motors
            motor1_pos = positions["motor1"]
            motor2_pos = positions["motor2"]

            # Check if either motor is at or above the 900 position threshold
            if motor1_pos >= 900 or motor2_pos >= 900:
                print("Threshold reached, stopping...")
                break

            # Increment positions by 5, ensuring they do not exceed 995
            new_motor1_pos = min(motor1_pos + 10, 995)
            new_motor2_pos = min(motor2_pos + 10, 995)

            # Write new positions
            hw_bus.write([new_motor1_pos, new_motor2_pos], ["motor1", "motor2"])
            print(f"Positions {positions}.")
            # Sleep to prevent too rapid updates
            time.sleep(0.003)  # Adjust delay as needed for your application
    finally:
        print(f"Positions {positions}.")
        hw_bus.disconnect()


if __name__ == "__main__":
    main()

Note: @Cadene

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants