Introduction: Through the test of KYTB angle encoder by ESP32, the basic functions of KYTB are preliminarily verified. Since the port interrupt of ESP32 has the maximum upper limit frequency, it is experimentally measured that this frequency is about 26kHz.
Key words: angle encoder, KYTB, ESP32
§ 01 angle encoder
this angle encoder comes from the micro encoder on the car model provided by Beijing Keyu. Model: KYTB-1503-1024.
1, Basic information
1. External interface
according to the nameplate of the sensor, the external lead is 6-wire, and the corresponding function is defined as:
[Table 1-1 function definition of encoder pin] PIN1PIN2PIN3PIN4PIN5PIN6GND3 ~ 5VABZNC2. Signal measurement
Test conditions: VCC voltage: + 5V ▲ figure 1.1.1 voltage waveform of a and B ▲ figure 1.1.2 Z,A waveforms§ 02 counting experiment
1, Experimental scheme
1. Experimental platform
use Design and implementation of ESP32-S module adapter board Hardware module. according to MicroPython: Interrupts with ESP32 and ESP8266 | Random Nerd Tutorials The ESP32 port interrupt is used to count the output pulses of KYTB-1503.
2. Connection scheme
install the following table and connect the sensor, such as the three output pins on the far right of the ESP32 adapter module.
[table 2-1 connection scheme] ABCGPIO2GPIO16GPIO17in order to make the output signal compatible with ESP32 level (3.3V), the working power supply for angle sensor is set to 3.3V.
▲ figure 2.0 experimental platform2, Programming experiment
1. Basic counting experiment
(1) Experimental Python codefrom machine import Pin,Timer import time zcount = 0 acount = 0 bcount = 0 allcount = 0 pina = Pin(2, Pin.IN) pinb = Pin(16, Pin.IN) pinz = Pin(17, Pin.IN) def ISR_abz(pin): global acount, bcount, zcount, allcount if pin == pina: acount += 1 if pin == pinb: bcount += 1 if pin == pinz: zcount += 1 allcount += 1 pina.irq(trigger=Pin.IRQ_RISING, handler=ISR_abz) pinb.irq(trigger=Pin.IRQ_RISING, handler=ISR_abz) pinz.irq(trigger=Pin.IRQ_RISING, handler=ISR_abz) while True: print((acount, bcount, zcount, allcount)) time.sleep_ms(250)(2) Output results ▲ figure 2.1 program output counting results
2. Angle count
count according to the rising edge pulse of A, and judge whether to increase or decrease according to the level of B.
zero clearing is completed by using Z pulse.
(1) Implementation codefrom machine import Pin,Timer import time acount = 0 allcount = 0 pina = Pin(2, Pin.IN) pinb = Pin(16, Pin.IN) pinz = Pin(17, Pin.IN) def ISR_abz(pin): global acount, allcount if pin == pina: acount += 1 if pinb.value() == 0: allcount += 1 else: allcount -= 1 if pin == pinz: allcount = 0 acount = 0 pina.irq(trigger=Pin.IRQ_RISING, handler=ISR_abz) pinz.irq(trigger=Pin.IRQ_RISING, handler=ISR_abz) while True: print((acount, allcount)) time.sleep_ms(25X)(2) Output results
you can see that the output variables A and allcount are the same increment when turning to one side, and they are positive and negative values when rotating in the opposite direction.
it can be verified that the counting range is 0 ~ 1023.
▲ Figure 2.2 technical mode output results3. Pulse loss
due to the use of port interrupt to obtain the corresponding number of pulses, if the angle encoder rotates too fast, the pulse will be lost.
in ISR, when Z pulse appears, maintain the ALLCOUNT variable. It can be seen that when the encoder rotates fast, the maximum value of ALLCOUNT is less than 1024. This shows that there will be a lot of pulse loss.
§ 03 ESP32 count
in the previous counting process using ESP32, the pulse is lost due to the use of port interrupt to count the pulse. How fast can the ESP32 port interrupt respond to the count of frequency?
1, Test the maximum interrupt frequency of ESP32 port
1. Test scheme
use DG1062 digital programmable signal generator to output square wave and send it to ESP32 GPIO2, causing port interruption. In ESP32, a timer is used to generate an interrupt in 1 second and read the count of GPIO2. Test how big the input signal is, and the count begins to be lost.
(1) Test Python codefrom machine import Pin,Timer import time acount = 0 allcount = 0 pina = Pin(2, Pin.IN) keepa = 0 time1count = 0 def ISR_abz(pin): global acount, allcount, keepz if pin == pina: acount += 1 pina.irq(trigger=Pin.IRQ_RISING, handler=ISR_abz) def time_isr(n): global keepa, acount, time1count keepa = acount acount = 0 time1count += 1 tim0 = Timer(0) tim0.init(period=1000, mode=Timer.PERIODIC, callback=time_isr) while True: print((acount, keepa, time1count)) time.sleep_ms(250)
2. Test results
when the input frequency is less than 15kHz, the output number is always the same as the frequency. When the input frequency is greater than 15kHz, the output number begins to lose numbers.
(1) Micro Python programfrom machine import Pin,Timer import time acount = 0 allcount = 0 pina = Pin(2, Pin.IN) keepa = 0 time1count = 0 def ISR_abz(pin): global acount, allcount, keepz if pin == pina: acount += 1 pina.irq(trigger=Pin.IRQ_RISING, handler=ISR_abz) def time_isr(n): global keepa, acount, time1count keepa = acount acount = 0 time1count += 1 tim0 = Timer(0) tim0.init(period=1000, mode=Timer.PERIODIC, callback=time_isr) while True: key = input() print(keepa)(2) Measurement procedure
from headm import * from tsmodule.tsvisa import * dg1062open(113) dg1062freq(1, 10000) def thonnycmd(cmd): tspsendwindowkey('Thonny', 's', alt=1, noreturn=1) tspsendwindowkey('Thonny', '%s '%cmd, noreturn=1) def thonnyshs(cmd='', wait=0): tspsendwindowkey('Thonny', 's', alt=1, noreturn=1) if len(cmd) > 0: tspsendwindowkey('Thonny', '%s\r'%cmd, noreturn=1) if wait > 0: time.sleep(wait) tspsendwindowkey('Thonny', 'ac', control=1, noreturn=1) tspfocuswindow('TEASOFT:1') return clipboard.paste() setf = linspace(25000, 27000, 50) testdim = [] for f in setf: dg1062freq(1, floor(f)) time.sleep(2) pastestr = thonnyshs('\r', 0.1).split() x = float(pastestr[-1]) testdim.append(floor(f) - x) printff(f, x) tspsave('measure', setf=setf, out=testdim) plt.plot(setf, testdim) plt.xlabel("Frequency(Hz)") plt.ylabel("Error") plt.grid(True) plt.tight_layout() plt.show() printf('\a')(3) Measurement results ▲ figure 3.1.1 counting errors at different frequencies ▲ figure 3.1.2 counting errors at different frequencies ▲ figure 3.1.3 counting errors at different frequencies
it can be seen that when the frequency actually exceeds 26kHz, the output error will suddenly increase.
※ experimental conclusion ※
the angle encoder from Keyu has been preliminarily tested. Confirm its interface signal function.
use the port interrupt function of ESP32 to count the pulses of the angle sensor. If the pulse frequency is fast, there will be some pulse loss.
use DG1062 programmable signal source to test the maximum port interrupt program of ESP32. When the frequency exceeds 26kHz, the linearity of lost pulse will suddenly increase.
■ links to relevant literature:
- Design and implementation of ESP32-S module adapter board
- MicroPython: Interrupts with ESP32 and ESP8266 | Random Nerd Tutorials
● relevant chart links:
- Table 1-1 function definition of encoder pin
- Figure 1.1.1 voltage waveform of a and B
- Figure 1.1.2 Z,A waveforms
- Table 2-1 connection scheme
- Figure 2.0 experimental platform
- Figure 2.1 program output count results
- Figure 2.2 technical mode output results
- Figure 3.1.1 counting errors at different frequencies
- Figure 3.1.2 counting errors at different frequencies
- Figure 3.1.3 counting errors at different frequencies