|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (c) 2022 Hanson Robotics. |
| 4 | +# |
| 5 | +# This file is part of Hanson AI. |
| 6 | +# See https://www.hansonrobotics.com/hanson-ai for further info. |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +# |
| 20 | + |
| 21 | +import argparse |
| 22 | +import json |
| 23 | +from pprint import pprint as pp |
| 24 | +from serial.tools.list_ports import grep as grep_serial_ports |
| 25 | +import scservo_sdk as sdk |
| 26 | + |
| 27 | + |
| 28 | +def argument_parser(): |
| 29 | + parser = argparse.ArgumentParser(description="Ping all feetech servos", formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 30 | + # By default we search for all CH430 ports with |
| 31 | + parser.add_argument("--hardware_regex", type=str, default='1A86:7523', help='Serial port filter for detecting multiple boards') |
| 32 | + parser.add_argument("--baud", type=int, default=1000000, help='Baudrate') |
| 33 | + parser.add_argument("--from_id", type=int, default=0, help='From ID') |
| 34 | + parser.add_argument("--to_id", type=int, default=110, help='To ID') |
| 35 | + parser.add_argument("--json", type=bool, default=False, help="Output as JSON") |
| 36 | + args = parser.parse_args() |
| 37 | + return vars(args) |
| 38 | + |
| 39 | +def pingservos(port, baud, from_id, to_id): |
| 40 | + port = sdk.PortHandler(port) |
| 41 | + # Ping does not depend on the protocol version so can use 0 by default |
| 42 | + handler = sdk.PacketHandler(0) |
| 43 | + servos = [] |
| 44 | + try: |
| 45 | + port.openPort() |
| 46 | + port.setBaudRate(baud) |
| 47 | + for id in range(from_id, to_id+1): |
| 48 | + # Ping the servo |
| 49 | + model_number, result, error = handler.ping(port, id) |
| 50 | + if result == sdk.COMM_SUCCESS: |
| 51 | + servos.append((id, model_number)) |
| 52 | + return servos |
| 53 | + except Exception as e: |
| 54 | + print(e) |
| 55 | + return None |
| 56 | + |
| 57 | +def find_all_servos(args): |
| 58 | + ports = [p.device for p in grep_serial_ports(args['hardware_regex'])] |
| 59 | + devices = {} |
| 60 | + for p in ports: |
| 61 | + devices[p] = pingservos(p, args['baud'], args['from_id'], args['to_id']) |
| 62 | + return devices |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + args = argument_parser() |
| 67 | + device_servos = find_all_servos(args) |
| 68 | + if args['json']: |
| 69 | + print(json.dumps(device_servos, indent=4, sort_keys=True)) |
| 70 | + else: |
| 71 | + pp(device_servos) |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + main() |
0 commit comments