找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3333|回复: 2
打印 上一主题 下一主题
收起左侧

easy_trace算法源码 OV7670和stm32的颜色识别

[复制链接]
跳转到指定楼层
楼主
ID:330915 发表于 2018-5-16 00:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于OV7670和stm32的颜色识别算法

单片机源程序如下:
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "lcd.h"
  5. #include "usart.h"         
  6. #include "string.h"
  7. #include "ov7670.h"
  8. #include "timer.h"
  9. #include "exti.h"
  10. #include "Trace.h"
  11. #include "can.h"

  12. u8 Way_Angle=1;                             //获取角度的算法,1:四元数  2:卡尔曼  3:互补滤波
  13. u8 Flag_Qian,Flag_Hou,Flag_Left,Flag_Right,Flag_Direction=0; //蓝牙遥控相关的变量
  14. u8 Flag_Stop=1,Flag_Show=0;                 //停止标志位和 显示标志位 默认停止 显示打开
  15. int Encoder_A,Encoder_B,Encoder_C;             //编码器的脉冲计数
  16. int Encoder_A_EXTI;
  17. int Motor_A,Motor_B,Motor_C;                   //电机PWM变量
  18. int Target_A,Target_B,Target_C;                   //电机目标速度
  19. int Temperature;                            //显示温度
  20. int Voltage;                                //电池电压采样相关的变量
  21. float Angle_Balance,Gyro_Balance,Gyro_Turn; //平衡倾角 平衡陀螺仪 转向陀螺仪
  22. float Show_Data_Mb;                         //全局显示变量,用于显示需要查看的数据
  23. u32 Distance;                               //超声波测距
  24. u8 delay_50,delay_flag;         //默认情况下,不开启避障功能,长按用户按键2s以上可以进入避障模式
  25. int Velocity=30;
  26. u8 Usart3_Receive;
  27. u8 rxbuf[8],Rxbuf[16];
  28. u8 txbuf[8];
  29. int AZ,GZ;
  30. float Pitch,Roll,Yaw;
  31. u32 count,usart_count;
  32. int temp,temp2,temp3,count_temp,flag;
  33. u8 ON_txbuf[8]={10,12,15,19,24,30,37,1} ;
  34. u8 Velocity_txbuf[8]={11,13,16,20,25,31,38,50} ;
  35. void car(u16 command);
  36. void carturn(u16 commandturn,u16 turnangle);

  37. char* bian(int a);

  38. u8 flag2=1;


  39. const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"};                                                        //5种光照模式            
  40. const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"};        //7种特效
  41. extern u8 ov_sta;        //在exit.c里面定义
  42. extern u8 ov_frame;        //在timer.c里面定义       



  43. u16 r,y;

  44. u16 i;
  45. u16 j;
  46. unsigned short C16;
  47. //更新LCD显示
  48. void camera_refresh(void)
  49. {
  50.         RESULT Resu;
  51.         TARGET_CONDI Condition={0,240,0,240,200,210,30,30,320,240};        //API参数  hls的阈值,识别时用的       
  52.         u16 color;               

  53.         if(ov_sta==2)
  54.         {
  55.                 LCD_Scan_Dir(U2D_L2R);                //从上到下,从左到右
  56.                 LCD_SetCursor(0x00,0x0000);        //设置光标位置
  57.                 LCD_WriteRAM_Prepare();     //开始写入GRAM       
  58.                 OV7670_RRST=0;                                //开始复位读指针
  59.                 OV7670_RCK=0;
  60.                 OV7670_RCK=1;
  61.                 OV7670_RCK=0;
  62.                 OV7670_RRST=1;                                //复位读指针结束
  63.                 OV7670_RCK=1;         

  64.                 for(i=0;i<240;i++)
  65.                    {
  66.                     for(j=0;j<320;j++)
  67.                                                 {                       
  68.                                                         OV7670_RCK=0;
  69.                         color=GPIOC->IDR&0XFF;        //读数据
  70.                         OV7670_RCK=1;
  71.                         color<<=8;  
  72.                         OV7670_RCK=0;
  73.                         color|=GPIOC->IDR&0XFF;        //读数据
  74.                         OV7670_RCK=1;
  75.       LCD->LCD_RAM=color;

  76.                                                 }   
  77.                                 }       
  78.                        

  79.                
  80.                         if(Trace(&Condition,&Resu) )                      //API
  81.                         {
  82.                                
  83.                                
  84.                                 LCD_Fill(Resu.x-Resu.w/2,Resu.y-Resu.h/2,Resu.x+Resu.w/2,Resu.y-Resu.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
  85.                                 LCD_Fill(Resu.x-Resu.w/2,Resu.y-Resu.h/2,Resu.x-Resu.w/2+1,Resu.y+Resu.h/2,0xf800);
  86.                                 LCD_Fill(Resu.x-Resu.w/2,Resu.y+Resu.h/2,Resu.x+Resu.w/2,Resu.y+Resu.h/2+1,0xf800);
  87.                                 LCD_Fill(Resu.x+Resu.w/2,Resu.y-Resu.h/2,Resu.x+Resu.w/2+1,Resu.y+Resu.h/2,0xf800);
  88.                                 LCD_Fill(Resu.x-2,Resu.y-2,Resu.x+2,Resu.y+2,0xf800);
  89.                                     
  90.                                 //LED1=!LED1;
  91.                                 r=Resu.x;y=Resu.y;
  92.                                
  93.                                 LCD_ShowString(60,130,200,16,16,bian(r));
  94.                                
  95.                                 if(r>130) //偏左//右转                       
  96.                                 {
  97.                                         txbuf[0]=10;
  98.                                 txbuf[1]=12;
  99.                                 txbuf[2]=15;       
  100.                                 txbuf[3]=19;               
  101.                           txbuf[4]=24;
  102.                                 txbuf[5]=30;       
  103.                           txbuf[6]=37;
  104.                                 txbuf[7]=1;        //00000111                                               
  105.                                
  106.                                 CAN1_SEND(0X121,txbuf);//CAN发送
  107.                                 carturn(2,3);
  108.                                 //flag2=1;
  109.                                 //car(1);                       
  110.                                 }
  111.                                 if(r<110)//偏右//左转
  112.                                                 {       
  113.                                                         txbuf[0]=10;
  114.                                 txbuf[1]=12;
  115.                                 txbuf[2]=15;       
  116.                                 txbuf[3]=19;               
  117.                           txbuf[4]=24;
  118.                                 txbuf[5]=30;       
  119.                           txbuf[6]=37;
  120.                                 txbuf[7]=1;        //00000111                                               
  121.                                
  122.                                 CAN1_SEND(0X121,txbuf);//CAN发送
  123.                                 carturn(1,3);
  124.                                 //flag2=2;
  125.                                 //car(1);
  126.                        
  127.                                         }
  128.                                         /*else if((r<=130)&&(r>=110))//前
  129.                                                 {
  130.                                 car(1);
  131.                                         }
  132.                                                 else
  133.                                                 LCD_ShowString(60,130,200,16,16,"Error");*/
  134.                         }
  135.                         /*else
  136.                         {
  137.                                
  138.                                 carturn(1,3);
  139.                         }*/
  140.                
  141.                                
  142.                        
  143.                 EXTI_ClearITPendingBit(EXTI_Line8);  //清除LINE8上的中断标志位
  144.                 ov_sta=0;                                        //开始下一次采集
  145.                 ov_frame++;
  146.                 LCD_Scan_Dir(DFT_SCAN_DIR);        //恢复默认扫描方向
  147.         }
  148. }               
  149. int main(void)
  150. {         
  151.         u8 key;
  152.         u8 lightmode=2,saturation=2,brightness=2,contrast=2;
  153.         u8 effect=0;         
  154.         u8 i=0;            
  155.         u8 msgbuf[15];//消息缓存区
  156.         u8 tm=0;

  157.         delay_init();                     //延时函数初始化          
  158.         NVIC_Configuration();          //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
  159.         uart_init(9600);                 //串口初始化为9600
  160.         LED_Init();                             //LED端口初始化
  161.         LCD_Init();       
  162.         if(lcddev.id==0X6804||lcddev.id==0X5310)         //强制设置屏幕分辨率为320*240.以支持3.5寸大屏
  163.         {
  164.                 lcddev.width=240;
  165.                 lcddev.height=320;
  166.         }
  167.                             
  168.         POINT_COLOR=RED;//设置字体为红色  
  169.         LCD_ShowString(60,130,200,16,16,"S1:Light Mode");
  170.         LCD_ShowString(60,150,200,16,16,"KS2:Saturation");
  171.         LCD_ShowString(60,170,200,16,16,"S3:Brightness");
  172.         LCD_ShowString(60,190,200,16,16,"S4:Contrast");
  173.         LCD_ShowString(60,210,200,16,16,"TPAD(SD2):Effects");         
  174.   LCD_ShowString(60,230,200,16,16,"OV7670 Init...");       


  175.         while(OV7670_Init())//初始化OV7670
  176.         {
  177.                 LCD_ShowString(60,230,200,16,16,"OV7670 Error!!");
  178.                 delay_ms(200);
  179.           LCD_Fill(60,230,239,246,WHITE);
  180.                 delay_ms(200);
  181.         }
  182.         LCD_ShowString(60,230,200,16,16,"OV7670 Init OK");
  183.         delay_ms(1500);                   
  184.         OV7670_Light_Mode(lightmode);
  185.         OV7670_Color_Saturation(saturation);
  186.         OV7670_Brightness(brightness);
  187.         OV7670_Contrast(contrast);
  188.         OV7670_Special_Effects(effect);         
  189.        
  190.         TIM6_Int_Init(10000,7199);                        //10Khz计数频率,1秒钟中断                                                                          
  191.         EXTI8_Init();                                                //使能定时器捕获
  192.         OV7670_Window_Set(10,174,240,320);        //设置窗口          
  193.   OV7670_CS=0;                                                 
  194.         CAN1_Mode_Init(1,2,3,6,0);  //CAN初始化
  195.        
  196.         while(1)
  197.         {
  198.                
  199.                                           
  200.                                 /*txbuf[0]=10;
  201.                                 txbuf[1]=12;
  202.                                 txbuf[2]=15;       
  203.                                 txbuf[3]=19;               
  204.                           txbuf[4]=24;
  205.                                 txbuf[5]=30;       
  206.                           txbuf[6]=37;
  207.                                 txbuf[7]=1;        //00000111                                                                       
  208.                                 CAN1_SEND(0X121,txbuf);//CAN发送*/
  209.                                
  210.                                 camera_refresh();//更新显示
  211.                                
  212.                                  
  213.         }          
  214. }







  215. void car(u16 command)
  216. {
  217.    switch(command)
  218.          {
  219.                   case 1:                       //前进,速度为25
  220.                           txbuf[0]=1;
  221.                                 txbuf[1]=0;
  222.                                 txbuf[2]=0;       
  223.                                 txbuf[3]=0;               
  224.                           txbuf[4]=3;
  225.                                 txbuf[5]=0;       
  226.                           txbuf[6]=0;
  227.                                 txbuf[7]=0x00;        //00000111                                               
  228.                                 CAN1_SEND(0X121,txbuf);
  229.                           break;
  230.                         case 2:                   //后退,速度为25
  231.                           txbuf[0]=1;
  232.                                 txbuf[1]=0;
  233.                                 txbuf[2]=0;       
  234.                                 txbuf[3]=0;               
  235.                           txbuf[4]=25;
  236.                                 txbuf[5]=0;       
  237.                           txbuf[6]=0;
  238.                                 txbuf[7]=0x02;        //00000111                                               
  239.                                 CAN1_SEND(0X121,txbuf);
  240.                           break;
  241.                         case 3:                    //向右,速度为25
  242.                           txbuf[0]=1;
  243.                                 txbuf[1]=0;
  244.                                 txbuf[2]=25;       
  245.                                 txbuf[3]=0;               
  246.                           txbuf[4]=0;
  247.                                 txbuf[5]=0;       
  248.                           txbuf[6]=0;
  249.                                 txbuf[7]=0x00;        //00000111                                               
  250.                                 CAN1_SEND(0X121,txbuf);
  251.                           break;
  252.                         case 4:                  //向左,速度为25
  253.                           txbuf[0]=1;
  254.                                 txbuf[1]=0;
  255.                                 txbuf[2]=25;       
  256.                                 txbuf[3]=0;               
  257.                           txbuf[4]=0;
  258.                                 txbuf[5]=0;       
  259.                           txbuf[6]=0;
  260.                                 txbuf[7]=0x04;        //00000111                                               
  261.                                 CAN1_SEND(0X121,txbuf);
  262.         break;
  263.                         default:printf("error");                       
  264.          
  265.          }
  266. }
  267. void carturn(u16 commandturn,u16 turnangle)
  268. {
  269.      if(commandturn==1)//右转turnangle 角度
  270.                  {
  271.                     txbuf[0]=1;
  272.                                 txbuf[1]=0;
  273.                                 txbuf[2]=0;       
  274.                                 txbuf[3]=0;               
  275.                           txbuf[4]=0;
  276.                                 txbuf[5]=0;       
  277.                           txbuf[6]=turnangle;
  278.                                 txbuf[7]=0x00;        //00000111                                               
  279.                                 CAN1_SEND(0X121,txbuf);
  280.                  }
  281.                  else if(commandturn==2)//左转turnangle角度
  282.                  {
  283.                                  txbuf[0]=1;
  284.                                 txbuf[1]=0;
  285.                                 txbuf[2]=0;       
  286.                                 txbuf[3]=0;               
  287.                           txbuf[4]=0;
  288.                                 txbuf[5]=0;       
  289.                           txbuf[6]=turnangle;
  290.                                 txbuf[7]=0x03;        //00000111                                               
  291.                                 CAN1_SEND(0X121,txbuf);
  292.                          }
  293.                  else
  294.                          printf("error");

  295. }

  296. char* bian(int a){
  297.     int i=0,l;
  298.                 char*s;
  299.         char n[10];
  300.         while(a>0){n[i++]=a%10;a/=10;}
  301.         l=--i;
  302.         for(;i>=0;i--)s[l-i]=n[i]+'0';
  303.         s[l+1]=0;
  304.         return s;

  305. }

复制代码
所有资料51hei提供下载:
Track2.rar (439.27 KB, 下载次数: 31)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:308469 发表于 2018-6-3 19:31 | 只看该作者
颜色识别这部分应该看什么资料学习
回复

使用道具 举报

板凳
ID:138247 发表于 2019-5-2 15:20 | 只看该作者

谢谢楼主分享。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表