forked from kindredresearch/SenseAct
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdxl_unit_conv.py
executable file
·323 lines (252 loc) · 7.76 KB
/
dxl_unit_conv.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Copyright (c) 2018, The SenseAct Authors.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""
Unit conversion
"""
#pylint: disable=too-many-arguments,invalid-name,unused-argument
#pylint: disable=missing-docstring
import numpy as np
from .dxl_exceptions import UnitConversionNotImplemented
class UnitConversion(object):
"""Base class for converting values to physics metrics.
Attributes:
y_min: A float representing lower limit of the converted value
y_max: A float representing upper limit of the converted value
"""
def __init__(self):
self.y_min = None
self.y_max = None
def set_x_lim(self, x_lim):
pass
@staticmethod
def fwd(x):
raise UnitConversionNotImplemented()
@staticmethod
def inv(y):
raise UnitConversionNotImplemented()
class Affine(UnitConversion):
"""Class representing affine transformations to the raw register values."""
def __init__(self, y_min, y_max):
"""See the base class for arguments description"""
UnitConversion.__init__(self)
self.y_min = y_min
self.y_max = y_max
self.x_min = None
self.x_max = None
self.coefs = None
def set_x_lim(self, x_lim):
self.x_min, self.x_max = x_lim
Y = float(self.y_max - self.y_min)
X = float(self.x_max - self.x_min)
a = Y / X
b = -self.x_min * Y / X + self.y_min
self.coefs = float(a), float(b)
def fwd(self, x):
a, b = self.coefs
y = a * x + b
return y
def inv(self, y):
assert self.y_min <= y <= self.y_max, (
self.y_min, y, self.y_max)
a, b = self.coefs
x = int(round((float(y) - b) / a))
return x
class AngleVelRange(UnitConversion):
"""Class provides methods for velocity convertion.
Velocity registers on AX12 and AX18 servos encode present_speed.
"""
def __init__(self, y_max):
"""See the base class for arguments description"""
UnitConversion.__init__(self)
# Degrees per second
self.y_min = -y_max
self.y_max = y_max
self.x_min = None
self.x_max = None
def set_x_lim(self, x_lim):
assert x_lim == (0, 1024 + 1023)
self.x_min, self.x_max = x_lim
def fwd(self, x):
if x >= 1024:
a = self.y_min
xx = x - 1024
else:
a = self.y_max
xx = x
return a * xx / 1023.
def inv(self, y):
#
# The inverse of this UnitConversion would normally be required
# except that it is used to simulate the dynamics of a dynamixel
# in unit tests and simulations.
#
# y = a * xx / 1023.
assert -self.y_max <= y <= self.y_max
if y < 0:
# int or round?
return 1024 + int(1023. * abs(y) / self.y_max)
else:
return int(1023. * y / self.y_max)
class SignedPercentRange(UnitConversion):
"""Description //TODO"""
# This is the encoding used for present_load
def __init__(self):
UnitConversion.__init__(self)
self.y_min = -100.
self.y_max = 100.
self.x_min = None
self.x_max = None
def set_x_lim(self, x_lim):
assert x_lim == (0, 1024 + 1023)
self.x_min, self.x_max = x_lim
def fwd(self, x):
x -= 1023
return self.y_max * x / 1023.
class BaudConversion(UnitConversion):
"""Description //TODO"""
table = {1000000: 1,
500000: 3,
400000: 4,
250000: 7,
200000: 9,
117647: 16, # 115200
57124: 34, # 57600
19230: 103, # 19200
9615: 207, # 9600
9569: 208} # 9600
def __init__(self):
UnitConversion.__init__(self)
self.y_min = 0
self.y_max = 10 * 1000 * 1000
self.x_min = None
self.x_max = None
def set_x_lim(self, x_lim):
assert x_lim == (0, 255)
self.x_min, self.x_max = x_lim
def fwd(self, x):
# x is sensor reading
for baud, code in self.table.items():
if code == x:
break
else:
raise ValueError(x)
return baud # pylint: disable=undefined-loop-variable
def inv(self, y):
return self.table[y]
class CurrentConversion(UnitConversion):
"""Description //TODO
Unit in mA
"""
def __init__(self):
UnitConversion.__init__(self)
self.unit = 4.5
self.x_min = 0
self.x_max = 4095
self.y_min = self.fwd(self.x_min)
self.y_max = self.fwd(self.x_max)
def fwd(self, x):
return self.unit*(x - 2048)
def inv(self, y):
raise NotImplementedError
class GoalTorqueConversion(UnitConversion):
""" Description //TODO
Unit in mA
"""
def __init__(self):
UnitConversion.__init__(self)
self.unit = 4.5
self.x_min = 0
self.x_max = 2047
self.y_min = self.fwd(2047)
self.y_max = self.fwd(1023)
a = 0
def fwd(self, x):
sign = +1 if x < 1024 else -1
value = x % 1024
return sign * value * self.unit
def inv(self, y):
offset = 1024 if np.sign(y) == -1 else 0
value = int(np.round(np.abs(y)/self.unit))
return value + offset
class ReturnTimeDelayConversion(UnitConversion):
"""Description //TODO
Returns time delay register."""
# table maps microseconds -> int code
table = dict((2 * ii, ii) for ii in range(0, 254))
def __init__(self):
UnitConversion.__init__(self)
self.y_min = min(self.table)
self.y_max = max(self.table)
self.x_min = None
self.x_max = None
def set_x_lim(self, x_lim):
assert x_lim == (0, 255)
self.x_min, self.x_max = x_lim
def fwd(self, x):
# x is sensor reading
for us, code in self.table.items():
if code == x:
break
else:
raise ValueError(x)
return us # pylint: disable=undefined-loop-variable
def inv(self, us):
return self.table[us]
class DriveModeConversion(UnitConversion):
"""Description //TODO"""
table = {'normal, master': 0,
'reverse, master': 1,
'normal, slave': 2,
'reverse, slave': 3}
def __init__(self):
UnitConversion.__init__(self)
self.y_min = 0
self.y_max = 255
def set_x_lim(self, x_lim):
assert x_lim == (0, 255)
def fwd(self, x):
# x is sensor reading
for name, code in self.table.items():
if code == x:
break
else:
raise ValueError(x)
return name # pylint: disable=undefined-loop-variable
def inv(self, name):
return self.table[name]
class BooleanFlag(UnitConversion):
"""Represents unit conversion for boolean flags (e.g. torque_enable)."""
def __init__(self):
UnitConversion.__init__(self)
@staticmethod
def fwd(x):
return x
@staticmethod
def inv(y):
return y
class ComplianceSlope(UnitConversion):
"""Represents unit conversion for slope compliance registers."""
def __init__(self):
UnitConversion.__init__(self)
@staticmethod
def fwd(x):
return x
@staticmethod
def inv(y):
thresh = 128
while thresh > 2:
if y >= thresh:
return thresh
thresh >>= 1
return thresh
class RawValue(UnitConversion):
"""Represents dummy unit conversion object that is a pass-through."""
@staticmethod
def fwd(x):
return x
@staticmethod
def inv(y):
return y