当前位置:   article > 正文

OPENMV驱动云台实现颜色追踪_openmv识别最大色块

openmv识别最大色块

前言

本篇文章旨在记录我电赛期间学习OPENMV对颜色识别,以及通过串口通信的方式将坐标数据传给单片机,从而驱动舵机云台进行颜色追踪。


 一、OPENMV色块识别追踪代码

  1. # Single Color RGB565 Blob Tracking Example
  2. #
  3. # This example shows off single color RGB565 tracking using the OpenMV Cam.
  4. from pyb import UART#开启串口
  5. import sensor, image, time, math
  6. threshold_index = 0 # 0 for red, 1 for green, 2 for blue
  7. # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
  8. # The below thresholds track in general red/green/blue things. You may wish to tune them...
  9. thresholds = [(63, 100, 6, 69, -41, 42)] # generic_blue_thresholds
  10. uart = UART(3, 9600)
  11. x_max = 320
  12. x_min = 0
  13. x_1 = 155 #中心区域左边界
  14. x_2 = 163 #中心区域右边界
  15. y_max = 240
  16. y_min = 0
  17. y_1 = 115 #中心区域上边界
  18. y_2 = 123 #中心区域下边界
  19. flag = 0#位置信息标志
  20. sensor.reset()
  21. sensor.set_pixformat(sensor.RGB565)
  22. sensor.set_framesize(sensor.QVGA)
  23. sensor.skip_frames(time = 2000)
  24. sensor.set_auto_gain(False) # must be turned off for color tracking
  25. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  26. clock = time.clock()
  27. def find_max(blobs): #定义寻找色块面积最+大的函数
  28. max_size=0
  29. for blob in blobs:
  30. if blob.pixels() > max_size:
  31. max_blob = blob
  32. max_size = blob.pixels()
  33. return max_blob
  34. # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
  35. # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
  36. # camera resolution. "merge=True" merges all overlapping blobs in the image.
  37. while(True):
  38. clock.tick()
  39. img = sensor.snapshot()
  40. for blob in img.find_blobs([thresholds[threshold_index]], area_threshold=50, pixels_threshold=300, area_threshold=200, merge=True):
  41. # These values depend on the blob not being circular - otherwise they will be shaky.
  42. if blob.elongation() > 0.5:
  43. img.draw_edges(blob.min_corners(), color=(255,0,0))
  44. img.draw_line(blob.major_axis_line(), color=(0,255,0))
  45. img.draw_line(blob.minor_axis_line(), color=(0,0,255))
  46. # These values are stable all the time.
  47. img.draw_rectangle(blob.rect())
  48. img.draw_cross(blob.cx(), blob.cy())#坐标数据
  49. # Note - the blob rotation is unique to 0-180 only.
  50. img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
  51. if blob.cx()>= x_min and blob.cx() <= 160 and\
  52. blob.cy() >= 120 and blob.cy() <= y_max :
  53. flag = 1
  54. if blob.cx()>=160 and blob.cx() <= x_max and\
  55. blob.cy() >=120 and blob.cy() <= y_max :
  56. flag = 2
  57. if blob.cx()>= x_min and blob.cx() <= 160 and \
  58. blob.cy() >= y_min and blob.cy() <= 120 :
  59. flag = 3
  60. if blob.cx()>= 160 and blob.cx() <= x_max and \
  61. blob.cy() >= y_min and blob.cy() <= 120 :
  62. flag = 4
  63. if blob.cx()>= x_1 and blob.cx() <= x_2 and\
  64. blob.cy() >= y_1 and blob.cy() <=y_2 :
  65. flag = 5
  66. output_str="%d" %flag #方式1
  67. print('you send:',output_str)
  68. #time.sleep(0.02)
  69. uart.write('@'+output_str+'\r\n')

二、单片机驱动舵机云台进行颜色追踪

