可以识别两种颜色并反馈到STM32进行解析,标记并区分两种颜色的,内还有stm32追小球的云台图为,STM32通过串口与PC端通信上传数据的效果,帧率很高很稳定!
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include "holder.h"
- #include "oled.h"
- #include "mpu6050.h"
- #include "control.h"
- #include "HMI.h"
- #include "usart2.h"
- #include "openmv.h"
- /*全局变量*/
- int PWM=1665;
- int16_t p_x=100;
- int Holder_PID(int8_t Xerror,int8_t Dzone) ;
- int main(void)
- {
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); /*opemv串口接收*/
-
- uart3_init(9600); /*串口屏*/
- uart2_init(9600); /*串口发送printf*/
-
- while(1)
- {
-
- printf("\n\r X偏移=%d Y偏移=%d \n\r" ,OpenMV_RX ,OpenMV_RY); /*X轴左负右正 red */
- printf("\n\r X偏移=%d Y偏移=%d \n\r" ,OpenMV_GX ,OpenMV_GY); /*X轴左负右正 green */
- printf("\n\r 颜色合=%d \n\r",OpenMV_add); /*0x00没检测到0x01red 0x02green 0x03都有*/
-
- }
- }
- int Holder_PID(int8_t Xerror,int8_t Dzone) /*PWM在1750-1950之间对应0~180°*/
- {
- static float kp=0.08,kd=0.18,Ki=0;
- static float LastError,SumError,LastPWM,PWM_PID;
- float Error,dError;
- char flag;
- Error=Xerror;
- /*积分*/
- SumError+=Error;
-
- if(Xerror<=Dzone&&Xerror>=-Dzone)
- {
- return LastPWM;
- }
- /*微分*/
- dError=Error-LastError;
- LastError=Error;
-
- PWM_PID=(kp*Error+kd*dError+flag*Ki*SumError);
- if(PWM_PID<=Dzone&&PWM_PID>0.5)PWM_PID=1;
- if(PWM_PID>=-Dzone&&PWM_PID<-0.5)PWM_PID=-1;
-
- PWM=PWM+PWM_PID-2;
-
- /*限幅*/
- if(PWM>=1755)PWM=1755;
- if(PWM<=1575)PWM=1575;
- LastPWM=PWM;
-
- return PWM;
- }
复制代码
所有资料51hei提供下载:
识别两种颜色.7z
(273.48 KB, 下载次数: 196)
|