-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_dynamixel_client.py
98 lines (70 loc) · 2.46 KB
/
test_dynamixel_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import argparse
import itertools
import numpy as np
import time
from dynamixel_client import DynamixelClient
import py_dynamixel.io as io
ports = io.get_available_ports()
print('available ports:', ports)
if not ports:
raise IOError('No port available.')
port = ports[0]
print('Using the first on the list', port)
device_path = port
baud_rate = 3000000
# motor ids
motors = list()
for i in range(10, 50, 10):
for j in range(1, 4):
motors.append(i + j)
print("ids", motors)
#motors = [42, 43]
target = np.pi
way_points = [np.zeros(len(motors)), np.full(len(motors), target)]
targets = np.full(len(motors), target)
targets_1 = targets.copy()
targets_2 = targets.copy() + np.pi/4
#for i in range(1,12,3):
# targets_2[i] = np.pi + np.pi/4
targets_list = [targets_1, targets_2]
sel=1
print("Motors: ", motors)
print("Device: ", device_path)
print("Baudrate: ", baud_rate)
ctrl_freq = 50. # Hz
ctrl_dt = 1./ctrl_freq
start = time.time()
#dxl_client = DynamixelClient(motors, device_path, baud_rate, lazy_connect=True)
with DynamixelClient(motors, device_path, baud_rate, lazy_connect=True) as dxl_client:
while(1):
print("Targets: ", targets_list[sel])
dxl_client.write_desired_pos(motors, targets_list[sel])
elapsed = time.time() - start
print(int(elapsed))
#pos_now, vel_now, cur_now = dxl_client.read_pos_vel_cur()
if int(elapsed) % 5 == 4:
print("CHANGE TARGET")
if sel == 0:
sel = 1
else:
sel = 0
'''
for step in itertools.count():
write_start = time.time()
if step > 0 and step % 50 == 0:
way_point = way_points[(step // 100) % len(way_points)]
print('Writing: {}'.format(way_point.tolist()))
dxl_client.write_desired_pos(motors, way_point)
#print('[{}] Write Frequency: {:.2f} Hz'.format(
# step, 1.0 / (time.time() - write_start)))
read_start = time.time()
pos_now, vel_now, cur_now = dxl_client.read_pos_vel_cur()
if step % 50 == 0:
print('[{}] Read Frequency: {:.2f} Hz'.format(
step, 1.0 / (time.time() - read_start)))
print('> Position: {}'.format(pos_now.tolist()))
print('> Velocity: {}'.format(vel_now.tolist()))
print('> Current: {}'.format(cur_now.tolist()))
if pos_now.tolist() == target:
way_points = [np.zeros(len(motors)), np.full(len(motors), -target)]
'''