当前位置:   article > 正文

openmv识别红色物体并返回坐标给stm32单片机,通过pid控制舵机云台_舵机云台用什么控制

舵机云台用什么控制

本人搜索了有关于舵机云台pid控制的代码,但是都没有搜到想要的结果,现在自己写出来了代码,所以就将自己写的代码分享出来,和大家一起学习进步。

1.openmv识别红色物体+返回中心坐标的的代码

  1. import sensor,image,time,math,pyb,lcd
  2. from pyb import UART,LED
  3. import json
  4. import ustruct
  5. lcd.init()
  6. sensor.reset()
  7. sensor.set_pixformat(sensor.RGB565)
  8. sensor.set_framesize(sensor.QQVGA)#QQVGA的分辨率为120*160
  9. sensor.skip_frames(time = 2000)
  10. sensor.set_auto_gain(False) # 关闭自动增益
  11. sensor.set_auto_whitebal(False) #关闭白平衡
  12. sensor.set_vflip(True)#垂直方向翻转
  13. red_threshold_01=(18, 56, 103, 35, -57, 116)
  14. clock = time.clock()
  15. uart = UART(3,9600) #定义串口3变量
  16. uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
  17. def find_max(blobs): #定义寻找色块面积最大的函数
  18. max_size=0
  19. for blob in blobs:
  20. if blob.pixels() > max_size:
  21. max_blob = blob
  22. max_size = blob.pixels()
  23. return max_blob
  24. def sending_data(cx,cy,cw,ch):
  25. global uart;
  26. data = ustruct.pack("<bbhhhhb", #格式为俩个字符俩个短整型(2字节)
  27. 0x2C, #帧头1
  28. 0x12, #帧头2
  29. int(cx), # up sample by 4 #数据1
  30. int(cy), # up sample by 4 #数据2
  31. int(cw), # up sample by 4 #数据1
  32. int(ch), # up sample by 4 #数据2
  33. 0x5B)
  34. uart.write(data); #必须要传入一个字节数组
  35. led = pyb.LED(3)
  36. while(True):
  37. led.on()
  38. clock.tick()
  39. img = sensor.snapshot()
  40. blobs = img.find_blobs([red_threshold_01])
  41. if blobs:
  42. max_b = find_max(blobs)
  43. cx=max_b[5]
  44. cy=max_b[6]
  45. cw=max_b[2]
  46. ch=max_b[3]
  47. img.draw_rectangle(max_b.rect(),color=(255,0,0)) # rect
  48. img.draw_cross(max_b[5], max_b[6]) # cx, cy
  49. FH = bytearray([0x2C,0x12,int(cx),int(cy),int(cw),int(ch),0x5B])
  50. uart.write(FH)
  51. print(cx,cy,cw,ch)
  52. lcd.display(img)
  53. else:
  54. FH = bytearray([0x2C,0x12,0,0,0,0,0x5B])
  55. uart.write(FH)
  56. img.draw_string(0,0,"no object",color = (120,0,0))
  57. lcd.display(img)

注意:

(1)sensor.set_framesize(sensor.QQVGA)#QQVGA的分辨率为120*160,这行代码是设置分辨率为120*160,QQVGA的分辨率为120*160,QVGA的分辨率为320*240,不同的分辨率对应stm32端的代码也不同,在追踪云台的代码里,分辨率不能设的太高,太高的话,虽然画质会更加清晰,但是画面会出现延时,响应会变慢。

(2)sensor.set_vflip(True)#垂直方向翻转,这行代码是将openmv传回的画面垂直翻转,因为我们的openmv是倒置放的,如果不进行垂直翻转,在电脑端显示的画面就是颠倒的。这行代码是否书写也会影响到后面stm32端代码的书写。

2.stm32通过pid控制舵机云台的代码

