-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
171 lines (140 loc) · 6.55 KB
/
main.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
from typing import Sequence, List, NamedTuple, Optional
from gym import register
import trio
from trio_serial import SerialStream, AbstractSerialStream
from trio_util import periodic
from controller.policy_executor import PolicyExecutor
from env.robot_arm_env import RobotArmEnv
from motors.gyems.rmd_r485 import Commands as RmdCommands
from motors.futaba.commands import Commands as FutabaCommands
from controller.oracle_recorder import OracleRecorder
register(
id='ScalaArm-v0',
entry_point=RobotArmEnv,
)
class _MotorConversion(NamedTuple):
input_id: int
output_id: int
invert_sign: bool = False
def convert(self, rs304_angle: float) -> Optional[float]:
if abs(rs304_angle) < 180:
return (-1 if self.invert_sign else 1) * rs304_angle
return None
_MOTOR_CONVERSIONS = (
_MotorConversion(input_id=49, output_id=1),
_MotorConversion(input_id=9, output_id=2, invert_sign=True),
)
async def _read_input_angles(serial: AbstractSerialStream, ids: Sequence[int]) -> List[float]:
angles = []
for id_ in ids:
read_pos_command = FutabaCommands.read_position(id_)
await serial.send_all(read_pos_command.command_bytes)
res = await serial.receive_some()
angles.append(read_pos_command.response_parser(res).angle)
return angles
async def _command_and_read_current_angles(
serial: AbstractSerialStream, ids: Sequence[int], angles: Sequence[Optional[float]]) -> List[float]:
current_angles = []
for id_, angle in zip(ids, angles):
commands = [RmdCommands.read_position(id_)]
if angle is not None:
commands.append(RmdCommands.move_to_position_with_speed(id_, angle, 360))
for command in commands:
await serial.send_all(command.command_bytes)
res = await serial.receive_some()
if command.response_parser:
current_angles.append(command.response_parser(res).angle)
return current_angles
async def _save_current_position_as_origin(serial: AbstractSerialStream, id_):
commands = (
RmdCommands.save_current_position_as_origin(id_),
RmdCommands.shutdown(id_),
RmdCommands.resume(id_),
RmdCommands.read_position(id_),
)
for command in commands:
await serial.send_all(command.command_bytes)
res = await serial.receive_some()
if command.response_parser:
angle = command.response_parser(res).angle
print('reset origin angle: ', angle)
assert angle == 0
async def _probe_futaba_id(serial: AbstractSerialStream):
for id_ in range(1, 128):
read_pos_command = FutabaCommands.read_position(id_)
await serial.send_all(read_pos_command.command_bytes)
with trio.move_on_after(.1):
await serial.receive_some()
print('got response from id: ', id_)
return
print('timed out', id_)
async def _direct_connect_input_output(ttl_serial, rs485_serial):
input_ids = [conv.input_id for conv in _MOTOR_CONVERSIONS]
output_ids = [conv.output_id for conv in _MOTOR_CONVERSIONS]
try:
async for _ in periodic(.02):
input_angles = await _read_input_angles(ttl_serial, input_ids)
target_angles = [conv.convert(angle) for conv, angle in zip(_MOTOR_CONVERSIONS, input_angles)]
current_angles = await _command_and_read_current_angles(rs485_serial, output_ids, target_angles)
finally:
with trio.CancelScope(shield=True):
for id_ in output_ids:
await rs485_serial.send_all(RmdCommands.shutdown(id_).command_bytes)
await rs485_serial.receive_some()
async def _read_control_params(serial: AbstractSerialStream):
for conv in _MOTOR_CONVERSIONS:
command = RmdCommands.read_control_param(conv.output_id)
await serial.send_all(command.command_bytes)
print(command.response_parser(await serial.receive_some()), conv.output_id)
async def _write_control_params(serial: AbstractSerialStream, *args):
for conv in _MOTOR_CONVERSIONS:
command = RmdCommands.write_control_param_ram(
conv.output_id, *args)
await serial.send_all(command.command_bytes)
res = await serial.receive_some()
print('control param written', res)
async def main_master_slave():
async with SerialStream('/dev/tty.usbserial-11110', baudrate=115200) as ttl_serial, \
SerialStream('/dev/tty.usbserial-0001', baudrate=115200) as rs485_serial:
await _write_control_params(rs485_serial, 10, 100, 200, 30)
await _direct_connect_input_output(ttl_serial, rs485_serial)
async def main_old2():
async with SerialStream('/dev/tty.usbserial-0001', baudrate=115200) as rs485_serial:
rs485_commands = (
RmdCommands.move_to_position_with_speed(1, 0, 90),
RmdCommands.move_to_position_with_speed(2, 0, 90),
# RmdCommands.move_to_position_with_speed(gyems_id, 0, 90),
RmdCommands.read_position(1),
RmdCommands.read_position(2),
)
for command in rs485_commands:
await rs485_serial.send_all(command.command_bytes)
res = await rs485_serial.receive_some()
if command.response_parser:
print(command.response_parser(res).angle)
# await _save_current_position_as_origin(rs485_serial, 1)
# await _save_current_position_as_origin(rs485_serial, 2)
async def main_old():
# reset
async with SerialStream('/dev/tty.usbserial-0001', baudrate=115200) as rs485_serial, \
SerialStream('/dev/tty.usbserial-130', baudrate=115200) as ttl_serial:
futaba_id = 9
gyems_id = 1
read_pos_command = FutabaCommands.read_position(futaba_id)
while True:
await ttl_serial.send_all(read_pos_command.command_bytes)
res = await ttl_serial.receive_some()
if read_pos_command.response_parser:
response = read_pos_command.response_parser(res)
if abs(response.angle) < 180:
rs485_commands = (
RmdCommands.move_to_position_with_speed(gyems_id, response.angle, 90),
RmdCommands.read_position(gyems_id),
)
for command in rs485_commands:
await rs485_serial.send_all(command.command_bytes)
res = await rs485_serial.receive_some()
if command.response_parser:
print(command.response_parser(res).angle)
await trio.sleep(.05)
trio.run(main)