stm32串口无法接收OpenMV发送过来的数据
cx和cy是三位数字
代码不知道怎么改了
串口代码:
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
extern uint16_t cx,cy;
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void Serial_SendByte(uint8_t Byte)
{
USART_SendData(USART1, Byte);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
uint16_t i;
for (i = 0; i < Length; i ++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i ++)
{
Serial_SendByte(String[i]);
}
}
uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while (Y --)
{
Result *= X;
}
return Result;
}
void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i ++)
{
Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
}
}
int fputc(int ch, FILE *f)
{
Serial_SendByte(ch);
return ch;
}
void Serial_Printf(char *format, ...)
{
char String[100];
va_list arg;
va_start(arg, format);
vsprintf(String, format, arg);
va_end(arg);
Serial_SendString(String);
}
//文本数据包处理格式
void USART1_IRQHandler(void)
{
static uint8_t RxState = 0;
static uint8_t RxBuffer[6]; // 用于存储接收到的数据
static uint8_t RxIndex = 0; // 用于记录当前接收到的数据位置
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
uint8_t RxData = USART_ReceiveData(USART1);
if (RxState == 0)
{
if (RxData == 'a' && Serial_RxFlag == 0)
{
RxState = 1;
RxIndex = 0;
}
}
else if (RxState == 1)
{
if (RxData == 'b')
{
RxState = 0;
Serial_RxFlag = 1;
// 解析接收到的数据
cx = (RxBuffer[0] - '0') * 100 + (RxBuffer[1] - '0') * 10 + (RxBuffer[2] - '0');
cy = (RxBuffer[3] - '0') * 100 + (RxBuffer[4] - '0') * 10 + (RxBuffer[5] - '0');
}
else
{
RxBuffer[RxIndex++] = RxData;
}
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
主函数:
#include "stm32f10x.h" // Device header
#include "PWM.h"
#include "usart.h"
#include "Servo.h"
#include "Delay.h"
#include "PID.h"
uint16_t Compare2,Compare1;
uint16_t cx,cy;
int main(void)
{
Servo_Init();
PWM_Init();
PID_init();
while(1)
{
Compare2 = Compare2 + PIDx_realize(cx,160);
Compare1 = Compare1 + PIDy_realize(cy,120);
TIM_SetCompare2(TIM2, Compare2); //设置CCR2的值
TIM_SetCompare1(TIM3, Compare1); //设置CCR2的值
}
}
OpenMV代码:
import sensor, image, time, pyb
uart = pyb.UART(3, 115200) # 使用UART3进行串口通信,波特率为115200
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # 颜色跟踪必须关闭自动增益
sensor.set_auto_whitebal(False) # 颜色跟踪必须关闭白平衡
def find_max(blobs):
max_blob = None
max_size = 0
for blob in blobs:
if blob[2]*blob[3]>max_size: # blob[2]*blob[3],计算包含色块的最小矩形面积大小
max_blob = blob
max_size = blob[2]*blob[3]
return max_blob
# 捕捉图像中心的颜色阈值。
r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50] # 感兴趣区
for i in range(60): # 这个60大概值,没有实际意义
img = sensor.snapshot()
img.draw_rectangle(r) # 画出想确定阈值的感兴趣区
threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values.
for i in range(60):
img = sensor.snapshot().lens_corr(1.8) # 镜头矫正
hist = img.get_histogram(roi=r)
lo = hist.get_percentile(0.01) # 获取1%范围的直方图的CDF(根据需要调整)!
hi = hist.get_percentile(0.99) # 获取99%范围的直方图的CDF(根据需要调整)!
# 平均百分位值。
threshold[0] = (threshold[0] + int(lo.l_value()/2)) // 2 # Lmin
threshold[1] = (threshold[1] + int(hi.l_value()/2)) // 2 # Lmax
threshold[2] = (threshold[2] + int(lo.a_value()*1.2)) // 2 # Amin
threshold[3] = (threshold[3] + int(hi.a_value()*1.2)) // 2 # Amax
threshold[4] = (threshold[4] + int(lo.b_value()*1.2)) // 2 # Bmin
threshold[5] = (threshold[5] + int(hi.b_value()*1.2)) // 2 # Bmax
print("threshold", threshold)
while(True):
img = sensor.snapshot().lens_corr(1.8) # 鱼眼矫正
blobs_list = img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10)
max_blob = find_max(blobs_list) # 返回最大的色块
if max_blob:
img.draw_rectangle(max_blob.rect()) # 画出找到色块
img.draw_cross(max_blob.cx(), max_blob.cy())
cx = max_blob.cx() # 获取blob的中心点X坐标值(忽略Y坐标)
cy = max_blob.cy()
print(cx,cy) # 打印出X坐标值到串口
uart.write('a' + str(cx) + str(cy) + 'b')