I’m struggling to read the speed value of the motor encoders on the Inventor 2040W. I just don’t understand what syntax / command to run?
I’d like to read the speed and or RPM. If I do a
print (cature_A)
I get all the info, I just don’t get how to pick out individual info.
I’m going by this.
pimoroni-pico/micropython/modules/motor at main · pimoroni/pimoroni-pico · GitHub
Here is my test code for running the motors.
import time
import math
from inventor import Inventor2040W, MOTOR_A, MOTOR_B, NUM_LEDS
from pimoroni import PID, REVERSED_DIR
"""
Demonstrates how to control a motor on Inventor 2040 W.
"""
GEAR_RATIO = 110
DIRECTION = REVERSED_DIR
# Create a new Inventor2040W
board = Inventor2040W(motor_gear_ratio=GEAR_RATIO)
enc_A = board.encoders[MOTOR_A]
enc_B = board.encoders[MOTOR_B]
#board = Inventor2040W()
A = board.motors[MOTOR_A]
B = board.motors[MOTOR_B]
A.direction(DIRECTION)
enc_A.direction(DIRECTION)
A.enable()
B.enable()
time.sleep(1)
# Drive at full positive
A.speed(1.0)
B.speed(1.0)
time.sleep(1)
capture_A = enc_A.capture()
capture_B = enc_B.capture()
#print (capture_A)
print("A Speed =", A.speed())
print("B Speed =", B.speed())
time.sleep(5)# Access the motor from Inventor and enable it
# Stop moving
A.stop()
B.stop()
capture_A = enc_A.capture()
capture_B = enc_B.capture()
print("A Speed =", A.speed())
print("B Speed =", B.speed())
time.sleep(2)# Access the motor from Inventor and enable it
A.speed(0.5)
B.speed(0.5)
time.sleep(1)
capture_A = enc_A.capture()
capture_B = enc_B.capture()
print("A Speed =", A.speed())
print("B Speed =", B.speed())
time.sleep(5)
# Stop moving
A.stop()
B.stop()
capture_A = enc_A.capture()
capture_B = enc_B.capture()
print("A Speed =", A.speed())
print("B Speed =", B.speed())
time.sleep(2)# Access the motor from Inventor and enable it
A.speed(-0.5)
B.speed(-0.5)
time.sleep(1)
capture_A = enc_A.capture()
capture_B = enc_B.capture()
print("A Speed =", A.speed())
print("B Speed =", B.speed())
time.sleep(5)
# Stop moving
A.stop()
B.stop()
capture_A = enc_A.capture()
capture_B = enc_B.capture()
print("A Speed =", A.speed())
print("B Speed =", B.speed())
time.sleep(2)# Access the motor from Inventor and enable it
# Drive at full negative
A.speed(-1.0)
B.speed(-1.0)
A.full_negative()
B.full_negative()
time.sleep(1)
capture_A = enc_A.capture()
capture_B = enc_B.capture()
print("A Speed =", A.speed())
print("B Speed =", B.speed())
time.sleep(5)
# Coast to a gradual stop
A.coast()
B.coast()
capture_A = enc_A.capture()
capture_B = enc_B.capture()
print("A Speed =", A.speed())
print("B Speed =", B.speed())
time.sleep(5)# Access the motor from Inventor and enable it
time.sleep(5)
# Disable the motor
A.disable()
B.disable()