Skip to content

Commit d153043

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 <Ihor.Nehrutsa@gmail.com>
1 parent 744270a commit d153043

File tree

1 file changed

+67
-21
lines changed

1 file changed

+67
-21
lines changed

tests/extmod_hardware/machine_encoder.py

Lines changed: 67 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,49 @@
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: print('\nout0_pin() out1_pin() enc.value() enc2.value() enc4.value() |', out0_pin(), out1_pin(), '|', self.enc.value(), self.enc2.value(), self.enc4.value())
4053

4154
def tearDown(self):
4255
self.enc.deinit()
56+
try:
57+
self.enc2.deinit()
58+
except:
59+
pass
60+
try:
61+
self.enc4.deinit()
62+
except:
63+
pass
4364

4465
def rotate(self, pulses):
4566
for _ in range(abs(pulses)):
4667
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))
68+
if pulses > 0:
69+
if self.pulses % 2:
70+
out0_pin(not out0_pin())
71+
else:
72+
out1_pin(not out1_pin())
73+
else:
74+
if self.pulses % 2:
75+
out1_pin(not out1_pin())
76+
else:
77+
out0_pin(not out0_pin())
78+
if PRINT: print('out0_pin() out1_pin() enc.value() enc2.value() enc4.value() pulses self.pulses |', out0_pin(), out1_pin(), '|', self.enc.value(), self.enc2.value(), self.enc4.value(), '|', pulses, self.pulses)
5179

52-
def assertPosition(self, value):
80+
def assertPosition(self, value, value2=None, value4=None):
5381
self.assertEqual(self.enc.value(), value)
82+
if not value2 is None:
83+
self.assertEqual(self.enc2.value(), value2)
84+
if not value4 is None:
85+
self.assertEqual(self.enc4.value(), value4)
86+
pass
5487

88+
@unittest.skipIf(sys.platform == "mimxrt", "cannot read back the pin")
5589
def test_connections(self):
5690
# Test the hardware connections are correct. If this test fails, all tests will fail.
5791
for ch, outp, inp in ((0, out0_pin, in0_pin), (1, out1_pin, in1_pin)):
@@ -64,35 +98,47 @@ def test_connections(self):
6498
def test_basics(self):
6599
self.assertPosition(0)
66100
self.rotate(100)
67-
self.assertPosition(100 // 4)
101+
self.assertPosition(100 // 4, 100 // 2, 100)
68102
self.rotate(-100)
69103
self.assertPosition(0)
70104

71105
def test_partial(self):
72106
# With phase=1 (default), need 4x pulses to count a rotation
73107
self.assertPosition(0)
74108
self.rotate(1)
75-
self.assertPosition(0)
109+
self.assertPosition(1, 1, 1)
76110
self.rotate(1)
77-
self.assertPosition(0)
111+
self.assertPosition(1, 1, 2)
78112
self.rotate(1)
79-
self.assertPosition(1) # only 3 pulses to count first rotation?
113+
self.assertPosition(1, 2, 3)
80114
self.rotate(1)
81-
self.assertPosition(1)
115+
self.assertPosition(1, 2, 4) # +4
82116
self.rotate(1)
83-
self.assertPosition(1)
117+
self.assertPosition(2, 3, 5)
84118
self.rotate(1)
85-
self.assertPosition(1)
119+
self.assertPosition(2, 3, 6)
86120
self.rotate(1)
87-
self.assertPosition(2) # 4 for next rotation
121+
self.assertPosition(2, 4, 7)
122+
self.rotate(1)
123+
self.assertPosition(2, 4, 8) # +4
88124
self.rotate(-1)
89-
self.assertPosition(1)
90-
self.rotate(-4)
91-
self.assertPosition(0)
125+
self.assertPosition(2, 4, 7)
126+
self.rotate(-3)
127+
self.assertPosition(1, 2, 4) # -4
92128
self.rotate(-4)
93-
self.assertPosition(-1)
129+
self.assertPosition(0, 0, 0) # -4
130+
self.rotate(-1)
131+
self.assertPosition(0, 0, -1)
132+
self.rotate(-1)
133+
self.assertPosition(0, -1, -2)
134+
self.rotate(-1)
135+
self.assertPosition(0, -1, -3)
136+
self.rotate(-1)
137+
self.assertPosition(-1, -2, -4) # -4
138+
self.rotate(-1)
139+
self.assertPosition(-1, -2, -5)
94140
self.rotate(-3)
95-
self.assertPosition(-1)
141+
self.assertPosition(-2, -4, -8) # -4
96142

97143

98144
if __name__ == "__main__":

0 commit comments

Comments
 (0)