2401_85308619 2024-07-23 10:45 采纳率: 44.4%
浏览 17

OpenMV和stm32串口接收问题

stm32串口无法接收OpenMV发送过来的数据
代码不知道怎么改了
串口代码:

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

uint16_t a,b;

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;
    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;
            }
        }
        else if (RxState == 1)
        {
            if (RxData == 'b')
            {
                RxState = 2;
            }
            else
            {
                a = RxData;
            }
        }
        else if (RxState == 2)
        {
            if (RxData == 'c')
            {
                RxState = 0;
                Serial_RxFlag = 1;
            }
            else
            {
                b = 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"
 
extern uint16_t a,b;
 
uint16_t cx,cy;
extern uint8_t Serial_RxFlag;
 
int Trace_Flag=0;//追踪标志位
 
uint16_t Compare2 = 1950,Compare1 = 1500;
 
int main(void)
{
    Servo_Init();
    PWM_Init();
    PWM2_Init();
    PID_init();
    while(1)
    {
        
        if (Serial_RxFlag==1)
        {
 
            cx=a-48;
            cy=b-48;
            
            Compare2 = Compare2 + PIDx_realize(cx,160);
            TIM_SetCompare2(TIM2, Compare2);        //设置CCR2的值
            
 
            Compare1 = Compare1 + PIDy_realize(cy,120);
            TIM_SetCompare1(TIM3, Compare1);        //设置CCR2的值
            
                        Serial_RxFlag = 0;
        }
    }
}
 
 

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.6)  # 鱼眼矫正
    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) + 'b' + str(cy) + 'c')
 
 
  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2024-07-23 10:45
    关注

    以下内容由AIGC及阿里嘎多学长共同生成、有用望采纳:


    针对您提出的四个问题,我将逐一进行分析和建议:

    1. 串口接收数据问题

      • 首先确保STM32和OpenMV的串口配置完全一致,包括波特率、数据位、停止位和奇偶校验位。
      • 检查STM32的串口中断是否被正确触发,可以通过在中断服务程序中添加一些调试代码来确认。
    2. 串口初始化代码调整

      • 您的串口初始化代码看起来是正确的。确保波特率设置为115200,数据位为8位,无奇偶校验,1个停止位,这是与OpenMV代码中匹配的。
      • 检查TX和RX引脚的GPIO配置是否正确,根据您的代码,TX是PA9,RX是PA10,这需要与您的硬件连接相匹配。
    3. 中断处理逻辑

      • 您的中断处理逻辑中,Serial_RxFlag 应该在接收到完整数据包后被设置。但您的代码中,Serial_RxFlag 在接收到 'c' 字符后被设置,这可能导致在数据包不完整时错误地触发标志位。
      • 考虑在中断服务程序的最后添加一个标志位检查,以确保数据包完整接收后再设置 Serial_RxFlag
    4. 主函数数据处理

      • 您的主函数中,将接收到的字符直接转换为数字。确保字符确实是数字字符('0'-'9'),否则这种转换将不准确。
      • 考虑添加错误检查,确保接收到的数据是有效的数字。

    以下是一些建议的代码更改:

    • 中断处理逻辑优化

      void USART1_IRQHandler(void)
      {
          static uint8_t RxBuffer[100]; // 假设最大数据包大小为100
          static uint8_t RxIndex = 0;
          char c;
      
          if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
          {
              c = USART_ReceiveData(USART1);
              if (RxIndex < sizeof(RxBuffer) - 1) { // 防止缓冲区溢出
                  RxBuffer[RxIndex++] = c;
              }
      
              // 检查是否接收到结束字符 'c' 并且数据包大小正确
              if (c == 'c' && RxIndex > 2) { // 至少包含 'a', 一个数字, 'b', 一个数字, 'c'
                  // 处理数据
                  a = RxBuffer[1] - '0'; // 假设第一个数字在索引1
                  b = RxBuffer[4] - '0'; // 假设第二个数字在索引4
                  Serial_RxFlag = 1;
                  RxIndex = 0; // 重置索引,准备接收下一组数据
              }
              USART_ClearITPendingBit(USART1, USART_IT_RXNE);
          }
      }
      
    • 主函数中添加错误检查

      int main(void)
      {
          // ... 省略初始化代码
          while(1)
          {
              if (Serial_RxFlag == 1)
              {
                  // 确保a和b是有效的数字
                  if (a >= 0 && a <= 9 && b >= 0 && b <= 9) {
                      // 使用a和b进行PWM控制
                      // ...
                  }
                  Serial_RxFlag = 0;
              }
          }
      }
      

    请注意,这些代码只是示例,您可能需要根据您的具体需求进行调整。希望这些建议能帮助您解决问题。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月23日