ESP32(MicroPython)编码器电机PID距离控制

本程序通过PID控制编码器电机的转动距离,程序运行时输入目标距离(距离为编码器计数,目标距离为距当前位置的距离,如果距离计数改为不归零则可以控制距开始位置的距离),程序累加编码器计数作为距离计数并计算与目标值的偏差,并据此进行PID控制。增大P值有助于确保电机正常启动。实测增大D值对减少抖动、加快距离稳定有明显帮助。最终P值调到8,I调到5,D调到6。由于难以准确评估不同调校的效果,上述参数不一定是最佳参数。认定是否到达目标位置时可以按实际需要留一定余量以加快相应,还要结合结束时的速度进行判断。

代码如下

python 复制代码
from machine import *
import time
from moto import *
import random
'''
本程序使用增量式PID,公式为
Pwm+=Kp[e(k)-e(k-1)]+Ki*e(k)+Kd[e(k)-2e(k-1)+e(k-2)]
e(k):本次偏差
e(k-1):上一次的偏差
e(k-2):上上次的偏差
Kp:比例项参数
Ki:积分项参数
Kd:微分项参数
Pwm:代表增量输出
'''
# 编码器初始化
pin18 = Pin(18, Pin.IN)
pin19 = Pin(19, Pin.IN)   
encoder = encoder(pin18, pin19, 0)   # 参数(编码器A相引脚,编码器B相引脚,定时器序号)

# 电机初始化
motor=Pin(15,Pin.OUT)
motor.value(0)
motor1=PWM(Pin(4),freq=1000,duty=0)
motor2=PWM(Pin(16),freq=1000,duty=0)
while True:
    speed = encoder.read() #测试时发现有时候占空比不能回0,就增加一项检测,确保占空比回零
    if speed>2 or speed<-2: 
        motor1.duty(0)    
        motor2.duty(0)
    else:
        break
duty=0
target=0
distance=0
offset=0 #e(k)
offset1=0 #e(k-1)
offset2=0 #e(k-2)
p=8 #PID参数
i=5
d=6
  
while True:
    motor1.duty(0)    
    motor2.duty(0)
    motor1.duty(0)    
    motor2.duty(0)
    target=int(input('target_distance'))
    while True:
        speed = encoder.read()
        distance+=speed
        offset2=offset1 #记录上一次偏差
        offset1=offset
        offset=target-distance
        adjustmentP=offset-offset1
        adjustmentP*=p
        adjustmentI=offset
        adjustmentI=round(adjustmentI*i)
        adjustmentD=offset-offset1-offset1+offset2
        adjustmentD*=d
        duty=adjustmentP+adjustmentI+adjustmentD
        if duty<-1023:
            duty=-1023
        elif duty>1023:
            duty=1023
        if duty>0:
            if duty<200:
                duty=200
            motor1.duty(duty)
            motor2.duty(0)
        if duty<0:
            if duty>-200:
                duty=-200
            motor1.duty(0)    
            motor2.duty(-duty)
        print(offset,duty)
        if offset<11 and offset>-11 and speed<11 and speed>-11: #误差的速度较小时停止
            motor1.duty(0)    
            motor2.duty(0)
            while True:
                speed = encoder.read() #测试时发现有时候占空比不能回0,就增加一项检测,确保占空比回零
                if speed>2 or speed<-2: 
                    motor1.duty(0)    
                    motor2.duty(0)
                else:
                    break
            distance=0 #不归零则可以控制距开始位置的角度
            break
        time.sleep(0.05)
    
相关推荐
ghie909010 分钟前
MATLAB中编写不平衡磁拉力方程
开发语言·matlab
啥都想学点13 分钟前
关于制作技术视频讲解的问卷调查
python
喵手14 分钟前
Python爬虫实战:博物馆官网的“展览预告/正在热展”栏目,抓取展览名称、精确展期、具体展厅位置以及票务/预约规则(附CSV导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·博物馆信息采集·采集展览预告/正在热展等·采集数据csv导出
喵手15 分钟前
Python爬虫实战:电商实体消歧完整实战 - 从混乱店铺名到标准化知识库的工程化实现,一文带你搞定!
爬虫·python·算法·爬虫实战·零基础python爬虫教学·同名实体消除·从混乱店铺名到标准化知识库
weixin_4521595519 分钟前
C++与Java性能对比
开发语言·c++·算法
会叫的恐龙22 分钟前
C++ 核心知识点汇总(第一日)(输入输出与变量、类型转换)
开发语言·c++
aluluka24 分钟前
Emacs折腾日记(三十六)——打造个人笔记系统
笔记·python·emacs
2301_7657031426 分钟前
C++中的工厂模式实战
开发语言·c++·算法
黎子越27 分钟前
python相关练习
java·前端·python
电商API&Tina31 分钟前
电商数据采集 API 接口 全维度解析(技术 + 商业 + 合规)
java·大数据·开发语言·数据库·人工智能·json