From 0129458337b7b18aac4f47f52ff84e37760e007e Mon Sep 17 00:00:00 2001 From: Anton Tetov <anton@tetov.se> Date: Fri, 11 Feb 2022 12:35:21 +0100 Subject: [PATCH] draft of argparse setup --- abb_egm_pyclient/run/__init__ | 0 abb_egm_pyclient/run/__main__.py | 55 +++++++++++++++++++ .../run}/print_egm_feedback.py | 8 +-- .../run}/send_configuration.py | 11 ++-- abb_egm_pyclient/run/send_pose.py | 6 ++ examples/send_frame.py | 4 -- 6 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 abb_egm_pyclient/run/__init__ create mode 100644 abb_egm_pyclient/run/__main__.py rename {examples => abb_egm_pyclient/run}/print_egm_feedback.py (81%) rename {examples => abb_egm_pyclient/run}/send_configuration.py (88%) create mode 100644 abb_egm_pyclient/run/send_pose.py delete mode 100644 examples/send_frame.py diff --git a/abb_egm_pyclient/run/__init__ b/abb_egm_pyclient/run/__init__ new file mode 100644 index 0000000..e69de29 diff --git a/abb_egm_pyclient/run/__main__.py b/abb_egm_pyclient/run/__main__.py new file mode 100644 index 0000000..dc2a6af --- /dev/null +++ b/abb_egm_pyclient/run/__main__.py @@ -0,0 +1,55 @@ +import argparse +from abb_egm_pyclient.run import print_egm_feedback +from abb_egm_pyclient.run import send_configuration +from abb_egm_pyclient.run import send_pose + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Run one of the example EGM scripts.") + parser.add_argument("port", type=int, help="UDP port") + + subparsers = parser.add_subparsers(help="sub-command help") + + func_sel_mapping = { + "print": print_egm_feedback, + "joint": send_configuration, + "pose": send_pose, + } + + parser_print = subparsers.add_parsers( + "print", + help="print messages recieved from the EGM interface on the controller", + dest="func_selection", + ) + + parser_joint = subparsers.add_parsers( + "joint", + help="print messages recieved from the EGM interface on the controller", + dest="func_selection", + ) + parser_joint.add_argument("joint_values", nargs="*", type=float, metavar=("j1", "j2", "j3", "j4", "j5", "j6", "j7")) + + parser_world = subparsers.add_parsers( + "pose", + help="print messages recieved from the EGM interface on the controller", + dest="func_selection", + ) + parser_joint.add_argument("pose_values", nargs="6", type=float, metavar=("x", "y", "z", "rx", "ry", "rz")) + + args = parser.parse_args() + + if args.func_selection == "print": + print_egm_feedback(args.port) + + if args.func_selection == "joint": + if len(args.joint_values) not in (6, 7): + raise argparse.ArgumentError() + send_configuration(args.port, args.joint_values) + + if args.func_selection == "pose": + send_configuration(args.port, *args.pose_values) + + func = func_sel_mapping[args.func_selection] + + + + diff --git a/examples/print_egm_feedback.py b/abb_egm_pyclient/run/print_egm_feedback.py similarity index 81% rename from examples/print_egm_feedback.py rename to abb_egm_pyclient/run/print_egm_feedback.py index 1e31778..5f88e96 100644 --- a/examples/print_egm_feedback.py +++ b/abb_egm_pyclient/run/print_egm_feedback.py @@ -6,12 +6,12 @@ try: except ImportError: raise ImportWarning("abb_egm not found, have you installed the package?") -UDP_PORT = 6510 +DEFAULT_UDP_PORT = 6510 log = logging.getLogger("egm_client") -def print_egm_feedback() -> None: +def print_egm_feedback(port: int) -> None: """Print EGM feedback. Parameters @@ -20,7 +20,7 @@ def print_egm_feedback() -> None: Frequency of prints in hertz. """ - egm_client = EGMClient(port=UDP_PORT) + egm_client = EGMClient(port=port) while True: try: @@ -34,4 +34,4 @@ def print_egm_feedback() -> None: if __name__ == "__main__": - print_egm_feedback() + print_egm_feedback(port=DEFAULT_UDP_PORT) diff --git a/examples/send_configuration.py b/abb_egm_pyclient/run/send_configuration.py similarity index 88% rename from examples/send_configuration.py rename to abb_egm_pyclient/run/send_configuration.py index 5c698cb..2677666 100644 --- a/examples/send_configuration.py +++ b/abb_egm_pyclient/run/send_configuration.py @@ -6,15 +6,12 @@ import time import numpy as np import numpy.typing as npt -try: - from abb_egm_pyclient import EGMClient -except ImportError: - raise ImportWarning("abb_egm not found, have you installed the package?") +from abb_egm_pyclient import EGMClient -UDP_PORT = 6510 +DEFAULT_UDP_PORT = 6510 -def send_configuration(target_conf: Sequence[float], joint_vel=1.0) -> None: +def send_configuration(port: int, target_conf: Sequence[float], joint_vel=1.0) -> None: """Move robot to target configuration Parameters @@ -27,7 +24,7 @@ def send_configuration(target_conf: Sequence[float], joint_vel=1.0) -> None: # send rate in hz rate = 10 - egm_client = EGMClient(port=UDP_PORT) + egm_client = EGMClient(port=port) pb_robot_msg = egm_client.receive_msg() start_conf: Sequence[float] = pb_robot_msg.feedBack.joints.joints diff --git a/abb_egm_pyclient/run/send_pose.py b/abb_egm_pyclient/run/send_pose.py new file mode 100644 index 0000000..e709586 --- /dev/null +++ b/abb_egm_pyclient/run/send_pose.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python +"""Send pose example.""" + + +def send_pose(port, x, y, z, rx, ry, rz): + raise NotImplementedError("Planned example") diff --git a/examples/send_frame.py b/examples/send_frame.py deleted file mode 100644 index 82d32bc..0000000 --- a/examples/send_frame.py +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env python -"""Send frame example.""" - -raise NotImplementedError("Planned example") -- GitLab