-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_robotdriver.py
More file actions
253 lines (213 loc) · 10.6 KB
/
test_robotdriver.py
File metadata and controls
253 lines (213 loc) · 10.6 KB
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
import unittest
from typing_extensions import override
from edg import *
from .util import run_test_board
class MotorConnector(Connector, Block):
def __init__(self, current_draw: RangeLike):
super().__init__()
self.a = self.Port(DigitalSink(current_draw=current_draw))
self.b = self.Port(DigitalSink(current_draw=current_draw))
self.conn = self.Block(PassiveConnector()).connected({"1": self.b, "2": self.a})
class PwmConnector(Connector, Block):
def __init__(self, current_draw: RangeLike):
super().__init__()
self.gnd = self.Port(Ground(), [Common])
self.pwr = self.Port(VoltageSink(current_draw=current_draw), [Power])
self.pwm = self.Port(DigitalSink(), [Input])
self.conn = self.Block(PinHeader254()).connected({"3": self.gnd, "2": self.pwr, "1": self.pwm})
class LedConnector(Connector, Block):
"""Connector for external WS2812s.
TODO: no power or digital model given, should find a mostly-common spec"""
def __init__(self, num_leds: FloatLike = 0):
super().__init__()
led_current = 36.6
self.gnd = self.Port(Ground(), [Common])
self.vdd = self.Port(VoltageSink(current_draw=(0 * mAmp, led_current * num_leds)), [Power])
self.din = self.Port(DigitalSink(), [Input])
self.conn = self.Block(PassiveConnector()).connected({"3": self.gnd, "1": self.vdd, "2": self.din})
class RobotDriver(JlcBoardTop):
"""Robot driver that uses a ESP32 (non-C) chip and includes a few more blocks
to use the extra available IOs
"""
@override
def contents(self) -> None:
super().contents()
self.batt = self.Block(LipoConnector(actual_voltage=(3.7, 4.2) * Volt))
# actually on the 3V3 domain but need the battery output here
self.isense = self.Block(
OpampCurrentSensor(
resistance=0.05 * Ohm(tol=0.01),
ratio=Range.from_tolerance(20, 0.05),
input_impedance=10 * kOhm(tol=0.05),
)
)
self.connect(self.isense.pwr_in, self.batt.pwr)
self.vbatt = self.connect(self.isense.pwr_out)
self.gnd = self.connect(self.batt.gnd)
self.tp_vbatt = self.Block(VoltageTestPoint()).connected(self.isense.pwr_out)
self.tp_gnd = self.Block(GroundTestPoint()).connected(self.batt.gnd)
# POWER
with self.implicit_connect(
ImplicitConnect(self.gnd, [Common]),
) as imp:
(self.reg_3v3, self.tp_3v3, self.prot_3v3), _ = self.chain(
self.vbatt,
imp.Block(BuckConverter(output_voltage=3.3 * Volt(tol=0.05))),
self.Block(VoltageTestPoint()),
imp.Block(ProtectionZenerDiode(voltage=(3.45, 3.9) * Volt)),
)
self.v3v3 = self.connect(self.reg_3v3.pwr_out)
# 3V3 DOMAIN
with self.implicit_connect(
ImplicitConnect(self.v3v3, [Power]),
ImplicitConnect(self.gnd, [Common]),
) as imp:
self.mcu = imp.Block(IoController())
self.i2c = self.mcu.i2c.request("i2c")
self.tof = imp.Block(Vl53l0xArray(3))
(self.i2c_pull, self.i2c_tp), self.i2c_chain = self.chain(
self.i2c, imp.Block(I2cPullup()), imp.Block(I2cTestPoint()), self.tof.i2c
)
self.lcd = imp.Block(Er_Oled_091_3())
self.connect(self.mcu.spi.request("spi"), self.lcd.spi)
self.connect(self.lcd.cs, self.mcu.gpio.request("lcd_cs"))
self.connect(self.lcd.reset, self.mcu.gpio.request("lcd_reset"))
self.connect(self.lcd.dc, self.mcu.gpio.request("lcd_dc"))
# IMU
self.imu = imp.Block(Lsm6ds3trc())
self.connect(self.i2c, self.imu.i2c)
# Current sensor
self.connect(self.isense.pwr, self.v3v3)
self.connect(self.isense.gnd, self.gnd)
self.connect(self.isense.ref, self.batt.gnd.as_analog_source())
self.connect(self.isense.out, self.mcu.adc.request("isense"))
self.expander = imp.Block(Pcf8574(0))
self.connect(self.i2c, self.expander.i2c)
self.connect(self.expander.io.request_vector("tof_reset"), self.tof.reset)
self.leds = imp.Block(IndicatorSinkLedArray(4))
self.connect(self.expander.io.request_vector("led"), self.leds.signals)
# SPEAKER AND LOW POWER VBATT DOMAIN
with self.implicit_connect(
ImplicitConnect(self.vbatt, [Power]),
ImplicitConnect(self.gnd, [Common]),
) as imp:
(self.spk_tp, self.spk_drv, self.spk), self.spk_chain = self.chain(
self.mcu.with_mixin(IoControllerDac()).dac.request("spk"),
self.Block(AnalogTestPoint()),
imp.Block(Tpa2005d1(gain=Range.from_tolerance(10, 0.2))),
self.Block(Speaker()),
)
self.ws2812bArray = imp.Block(NeopixelArray(5))
self.connect(self.mcu.gpio.request("ledArray"), self.ws2812bArray.din)
self.led_pixel = imp.Block(LedConnector())
self.connect(self.ws2812bArray.dout, self.led_pixel.din)
# MOTORS AND SERVOS
with self.implicit_connect(
ImplicitConnect(self.gnd, [Common]),
) as imp:
self.motor_driver1 = imp.Block(Drv8833())
self.connect(self.vbatt, self.motor_driver1.pwr)
self.connect(self.mcu.gpio.request("motor_1a1"), self.motor_driver1.ain1)
self.connect(self.mcu.gpio.request("motor_1a2"), self.motor_driver1.ain2)
self.connect(self.mcu.gpio.request("motor_1b1"), self.motor_driver1.bin1)
self.connect(self.mcu.gpio.request("motor_1b2"), self.motor_driver1.bin2)
self.m1_a = self.Block(MotorConnector((-600, 600) * mAmp))
self.connect(self.m1_a.a, self.motor_driver1.aout1)
self.connect(self.m1_a.b, self.motor_driver1.aout2)
self.m1_b = self.Block(MotorConnector((-600, 600) * mAmp))
self.connect(self.m1_b.a, self.motor_driver1.bout1)
self.connect(self.m1_b.b, self.motor_driver1.bout2)
self.motor_driver2 = imp.Block(Drv8833())
self.connect(self.vbatt, self.motor_driver2.pwr)
self.connect(self.mcu.gpio.request("motor_2a1"), self.motor_driver2.ain1)
self.connect(self.mcu.gpio.request("motor_2a2"), self.motor_driver2.ain2)
self.connect(self.mcu.gpio.request("motor_2b1"), self.motor_driver2.bin1)
self.connect(self.mcu.gpio.request("motor_2b2"), self.motor_driver2.bin2)
self.connect(self.motor_driver1.sleep, self.motor_driver2.sleep, self.isense.pwr_out.as_digital_source())
self.m2_a = self.Block(MotorConnector((-600, 600) * mAmp))
self.connect(self.m2_a.a, self.motor_driver2.aout1)
self.connect(self.m2_a.b, self.motor_driver2.aout2)
self.m2_b = self.Block(MotorConnector((-600, 600) * mAmp))
self.connect(self.m2_b.a, self.motor_driver2.bout1)
self.connect(self.m2_b.b, self.motor_driver2.bout2)
self.servo = self.Block(PwmConnector((0, 0) * mAmp)) # TODO current modeling
self.connect(self.vbatt, self.servo.pwr)
self.connect(self.gnd, self.servo.gnd)
self.connect(self.mcu.gpio.request("pwm"), self.servo.pwm)
@override
def multipack(self) -> None:
self.led_res = self.PackedBlock(ResistorArray())
self.pack(self.led_res.elements.request("0"), ["leds", "led[0]", "res"])
self.pack(self.led_res.elements.request("1"), ["leds", "led[1]", "res"])
self.pack(self.led_res.elements.request("2"), ["leds", "led[2]", "res"])
self.pack(self.led_res.elements.request("3"), ["leds", "led[3]", "res"])
@override
def refinements(self) -> Refinements:
return super().refinements() + Refinements(
instance_refinements=[
(["mcu"], Esp32_Wroom_32),
(["reg_3v3"], Ap3418),
],
instance_values=[
(
["mcu", "pin_assigns"],
[
"spi.miso=NC",
"i2c.scl=16",
"i2c.sda=14",
"spk=11", # only 10 and 11 are DAC out
"lcd_cs=13",
"lcd_reset=12",
"lcd_dc=10",
"spi.sck=9",
"spi.mosi=8",
"ledArray=23",
"isense=4", # use an input only pin
"motor_2a1=26",
"motor_2a2=27",
"motor_2b2=28",
"motor_2b1=29",
"motor_1a1=30",
"motor_1a2=31",
"motor_1b2=33",
"motor_1b1=36",
"pwm=37",
],
),
(
["expander", "pin_assigns"],
[
"led_0=4",
"led_1=5",
"led_2=6",
"led_3=7",
"tof_reset_0=10",
"tof_reset_1=11",
"tof_reset_2=12",
],
),
(["isense", "out", "signal_out"], Range(0.1, 2.45)), # trade range for resolution
(["isense", "sense", "res", "res", "footprint_spec"], "Resistor_SMD:R_2512_6332Metric"),
(["isense", "sense", "res", "res", "require_basic_part"], False),
# JLC does not have frequency specs, must be checked TODO
(["reg_3v3", "power_path", "inductor", "frequency"], Range(0, 0)),
(["reg_3v3", "power_path", "efficiency"], Range(1.0, 1.0)), # waive this check
(
["mcu", "dac", "spk", "signal_out"],
Range(0.65, 2.65),
), # restrict the range to the speaker driver limits,
],
class_refinements=[
(PassiveConnector, JstPhKVertical), # default connector series unless otherwise specified
(Vl53l0x, Vl53l0xConnector),
(TestPoint, TeRc),
(Speaker, ConnectorSpeaker),
(Neopixel, Ws2812b),
],
class_values=[
(Nonstrict3v3Compatible, ["nonstrict_3v3_compatible"], True),
],
)
class RobotDriverTestCase(unittest.TestCase):
def test_design(self) -> None:
run_test_board(RobotDriver)