当前位置: 首页 > news >正文

STM32CubeMx学习与K210串口通信+识别橘色色块——点亮小灯

 K210模块的串口发送代码

引入模块

import sensor, image,time,lcd,utime
import KPU as kpu
import gc, sys
from fpioa_manager import fm
from machine import UART

锁定引脚 和 申明串口

fm.register(9, fm.fpioa.UART1_TX, force=True)
fm.register(10, fm.fpioa.UART1_RX, force=True)  #映射串口IO口
uart_A = UART(UART.UART1, 115200, 8, 0, 1, timeout=1000, read_buf_len=4096) #初始化串口

发送函数

data = bytearray([0xa3,0xb3,b[5],b[6],qianbai,shige,0xff])
uart_A.write(data)

识别色块 +数字处理

img = sensor.snapshot() #Take a picture and return the image.lcd.display(img)   #lcd屏幕显示blobs = img.find_blobs([orginal_threshold],pixels_threshold = 2,area_threshold = 125,merge = 1)if blobs:for b in blobs:x = b[0]y = b[1]width = b[2]height = b[3]img.draw_rectangle([x,y,width,width]) # rectimg.draw_cross(b[5], b[6]) # cx, cyLm = (b[2]+b[3])/2 #b[2]色块的外框的宽 ,b[3]色块的外框的高print(b[5])#中心X坐标print(b[6])#中心Y坐标x1=b[5]//100x2=b[5]%100y1=b[6]//100y2=b[6]%100length = K/Lmhh = round(length, 2)#两位小数zz=hh*100#转换为整数zz=int(zz)

这里zz是4个数字的数,则需要分成千百位和低二位传输 zz是距离。也映射了距离的算法

整体K210代码

import sensor, image,time,lcd,utime
import KPU as kpu
import gc, sys
from fpioa_manager import fm
from machine import UART# need your connect hardware IO 9/10 to loopback
fm.register(9, fm.fpioa.UART1_TX, force=True)
fm.register(10, fm.fpioa.UART1_RX, force=True)  #映射串口IO口
uart_A = UART(UART.UART1, 115200, 8, 0, 1, timeout=1000, read_buf_len=4096) #初始化串口
orginal_threshold =(16, 47, 25, 57, 9, 62)#摄像头初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565) #设置帧格式
sensor.set_framesize(sensor.QVGA)   #设置帧大小,成功返回True,失败返回False
sensor.set_vflip(1) #后置拍摄模式
sensor.skip_frames(50)  #跳过指定(这里是10帧)帧数或者跳过指定时间内的图像,让相机图像在改变相机设置后稳定下来
sensor.set_auto_whitebal(False) #白平衡关闭
#sensor.snapshot(1.8)   #去鱼眼化#LCD初始化
lcd.init()
img = sensor.snapshot() #用摄像头拍摄一张图片
K=650  #the value should be measured K=length*Lm # 实际的大小=K2*直径的像素
K2=10.5/279 #QQVGA模式下K2=10.5/139  #QVGA模式下K2=10.5/279while(True):img = sensor.snapshot() #Take a picture and return the image.lcd.display(img)   #lcd屏幕显示blobs = img.find_blobs([orginal_threshold],pixels_threshold = 2,area_threshold = 125,merge = 1)if blobs:for b in blobs:x = b[0]y = b[1]width = b[2]height = b[3]img.draw_rectangle([x,y,width,width]) # rectimg.draw_cross(b[5], b[6]) # cx, cyLm = (b[2]+b[3])/2 #b[2]色块的外框的宽 ,b[3]色块的外框的高print(b[5])#中心X坐标print(b[6])#中心Y坐标x1=b[5]//100x2=b[5]%100y1=b[6]//100y2=b[6]%100length = K/Lmhh = round(length, 2)#两位小数zz=hh*100#转换为整数zz=int(zz)qianbai=zz//100shige=zz%100print(zz)data = bytearray([0xa3,0xb3,b[5],b[6],qianbai,shige,0xff])uart_A.write(data)else:   data = bytearray([0xa3,0xb3,0,0,0,0,0xff])uart_A.write(data)#如果没有找到的话,返回print('0000'+' '+'0000'+'\n')#lcd.display(img)#lcd屏幕显示

STM32串口接受端函数

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{if(huart==&huart5){if(rx_buff[0]==0xa3){if(rx_buff[1]==0xb3){if(rx_buff[Size-1]==0xff){if((rx_buff[2]>106&&rx_buff[2]<212)&&(rx_buff[3]>80&&rx_buff[3]<160)){HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,0);}else{HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,1);}}}}}}

判断帧头帧尾 如果数据正常 判断 数据是否合格 若合格则点灯 否则灭灯

不要忘记加入

HAL_UARTEx_ReceiveToIdle_DMA(&huart5,rx_buff,sizeof(rx_buff));
#define printfl(...) HAL_UART_Transmit(&huart1,(uint8_t*)u_buff,sprintf((char *)u_buff,__VA_ARGS__),0xffff)
uint8_t u_buff[256];
uint8_t rx_buff[50];
extern DMA_HandleTypeDef hdma_uart5_rx;

http://www.lryc.cn/news/109661.html

相关文章:

  • 睿讯微带你深度了解汽车交流充电桩
  • word怎么压缩到10m以下?文件压缩很简单
  • I.MX6ULL_Linux_驱动篇(43)linux通用LED驱动
  • OPTEE之sonarlint静态代码分析实战二——optee_client
  • c++调用ffmpeg api将视频文件内容进行udp推流
  • 助力工业物联网,工业大数据之服务域:油站主题分析【二十六】
  • MySql之索引
  • adb调试
  • ElasticSearch_学习笔记
  • Portraiture 4.0.3 for windows/Mac简体中文版(ps人像磨皮滤镜插件)
  • Java精品项目源码第152期火车票预订系统(编号M062)
  • 嵌入式软件C/C++(技术面试题)
  • Idea中侧面栏不见了,如何设置?
  • 构建高效读写分离MySQL主从复制架构,应对高可用挑战!
  • Stable Diffusion系列课程二:ControlNet
  • 【css】使用float实现水平导航栏
  • IDEA超强XSD文件编辑插件-XSD / WSDL Visualizer
  • Nodejs 第三章(Npm Package json)
  • Tool Documentation Enables Zero-Shot Tool-Usage with Large Language Models
  • 16 Springboot——登录功能实现
  • 数据结构-栈队列链表树
  • clickhouse功能使用
  • java中使用Jsoup和Itext实现将html转换为PDF
  • 无人驾驶实战-第七课(高精地图和V2X )
  • springboot集成Sentinel
  • 数据结构——单链表OJ题
  • 【雕爷学编程】MicroPython动手做(31)——物联网之Easy IoT
  • 《前端开发 实践之 构建工具的了解》
  • MySQL 主从搭建
  • 国内GitHub加速访问工具-Fetch GitHub Hosts