找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2140|回复: 5
收起左侧

STM32颜色识别循迹代码 STM32四驱车运动库函数版本

[复制链接]
ID:640768 发表于 2021-8-6 10:38 | 显示全部楼层 |阅读模式
  1. /**
  2. * @par Copyright (C): 2010-2019, Shenzhen Yahboom Tech
  3. * @file         main.c       
  4. * @author       liusen
  5. * @version      V1.0
  6. * @date         2017.07.17
  7. * @brief        主函数
  8. * @details      
  9. * @par History  
  10. *                 
  11. * version:                liusen_20170717
  12. */
  13. //本实验对环境的要求较高,需要用户自行调整循迹模块的灵敏度
  14. //灰度传感器在不同的光照条件下对不同的颜色有不同的数值
  15. #include "stm32f10x.h"
  16. #include "app_motor.h"
  17. #include "app_ultrasonic.h"
  18. #include "bsp.h"
  19. #include "sys.h"
  20. #include "delay.h"
  21. #include "app_buzzer.h"
  22. #include "bsp_linewalking.h"
  23. #include "bsp_colorful.h"


  24. #define LOW         (0)
  25. #define HIGH         (1)
  26. //第二位 1:左90 ; 2: 右90; 3: 180度掉头  4:直行
  27. //这里可以自定义路径规划, 小车放置在起始位置深圳, 根据下一个十字或者丁字路口来写 如上行注释来左转 、右转 、直行 、掉头。
  28. char mapLocation[38][2] = {
  29.   {0, 2},  {1, 1},  {2, 2}, {3, 3},
  30.   {4, 2},  {5, 4},  {6, 3},
  31.   {7, 2},  {8, 2},  {9, 2}, {10, 3},
  32.   {11, 4}, {12, 4}, {13, 3},
  33.   {14, 2}, {15, 1}, {16, 1}, {17, 3},
  34.   {18, 2}, {19, 1}, {20, 3},
  35.   {21, 1}, {22, 1}, {23, 1}, {24, 2}, {25, 3},
  36.   {26, 2}, {27, 2}, {28, 3},
  37.   {29, 2}, {30, 2}, {31, 3},
  38.   {32, 2}, {33, 1}, {34, 3},
  39.   {35, 1}, {36, 2}, {37, 3}
  40. };
  41. int position = 0; //位置定义

  42. /**
  43. * Function       Color_Dis
  44. * @author        liusen
  45. * @date          2017.08.15   
  46. * @brief         颜色设置根据实际情况采集数据显示
  47. * @param[in]     v_ad : 采集颜色AD
  48. * @param[out]    void
  49. * @retval        void
  50. * @par History   无
  51. */

  52. void Color_Dis(int v_ad)
  53. {
  54.   int Color = v_ad;
  55.   if (Color >= 370 && Color <= 410)         
  56.   {
  57.     bsp_Colorful_RGB_PWM(0, 0, 255);
  58.   }
  59.   else if (Color >= 300 && Color <= 320)
  60.   {
  61.     bsp_Colorful_RGB_PWM(0, 255, 0);
  62.   }
  63.   else if (Color >= 345 && Color <= 365)
  64.   {
  65.     bsp_Colorful_RGB_PWM(255, 0, 0);
  66.   }
  67.   else if (Color >= 330 && Color < 345)
  68.   {
  69.     bsp_Colorful_RGB_PWM(255, 0, 255);
  70.   }
  71.   else if (Color >= 270 && Color <= 290)
  72.   {
  73.     bsp_Colorful_RGB_PWM(0, 255, 255);
  74.   }
  75.   else if (Color >= 250 && Color < 270)
  76.   {
  77.     bsp_Colorful_RGB_PWM(255, 255, 0);
  78.   }
  79.   else
  80.   {
  81.     bsp_Colorful_RGB_PWM(0, 0, 0);
  82.   }
  83. }

  84. int main(void)
  85. {       
  86.         int iGs = 0;
  87.         int TrackSensorLeftValue1 = 1, TrackSensorLeftValue2 = 1, TrackSensorRightValue1 = 1, TrackSensorRightValue2 = 1;
  88.         /*外设初始化*/
  89.         bsp_init();

  90.         /*按键启动*/
  91.         while(bsp_GetKEY());
  92.         Buzzer_GPIO_Init();
  93.         while (1)
  94.         {
  95.                 /*下面两行主要是测试当前环境光不同颜色的AD值,方便修改上面Color_Dis函数的范围值*/
  96.                 //iGs = Get_GS_Value();
  97.                 //printf("Color:%d\r\n", iGs);
  98.                
  99.             bsp_GetLineWalking(&TrackSensorLeftValue1, &TrackSensorLeftValue2, &TrackSensorRightValue1, &TrackSensorRightValue2);
  100.                
  101.                 if ( ((TrackSensorLeftValue1 == LOW || TrackSensorLeftValue2 == LOW)
  102.                 &&  TrackSensorRightValue2 == LOW) || ( TrackSensorLeftValue1 == LOW
  103.                 && (TrackSensorRightValue1 == LOW ||  TrackSensorRightValue2 == LOW)))
  104.                 {
  105.                     Car_Run(3400);
  106.                     delay_ms(60);
  107.                         Car_Stop();
  108.                         delay_ms(200);
  109.                     switch (mapLocation[position][1])
  110.                     {
  111.                       case 0: Car_Stop(); break;
  112.                       case 1: //左转90,这里所说的左转90度其实是会让小车转过一定的角度,然后让下面的while(1)去判断
  113.                       //当中间的两路检测到时会停止转动,所以会有一个转动90度的效果.
  114.                       {
  115.                           /*延迟旋转转过丁字路口黑线解决探头开始就在黑线上判断*/
  116.                           Car_SpinLeft(4300, 4300);
  117.                           delay_ms(200);
  118.                           while (1)
  119.                           {
  120.                             Car_SpinLeft(4300, 4300);
  121.                             bsp_GetLineWalking(&TrackSensorLeftValue1, &TrackSensorLeftValue2, &TrackSensorRightValue1, &TrackSensorRightValue2);
  122.                             if (TrackSensorLeftValue2 == LOW ||  TrackSensorRightValue1 == LOW)
  123.                             {
  124.                               Car_Stop();
  125.                               break;
  126.                             }
  127.                           }
  128.                       } break;
  129.                       case 2://右转90
  130.                       {   
  131.                           /*延迟旋转转过丁字路口黑线解决探头开始就在黑线上判断*/
  132.                           Car_SpinRight(4300, 4300);
  133.                           delay_ms(200);
  134.                           while (1)
  135.                           {
  136.                             Car_SpinRight(4300, 4300);
  137.                             bsp_GetLineWalking(&TrackSensorLeftValue1, &TrackSensorLeftValue2, &TrackSensorRightValue1, &TrackSensorRightValue2);
  138.                             if (TrackSensorLeftValue2 == LOW  ||  TrackSensorRightValue1 == LOW)
  139.                             {
  140.                               Car_Stop();
  141.                               break;
  142.                             }
  143.                           }
  144.                
  145.                       } break;
  146.                       case 3://左旋180掉头
  147.                       {
  148.                                   Car_Stop();
  149.                                   delay_ms(200);
  150.                           BeepOnOffMode();
  151.                           
  152.                                   iGs = Get_GS_Value();
  153.                                   //printf("Color:%d\r\n", iGs);          //这里可以打开调试数据
  154.                                   Color_Dis(iGs);
  155.                           if (position == 37) //走完地图直接停止
  156.                           {
  157.                             ModeBEEP(3);
  158.                                         BeepOnOffMode();
  159.                             position = 0;
  160.                             while(1);        
  161.                           }         
  162.                           Car_SpinLeft(4300, 4300);
  163.                                   //下面的延时相比上面的而言因为赛道的颜色区域前有一段黑线,延时长就是为了略过这条黑线的检测
  164.                           delay_ms(450);
  165.                           while (1)
  166.                           {
  167.                             Car_SpinLeft(4300, 4300);
  168.                             bsp_GetLineWalking(&TrackSensorLeftValue1, &TrackSensorLeftValue2, &TrackSensorRightValue1, &TrackSensorRightValue2);
  169.                             if (TrackSensorLeftValue2 == LOW ||  TrackSensorRightValue1 == LOW)
  170.                             {
  171.                               Car_Stop();
  172.                               break;
  173.                             }
  174.                           }         
  175.                       } break;
  176.                       case 4: Car_Run(5000); delay_ms(50); break;//直行
  177.                     }
  178.                     position++;
  179.                   }
  180.        
  181.                   // 0 X X X
  182.                   //最左边检测到
  183.                   else if ( TrackSensorLeftValue1 == LOW)
  184.                   {
  185.                         Car_SpinLeft(3400, 3400);
  186.                   }
  187.                   // X X X 0
  188.                   //最右边检测到
  189.                   else if ( TrackSensorRightValue2 == LOW )
  190.                   {
  191.                         Car_SpinRight(3400, 3400);
  192.                   }
  193.                   //四路循迹引脚电平状态
  194.                   // X 0 1 X
  195.                   //处理左小弯
  196.                   else if ( TrackSensorLeftValue2 == LOW && TrackSensorRightValue1 == HIGH)
  197.                   {
  198.                           Car_Left(3400);
  199.                   }
  200.                   //四路循迹引脚电平状态
  201.                   // X 1 0 X
  202.                   //处理右小弯
  203.                   else if (TrackSensorLeftValue2 == HIGH && TrackSensorRightValue1 == LOW)
  204.                   {
  205.                     Car_Right(3400);
  206.                   }
  207.                   else if (TrackSensorLeftValue2 == LOW && TrackSensorRightValue1 == LOW)
  208.                   {
  209.                     Car_Run(3400);
  210.                   }

  211.         }
  212.                                                                     
  213. }
