Hi kwagyeman,here is my code.I’m sure my coding skill is bad ,but hope it helps others.
How to connect:use 4 pins (P5,P6,P7,P8) for the motor driver,and 4 pins for encoder (P0,P1,P2,P3),just like the picture below.
And I got a record:
encoder test3 - YouTube
import sensor, image, time, pyb, math
from pyb import Pin, Timer
tim4 = Timer(4, freq=10000)
tim2 = Timer(2, freq=10000)
Lposition = 0
Rposition = 0
baseV=6600
p8 = pyb.Pin("P8",pyb.Pin.OUT_PP)
p7 = pyb.Pin("P7",pyb.Pin.OUT_PP)
p6 = pyb.Pin("P6",pyb.Pin.OUT_PP)
p5 = pyb.Pin("P5",pyb.Pin.OUT_PP)
pin3 = pyb.Pin("P3") #LencoderA
pin2 = pyb.Pin("P2") #LencoderB
pin1 = pyb.Pin("P1") #RencoderA
pin0 = pyb.Pin("P0") #RencoderB
def callback3(line):
global Lposition
if pin3.value(): # rising-edge
Lposition += 1 if pin2.value() else -1
else: # falling-edge
Lposition += -1 if pin2.value() else 1
def callback2(line):
global Lposition
if pin2.value(): # rising-edge
Lposition += -1 if pin3.value() else 1
else: # falling-edge
Lposition += 1 if pin3.value() else -1
def callback1(line):
global Rposition
if pin1.value(): # rising-edge
Rposition += -1 if pin0.value() else 1
else: # falling-edge
Rposition += 1 if pin0.value() else -1
def callback0(line):
global Rposition
if pin0.value(): # rising-edge
Rposition += 1 if pin1.value() else -1
else: # falling-edge
Rposition += -1 if pin1.value() else 1
def R():
ch4 = tim2.channel(4, Timer.PWM, pin=Pin("P5"), pulse_width=baseV)
p6.low()
def RB():
ch1 = tim2.channel(1, Timer.PWM, pin=Pin("P6"), pulse_width=baseV)
p5.low()
def L():
ch71 = tim4.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=baseV)
p8.low()
def LB():
ch2 = tim4.channel(2, Timer.PWM, pin=Pin("P8"), pulse_width=baseV)
p7.low()
extint3 = pyb.ExtInt(pin3, pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_UP, callback3)
extint2 = pyb.ExtInt(pin2, pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_UP, callback2)
extint1 = pyb.ExtInt(pin1, pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_UP, callback1)
extint0 = pyb.ExtInt(pin0, pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_UP, callback0)
while(True):
Lp=(int(Lposition/4))
Rp=(int(Rposition/4))
if Rp>65535:
Rencoder=Rp-(65536*(int(Rposition/262144)))
elif Rp<0:
Rencoder=(65536*(abs(int(Rposition/262144))+1)-abs(Rp))
else:
Rencoder=abs(Rp)
if Lp>65535:
Lencoder=Lp-(65536*(int(Lposition/262144)))
elif Lp<0:
Lencoder=(65536*(abs(int(Lposition/262144))+1)-abs(Lp))
else:
Lencoder=abs(Lp)
#R()
#L()
print('Lenc',Lencoder,'Renc',Rencoder)
Hi iabdalkader,i have a record for last time i use Tim2_CH1 to counting pulses:encoder test4 - YouTube
And my code below.
So did you mean openmv H7 can easier use timer’s encoder mode for two motors.Sounds pretty good!
import sensor, image, time, pyb, math
from pyb import Pin, Timer
tim4 = pyb.Timer(4,prescaler=1, period=65535)
tim2 = pyb.Timer(2,prescaler=1, period=65535)
ch1 = tim4.channel(1, Timer.ENC_A, pin=Pin("P7"))
ch2 = tim4.channel(2, Timer.ENC_B, pin=Pin("P8"))
ch21 = tim2.channel(1, Timer.ENC_A, pin=Pin("P6"))
while(True):
encRencoder=tim4.ENC_AB
encLencoder=tim2.ENC_AB
print('LEFT',tim2.counter(),'RIGHT',tim4.counter())