From 9ac40bd3687c16bff810e5fadc235488c1c0fbe0 Mon Sep 17 00:00:00 2001 From: Denys Bezmenov Date: Wed, 23 Oct 2024 23:24:21 -0700 Subject: [PATCH] cli for torque --- openlch/__init__.py | 2 +- openlch/cli.py | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/openlch/__init__.py b/openlch/__init__.py index e4f40e4..b73c648 100644 --- a/openlch/__init__.py +++ b/openlch/__init__.py @@ -1,3 +1,3 @@ -__version__ = "0.6.0" +__version__ = "0.6.1" from .hal import HAL diff --git a/openlch/cli.py b/openlch/cli.py index 559c708..0a9c15a 100644 --- a/openlch/cli.py +++ b/openlch/cli.py @@ -3,6 +3,7 @@ import subprocess import click from .hal import HAL +from typing import List, Tuple DEFAULT_IP = "192.168.42.1" @@ -47,13 +48,13 @@ def ping(ip: str) -> None: @cli.command() @click.argument("ip", default=DEFAULT_IP) def get_positions(ip: str) -> None: - """Get current positions of all servos.""" + """Get current positions and speeds of all servos.""" hal = HAL(ip) try: positions = hal.servo.get_positions() - click.echo("Current positions:") - for id, position in positions: - click.echo(f"Servo {id}: {position}") + click.echo("Current positions and speeds:") + for id, position, speed in positions: + click.echo(f"Servo {id}: Position = {position:.2f}, Speed = {speed:.2f}") except Exception as e: click.echo(f"An error occurred: {str(e)}") finally: @@ -231,5 +232,41 @@ def get_video_stream_urls(ip: str) -> None: finally: hal.close() +@cli.command() +@click.option("--settings", "-s", type=(int, float), multiple=True, required=True, + help="Servo ID and torque value pairs (e.g., -s 1 0.5 -s 2 0.7)") +@click.argument("ip", default=DEFAULT_IP) +def set_torque(settings: List[Tuple[int, float]], ip: str) -> None: + """Set torque for multiple servos.""" + hal = HAL(ip) + try: + hal.servo.set_torque(settings) + click.echo("Torque settings applied successfully:") + for servo_id, torque in settings: + click.echo(f"Servo {servo_id}: Torque = {torque:.2f}") + except Exception as e: + click.echo(f"An error occurred: {str(e)}") + finally: + hal.close() + +@cli.command() +@click.option("--settings", "-s", type=(int, click.Choice(['true', 'false'])), multiple=True, required=True, + help="Servo ID and enable status pairs (e.g., -s 1 true -s 2 false)") +@click.argument("ip", default=DEFAULT_IP) +def set_torque_enable(settings: List[Tuple[int, str]], ip: str) -> None: + """Enable or disable torque for multiple servos.""" + hal = HAL(ip) + try: + # Convert 'true'/'false' strings to boolean values + bool_settings = [(id, status.lower() == 'true') for id, status in settings] + hal.servo.set_torque_enable(bool_settings) + click.echo("Torque enable settings applied successfully:") + for servo_id, status in bool_settings: + click.echo(f"Servo {servo_id}: Torque {'enabled' if status else 'disabled'}") + except Exception as e: + click.echo(f"An error occurred: {str(e)}") + finally: + hal.close() + if __name__ == "__main__": cli()