|
電賽板球系統(tǒng)STM32單片機(jī)源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "string.h"
- #include "ov7670.h"
- #include "tpad.h"
- #include "timer.h"
- #include "exti.h"
- #include "usmart.h"
- #include "EasyTracered.h"
- #include "io_in_out.h"
- #include "oled.h"
- #include "gui.h"
- #include "pid.h"
- /************************************************
- 作者:TZT
- ************************************************/
- void camera_refresh(void);
- /*---------------------結(jié)構(gòu)體全局定義-----------------*/
- RESULT Resured;//判定為的目標(biāo)條件
- /*---------------------LCD顯示-----------------*/
- const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"}; //5種光照模式
- const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"}; //7種特效
- extern u8 ov_sta; //在exit.c里 面定義
- extern u8 ov_frame; //在timer.c里面定義
- /*---------------------------結(jié)構(gòu)體變量-------------------*/
- extern PIDTypdDef M1PID; //PID1
- extern PIDTypdDef M2PID; //PID2
- /*---------------------------x和y的誤差-------------------*/
- float x1_error;
- float x2_error;
- float x3_error;
- float x4_error;
- float y1_error;
- float y2_error;
- float y3_error;
- float y4_error;
- float x1_1error;
- float x2_2error;
- float x3_3error;
- float x4_4error;
- float y1_1error;
- float y2_2error;
- float y3_3error;
- float y4_4error;
-
- float sx_1error;
- float sx_2error;
- float sx_3error;
- float sx_4error;
- float sx_5error;
- float sx_6error;
- float sy_1error;
- float sy_2error;
- float sy_3error;
- float sy_4error;
- float sy_5error;
- float sy_6error;
-
- float sx_1need = 209.0;
- float sx_2need = 254.0;
- float sx_3need = 209.0;
- float sx_4need = 163.0;
- float sx_5need = 117.0;
- float sx_6need = 70.0;
- float sx_7need = 118.0;
-
- float sy_1need = 71.0 ;
- float sy_2need = 116.0;
- float sy_3need = 161.0;
- float sy_4need = 117.0;
- float sy_5need = 71.0;
- float sy_6need = 118.0;
- float sy_7need = 162.0;
- /*---------------------------菜單選擇---------------------*/
- extern unsigned char RWXZ; //#任務(wù)選擇
- int32_t PWM1; //#PWM1
- int32_t PWM2; //#PWM2
- extern float need_x1 = 209.0;
- extern float need_x2 = 209.0;
- extern float need_x3 = 118.0;
- extern float need_x4 = 118.0;
- extern float need_y1 = 182.0;
- extern float need_y2 = 71.0;
- extern float need_y3 = 71.0;
- extern float need_y4 = 182.0;
- float new_x1 ;
- float new_x2 ;
- float new_x3 ;
- float new_x4 ;
- float new_y1 ;
- float new_y2 ;
- float new_y3 ;
- float new_y4 ;
- extern int32_t SET_1;
- extern int32_t SET_2;
- extern int32_t SET_3;
- extern int32_t SET_4;
- /*---------------------位置和PWM變量-----------------*/
- u16 r,y;
- u16 s,z;
- int32_t wz_x,wz_y; //顯示坐標(biāo)
- #define M1_PWMMAX 180 //舵機(jī)一最大值
- #define M2_PWMMAX 140 //舵機(jī)二最大值
- void wkqwz(void)
- {
- if(PFin(7)==0)
- {
- delay_ms(10);
- if(PFin(7)==0)
- {
- while(1)
- {
- camera_refresh();//更新顯示
- wz_x = Resured.x;
- wz_y = Resured.y;
- OLED_ShowNum(0,0,wz_x,4,16); //這句話把存進(jìn)緩存的數(shù)顯示出來(lái)
- OLED_ShowNum(64,0,wz_y,4,16);
- OLED_Refresh_Gram();
- if(PFin(7)==1)
- {
- delay_ms(10);
- if(PFin(7)==1)
- {
- break;
- }
- }
- }
- }
- }
- }
- /*----------------控制舵機(jī)的轉(zhuǎn)動(dòng)角度----------*/
- void kz_hs(int32_t pwm1,int32_t pwm2)
- {
- pwm1 = pwm1 + 1235; //目標(biāo)121,158 實(shí)際219,260
- pwm2 = 1275 - pwm2; //
- TIM_SetCompare1(TIM4,pwm1);//前后
- TIM_SetCompare4(TIM4,pwm2);//
- }
- /*----------------------任務(wù)------------------*/
- void MODE1(void) //任務(wù)一
- {
- TIM_SetCompare1(TIM4,1235);//前后
- TIM_SetCompare4(TIM4,1275);//左右
- }
- void MODE2(void) //任務(wù)二
- {
-
- // x = 163;
- // y = 117;
- const float priod = 2000.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float set_x,set_y;
-
- MoveTimeCnt += 5;
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 254.0 - (91.0 * Normalization) ;
- set_y = 205.0 - (88.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.5);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.4);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX;
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);//控制函數(shù)
- }
- void MODE3(void) //任務(wù)三
- {
- // x1 = 163 , y1 = 207
- // x2 = 163 , y2 = 117
- const float priod = 1900.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float set_x,set_y;
- float hc_time = 600;
-
- MoveTimeCnt += 5; //每5ms運(yùn)算1次
-
- if(MoveTimeCnt<=priod)
- {
-
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 254.0 - (91.0 * Normalization);// x = 72 ,y = 28;
- set_y = 205.0 - (-2.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.7);
- PID_M1_SetKi(0.07);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.6);
- PID_M2_SetKi(0.07);
- PID_M2_SetKd(100);
- }
- else if(MoveTimeCnt>(priod+hc_time))
- {
- Normalization = (float)(MoveTimeCnt-(priod+hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (0.0 * Normalization);// x = 72 ,y = 28;
- set_y = 207.0 - (90.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
-
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX;
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);//控制函數(shù)
-
- }
- void MODE4(void) //任務(wù)四
- {
- // x = 72; y = 28;
- const float priod = 2000.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float set_x,set_y;
- float hc_time = 1000;
-
- MoveTimeCnt += 5;
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(MoveTimeCnt<=priod)
- {
-
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 254.0 - (66.0 * Normalization);// x = 72 ,y = 28;
- set_y = 205.0 - (120.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.07);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.07);
- PID_M2_SetKd(100);
- }
- /*---------------------------------------------*/
- else if(MoveTimeCnt>priod)
- {
- Normalization = (float)(MoveTimeCnt-priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 188.0 - (116.0 * Normalization);// x = 72 ,y = 28;
- set_y = 85.0 - (57.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
- }
-
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX;
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);//控制函數(shù)
- }
- void MODE5(void) //任務(wù)五
- {
- // x1 = 254; y1 = 205;
- // x2 = 254; y2 = 116;
- // x3 = 163; y3 = 25;
- // x3 = 72 ; y4 = 28;
-
- const float priod = 2000.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float set_x,set_y;
- float hc_time = 500;
-
-
- MoveTimeCnt += 5; //每5ms運(yùn)算1次
- /*-----------------1 到 2 ----------------------------*/
- if(MoveTimeCnt<=priod)
- {
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 254.0 - (0.0 * Normalization);// x = 254 ,y = 116;
- set_y = 205.0 - (89.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- /*-----------------2 到 6 ----------------------------*/
- else if((MoveTimeCnt > (priod+hc_time))&&(MoveTimeCnt < (2.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt-(priod+hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 254.0 - (91.0 * Normalization);// x = 72 ,y = 28;
- set_y = 116.0 - (91.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*-----------------6 到 9 ----------------------------*/
- else if(MoveTimeCnt>( 2.0 * priod + hc_time ))
- {
- Normalization = (float)(MoveTimeCnt-(2.0 * priod + hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (91.0 * Normalization);// x = 163 ,y = 25;
- set_y = 25.0 - (0.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
- }
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
-
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX;
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);//控制函數(shù)
- }
- void MODE6(void) //任務(wù)六 走 四個(gè)點(diǎn) 走不了對(duì)角線
- {
- const float priod = 1500.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float set_x,set_y;
- float hc_time = 100;
-
- MoveTimeCnt += 5; //每5ms運(yùn)算1次
- /*----------------------180------------a到b-----------------------*/
- if(((need_x1-need_x2)==180)||((need_x1-need_x2)==-180)||((need_y1-need_y2)==180)||((need_y1-need_y2)==-180))
- {
- if(MoveTimeCnt<=priod)
- {
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- new_x1 = (need_x1 + need_x2)/2.0 ; //209-209
- new_y1 = (need_y1 + need_y2)/2.0 ; //209-209
-
- new_x1= (new_x1 + 163) /2.0; //209
- new_y1 = (new_y1 + 117)/2.0 ; //117
-
- x1_1error = new_x1 - need_x1; //163-250 = -87
- y1_1error = new_y1 - need_y1; //72-28 = 44
-
- set_x = need_x1 + (x1_1error * Normalization); //250 + -87 = 163
- set_y = need_y1 + (y1_1error * Normalization) ;//26 + 44 = 70
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
- }
- else if((MoveTimeCnt > (priod + hc_time) )&&( MoveTimeCnt < (2.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt- priod ) / priod; //對(duì)單擺周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x1_error = need_x2 - new_x1 ;
- y1_error = need_y2 - new_y1;
-
- set_x = new_x1 + (x1_error* Normalization);// x = 254 ,y = 116;
- set_y = new_y1 + (y1_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
- }
- }
- else if(MoveTimeCnt<= 2.0 * priod )
- {
- Normalization = (float)MoveTimeCnt / 2.0 * priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x1_error = need_x1 - need_x2; //209-209
- y1_error = need_y1 - need_y2; // 182-71
- set_x = need_x1 - (x1_error * Normalization);// x = 254 ,y = 116;
- set_y = need_y1 - (y1_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(20.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(20.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*----------------------180------------b到c-----------------------*/
- if(((need_x1-need_x2)==180)||((need_x1-need_x2)==-180)||((need_y1-need_y2)==180)||((need_y1-need_y2)==-180))
- {
- if((MoveTimeCnt > (2.0 * priod+ hc_time) )&&( MoveTimeCnt < (3.0 * priod+ hc_time)))
- {
- Normalization = (float)(MoveTimeCnt- (2.0 * priod- hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- new_x2 = (need_x2 + need_x3)/2.0 ; //209-209
- new_y2 = (need_y2 + need_y3)/2.0 ; //209-209
-
- new_x2= (new_x2 + 163) /2.0; //209
- new_y2 = (new_y2 + 117)/2.0 ; //117
-
- x2_2error = new_x2 - need_x2; //163-250 = -87
- y2_2error = new_y2 - need_y2; //72-28 = 44
-
- set_x = need_x2 + (x2_2error * Normalization); //250 + -87 = 163
- set_y = need_y2 + (y2_2error * Normalization) ;//26 + 44 = 70
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
- }
- else if((MoveTimeCnt > (3.0 * priod + hc_time))&&( MoveTimeCnt < (4.0 * priod+ hc_time)))
- {
- Normalization = (float)(MoveTimeCnt- (3.0*priod- hc_time) ) / priod; //對(duì)單擺周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x2_error = need_x3 - new_x2 ;
- y2_error = need_y3 - new_y2;
-
- set_x = new_x2 + (x2_error* Normalization);// x = 254 ,y = 116;
- set_y = new_y2 + (y2_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
- }
- }
- else if((MoveTimeCnt > (2.0 * priod + hc_time))&&(MoveTimeCnt < (4.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt-(2.0 * priod+ hc_time)) / 2.0 * priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x2_error = need_x2 - need_x3; // 209- 118
- y2_error = need_y2 - need_y3; // 71 -71
- set_x = need_x2 - (x2_error * Normalization);// x = 72 ,y = 28;
- set_y = need_y2 - (y2_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(20.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(20.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*----------------------180------------c到d-----------------------*/
- if(((need_x1-need_x2)==180)||((need_x1-need_x2)==-180)||((need_y1-need_y2)==180)||((need_y1-need_y2)==-180))
- {
- if((MoveTimeCnt > (4.0 * priod+ hc_time) )&&( MoveTimeCnt < (5.0 * priod+ hc_time)))
- {
- Normalization = (float)(MoveTimeCnt- (4.0 * priod- hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- new_x3 = (need_x3 + need_x4)/2.0 ; //209-209
- new_y3 = (need_y3 + need_y4)/2.0 ; //209-209
-
- new_x3= (new_x3 + 163) /2.0; //209
- new_y3 = (new_y3 + 117)/2.0 ; //117
-
- x3_3error = new_x3 - need_x3; //163-250 = -87
- y3_3error = new_y3 - need_y3; //72-28 = 44
-
- set_x = need_x3 + (x3_3error * Normalization); //250 + -87 = 163
- set_y = need_y3 + (y3_3error * Normalization) ;//26 + 44 = 70
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
- }
- else if((MoveTimeCnt > (5.0 * priod + hc_time))&&( MoveTimeCnt < (6.0 * priod+ hc_time)))
- {
- Normalization = (float)(MoveTimeCnt- (5.0 * priod - hc_time)) / priod; //對(duì)單擺周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x3_error = need_x4 - new_x3 ;
- y3_error = need_y4 - new_y3;
-
- set_x = new_x3 + (x3_error* Normalization);// x = 254 ,y = 116;
- set_y = new_y3 + (y3_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
- }
- }
- else if(MoveTimeCnt> (6.0 * priod + hc_time ))
- {
- Normalization = (float)(MoveTimeCnt-(4.0 * priod+ hc_time) ) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x3_error = need_x3 - need_x4; //118-118
- y3_error = need_y3 - need_y4; //73 - 182
- set_x = need_x3 - (x3_error * Normalization);// x = 163 ,y = 25;
- set_y = need_y3 - (y3_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(20.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(20.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
-
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX;
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);//控制函數(shù)
- }
- void MODE7(void) //任務(wù)七 轉(zhuǎn) 3 圈 回 9
- {
- const float priod = 1000.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float set_x,set_y;
- float hc_time = 1500;
-
- MoveTimeCnt += 5; //每5ms運(yùn)算1次
-
- /*--------------------4-------------------------*/
- if(MoveTimeCnt<=priod)
- {
-
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (0.0 * Normalization);// x = 72 ,y = 28;
- set_y = 207.0 - (45.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.3);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.2);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R1_1-----------------------*/
- else if((MoveTimeCnt>priod)&&(MoveTimeCnt<2.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (-46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 162.0 - (45.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R1_2-----------------------*/
- else if((MoveTimeCnt>2.0*priod)&&(MoveTimeCnt<3.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-2.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 209.0 - (46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 117.0 - (46.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R1_3-----------------------*/
- else if((MoveTimeCnt>(4.0*priod))&&(MoveTimeCnt<(5.0*priod))) //4
- {
- Normalization = (float)(MoveTimeCnt-4.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 71.0 - (-47.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R1_4-----------------------*/
- else if((MoveTimeCnt>5.0*priod)&&(MoveTimeCnt<6.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-5.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 117.0 - (-46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 118.0 - (-47.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R2_1-----------------------*/
- else if((MoveTimeCnt>6.0*priod)&&(MoveTimeCnt<7.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-6.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (-46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 162.0 - (45.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R2_2-----------------------*/
- else if((MoveTimeCnt>7.0*priod)&&(MoveTimeCnt<8.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-7.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 209.0 - (46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 117.0 - (46.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R2_3-----------------------*/
- else if((MoveTimeCnt>(8.0*priod))&&(MoveTimeCnt<(9.0*priod))) //4
- {
- Normalization = (float)(MoveTimeCnt-8.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 71.0 - (-47.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R2_4-----------------------*/
- else if((MoveTimeCnt>9.0*priod)&&(MoveTimeCnt<10.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-9.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 117.0 - (-46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 118.0 - (-47.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R3_1-----------------------*/
- else if((MoveTimeCnt>10.0*priod)&&(MoveTimeCnt<11.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-10.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (-46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 162.0 - (45.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R3_2-----------------------*/
- else if((MoveTimeCnt>11.0*priod)&&(MoveTimeCnt<12.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-11.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 209.0 - (46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 117.0 - (46.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R3_3-----------------------*/
- else if((MoveTimeCnt>(12.0*priod))&&(MoveTimeCnt<(13.0*priod))) //4
- {
- Normalization = (float)(MoveTimeCnt-12.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 71.0 - (-47.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R3_4-----------------------*/
- else if((MoveTimeCnt>13.0*priod)&&(MoveTimeCnt<14.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-13.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 117.0 - (-46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 118.0 - (-47.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R4_1-----------------------*/
- else if((MoveTimeCnt>14.0*priod)&&(MoveTimeCnt<15.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-14.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (-46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 162.0 - (45.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------R4_2-----------------------*/
- else if((MoveTimeCnt>15.0*priod)&&(MoveTimeCnt<16.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-15.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 209.0 - (46.0 * Normalization);// x = 72 ,y = 28;
- set_y = 117.0 - (46.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
- /*---------------------回九-----------------------*/
- else if((MoveTimeCnt>16.0*priod)&&(MoveTimeCnt<17.0*priod))
- {
- Normalization = (float)(MoveTimeCnt-16.0*priod) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- set_x = 163.0 - (91.0 * Normalization);// x = 72 ,y = 28;
- set_y = 71.0 - (43.0 * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(6.2);
- PID_M1_SetKi(0.01);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(6.1);
- PID_M2_SetKi(0.01);
- PID_M2_SetKd(100);
- }
-
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
-
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX;
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);
- }
- void MODE8(void) //發(fā)揮
- {
-
- // float sx_1need = 209.0;
- // float sx_2need = 254.0;
- // float sx_3need = 209.0;
- // float sx_4need = 163.0;
- // float sx_5need = 117.0;
- // float sx_6need = 70.0;
- // float sx_7need = 118.0;
- //
- // float sy_1need = 71.0 ;
- // float sy_2need = 116.0;
- // float sy_3need = 161.0;
- // float sy_4need = 117.0;
- // float sy_5need = 71.0;
- // float sy_6need = 118.0;
- // float sy_7need = 162.0;
- const float priod = 2000.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float set_x,set_y;
- float hc_time = 500;
-
- MoveTimeCnt += 5; //每5ms運(yùn)算1次
- /*-----------------A 到 B ----------------------------*/
-
-
- if(MoveTimeCnt<=priod)
- {
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- sx_1error = sx_1need - sx_2need; //209-209
- sy_1error = sy_1need - sy_2need; // 182-71
- set_x = sx_1need - (sx_1error * Normalization);// x = 254 ,y = 116;
- set_y = sy_1need - (sy_1error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- /*-----------------B 到 C ----------------------------*/
- else if((MoveTimeCnt > (priod+hc_time))&&(MoveTimeCnt < (2.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt-(priod+hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
-
- sx_2error = sx_2need - sx_3need; //209-209
- sy_2error = sy_2need - sy_3need; // 182-71
- set_x = sx_2need - (sx_2error * Normalization);// x = 254 ,y = 116;
- set_y = sy_2need - (sy_2error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- /*-----------------C 到 D ----------------------------*/
- else if((MoveTimeCnt > (2.0* priod+hc_time))&&(MoveTimeCnt < (3.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt-(2.0 * priod + hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
-
- sx_3error = sx_3need - sx_4need; //209-209
- sy_3error = sy_3need - sy_4need; // 182-71
- set_x = sx_3need - (sx_3error * Normalization);// x = 254 ,y = 116;
- set_y = sy_3need - (sy_3error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- else if((MoveTimeCnt > (3.0* priod+hc_time))&&(MoveTimeCnt < (4.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt-(3.0 * priod + hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
-
- sx_4error = sx_4need - sx_5need; //209-209
- sy_4error = sy_4need - sy_5need; // 182-71
- set_x = sx_4need - (sx_4error * Normalization);// x = 254 ,y = 116;
- set_y = sy_4need - (sy_4error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- else if((MoveTimeCnt > (4.0* priod+hc_time))&&(MoveTimeCnt < (5.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt-(4.0 * priod + hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
-
- sx_5error = sx_5need - sx_6need; //209-209
- sy_5error = sy_5need - sy_6need; // 182-71
- set_x = sx_5need - (sx_5error * Normalization);// x = 254 ,y = 116;
- set_y = sy_5need - (sy_5error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- else if((MoveTimeCnt > (5.0* priod+hc_time))&&(MoveTimeCnt < (6.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt-(5.0 * priod + hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
-
- sx_6error = sx_6need - sx_7need; //209-209
- sy_6error = sy_6need - sy_7need; // 182-71
- set_x = sx_6need - (sx_6error * Normalization);// x = 254 ,y = 116;
- set_y = sy_6need - (sy_6error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
-
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX;
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);
- }
- void MODE9(void) //附加 簡(jiǎn)單任務(wù)六
- {
- const float priod = 2000.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float set_x,set_y;
- float hc_time = 100;
-
- MoveTimeCnt += 5;
-
- if(MoveTimeCnt<=priod)
- {
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x1_error = need_x1 - need_x2; //209-209
- y1_error = need_y1 - need_y2; // 182-71
- set_x = need_x1 - (x1_error * Normalization);// x = 254 ,y = 116;
- set_y = need_y1 - (y1_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- /*-----------------B 到 C ----------------------------*/
- else if((MoveTimeCnt > (priod+hc_time))&&(MoveTimeCnt < (2.0 * priod + hc_time)))
- {
- Normalization = (float)(MoveTimeCnt-(priod+hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x2_error = need_x2 - need_x3; // 209- 118
- y2_error = need_y2 - need_y3; // 71 -71
- set_x = need_x2 - (x2_error * Normalization);// x = 72 ,y = 28;
- set_y = need_y2 - (y2_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- /*-----------------C 到 D ----------------------------*/
- else if(MoveTimeCnt>( 2.0 * priod + hc_time ))
- {
- Normalization = (float)(MoveTimeCnt-(2.0 * priod + hc_time)) / priod; //對(duì)板球周期歸一化
- if(Normalization>=1.0)
- {
- Normalization = 1.0;
- }
- x3_error = need_x3 - need_x4; //118-118
- y3_error = need_y3 - need_y4; //73 - 182
- set_x = need_x3 - (x3_error * Normalization);// x = 163 ,y = 25;
- set_y = need_y3 - (y3_error * Normalization) ;
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.04);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.04);
- PID_M2_SetKd(100);
- }
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
-
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX; //100
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);//控制函數(shù)
- }
- void MODE10(void)//無(wú)效
- {
- const float priod = 50000.0; //周期(毫秒)
- const uint32_t x_1 = 219;
- const uint32_t y_1 = 260;
- const uint32_t need_x = 121;
- const uint32_t need_x_erro = 98;
- const uint32_t need_y = 158;
- const uint32_t need_y_erro = 102;
- static uint32_t MoveTimeCnt = 0;
- float Normalization = 0.0;
- float A = 26.0;
- float phase = 0.0;
- float Omega = 0.0;
- float set_x = 0.0;
- float set_y = 0.0;
-
- MoveTimeCnt += 5;
- Normalization = (float)MoveTimeCnt / priod; //對(duì)板球周期歸一化
- Omega = 2.0*3.14159*Normalization; //對(duì)2π進(jìn)行歸一化處理
-
- phase = 3.141592/2.0; //逆時(shí)針旋轉(zhuǎn)相位差90°
- set_x = A*sin(Omega) + 163.0; //計(jì)算出X方向當(dāng)前擺角
- set_y = A*sin(Omega+phase) + 117.0; //計(jì)算出Y方向當(dāng)前擺角
-
- PID_M1_SetPoint(set_x); //x的期望值
- PID_M1_SetKp(8.2);
- PID_M1_SetKi(0.02);
- PID_M1_SetKd(100);
- PID_M2_SetPoint(set_y); //y的期望值
- PID_M2_SetKp(8.1);
- PID_M2_SetKi(0.02);
- PID_M2_SetKd(100);
-
- PWM1=PID_M1_PosLocCalc((float)Resured.x);
-
- PWM2=PID_M2_PosLocCalc((float)Resured.y);
-
- if(PWM1 > M1_PWMMAX) PWM1=M1_PWMMAX; //100
-
- if(PWM1 < -M1_PWMMAX) PWM1=-M1_PWMMAX;
-
- if(PWM2 > M2_PWMMAX) PWM2=M2_PWMMAX;
-
- if(PWM2< -M2_PWMMAX) PWM2=-M2_PWMMAX;
- kz_hs(PWM1,PWM2);//控制函數(shù)
- }
- /*--------------全國(guó)電子設(shè)計(jì)競(jìng)賽顯示---------------*/
- void QGDZJS(void)
- {
- OLED_Init(); //初始化OLED
- OLED_Showone16x16(0,0,1,0); //全
- OLED_Showone16x16(16,0,1,1); //國(guó)
- OLED_Showone16x16(32,0,1,2); //電
- OLED_Showone16x16(48,0,1,3); //子
- OLED_Showone16x16(64,0,1,4); //設(shè)
- OLED_Showone16x16(80,0,1,5); //計(jì)
- OLED_Showone16x16(96,0,1,6); //競(jìng)
- OLED_Showone16x16(112,0,1,7); //賽
- OLED_ShowChar(0,16,'X',16,1); //顯示一個(gè)字符
- OLED_ShowChar(0,32,'Y',16,1); //顯示一個(gè)字符
- OLED_Refresh_Gram(); //oled顯示函數(shù)
- }
- /*---------------------攝像頭初始化----------------*/
- void show_7670(void)
- {
- u8 lightmode=0,saturation=2,contrast=2;
- u8 effect=0;
- POINT_COLOR=RED; //設(shè)置字體為紅色
-
- LCD_ShowString(30,50,200,16,16,"ELITE STM32F103 ^_^");
- LCD_ShowString(30,210,200,16,16,"OV7670 Init...");
- while(OV7670_Init())//初始化OV7670
- {
- LCD_ShowString(30,210,200,16,16,"OV7670 Error!!");
- delay_ms(200);
- LCD_Fill(30,210,239,246,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(30,210,200,16,16,"OV7670 Init OK");
- delay_ms(1500);
- OV7670_Light_Mode(lightmode);
- OV7670_Color_Saturation(saturation);
- OV7670_Contrast(contrast);
- OV7670_Special_Effects(effect);
-
- //TIM6_Int_Init(10000,7199); //10Khz計(jì)數(shù)頻率,1秒鐘中斷
- EXTI8_Init(); //使能定時(shí)器捕獲
- OV7670_Window_Set(12,176,240,320); //設(shè)置窗口
- OV7670_CS=0;
- LCD_Clear(BLACK);
- }
- /*---------------------更新LCD顯示-----------------*/
- void camera_refresh(void)
- {
-
- // TARGET_CONDI Conditionred={215,240,20,240,30,160,30,30,320,240}; //紅色1 API參數(shù) hsl的閾值,識(shí)別時(shí)用的
- // unsigned char H_MIN;//目標(biāo)最小色調(diào)
- // unsigned char H_MAX;//目標(biāo)最大色調(diào)
- //
- // unsigned char S_MIN;//目標(biāo)最小飽和度
- // unsigned char S_MAX;//目標(biāo)最大飽和度
- //
- // unsigned char L_MIN;//目標(biāo)最小亮度
- // unsigned char L_MAX;//目標(biāo)最大亮度
- //
- // unsigned int WIDTH_MIN;//目標(biāo)最小寬度
- // unsigned int HIGHT_MIN;//目標(biāo)最小高度
- // unsigned int WIDTH_MAX;//目標(biāo)最大寬度
- // unsigned int HIGHT_MAX;//目標(biāo)最大高度
- TARGET_CONDI Conditionred={215,240,20,240,30,160,15,15,320,240}; //紅色1 API參數(shù) hsl的閾值,識(shí)別時(shí)用的
- u32 j;
- u16 color;
- if(ov_sta)//有幀中斷更新?
- {
- LCD_Scan_Dir(U2D_L2R); //從上到下,從左到右
- if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,240,320);//將顯示區(qū)域設(shè)置到屏幕中央
- else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//將顯示區(qū)域設(shè)置到屏幕中央
- LCD_WriteRAM_Prepare(); //開(kāi)始寫(xiě)入GRAM
- OV7670_RRST=0; //開(kāi)始復(fù)位讀指針
- OV7670_RCK_L;
- OV7670_RCK_H;
- OV7670_RCK_L;
- OV7670_RRST=1; //復(fù)位讀指針結(jié)束
- OV7670_RCK_H;
- for(j=0;j<76800;j++)
- {
- OV7670_RCK_L;
- color=GPIOC->IDR&0XFF; //讀數(shù)據(jù)
- OV7670_RCK_H;
- color<<=8;
- OV7670_RCK_L;
- color|=GPIOC->IDR&0XFF; //讀數(shù)據(jù)
- OV7670_RCK_H;
- LCD->LCD_RAM=color;
- }
- if(Trace(&Conditionred,&Resured) ) //API
- {
- LCD_Fillred(Resured.x-Resured.w/2,Resured.y-Resured.h/2,Resured.x+Resured.w/2,Resured.y-Resured.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
- LCD_Fillred(Resured.x-Resured.w/2,Resured.y-Resured.h/2,Resured.x-Resured.w/2+1,Resured.y+Resured.h/2,0xf800);
- LCD_Fillred(Resured.x-Resured.w/2,Resured.y+Resured.h/2,Resured.x+Resured.w/2,Resured.y+Resured.h/2+1,0xf800);
- LCD_Fillred(Resured.x+Resured.w/2,Resured.y-Resured.h/2,Resured.x+Resured.w/2+1,Resured.y+Resured.h/2,0xf800);
- LCD_Fillred(Resured.x-2,Resured.y-2,Resured.x+2,Resured.y+2,0xf800);
-
- r=Resured.x;
- y=Resured.y;
- }
- //printf("%d %d\r\n ",r,y);
- ov_sta=0; //清零幀中斷標(biāo)記
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢復(fù)默認(rèn)掃描方向
- }
- }
- /*---------------------主函數(shù)----------------------*/
- int main(void)
- {
- u8 msgbuf[15]; //消息緩存區(qū)
- u8 i=0;
- u8 tm=0;
- delay_init(); //延時(shí)函數(shù)初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置中斷優(yōu)先級(jí)分組為組2:2位搶占優(yōu)先級(jí),2位響應(yīng)優(yōu)先級(jí)
- uart_init(115200); //串口初始化為 115200
- LED_Init(); //初始化與LED連接的硬件接口
- LCD_Init(); //初始化LCD
- io_in_out_Init(); // io口輸入輸出初始化
- TIM4_PWM_Init(3599,49); //pwm 20khz
- show_7670(); //7670和lcd的相關(guān)配置
- QGDZJS(); //OLED初始化 并且顯示全國(guó)大學(xué)生電子設(shè)計(jì)競(jìng)賽字樣
- TIM_SetCompare1(TIM4,1235);// 控制前后舵機(jī)初始化角度
- TIM_SetCompare4(TIM4,1275);// 控制左右舵機(jī)初始化角度
- while(1)
- {
- /*--------------選擇 、修改 、確定 ...都是通過(guò) 4個(gè) 自鎖開(kāi)關(guān) 和 4個(gè)按鍵來(lái)選擇。------*/
- PWMMENUS(); //修改 A B C D 四個(gè)區(qū)域 出發(fā)到結(jié)束的位置
- wkqwz(); //未啟動(dòng)位置 作用:在還沒(méi)啟動(dòng)任務(wù)的時(shí)候先觀察小球在的位置是否滿足要求
- MENUS(); //總?cè)蝿?wù)選擇
- camera_refresh();//更新顯示
- }
- }
- /*---------------------定時(shí)器二--------------------*/
- void TIM2_IRQHandler(void) //TIM2中斷 TIM2的初始化在 總?cè)蝿?wù)選擇里
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //檢查指定的TIM中斷發(fā)生與否:TIM 中斷源
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx的中斷待處理位:TIM 中斷源
- if(((Resured.x>5)&&(Resured.x<315))&&((Resured.y>5)&&(Resured.y<235)))
- {
- switch(RWXZ)
- {
- case 1 : MODE1(); break;
- case 2 : MODE2(); break;
- case 3 : MODE3(); break;
- case 4 : MODE4(); break;
- case 5 : MODE5(); break;
- case 6 : MODE6(); break;
- case 7 : MODE7(); break;
- case 8 : MODE8(); break;
- case 9 : MODE9(); break;
- case 10 : MODE10(); break;
- }
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
F1紅色小球識(shí)別的板球控制-ov7670.7z
(276.36 KB, 下載次數(shù): 83)
2019-1-25 18:36 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|