Hi,
I have a motor2040 quad motor board from pimoroni
Connected to it is a micromotor with an 11:1 gearbox, and a MME encoder
I can read from the encoder fine, and drive the motor at arbitrary speeds
What I am struggling so far to acheive is to drive the motor at a defined target RPM
Can anyone give me any pointers? It feels like this is possible…but I haven’t managed it yet!
I have based the below on the velocity example script, to try and set a specific “revolutions per second” but so far it only outputs:
RPS TARGET = 10, RPS ACTUAL = -25.02543…
The actual never approaches the target, which makes me think I must have got something fairly fundamental wrong here!
import gc
import time
from motor import Motor, motor2040
from encoder import Encoder, MMME_CPR
from pimoroni import Button, PID
"""
Attempting to drive a connected 1:11 ratio motor ar a defined RPM
Press "Boot" to exit the program.
"""
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 = 11 # The gear ratio of the motor
COUNTS_PER_REV = MMME_CPR * GEAR_RATIO # The counts per revolution of the motor's output shaft
DIRECTION = 1 # The direction to spin the motor in. NORMAL_DIR (0), REVERSED_DIR (1)
UPDATE_LIMIT = 100 # How many times to update the motor per second
UPDATE_RATE = 1 / UPDATE_LIMIT
PRINT_DIVIDER = 4 # How many of the updates should be printed (i.e. 2 would be every other update)
# PID values
RPS_KP = 30.0 # RPS proportional (P) gain
RPS_KI = 0.0 # RPS integral (I) gain
RPS_KD = 0.4 # RPS derivative (D) gain
# Free up hardware resources ahead of creating a new Encoder
gc.collect()
# Create a motor
m = Motor(MOTOR_PINS, direction=DIRECTION)
# Create an encoder, using PIO 0 and State Machine 0
enc = Encoder(0, 0, ENCODER_PINS, direction=DIRECTION, counts_per_rev=COUNTS_PER_REV, count_microsteps=True)
# Create the user button
user_sw = Button(motor2040.USER_SW)
# Create PID object for velocity control
rps_pid = PID(RPS_KP, RPS_KI, RPS_KD, UPDATE_RATE)
# Enable the motor to get started
m.enable()
update = 0
# Set the initial value and create a random end value between the extents
target_rps = 1 #rps
rps_pid.setpoint = target_rps
# Continually move the motor until the user button is pressed
while not user_sw.raw():
# Capture the state of the encoder
capture = enc.capture()
# Calculate the acceleration to apply to the motor to move it closer to the velocity setpoint
accel = rps_pid.calculate(capture.revolutions_per_second)
#m.speed(m.speed() + (accel * UPDATE_RATE))
m.speed(0.1)
# Print out the current motor values and their setpoints, but only on every multiple
if update == 0:
print("RPM ACTUAL =", capture.revolutions_per_minute, end=", ")
print("RPS TARGET =", rps_pid.setpoint, end=", ")
print("RPS ACTUAL =", capture.revolutions_per_second, end=", ")
print("RPS TARGET =", rps_pid.setpoint, end=", ")
print("ACCEL VAL =", accel, end="\n")
update += 1 # Move along in time
# Have we reached the end of this sequence
if update >= UPDATE_LIMIT:
update = 0 # Reset the counter
time.sleep(UPDATE_RATE)
# Disable the motor
m.disable()
Shows how to seek to a specific speed - but so far I haven’t got my head around how to map that speed value to an RPM.
Interestingly, whilst I can change the direction of the motor, whichever direction it is moving in, capture.revolutions_per_second reads a negative value - which may be causing the PID issues?