|
一、核心的思路
通過(guò)openmv識(shí)別小球與stm32進(jìn)行串口通行,stm32通過(guò)增量式PID算法控制云臺(tái)的精準(zhǔn)移動(dòng)
二、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ù)位,無(wú)校驗(yàn)位,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),計(jì)算誤差對(duì)控制 量計(jì)算的影響較小。而位置式算法要用到過(guò)去偏差的累加值,容易產(chǎn)生較大的累加誤差。
2增量式算法得出的是控制量的增量,例如在閥門控制中,只輸出閥門開度的變化部分,誤動(dòng)作 影響小,必要時(shí)還可通過(guò)邏輯判斷限制或禁止本次輸出,不會(huì)嚴(yán)重影響系統(tǒng)的工作。 而位置式的輸出直接對(duì)應(yīng)對(duì)象的輸出,因此對(duì)系統(tǒng)影響較大。
3增量式PID控制輸出的是控制量增量,并無(wú)積分作用,因此該方法適用于執(zhí)行機(jī)構(gòu)帶積分部件的對(duì)象,如步進(jìn)電機(jī)等,而位置式PID適用于執(zhí)行機(jī)構(gòu)不帶積分部件的對(duì)象,如電液伺服閥。
4在進(jìn)行PID控制時(shí),位置式PID需要有積分限幅和輸出限幅,而增量式PID只需輸出限幅
2、實(shí)現(xiàn)代碼
static double Proportion=2; //比例常數(shù) Proportional Const
static double Integral=0.4; // //積分常數(shù) Integral Const
static double Derivative=0.02; //
/********************增量式PID控制設(shè)計(jì)************************************/
//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; //增量計(jì)算
Outpid=(Proportion * iError) //E[k]項(xiàng)
-(Integral * LastError) //E[k-1]項(xiàng)
+(Derivative * PrevError); //E[k-2]項(xiàng)
PrevError=LastError; //存儲(chǔ)誤差,用于下次計(jì)算
LastError=iError;
return(Outpid); //返回增量值
}
全部代碼51hei下載地址:
云臺(tái)控制.7z
(187.29 KB, 下載次數(shù): 49)
2021-3-3 14:57 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|