当前位置:   article > 正文

【STM32+HAL】与OpenMV通信_32与openmv通信

32与openmv通信

一、OpenMV  IDE

1、简单数据通信
  1. import time
  2. from pyb import UART
  3. uart = UART(3, 115200, timeout_char=200)
  4. uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
  5. while True:
  6. FH = bytearray([0xb3,0xb3]) #帧头,控制板接收数据以后判断,转化为字节传送,不能用16进制直接传
  7. uart.write(FH) #写到串口
  8. uart.write("Logan") #写数据
  9. FH = bytearray([0x0d,0x0a]) #结束标志,换行和回车的ascll
  10. uart.write(FH)
  11. time.sleep_ms(1000) #延时

2、多字节数据通信
  1. def send_data(x,y,w,h):
  2. global uart;
  3. uart.write(str(x))
  4. uart.write(bytearray([0x20])) # 发送空格
  5. uart.write(str(y))
  6. uart.write(bytearray([0x20]))
  7. uart.write(str(w))
  8. uart.write(bytearray([0x20]))
  9. uart.write(str(h))
  10. uart.write(bytearray([0x20]))

3、寻找最大色块,传递中心点坐标及矩形长宽
  1. import time
  2. import sensor
  3. import math
  4. import image
  5. import ustruct
  6. from pyb import UART
  7. uart = UART(3, 115200, timeout_char=200)
  8. uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
  9. threshold_index = 4 # 0 for red, 1 for green, 2 for blue
  10. thresholds = [
  11. (30, 100, 15, 127, 15, 127), # generic_red_thresholds
  12. (30, 100, -64, -8, -32, 32), # generic_green_thresholds
  13. (0, 30, 0, 64, -128, 0), # generic_blue_thresholds
  14. (82, 100, 75, -49, -22, 31), # generic_white_thresholds
  15. (21, 83, 32, 65, 31, 63),
  16. ]
  17. sensor.reset()
  18. sensor.set_pixformat(sensor.RGB565)
  19. sensor.set_framesize(sensor.QVGA)# QVGA的中心坐标:160,120
  20. sensor.skip_frames(time=2000) # 跳过2000毫秒的帧让相机图像在改变相机设置后稳定下来
  21. sensor.set_auto_gain(False) # 必须关闭才能进行颜色跟踪
  22. sensor.set_auto_whitebal(False) # 必须关闭才能进行颜色跟踪
  23. clock = time.clock()
  24. def find_max(blobs):
  25. max_size=0
  26. for blob in blobs:
  27. if blob.pixels() > max_size:
  28. max_blob = blob
  29. max_size = blob.pixels()
  30. return max_blob
  31. def send_data(x,y,w,h):
  32. global uart;
  33. FH = bytearray([0xb3,0xb3]) # 帧头
  34. uart.write(FH) # 写到串口
  35. uart.write(str(x))
  36. uart.write(bytearray([0x20])) # 发送空格
  37. uart.write(str(y))
  38. uart.write(bytearray([0x20]))
  39. uart.write(str(w))
  40. uart.write(bytearray([0x20]))
  41. uart.write(str(h))
  42. uart.write(bytearray([0x20]))
  43. FH = bytearray([0x0d,0x0a]) # 帧尾,换行和回车的ascll
  44. uart.write(FH)
  45. while True:
  46. clock.tick()
  47. img = sensor.snapshot()
  48. blobs = img.find_blobs([thresholds[threshold_index]])
  49. #如果找到了目标颜色
  50. if blobs:
  51. max_blob = find_max(blobs)
  52. cx=max_blob[5]
  53. cy=max_blob[6]
  54. cw=max_blob[2]
  55. ch=max_blob[3]
  56. # 这些值取决于max_blob不是圆形的,否则它们将不稳定.
  57. # 检查max_blob是否显著偏离圆形
  58. if max_blob.elongation() > 0.5:
  59. img.draw_edges(max_blob.min_corners(), color=(255, 0, 0))
  60. img.draw_line(max_blob.major_axis_line(), color=(0, 255, 0))
  61. img.draw_line(max_blob.minor_axis_line(), color=(0, 0, 255))
  62. # 这些值始终是稳定的。
  63. # img.draw_rectangle(max_blob.rect())
  64. img.draw_rectangle(160,120,35,35)
  65. img.draw_cross(cx, cy)
  66. # 注意-max_blob旋转仅限于0-180。
  67. img.draw_keypoints(
  68. [(cx, cy, int(math.degrees(max_blob.rotation())))], size=20
  69. )
  70. send_data(cx,cy,cw,ch) # 发送数据
  71. print(cx,cy,cw,ch)
  72. print(clock.fps())

二、Keil

