Skip to content

Commit 4c36f80

Browse files
committed
Added ping tool
1 parent 2dd20fb commit 4c36f80

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

README.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## REQUIREMENT
22
- Python 3.8
33
- Serial port access, for Ubuntu current user needs to be in dialout group
4-
- Feetech SCS or STS servos.
4+
- Feetech SCS or STS servos.
55
## INSTALLATION
66
For Ubuntu:
77

@@ -15,9 +15,34 @@ pip install -r requirements.txt
1515

1616
## Tools
1717

18+
### Ping
19+
Pings all available servos on multiple boards connected
20+
```bash
21+
./ping.py --help
22+
```
23+
```
24+
-h, --help show this help message and exit
25+
--hardware_regex HARDWARE_REGEX
26+
Serial port filter for detecting multiple boards (default: 1A86:7523)
27+
--baud BAUD Baudrate (default: 1000000)
28+
--from_id FROM_ID From ID (default: 0)
29+
--to_id TO_ID To ID (default: 110)
30+
--json JSON Output as JSON (default: False)
31+
```
32+
Examples:
33+
```
34+
./ping.py --from_id 80 --to_id 90
35+
```
36+
Output:
37+
```
38+
{'/dev/ttyUSB0': [(83, 6662)]}
39+
```
40+
Where device port is `/dev/ttyUSB0` and `83` is servo ID and `6662` internal servo model.
41+
42+
1843
### Meassure
1944
Measures the time it takes for servo to move from one position to another
20-
Useage:
45+
Usage:
2146
```bash
2247
./meassure.py --help
2348
```
@@ -43,4 +68,4 @@ Examples:
4368
```bash
4469
./meassure.py --id 1 --protocol 0 --from 300 --to 910 --goback true --percieved 5
4570

46-
```
71+
```

ping.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)