Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf658f0

Browse files
committed
tests/machine_encoder.py: Fix esp32 only 3 pulses.
Add pin initial level. Add test Encoder phases x2 and x4. Signed-off-by: Ihor Nehrutsa <[email protected]>
1 parent 744270a commit cf658f0

File tree

1 file changed

+88
-21
lines changed

1 file changed

+88
-21
lines changed

tests/extmod_hardware/machine_encoder.py

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@
1313
import sys
1414
from machine import Pin
1515

16+
PRINT = False
17+
PIN_INIT_VALUE = 1
18+
1619
if "esp32" in sys.platform:
1720
id = 0
1821
out0_pin = 4
1922
in0_pin = 5
2023
out1_pin = 12
2124
in1_pin = 13
25+
elif sys.platform == "mimxrt":
26+
if "Teensy" in sys.implementation._machine:
27+
id = 0
28+
out0_pin = "D2"
29+
in0_pin = "D3"
30+
out1_pin = "D5"
31+
in1_pin = "D4"
2232
else:
2333
print("Please add support for this test on this platform.")
2434
raise SystemExit
@@ -33,25 +43,70 @@
3343

3444
class TestEncoder(unittest.TestCase):
3545
def setUp(self):
36-
out0_pin(0)
37-
out1_pin(0)
38-
self.enc = Encoder(id, in0_pin, in1_pin)
46+
out0_pin(PIN_INIT_VALUE)
47+
out1_pin(PIN_INIT_VALUE)
48+
self.enc = Encoder(id, in0_pin, in1_pin, phases=1)
49+
self.enc2 = Encoder(id + 1, in0_pin, in1_pin, phases=2)
50+
self.enc4 = Encoder(id + 2, in0_pin, in1_pin, phases=4)
3951
self.pulses = 0 # track the expected encoder position in software
52+
if PRINT:
53+
print(
54+
"\nout0_pin() out1_pin() enc.value() enc2.value() enc4.value() |",
55+
out0_pin(),
56+
out1_pin(),
57+
"|",
58+
self.enc.value(),
59+
self.enc2.value(),
60+
self.enc4.value(),
61+
)
4062

4163
def tearDown(self):
4264
self.enc.deinit()
65+
try:
66+
self.enc2.deinit()
67+
except:
68+
pass
69+
try:
70+
self.enc4.deinit()
71+
except:
72+
pass
4373

4474
def rotate(self, pulses):
4575
for _ in range(abs(pulses)):
4676
self.pulses += 1 if (pulses > 0) else -1
47-
q = self.pulses % 4
48-
# Only one pin should change state each "step" so output won't glitch
49-
out0_pin(q in (1, 2))
50-
out1_pin(q in (2, 3))
77+
if pulses > 0:
78+
if self.pulses % 2:
79+
out0_pin(not out0_pin())
80+
else:
81+
out1_pin(not out1_pin())
82+
else:
83+
if self.pulses % 2:
84+
out1_pin(not out1_pin())
85+
else:
86+
out0_pin(not out0_pin())
87+
if PRINT:
88+
print(
89+
"out0_pin() out1_pin() enc.value() enc2.value() enc4.value() pulses self.pulses |",
90+
out0_pin(),
91+
out1_pin(),
92+
"|",
93+
self.enc.value(),
94+
self.enc2.value(),
95+
self.enc4.value(),
96+
"|",
97+
pulses,
98+
self.pulses,
99+
)
51100

52-
def assertPosition(self, value):
101+
def assertPosition(self, value, value2=None, value4=None):
53102
self.assertEqual(self.enc.value(), value)
103+
if not value2 is None:
104+
self.assertEqual(self.enc2.value(), value2)
105+
if not value4 is None:
106+
self.assertEqual(self.enc4.value(), value4)
107+
pass
54108

109+
@unittest.skipIf(sys.platform == "mimxrt", "cannot read back the pin")
55110
def test_connections(self):
56111
# Test the hardware connections are correct. If this test fails, all tests will fail.
57112
for ch, outp, inp in ((0, out0_pin, in0_pin), (1, out1_pin, in1_pin)):
@@ -64,35 +119,47 @@ def test_connections(self):
64119
def test_basics(self):
65120
self.assertPosition(0)
66121
self.rotate(100)
67-
self.assertPosition(100 // 4)
122+
self.assertPosition(100 // 4, 100 // 2, 100)
68123
self.rotate(-100)
69124
self.assertPosition(0)
70125

71126
def test_partial(self):
72127
# With phase=1 (default), need 4x pulses to count a rotation
73128
self.assertPosition(0)
74129
self.rotate(1)
75-
self.assertPosition(0)
130+
self.assertPosition(1, 1, 1)
76131
self.rotate(1)
77-
self.assertPosition(0)
132+
self.assertPosition(1, 1, 2)
78133
self.rotate(1)
79-
self.assertPosition(1) # only 3 pulses to count first rotation?
134+
self.assertPosition(1, 2, 3)
80135
self.rotate(1)
81-
self.assertPosition(1)
136+
self.assertPosition(1, 2, 4) # +4
82137
self.rotate(1)
83-
self.assertPosition(1)
138+
self.assertPosition(2, 3, 5)
84139
self.rotate(1)
85-
self.assertPosition(1)
140+
self.assertPosition(2, 3, 6)
86141
self.rotate(1)
87-
self.assertPosition(2) # 4 for next rotation
142+
self.assertPosition(2, 4, 7)
143+
self.rotate(1)
144+
self.assertPosition(2, 4, 8) # +4
88145
self.rotate(-1)
89-
self.assertPosition(1)
90-
self.rotate(-4)
91-
self.assertPosition(0)
146+
self.assertPosition(2, 4, 7)
147+
self.rotate(-3)
148+
self.assertPosition(1, 2, 4) # -4
92149
self.rotate(-4)
93-
self.assertPosition(-1)
150+
self.assertPosition(0, 0, 0) # -4
151+
self.rotate(-1)
152+
self.assertPosition(0, 0, -1)
153+
self.rotate(-1)
154+
self.assertPosition(0, -1, -2)
155+
self.rotate(-1)
156+
self.assertPosition(0, -1, -3)
157+
self.rotate(-1)
158+
self.assertPosition(-1, -2, -4) # -4
159+
self.rotate(-1)
160+
self.assertPosition(-1, -2, -5)
94161
self.rotate(-3)
95-
self.assertPosition(-1)
162+
self.assertPosition(-2, -4, -8) # -4
96163

97164

98165
if __name__ == "__main__":

0 commit comments

Comments
 (0)