1、初始化
  1. #include "string.h"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #define RXBUFFERSIZE 256
  5. char RxBuffer[RXBUFFERSIZE],rx_buf[RXBUFFERSIZE];
  6. uint8_t aRxBuffer;
  7. uint8_t Uart1_Rx_Cnt = 0;
  8. int flag=0;
  9. printf("Hello World!\r\n");
  10. HAL_Delay(200);
  11. HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1);
  12. int fputc(int ch, FILE *f)
  13. {
  14. HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
  15. HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xffff);
  16. return ch;
  17. }
  18. int fgetc(FILE *f)
  19. {
  20. uint8_t ch = 0;
  21. HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
  22. HAL_UART_Receive(&huart2, &ch, 1, 0xffff);
  23. return ch;
  24. }

2、串口回调
  1. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  2. {
  3. UNUSED(huart);
  4. if(huart==&huart2){
  5. HAL_GPIO_TogglePin(GPIOF,LED_Pin); //有数据则翻转LED灯
  6. RxBuffer[Uart1_Rx_Cnt] = aRxBuffer;
  7. Uart1_Rx_Cnt++;
  8. if((RxBuffer[Uart1_Rx_Cnt-1] == 0xb3)&&(RxBuffer[Uart1_Rx_Cnt-2] == 0xb3)) flag=1; //帧头判定
  9. else if((RxBuffer[Uart1_Rx_Cnt-2] == 0x0d)&&(RxBuffer[Uart1_Rx_Cnt-1] == 0x0a)) flag=2; //帧尾判定
  10. else flag=0;
  11. switch (flag)
  12. {
  13. case 1:
  14. Uart1_Rx_Cnt = 0;
  15. memset(RxBuffer,0x00,sizeof(RxBuffer));
  16. break;
  17. case 2:
  18. RxBuffer[Uart1_Rx_Cnt-1] = '\0';
  19. RxBuffer[Uart1_Rx_Cnt-2] = '\0';
  20. strcpy(rx_buf,RxBuffer);
  21. printf("%s\r\n",rx_buf);
  22. while(HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX);
  23. Uart1_Rx_Cnt = 0;
  24. memset(RxBuffer,0x00,sizeof(RxBuffer));
  25. break;
  26. default:break;
  27. }
  28. HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1);
  29. }
  30. }

三、CUBEMX

1、开启串口

USART1:STM32与电脑通信

USART2:STM32与OPENMV通信

2、中断

四、接线图

五、巨人之肩

STM32与openmv通信(HAL库)

Openmv 与 Stm32f407通信

OpenMV与STM32之间的通信(附源码)

六、DMA传输版本

1、main.c

  1. #include "stdio.h"
  2. #include "string.h"
  3. #include "stdlib.h"
  4. #define RXBUFFERSIZE 256
  5. volatile uint8_t recv_end_flag = 0; //接收结束标志位
  6. uint8_t RxBuffer2[RXBUFFERSIZE],rx_buf2[RXBUFFERSIZE];
  7. HAL_UART_Transmit_DMA(&huart1,(uint8_t *)"Hello\r\n",sizeof("Hello\r\n"));
  8. HAL_Delay(200);
  9. __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
  10. HAL_UART_Receive_DMA(&huart2,RxBuffer2,sizeof(RxBuffer2));

2、stm32f4xx_hal.c

  1. extern volatile uint8_t recv_end_flag; //接收结束标志位
  2. extern uint8_t RxBuffer2[RXBUFFERSIZE],rx_buf2[RXBUFFERSIZE];
  3. int cx=0,cy=0;
  4. void USART2_IRQHandler(void)
  5. {
  6. /* USER CODE BEGIN USART2_IRQn 0 */
  7. /* USER CODE END USART2_IRQn 0 */
  8. HAL_UART_IRQHandler(&huart2);
  9. /* USER CODE BEGIN USART2_IRQn 1 */
  10. uint8_t tmp_flag =__HAL_UART_GET_FLAG(&huart2,UART_FLAG_IDLE); //获取IDLE标志位
  11. if((tmp_flag != RESET)) //通过标志位判断接收是否结束
  12. {
  13. recv_end_flag = 1; //置1表明接收结束
  14. __HAL_UART_CLEAR_IDLEFLAG(&huart2); //清除标志位
  15. HAL_UART_DMAStop(&huart2);
  16. strcpy((char *)rx_buf2,(char *)RxBuffer2);
  17. int st=0,cnt_blank=0;
  18. for(int i=0;rx_buf2[i];i++){
  19. if(rx_buf2[i]==' ') {
  20. cnt_blank++;
  21. int temp=0;
  22. for(int j=st;j<i;j++) temp=temp*10+(rx_buf2[j]-'0');
  23. switch (cnt_blank)
  24. {
  25. case 1:cx=temp;st=i+1;break;
  26. case 2:cy=temp;break;
  27. default:break;
  28. }
  29. }
  30. }
  31. printf("%d %d\r\n",cx,cy);
  32. HAL_UART_Receive_DMA(&huart2,RxBuffer2,sizeof(RxBuffer2)); //开启DMA接收,方便下一次接收数据
  33. }
  34. /* USER CODE END USART2_IRQn 1 */
  35. }

详见【STM32+HAL】DMA应用

源码【STM32+HAL】DMA应用NO.2

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/570824
推荐阅读
相关标签
  

闽ICP备14008679号