|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | + |
| 4 | +from ast import Return |
| 5 | +from dataclasses import dataclass |
| 6 | +import time |
| 7 | +import argparse |
| 8 | +import time |
| 9 | +import scservo_sdk as sdk |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +# Control table address |
| 14 | +ADDR_SCS_TORQUE_ENABLE = 40 |
| 15 | +ADDR_SCS_GOAL_ACC = 41 |
| 16 | +ADDR_SCS_GOAL_POSITION = 42 |
| 17 | +ADDR_SCS_GOAL_SPEED = 46 |
| 18 | +ADDR_SCS_PRESENT_POSITION = 56 |
| 19 | +MOVING_THRESHOLD = 5 |
| 20 | +STEPS_IN_DEG = { |
| 21 | + 0: 11.38, |
| 22 | + 1: 4.65, |
| 23 | +} |
| 24 | + |
| 25 | +def argument_parser(): |
| 26 | + parser = argparse.ArgumentParser(description="Meassure servo speed based on feedback", formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 27 | + parser.add_argument("--port", type=str, default='/dev/ttyUSB0', help='Serial port') |
| 28 | + parser.add_argument("--baud", type=int, default=1000000, help='Baudrate') |
| 29 | + parser.add_argument("--protocol", type=int, default=0, help='SCS Protocol') |
| 30 | + parser.add_argument("--id", type=int, default=1, help='Servo ID') |
| 31 | + parser.add_argument("--from", type=int, default=500, help='From Position') |
| 32 | + parser.add_argument("--to", type=int, default=1000, help='To Position') |
| 33 | + parser.add_argument("--goback", type=bool, default=False, help='Meassures time to get back to initial position as well') |
| 34 | + parser.add_argument("--title", type=str, default="Servo Position", help='Title for graph') |
| 35 | + parser.add_argument("--percieved", type=int, default=0, help='Remove first n percent of movement, to show percieved stats') |
| 36 | + |
| 37 | + args = parser.parse_args() |
| 38 | + return vars(args) |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +def plot(x, y, xlabel='Time (s)', ylabel='Degrees', title='Servo Position'): |
| 43 | + plt.plot(x, y) |
| 44 | + plt.xlabel(xlabel) |
| 45 | + plt.ylabel(ylabel) |
| 46 | + plt.title(title) |
| 47 | + plt.show() |
| 48 | + |
| 49 | + |
| 50 | +def move_to(group_sync_write, id, position): |
| 51 | + param_goal_position = [sdk.SCS_LOBYTE(position), sdk.SCS_HIBYTE(position)] |
| 52 | + group_sync_write.addParam(id, param_goal_position) |
| 53 | + group_sync_write.txPacket() |
| 54 | + group_sync_write.clearParam() |
| 55 | + |
| 56 | +def move_to_single(port, packets, id, position): |
| 57 | + packets.write2ByteTxRx(port, id, ADDR_SCS_GOAL_POSITION, position) |
| 58 | + |
| 59 | +def current_position(group_sync_read, id, ph=None): |
| 60 | + scs_comm_result = group_sync_read.txRxPacket() |
| 61 | + if scs_comm_result != 0: |
| 62 | + print("Error") |
| 63 | + print(ph.getTxRxResult(scs_comm_result)) |
| 64 | + |
| 65 | + data = group_sync_read.getData(id, ADDR_SCS_PRESENT_POSITION, 2) |
| 66 | + return data |
| 67 | + |
| 68 | +def current_position_single(port, packets, id): |
| 69 | + data, status, error = packets.read2ByteTxRx(port, id, ADDR_SCS_PRESENT_POSITION) |
| 70 | + if status or error: |
| 71 | + raise Exception(f"Error reading position: {status} {error}") |
| 72 | + return data |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + args = argument_parser() |
| 79 | + port = sdk.PortHandler(args['port']) |
| 80 | + print(args['protocol']) |
| 81 | + |
| 82 | + packets = sdk.PacketHandler(args['protocol']) |
| 83 | + if not port.openPort() and port.setBaudRate(args['baud']): |
| 84 | + print(f"Cant Open port {args['port']}") |
| 85 | + return |
| 86 | + id = args['id'] |
| 87 | + scs_model_number, scs_comm_result, scs_error = packets.ping(port, id) |
| 88 | + if scs_comm_result or scs_error: |
| 89 | + print("Cant ping servo") |
| 90 | + return |
| 91 | + else: |
| 92 | + print(f"Servo with ID: {id} is found. Model {scs_model_number}") |
| 93 | + times, positions = meassure(port, packets, id, args['from'], args['to'], args['goback'], STEPS_IN_DEG[args['protocol']]) |
| 94 | + if args['percieved'] > 0: |
| 95 | + percieved_angle_threshold = (args['to'] - args['from']) * args['percieved'] / 100 / STEPS_IN_DEG[args['protocol']] |
| 96 | + print(percieved_angle_threshold) |
| 97 | + times, positions = perceived_data(times, positions, percieved_angle_threshold) |
| 98 | + print(f'Starting: {times[0]}, duration: {times[-1] - times[0]}') |
| 99 | + |
| 100 | + plot(times, positions, args['title']) |
| 101 | + |
| 102 | +def perceived_data(times, positions,threshold): |
| 103 | + percieved_times = [] |
| 104 | + percieved_positions = [] |
| 105 | + for p,t in zip(positions, times): |
| 106 | + if abs(p) > abs(threshold): |
| 107 | + percieved_positions.append(p) |
| 108 | + percieved_times.append(t) |
| 109 | + return percieved_times, percieved_positions |
| 110 | + |
| 111 | +def meassure(port,packets, id, start, end, back=True, steps_in_deg=1): |
| 112 | + group_sync_read = sdk.GroupSyncRead(port, packets, ADDR_SCS_PRESENT_POSITION, 2) |
| 113 | + group_sync_read.addParam(id) |
| 114 | + group_sync_write = sdk.GroupSyncWrite(port, packets, ADDR_SCS_GOAL_POSITION, 2) |
| 115 | + move_to(group_sync_write, id, start) |
| 116 | + print("Moving to initial position") |
| 117 | + time.sleep(1) |
| 118 | + #starting = current_position(group_sync_read, id, packets) |
| 119 | + starting = current_position_single(port, packets, id) |
| 120 | + time.sleep(1) |
| 121 | + print(f"Starting position: {starting}") |
| 122 | + # position |
| 123 | + positions = [starting] |
| 124 | + start_time = time.perf_counter() |
| 125 | + times = [0] |
| 126 | + current = starting |
| 127 | + move_to(group_sync_write, id, end) |
| 128 | + #move_to_single(port, packets, id, end) |
| 129 | + goal = end |
| 130 | + while True: |
| 131 | + current = current_position_single(port, packets, id) |
| 132 | + positions.append(current) |
| 133 | + times.append(time.perf_counter() - start_time) |
| 134 | + if abs(goal-current) < MOVING_THRESHOLD + 50*back: |
| 135 | + if back: |
| 136 | + back = False |
| 137 | + goal = start |
| 138 | + move_to(group_sync_write, id, goal) |
| 139 | + else: |
| 140 | + break |
| 141 | + if back: |
| 142 | + move_to(group_sync_write, id, start) |
| 143 | + print(f"Movement finished in {times[-1]} seconds. samples {len(times)}") |
| 144 | + positions = [(p-start)/steps_in_deg for p in positions] |
| 145 | + return times, positions |
| 146 | + |
| 147 | +if __name__ == '__main__': |
| 148 | + main() |
0 commit comments