From 08e23eb4fd1c7e2c52bd87bd69f5d860cbc14cb4 Mon Sep 17 00:00:00 2001 From: Niko Kivel Date: Thu, 12 Oct 2023 16:06:29 -0600 Subject: [PATCH] script for reading bunch of data objects --- scripts/python/EL5032.yaml | 28 +++++++++++ scripts/python/readElectronicNamePlate.py | 60 +++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 scripts/python/EL5032.yaml create mode 100755 scripts/python/readElectronicNamePlate.py diff --git a/scripts/python/EL5032.yaml b/scripts/python/EL5032.yaml new file mode 100644 index 000000000..440c9cf63 --- /dev/null +++ b/scripts/python/EL5032.yaml @@ -0,0 +1,28 @@ +0x9008: + - 0x01: { type: uint8, desc: Encoder type } + - 0x03: { type: uint8, desc: Reference system } + - 0x11: { type: string, desc: Encoder designation } + - 0x12: { type: string, desc: Encoder ident number } + - 0x13: { type: string, desc: Encoder serial number } + - 0x14: { type: uint32, desc: Multiturn resolution } + - 0x15: { type: uint32, desc: Singleturn resolution } + - 0x16: { type: uint32, desc: Measuring step } + - 0x21: { type: bool, desc: Light source error supported } + - 0x22: { type: bool, desc: Signal amplitude error supported } + - 0x23: { type: bool, desc: Position error supported } + - 0x24: { type: bool, desc: Overvoltage error supported } + - 0x25: { type: bool, desc: Undervoltage error supported } + - 0x26: { type: bool, desc: Overcurrent error supported } + - 0x27: { type: bool, desc: Battery error supported } + - 0x31: { type: bool, desc: Frequency warning supported } + - 0x32: { type: bool, desc: Temperature warning supported } + - 0x33: { type: bool, desc: Light source warning supported } + - 0x34: { type: bool, desc: Battery warning supported } + - 0x35: { type: bool, desc: Reference Mark warning supported } + - 0x36: { type: bool, desc: Acyclic mode warning supported } + - 0x37: { type: bool, desc: Limit position warning supported } + - 0x38: { type: bool, desc: Not ready warning supported } + - 0x39: { type: bool, desc: Diagnostic warning supported } + - 0x43: { type: bool, desc: Temperature 1 (external) supported } + - 0x44: { type: bool, desc: Temperature 2 (internal) supported } + - 0x51: { type: bool, desc: Clock pulse periods } diff --git a/scripts/python/readElectronicNamePlate.py b/scripts/python/readElectronicNamePlate.py new file mode 100755 index 000000000..95342fcda --- /dev/null +++ b/scripts/python/readElectronicNamePlate.py @@ -0,0 +1,60 @@ +#! /usr/bin/env python + +import argparse +import yaml +import json +import toml + +import subprocess + +def process_index(index, subindeces, master=0, slave=0) -> list: + root_cmd = f'ethercat -m{master} -p{slave} upload {index:#x}' + results = [] + for subindexDict in subindeces: + for subindex, value in subindexDict.items(): + data_type = value.get('type') + description = f'{subindex:#04x}: {value.get("desc", "")}' + cmd = f'{root_cmd} {subindex:#x} -t{data_type}' + result = subprocess.run(cmd.split(), stdout=subprocess.PIPE, text=True) + results.append(f'{description} = {result.stdout}') + return results + +# Create the parser +parser = argparse.ArgumentParser(description='Read a dictionary from a file.') + +# Add arguments +parser.add_argument('-y', '--yaml', type=str, help='YAML file to read') +parser.add_argument('-j', '--json', type=str, help='JSON file to read') +parser.add_argument('-t', '--toml', type=str, help='TOML file to read') +parser.add_argument('-m', '--master', type=int, default=0) +parser.add_argument('-p', '--position', type=int, default=1) + +# Parse the arguments +args = parser.parse_args() + +# Function to read and process the data +def read_and_process(file_path): + with open(file_path, 'r') as file: + if file_path.endswith('.yaml'): + data = yaml.safe_load(file) + elif file_path.endswith('.json'): + data = json.load(file) + elif file_path.endswith('.toml'): + data = toml.load(file) + else: + raise ValueError('Unsupported file type') + + for index, subindeces in data.items(): + print(f'INDEX ==== {index:#x} ====') + for response in process_index(index, subindeces, args.master, args.position): + print(response.strip()) + +# Call the function with the respective file path +if args.yaml: + read_and_process(args.yaml) +elif args.json: + read_and_process(args.json) +elif args.toml: + read_and_process(args.toml) +else: + parser.print_help()