复制代码

代码下载: STM32游园赛道.7z (203.43 KB, 下载次数: 36)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:584195 发表于 2021-8-8 19:37 | 显示全部楼层
楼主,这是用什么传感器来感知颜色的
回复

使用道具 举报

ID:640768 发表于 2022-3-21 14:16 | 显示全部楼层
zyluglugl 发表于 2021-8-8 19:37
楼主,这是用什么传感器来感知颜色的

红外循迹传感器
回复

使用道具 举报

ID:433680 发表于 2022-3-29 09:42 | 显示全部楼层

楼主,请问是用那颗传感器来感知颜色的? 红外循迹传感器? 应该不能感知颜色! 恳请解惑! 谢谢!
回复

使用道具 举报

ID:433680 发表于 2022-3-29 09:42 | 显示全部楼层
楼主,请问是用那颗传感器来感知颜色的? 红外循迹传感器? 应该不能感知颜色! 恳请解惑! 谢谢!
回复

使用道具 举报

ID:640768 发表于 2023-11-21 16:37 | 显示全部楼层
laigs218 发表于 2022-3-29 09:42
楼主,请问是用那颗传感器来感知颜色的? 红外循迹传感器? 应该不能感知颜色! 恳请解惑! 谢谢!

灰度传感器 就可以
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表