找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2005|回复: 0
收起左侧

基于图像识别的云台追小球(增量式PID)源程序

[复制链接]
ID:492026 发表于 2021-3-3 09:54 | 显示全部楼层 |阅读模式
一、核心的思路
通过openmv识别小球与stm32进行串口通行,stm32通过增量式PID算法控制云台的精准移动
二、openmv程序
import sensor, image, time
from pyb import UART
import json
yellow_threshold   = (50, 75,20, 60, 35, 70)
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.

sensor.set_contrast(3)
clock = time.clock() # Tracks FPS.
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1)  #8位数据位,无校验位,1位停止位
# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.
def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob
while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    yellow_blobs  =  img.find_blobs([yellow_threshold])
    if yellow_blobs:
        max_blob = find_max(yellow_blobs)
        img.draw_cross(max_blob.cx(),max_blob.cy())
        img.draw_circle(max_blob.cx(),max_blob.cy(),max_blob.cx()-max_blob.x(), color = (255, 255, 255))
    a=max_blob.cx()
    b=120-max_blob.cy()
    c=20
    d=50
    e=1
    print('横坐标:%d'%max_blob.cx())
    print('纵坐标:%d'%max_blob.cy())
    data=bytearray([0xb3,0xb3,int(a),int(b),int(c),int(d),int(e),0x5b])
    uart.write(data)
三、增量式pid
1、为什么使用增量式PID
1增量式算法不需要做累加,控制量增量的确定仅与最近几次偏差采样值有关,计算误差对控制 量计算的影响较小。而位置式算法要用到过去偏差的累加值,容易产生较大的累加误差。
2增量式算法得出的是控制量的增量,例如在阀门控制中,只输出阀门开度的变化部分,误动作 影响小,必要时还可通过逻辑判断限制或禁止本次输出,不会严重影响系统的工作。 而位置式的输出直接对应对象的输出,因此对系统影响较大。
3增量式PID控制输出的是控制量增量,并无积分作用,因此该方法适用于执行机构带积分部件的对象,如步进电机等,而位置式PID适用于执行机构不带积分部件的对象,如电液伺服阀。
4在进行PID控制时,位置式PID需要有积分限幅和输出限幅,而增量式PID只需输出限幅
2、实现代码
static double   Proportion=2;                               //比例常数 Proportional Const
        static double   Integral=0.4;          //                     //积分常数 Integral Const
        static double   Derivative=0.02;  //
/********************增量式PID控制设计************************************/
//NextPoint当前输出值
//SetPoint设定值
int PID_Calc1(float NextPoint,float SetPoint)
{
                             //微分常数 Derivative Const
        static float      LastError;                                //Error[-1]
        static float      PrevError;                                //Error[-2]
  int Outpid;  
  float        iError;//当前误差
        
  iError=SetPoint-NextPoint;                           //增量计算
  Outpid=(Proportion * iError)                   //E[k]项
              -(Integral * LastError)      //E[k-1]项
              +(Derivative * PrevError);   //E[k-2]项
              
  PrevError=LastError;                     //存储误差,用于下次计算
  LastError=iError;
  return(Outpid);                                      //返回增量值
}


全部代码51hei下载地址:
云台控制.7z (187.29 KB, 下载次数: 49)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表