找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2679|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

基于圖像識別的云臺追小球(增量式PID)源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:492026 發(fā)表于 2021-3-3 09:54 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
一、核心的思路
通過openmv識別小球與stm32進(jìn)行串口通行,stm32通過增量式PID算法控制云臺的精準(zhǔn)移動
二、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位數(shù)據(jù)位,無校驗位,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('橫坐標(biāo):%d'%max_blob.cx())
    print('縱坐標(biāo):%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增量式算法不需要做累加,控制量增量的確定僅與最近幾次偏差采樣值有關(guān),計算誤差對控制 量計算的影響較小。而位置式算法要用到過去偏差的累加值,容易產(chǎn)生較大的累加誤差。
2增量式算法得出的是控制量的增量,例如在閥門控制中,只輸出閥門開度的變化部分,誤動作 影響小,必要時還可通過邏輯判斷限制或禁止本次輸出,不會嚴(yán)重影響系統(tǒng)的工作。 而位置式的輸出直接對應(yīng)對象的輸出,因此對系統(tǒng)影響較大。
3增量式PID控制輸出的是控制量增量,并無積分作用,因此該方法適用于執(zhí)行機(jī)構(gòu)帶積分部件的對象,如步進(jìn)電機(jī)等,而位置式PID適用于執(zhí)行機(jī)構(gòu)不帶積分部件的對象,如電液伺服閥。
4在進(jìn)行PID控制時,位置式PID需要有積分限幅和輸出限幅,而增量式PID只需輸出限幅
2、實現(xiàn)代碼
static double   Proportion=2;                               //比例常數(shù) Proportional Const
        static double   Integral=0.4;          //                     //積分常數(shù) Integral Const
        static double   Derivative=0.02;  //
/********************增量式PID控制設(shè)計************************************/
//NextPoint當(dāng)前輸出值
//SetPoint設(shè)定值
int PID_Calc1(float NextPoint,float SetPoint)
{
                             //微分常數(shù) Derivative Const
        static float      LastError;                                //Error[-1]
        static float      PrevError;                                //Error[-2]
  int Outpid;  
  float        iError;//當(dāng)前誤差
        
  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, 下載次數(shù): 49)

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表