(1)usart2.c代码

  1. #include "usart2.h"
  2. #include "usart.h"
  3. #include "led.h"
  4. int Cx,Cy,Cw,Ch;
  5. void uart2_init(u32 bound)
  6. {
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. USART_InitTypeDef USART_InitStructure;
  9. NVIC_InitTypeDef NVIC_InitStructure;
  10. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
  11. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
  12. GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
  13. GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
  14. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  15. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  16. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  17. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  18. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  19. GPIO_Init(GPIOA,&GPIO_InitStructure);
  20. USART_InitStructure.USART_BaudRate = bound;
  21. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  22. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  23. USART_InitStructure.USART_Parity = USART_Parity_No;
  24. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  25. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  26. USART_Init(USART2, &USART_InitStructure);
  27. USART_Cmd(USART2, ENABLE);
  28. USART_ClearFlag(USART2, USART_FLAG_TC);
  29. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  30. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  31. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
  32. NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
  33. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  34. NVIC_Init(&NVIC_InitStructure);
  35. }
  36. void USART2_IRQHandler(void)
  37. {
  38. u8 com_data;
  39. u8 i;
  40. static u8 RxCounter1=0;
  41. static u16 RxBuffer1[10]={0};
  42. static u8 RxState = 0;
  43. static u8 RxFlag1 = 0;
  44. if( USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
  45. {
  46. USART_ClearITPendingBit(USART2,USART_IT_RXNE);
  47. com_data = USART_ReceiveData(USART2);
  48. if(RxState==0&&com_data==0x2C)
  49. {
  50. RxState=1;
  51. RxBuffer1[RxCounter1++]=com_data;
  52. }
  53. else if(RxState==1&&com_data==0x12)
  54. {
  55. RxState=2;
  56. RxBuffer1[RxCounter1++]=com_data;
  57. }
  58. else if(RxState==2)
  59. {
  60. RxBuffer1[RxCounter1++]=com_data;
  61. if(RxCounter1>=10||com_data == 0x5B)
  62. {
  63. RxState=3;
  64. RxFlag1=1;
  65. Cx=RxBuffer1[RxCounter1-5];
  66. Cy=RxBuffer1[RxCounter1-4];
  67. Cw=RxBuffer1[RxCounter1-3];
  68. Ch=RxBuffer1[RxCounter1-2];
  69. }
  70. }
  71. else if(RxState==3)
  72. {
  73. if(RxBuffer1[RxCounter1-1] == 0x5B)
  74. {
  75. USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
  76. if(RxFlag1)
  77. {
  78. //printf("Cx=%d\r,Cy=%d\r,Cw=%d\r,Ch=%d\r\n",Cx,Cy,Cw,Ch);
  79. //printf("xerror=%d, yerror=%d\r\n",80-Cx,60-Cy);
  80. }
  81. RxFlag1 = 0;
  82. RxCounter1 = 0;
  83. RxState = 0;
  84. USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
  85. }
  86. else
  87. {
  88. RxState = 0;
  89. RxCounter1=0;
  90. for(i=0;i<10;i++)
  91. {
  92. RxBuffer1[i]=0x00;
  93. }
  94. }
  95. }
  96. else
  97. {
  98. RxState = 0;
  99. RxCounter1=0;
  100. for(i=0;i<10;i++)
  101. {
  102. RxBuffer1[i]=0x00;
  103. }
  104. }
  105. }
  106. }

这是接受openmv端传回坐标的代码,此代码是借鉴网上其他博主写的代码,感兴趣的小伙伴也可搜索查阅。

(2)pid.c的代码

  1. #include "pid.h"
  2. //x轴舵机的pid运算
  3. float Kp_x=2, //2
  4. Ki_x=0.15, //0.15
  5. Kd_x=2; //2
  6. float www,zzz;
  7. int pwm_xpid(int xerror)
  8. {
  9. int pid_ActualPwm;
  10. static float pid_Integral,pid_Voltage,error_Last;//注意这里需要使用static关键字
  11. pid_Integral+=xerror;
  12. www=pid_Integral;
  13. if (pid_Integral<-6000) pid_Integral=-6000;//积分限幅
  14. if (pid_Integral>6000) pid_Integral=6000;//积分限幅
  15. pid_Voltage=Kp_x*xerror+Ki_x*pid_Integral+Kd_x*(xerror-error_Last);
  16. error_Last=xerror;
  17. pid_ActualPwm=pid_Voltage*1;
  18. if (pid_ActualPwm<-1000) pid_ActualPwm=-1000;//pwm的范围是5002500,这里要对pwm进行限幅
  19. if (pid_ActualPwm>1000) pid_ActualPwm=1000;
  20. return pid_ActualPwm;
  21. }
  22. //y轴舵机的pid运算
  23. float Kp_y=1, //1
  24. Ki_y=0.15, //0.15
  25. Kd_y=2; //2
  26. int pwm_ypid(int yerror)
  27. {
  28. int pid_ActualPwm;
  29. static float pid_Integral,pid_Voltage,error_Last;//注意这里需要使用static关键字
  30. pid_Integral+=yerror;
  31. zzz=pid_Integral;
  32. if (pid_Integral<-6000) pid_Integral=-6000;//积分限幅
  33. if (pid_Integral>6000) pid_Integral=6000;//积分限幅
  34. pid_Voltage=Kp_y*yerror+Ki_y*pid_Integral+Kd_y*(yerror-error_Last);
  35. error_Last=yerror;
  36. pid_ActualPwm=pid_Voltage*1;
  37. if (pid_ActualPwm<-1000) pid_ActualPwm=-1000;//pwm的范围是5002500,这里要对pwm进行限幅
  38. if (pid_ActualPwm>1000) pid_ActualPwm=1000;
  39. return pid_ActualPwm;
  40. }

(3)timer.c代码

  1. #include "timer.h"
  2. #include "led.h"
  3. #include "pid.h"
  4. #include "usart.h"
  5. int pid_xerror,pid_yerror,xpwm,ypwm;
  6. extern int Cx,Cy;
  7. void TIM3_Int_Init(u16 arr,u16 psc)
  8. {
  9. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  10. NVIC_InitTypeDef NVIC_InitStructure;
  11. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  12. TIM_TimeBaseInitStructure.TIM_Period = arr;
  13. TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
  14. TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
  15. TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  16. TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
  17. TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
  18. TIM_Cmd(TIM3,ENABLE);
  19. NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
  20. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01;
  21. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03;
  22. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  23. NVIC_Init(&NVIC_InitStructure);
  24. }
  25. void TIM3_IRQHandler(void)
  26. {
  27. if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
  28. {
  29. if (Cx>0&&Cy>0)//Cx、Cy分别为红色物体中心点的横、纵坐标,当Cx、Cy都大于0时代表识别到红色物体
  30. { pid_xerror=80-Cx;//80为画面中心点的横坐标,这行代码是计算红色物体中心点横坐标离画面中心点横坐标的偏差值
  31. pid_yerror=60-Cy;//60为画面中心点的纵坐标,这行代码是计算红色物体中心点纵坐标离画面中心点纵坐标的偏差值
  32. xpwm=pwm_xpid(pid_xerror);//通过pid计算得到x轴舵机运动的pwm值
  33. ypwm=pwm_ypid(pid_yerror);//通过pid计算得到y轴舵机运动的pwm值
  34. TIM_SetCompare1(TIM4,1500-xpwm);//1500对应x轴舵机转到90度,这行代码是让x轴舵机转到对应的角度
  35. TIM_SetCompare2(TIM4,1500-ypwm);//1500对应y轴舵机转到90度,这行代码是让y轴舵机转到对应的角度
  36. }
  37. else
  38. {
  39. TIM_SetCompare1(TIM4,1500-xpwm);//这两行代码是当没有识别到红色物体时,舵机在当前位置停下
  40. TIM_SetCompare2(TIM4,1500-ypwm);
  41. }
  42. }
  43. TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
  44. }

(4)pwm.c代码

  1. #include "pwm.h"
  2. #include "led.h"
  3. #include "usart.h"
  4. void TIM4_PWM_Init(u32 arr,u32 psc)
  5. {
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. TIM_OCInitTypeDef TIM_OCInitStructure;
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
  10. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  11. GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
  12. GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_TIM4);
  13. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
  14. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  15. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  16. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  17. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  18. GPIO_Init(GPIOB,&GPIO_InitStructure);
  19. TIM_TimeBaseStructure.TIM_Prescaler=psc;
  20. TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
  21. TIM_TimeBaseStructure.TIM_Period=arr;
  22. TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  23. TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
  24. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  25. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  26. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  27. TIM_OC1Init(TIM4, &TIM_OCInitStructure);
  28. TIM_OC2Init(TIM4, &TIM_OCInitStructure);
  29. TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
  30. TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
  31. TIM_ARRPreloadConfig(TIM4,ENABLE);
  32. TIM_Cmd(TIM4, ENABLE);
  33. }

