kopia lustrzana https://github.com/pimoroni/pimoroni-pico
101 wiersze
3.6 KiB
Python
101 wiersze
3.6 KiB
Python
![]() |
import gc
|
||
|
import time
|
||
|
from pimoroni import Button, PID
|
||
|
from motor import Motor, motor2040
|
||
|
from encoder import Encoder, MMME_CPR
|
||
|
|
||
|
"""
|
||
|
A program to aid in the discovery and tuning of motor PID
|
||
|
values for position control. It does this by commanding the
|
||
|
motor to move repeatedly between two target angles and
|
||
|
plots the measured response.
|
||
|
|
||
|
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 = 50 # The gear ratio of the motor
|
||
|
COUNTS_PER_REV = MMME_CPR * GEAR_RATIO # The counts per revolution of the motor's output shaft
|
||
|
|
||
|
DIRECTION = 0 # The direction to spin the motor in. NORMAL (0), REVERSED (1)
|
||
|
SPEED_SCALE = 5.4 # The scaling to apply to the motor's speed to match its real-world speed
|
||
|
|
||
|
UPDATES = 100 # How many times to update the motor per second
|
||
|
UPDATE_RATE = 1 / UPDATES
|
||
|
PRINT_WINDOW = 0.25 # The time (in seconds) after a new target, to display print out motor values
|
||
|
MOVEMENT_WINDOW = 2.0 # The time (in seconds) between each new target being set
|
||
|
PRINT_DIVIDER = 1 # How many of the updates should be printed (i.e. 2 would be every other update)
|
||
|
|
||
|
# Multipliers for the different printed values, so they appear nicely on the Thonny plotter
|
||
|
SPD_PRINT_SCALE = 10 # Driving Speed multipler
|
||
|
|
||
|
POSITION_EXTENT = 90 # How far from zero to move the motor, in degrees
|
||
|
|
||
|
# PID values
|
||
|
POS_KP = 0.14 # Position proportional (P) gain
|
||
|
POS_KI = 0.0 # Position integral (I) gain
|
||
|
POS_KD = 0.0022 # Position derivative (D) gain
|
||
|
|
||
|
|
||
|
# Free up hardware resources ahead of creating a new Encoder
|
||
|
gc.collect()
|
||
|
|
||
|
# Create a motor and set its speed scale
|
||
|
m = Motor(MOTOR_PINS, direction=DIRECTION, speed_scale=SPEED_SCALE, deadzone=0.0)
|
||
|
|
||
|
# 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 position control
|
||
|
pos_pid = PID(POS_KP, POS_KI, POS_KD, UPDATE_RATE)
|
||
|
|
||
|
# Enable the motor to get started
|
||
|
m.enable()
|
||
|
|
||
|
# Set the initial target position
|
||
|
pos_pid.target = POSITION_EXTENT
|
||
|
|
||
|
|
||
|
update = 0
|
||
|
print_count = 0
|
||
|
|
||
|
# Continually move the motor until the user button is pressed
|
||
|
while user_sw.raw() is not True:
|
||
|
|
||
|
# Capture the state of the encoder
|
||
|
capture = enc.capture()
|
||
|
|
||
|
# Calculate the velocity to move the motor closer to the position target
|
||
|
vel = pos_pid.calculate(capture.degrees, capture.degrees_per_second)
|
||
|
|
||
|
# Set the new motor driving speed
|
||
|
m.speed(vel)
|
||
|
|
||
|
# Print out the current motor values and their targets,
|
||
|
# but only for the first few updates and only every multiple
|
||
|
if update < (PRINT_WINDOW * UPDATES) and print_count == 0:
|
||
|
print("Pos =", capture.degrees, end=", ")
|
||
|
print("Targ Pos =", pos_pid.target, end=", ")
|
||
|
print("Speed = ", m.speed() * SPD_PRINT_SCALE)
|
||
|
|
||
|
# Increment the print count, and wrap it
|
||
|
print_count = (print_count + 1) % PRINT_DIVIDER
|
||
|
|
||
|
update += 1 # Move along in time
|
||
|
|
||
|
# Have we reached the end of this time window?
|
||
|
if update >= (MOVEMENT_WINDOW * UPDATES):
|
||
|
update = 0 # Reset the counter
|
||
|
|
||
|
# Set the new position target to be the inverse of the current target
|
||
|
pos_pid.target = 0.0 - pos_pid.target
|
||
|
|
||
|
time.sleep(UPDATE_RATE)
|
||
|
|
||
|
# Disable the servo
|
||
|
m.disable()
|