1、openmv与单片机通信

  1. #include "stm32f10x.h" // Device header
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. char Serial_RxPacket; //"@MSG\r\n"
  5. uint8_t Serial_RxFlag;
  6. void Serial_Init(void)
  7. {
  8. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  10. GPIO_InitTypeDef GPIO_InitStructure;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  12. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  13. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14. GPIO_Init(GPIOA, &GPIO_InitStructure);
  15. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  16. GPIO_InitStructure.GPIO_Pin = GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  17. GPIO_Init(GPIOA, &GPIO_InitStructure);
  18. USART_InitTypeDef USART_InitStructure;
  19. USART_InitStructure.USART_BaudRate = 9600;
  20. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  21. USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  22. USART_InitStructure.USART_Parity = USART_Parity_No;
  23. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  24. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  25. USART_Init(USART1, &USART_InitStructure);
  26. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  27. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  28. NVIC_InitTypeDef NVIC_InitStructure;
  29. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  30. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  31. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  32. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  33. NVIC_Init(&NVIC_InitStructure);
  34. USART_Cmd(USART1, ENABLE);
  35. }
  36. void Serial_SendByte(uint8_t Byte)
  37. {
  38. USART_SendData(USART1, Byte);
  39. while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  40. }
  41. void Serial_SendArray(uint8_t *Array, uint16_t Length)
  42. {
  43. uint16_t i;
  44. for (i = 0; i < Length; i ++)
  45. {
  46. Serial_SendByte(Array[i]);
  47. }
  48. }
  49. void Serial_SendString(char *String)
  50. {
  51. uint8_t i;
  52. for (i = 0; String[i] != '\0'; i ++)
  53. {
  54. Serial_SendByte(String[i]);
  55. }
  56. }
  57. uint32_t Serial_Pow(uint32_t X, uint32_t Y)
  58. {
  59. uint32_t Result = 1;
  60. while (Y --)
  61. {
  62. Result *= X;
  63. }
  64. return Result;
  65. }
  66. void Serial_SendNumber(uint32_t Number, uint8_t Length)
  67. {
  68. uint8_t i;
  69. for (i = 0; i < Length; i ++)
  70. {
  71. Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
  72. }
  73. }
  74. int fputc(int ch, FILE *f)
  75. {
  76. Serial_SendByte(ch);
  77. return ch;
  78. }
  79. void Serial_Printf(char *format, ...)
  80. {
  81. char String[100];
  82. va_list arg;
  83. va_start(arg, format);
  84. vsprintf(String, format, arg);
  85. va_end(arg);
  86. Serial_SendString(String);
  87. }
  88. //文本数据包处理格式
  89. void USART1_IRQHandler(void)
  90. {
  91. static uint8_t RxState = 0;
  92. static uint8_t pRxPacket = 0;
  93. if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
  94. {
  95. uint8_t RxData = USART_ReceiveData(USART1);
  96. if (RxState == 0)
  97. {
  98. if (RxData == '@' && Serial_RxFlag == 0)
  99. {
  100. RxState = 1;
  101. pRxPacket = 0;
  102. }
  103. }
  104. else if (RxState == 1)
  105. {
  106. if (RxData == '\r')
  107. {
  108. RxState = 2;
  109. }
  110. else
  111. {
  112. // strncpy(&Serial_RxPacket[pRxPacket],RxData,1);
  113. Serial_RxPacket = RxData;
  114. // pRxPacket ++;
  115. }
  116. }
  117. else if (RxState == 2)
  118. {
  119. if (RxData == '\n')
  120. {
  121. RxState = 0;
  122. // Serial_RxPacket[pRxPacket] = '\0';
  123. Serial_RxFlag = 1;
  124. }
  125. }
  126. USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  127. }
  128. }

2、云台舵机驱动

  1. #include "stm32f10x.h"
  2. #include "PWM.h"
  3. #include "delay.h"
  4. void Servo_Init(void)
  5. {
  6. PWM_Init();
  7. }
  8. void Servo_SetAngle(float AngleX,float AngleY)
  9. {
  10. PWM_SetCompare1( AngleX / 180 * 2000 + 500);
  11. PWM_SetCompare2(AngleY / 180 * 2000 + 500);
  12. }
  13. void Servo_SetAngle1(float Angle)
  14. {
  15. PWM_SetCompare1(Angle / 180 * 2000 + 500);
  16. }
  17. void Servo_SetAngle2(float Angle)
  18. {
  19. PWM_SetCompare2(Angle / 180 * 2000 + 500);
  20. }
  21. void Servo_SetAngle3(float Angle)
  22. {
  23. PWM_SetCompare3(Angle / 180 * 2000 + 500);
  24. }
  25. void Servo_SetAngle4(float Angle)
  26. {
  27. PWM_SetCompare4(Angle / 180 * 2000 + 500);
  28. }
  29. /*************************************************
  30. 如果想让舵机转不同的角度修改不同的数值即可
  31. **************************************************/

3、完整工程

https://download.csdn.net/download/m0_73931287/88783418icon-default.png?t=N7T8https://download.csdn.net/download/m0_73931287/88783418以上完整工程为免费下载资料,如果不能下载,读者可以下方留言或者私信我!

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

闽ICP备14008679号