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)
    
相关推荐
AI原吾2 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
机器视觉知识推荐、就业指导2 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
毕设木哥2 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
珞瑜·2 小时前
Matlab R2024B软件安装教程
开发语言·matlab
weixin_455446172 小时前
Python学习的主要知识框架
开发语言·python·学习
孤寂大仙v2 小时前
【C++】STL----list常见用法
开发语言·c++·list
D11_3 小时前
Pandas缺失值处理
python·机器学习·数据分析·numpy·pandas
花生了什么树~.3 小时前
python基础知识(四)--if语句,for\while循环
python
她似晚风般温柔7893 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
咩咩大主教3 小时前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用