(5)main.c代码

  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include "led.h"
  5. #include "timer.h"
  6. #include "pwm.h"
  7. #include "usart2.h"
  8. //PA9----TX PA10-----RX
  9. extern float www,zzz;
  10. extern int xpwm;
  11. extern int pid_xerror,pid_yerror,xpwm,ypwm;
  12. extern void TIM4_PWM_Init(u32 arr,u32 psc);
  13. int main(void)
  14. {
  15. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
  16. delay_init(168); //初始化延时函数
  17. LED_Init(); //初始化LED端口
  18. uart_init(9600);
  19. uart2_init(9600); // openmv stm32
  20. // p4(TX)------A3(RX)
  21. // p5(RX)------A2(TX)
  22. TIM3_Int_Init(100-1,8400-1); //定时器310ms中断一次
  23. TIM4_PWM_Init(20000-1,84-1); //舵机 20ms 0度---pwm50 90度---pwm150 180度-pwm250
  24. TIM_SetCompare1(TIM4,1500);//让x轴舵机转到90
  25. TIM_SetCompare2(TIM4,1500);//让y轴舵机转到90
  26. while(1)
  27. {
  28. //printf("xpwm=%d\r",xpwm);
  29. //printf("ypwm=%d\r\n",ypwm);
  30. };
  31. }

以上代码可直接复制到自己的工程中运行,openmv端识别的代码以及和32端通信的代码参考其他博主的代码,感兴趣的小伙伴可以自行搜索,欢迎大家在评论区评论,有什么问题可在评论区提问。

以下是完整的代码链接:

链接:https://pan.baidu.com/s/1lF4xOCTfemaRtiFwLq6W8w?pwd=y0rw 
提取码:y0rw 
--来自百度网盘超级会员V4的分享
 

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

闽ICP备14008679号