- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "pwm.h"
- #include "key.h"
- #include "openmv.h"
- #include "data_deal.h"
- #include "control.h"
- #include "ui.h"
- extern int32_t out_pwm,last_out_pwm;
- #define Focal 180//焦距 測量所得
- #define X_Error_MAX 0 //X 軸偏差允許范圍
- openmv_s Dealover_OV;
- TFmini Dealover_TF;
- int32_t X_error,cen_x;
- u16 OV_Deal_count=0,TF_Deal_count=0;
- int Shape_buf[100],i;
- u32 distance_buf[100];
- float Cx_buf[100],Cy_buf[100],Length_buf[100];
- float dealover_distance,Actual_length;
- int still_pwm=0;
- u8 First_start_test_flag,TF_over_flag=0,OV_over_flag=0,Third_start_test_flag=0;
- void GET_Size();
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設置系統中斷優先級分組2
- delay_init(168); //初始化延時函數
- LED_Init();
- KEY_Init();
- uart1_init(115200); //初始化串口波特率為115200
- openmv_init();
- TIM10_PWM_Init(40000,50); //設置為 20000 20ms/20000=0.001ms 脈寬范圍為(20000-500)19500-(20000-2500) 17500
- LCD_Init(); //初始化LCD FSMC接口
- POINT_COLOR=BLACK; //畫筆顏色:紅色
- LED1=0; //PF10 連接openMV p7 LED1亮為1,2
- Servo=1; // PF9 連接openMV p LED1滅為4
- delay_ms(1000);//等待數據穩定
-
- while(1)
- {
-
- /**********題1.2**********/
- if(KEY_Scan(1)==1)
- {
-
- First_start_test_flag=1;
- LED1=1;
- }
- /**********題3**********/
- if(KEY_Scan(1)==3)
- {
- Third_start_test_flag=1;
- }
- /***************開始讀取值并處理***************/
- if(First_start_test_flag)
- {
- LCD_ShowString(60,10,140,140,16,"Testing");
- GET_Size();
- //delay_ms(1000);//測完值就等待1s
- /***********所有數據處理完畢 顯示界面*******************/
- if(TF_over_flag&&OV_over_flag)
- {
- LCD_ShowString(20,10,140,140,16,"Test Over");
- if(First_start_test_flag)
- UI_1(Dealover_OV,Actual_length,dealover_distance);
- TF_over_flag=0;
- OV_over_flag=0;
- First_start_test_flag=0;
- }
- }
- if(Third_start_test_flag==1)
- {
- LCD_ShowString(60,10,140,140,16,"Start Find");
- if(openmvs.flag==1)
- {
- if(openmvs.shape==2)
- cen_x=openmvs.cx+openmvs.length/2;
- else
- cen_x=openmvs.cx;
- if((int)cen_x<80)
- X_error= 80-(int)cen_x;
- else
- X_error= (int)cen_x-80;
- LCD_ShowNum(80,160,(int)openmvs.cx,5,16);
- /*************獲取中點**************/
- /************得出偏差************/
- //LCD_ShowNum(80,160,openmvs.shape,5,16);
- LCD_ShowNum(80,200,X_error,5,16);
- if(X_error<=X_Error_MAX)
- {
- GET_Size();
- LCD_ShowNum(80,200,X_error,5,16);
- LCD_ShowString(20,10,140,140,16,"Find over");
- UI_1(openmvs,Actual_length,dealover_distance);
- Servo=0; //舵機斷電
- TF_over_flag=0;
- OV_over_flag=0;
- Third_start_test_flag=0;
- still_pwm=last_out_pwm;
- }
- if(Third_start_test_flag==1)
- {
- if(openmvs.shape!=0)
- {
- PlacePID_CS(cen_x);
- delay_ms(30);
- if(X_error<=15)
- delay_ms(50);
- }
- else
- {
- saomiao();
- delay_ms(30);
-
- }
- }
- }
- }
- }
- }
- void GET_Size(void )
- {
- if(tfmini.receiveComplete==1&&TF_Deal_count<100)
- {
- distance_buf[TF_Deal_count]=tfmini.distance;
- TF_Deal_count++;
-
- }
- if(TF_Deal_count>=100)
- {
- dealover_distance=tfmini_deal(distance_buf);
- //LCD_ShowNum(150,50,dealover_distance,5,16);
- TF_Deal_count=0;
- TF_over_flag=1;
- //LCD_ShowString(10,70,140,140,16,"2");
- }
-
- if((openmvs.flag==1)&&(OV_Deal_count<100) )
- {
- //LCD_ShowString(10,70,140,140,16,"2");
- Shape_buf[OV_Deal_count]=openmvs.shape ;
- Cx_buf[OV_Deal_count]= openmvs.cx;
- Cy_buf[OV_Deal_count]= openmvs.cx ;
- Length_buf[OV_Deal_count]= openmvs.length ;
- openmvs.flag=0;
- OV_Deal_count++;
- }
- // LCD_ShowNum(80,300,(u16)Shape_buf[OV_Deal_count-1],1,16);
- if(OV_Deal_count>=100)
- {
- // for(i=0;i<100;i++) //數據處理
- // LCD_ShowNum(80,200,Shape_buf[i],5,16);
- Dealover_OV=openmv_data_deal(Shape_buf,Cx_buf,Cy_buf,Length_buf) ;
- OV_Deal_count=0;
- OV_over_flag=1;
- //LCD_ShowNum(80,200,(u16)Dealover_OV.shape,5,16);
- }
- Actual_length=Get_Actuallength(Dealover_OV,dealover_distance,Focal);
- }
復制代碼
Keil代碼下載:
代碼.7z
(364.22 KB, 下載次數: 34)
2022-5-15 04:02 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|