- #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)
|