Hi,
I recently bought the Motor2040 board, some MMME motors and some MMME encoders, and I am looking into the counting possibilities of the combined eco-system.
My goal is to turn a motor, for example, 890 counts forwards and then backwards for 890 counts.
Forward counting works fine, however when I reverse the direction of the motor and the encoder, the counting does not count down.
When I initialise the encoder at the beginning as reversed, the counting goes negative which is what I expect.
Unfortunately, I understand the libraries/drivers for the motor2040 and the MMME encoder partially.
So is it possible to keep track of the encoder.count (adding/substracting) when changing the direction of the motor?
Please have a look at this code:
import time
from motor import Motor, motor2040
from encoder import Encoder
from pimoroni import NORMAL_DIR, REVERSED_DIR
MOTOR_PINS = motor2040.MOTOR_A # The pins of the motor being profiled
ENCODER_PINS = motor2040.ENCODER_A # The pins of the encoder attached to the profiled motor
GEAR_RATIO = 110
DIRECTION = NORMAL_DIR # The direction to spin the motor in. NORMAL_DIR (0), REVERSED_DIR (1)
m = Motor(MOTOR_PINS, direction=DIRECTION)
enc = Encoder(0, 0, ENCODER_PINS, direction=DIRECTION, counts_per_rev=GEAR_RATIO*4, count_microsteps=True)
#enc = Encoder(0, 0, ENCODER_PINS, direction=REVERSED_DIR, counts_per_rev=GEAR_RATIO*4, count_microsteps=True)
#Go forward
m.enable()
m.speed(1)
while enc.count() < 890: #445:
print("Count =", enc.count()," Turn =", enc.turn()) #print current count position while moving forward
time.sleep(.1)
#Stopped and wait for 3seconds
m.stop()
print("Count after stopped =", enc.count()," Turn =", enc.turn()) #print current count position when stopped
time.sleep(3)
#Go reverse
m.direction(REVERSED_DIR)
enc.direction(REVERSED_DIR)
m.speed(1)
while enc.count() > 0:
print("Count =", enc.count()," Turn =", enc.turn()) #print current count position while moving in reverse
time.sleep(.1)
#Stop motor and disable
m.stop()
print("Count after stopped =", enc.count()," Turn =", enc.turn()) #print current count position when stopped
m.disable()
And here is some output:
>>> %Run -c $EDITOR_CONTENT
Count = 0 Turn = 0
Count = 77 Turn = 0
Count = 188 Turn = 0
Count = 299 Turn = 0
Count = 411 Turn = 0
Count = 522 Turn = 1
Count = 633 Turn = 1
Count = 745 Turn = 1
Count = 857 Turn = 1
Count after stopped = 968 Turn = 2
Count = 996 Turn = 2
Count = 996 Turn = 2
Count = 995 Turn = 2
Count = 994 Turn = 2
Count = 996 Turn = 2
Count = 995 Turn = 2
Count = 994 Turn = 2
Count = 996 Turn = 2
Count = 995 Turn = 2
Count = 994 Turn = 2
Count = 996 Turn = 2
Count = 996 Turn = 2
Count = 994 Turn = 2
Count = 996 Turn = 2
Count = 996 Turn = 2
Count = 994 Turn = 2
Count = 996 Turn = 2
I hope that someone can help me further. Thank you for your time.
Guido