找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5327|回复: 3
打印 上一主题 下一主题
收起左侧

STM32F103智能红外循迹小车,包括完整码盘测速,蔽障,等功能完美实现,库函数编写

[复制链接]
跳转到指定楼层
楼主
ID:302936 发表于 2018-4-6 22:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
智能小车 寻迹、测速

单片机源程序如下:
  1. #include "stm32f10x.h"
  2. #include "CommonTypeDefine.h"
  3. #include "Wheel.h"
  4. #include <stdio.h>
  5. #include "LCD.h"


  6. int DELAY1[2]={5,60};
  7. static WheelVeer AaFLeftWheelVeer=positiveDirection;
  8. static WheelVeer AaFRightWheelVeer=positiveDirection;
  9. static char DirectionOfMotion=0; //运动方向 0正走 1反走
  10. static char AvoidanceStatus=0;  //0表示没有在避障1、2、3、4、5、6、7分别代表避障的7个阶段
  11. static char BarrierDirection=0;        //0表示没有在避障1、2分别代表在左边和右边
  12. int AaFWheelVeerArray1[8]={2,0,-2,0,-2,0,2,0};
  13. int AaFWheelVeerArray2[8]={-2,0,2,0,2,0,-2,0};

  14. static char CarStatus=1;
  15. static int timeCounter=0;
  16. static int LCDtimeCounter=100;
  17. void AaFDelay(int);
  18. void doSometingelse(void);
  19. void show(void);
  20. void showLeft(void);
  21. void showRight(void);
  22. void CarControlInit()
  23. {
  24.     GPIO_InitTypeDef GPIO_InitStructure;
  25.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);

  26.         //LCD相关配置
  27.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  28.           RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE);
  29.           LCD_Init();
  30.           LCD_Clr();
  31.     //蔽障中断口线
  32.     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_3;
  33.     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  34.     GPIO_Init(GPIOA,&GPIO_InitStructure);       
  35.   
  36.     //小车寻迹口线配置
  37.     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_7|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  38.     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  39.     GPIO_Init(GPIOB,&GPIO_InitStructure);
  40.     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
  41.     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  42.     GPIO_Init(GPIOA,&GPIO_InitStructure);
  43.     NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00000);
  44.     if (SysTick_Config(SystemCoreClock /100))
  45.     {
  46.        while (1);
  47.     }
  48.     NVIC_SetPriority(SysTick_IRQn, 0x0);
  49.     WheelInit(positiveDirection,(char)255);
  50. }
  51. void AvoidanceAndFollow()
  52. {
  53.         if(AvoidanceStatus==0)
  54.         {
  55.           if(DirectionOfMotion==0)
  56.           {
  57.             if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)==RESET)  //左边有障碍物
  58.             {
  59.                 AvoidanceStatus=8;
  60.                 BarrierDirection=1;
  61.             }
  62.             else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)==RESET)  //右边有障碍物
  63.             {
  64.                 AvoidanceStatus=8;
  65.                 BarrierDirection=2;
  66.             }
  67.             else if((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)==RESET)&&(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==RESET)&&(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==RESET))
  68.             {
  69.                 CarStatus=0;
  70.                 AaFLeftWheelVeer=stop;
  71.                 AaFRightWheelVeer=stop;       
  72.             }
  73.             else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)==RESET)  //黑线压在右边
  74.             {
  75.                 AaFLeftWheelVeer=positiveDirection;
  76.                 AaFRightWheelVeer=stop;       
  77.             }
  78.             else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==RESET)  //黑线压在左边
  79.             {
  80.                 AaFLeftWheelVeer=stop;
  81.                 AaFRightWheelVeer=positiveDirection;       
  82.             }
  83.             else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==RESET)  //黑线压在中间
  84.             {
  85.                 AaFLeftWheelVeer=positiveDirection;
  86.                 AaFRightWheelVeer=positiveDirection;       
  87.             }
  88.           }
  89.           else if(DirectionOfMotion==1)
  90.           {
  91.                   if((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==RESET)&&(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==RESET)&&(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==RESET))
  92.             {
  93.                 CarStatus=0;
  94.                 AaFLeftWheelVeer=stop;
  95.                 AaFRightWheelVeer=stop;       
  96.             }
  97.             else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==RESET)  //黑线压在右边
  98.             {
  99.                 AaFLeftWheelVeer=reverseDirection;
  100.                 AaFRightWheelVeer=stop;       
  101.             }
  102.             else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==RESET)  //黑线压在左边
  103.             {
  104.                 AaFLeftWheelVeer=stop;
  105.                 AaFRightWheelVeer=reverseDirection;       
  106.             }
  107.             else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==RESET)  //黑线压在中间
  108.             {
  109.                 AaFLeftWheelVeer=reverseDirection;
  110.                 AaFRightWheelVeer=reverseDirection;       
  111.             }
  112.           }
  113.         }
  114.     timeCounter--;
  115. }
  116. void CarControl()
  117. {
  118.     while(1)
  119.     {
  120.                 while(CarStatus==1)
  121.                 {
  122.                    if(AvoidanceStatus>0)
  123.                    {
  124.                                 if(BarrierDirection==1)
  125.                                 {
  126.                                           TurnCar(AaFWheelVeerArray1[8-AvoidanceStatus]);
  127.                                 }
  128.                                 else if(BarrierDirection==2)
  129.                                 {
  130.                                         TurnCar(AaFWheelVeerArray2[8-AvoidanceStatus]);
  131.                                 }
  132.                                 if(AvoidanceStatus==5)
  133.                                 {
  134.                                         AaFDelay(120);
  135.                                 }
  136.                                 else
  137.                                 {
  138.                                         AaFDelay(DELAY1[AvoidanceStatus%2]);
  139.                                 }
  140.                                 AvoidanceStatus--;
  141.                                 AaFDelay(2);
  142.                    }
  143.                    else
  144.                    {
  145.                         WheelSetVeer(leftWheel,AaFLeftWheelVeer);
  146.                         WheelSetVeer(rightWheel,AaFRightWheelVeer);
  147.                         AaFDelay(1);
  148.                    }
  149.                    show();
  150.                 }
  151.                 WheelSetVeer(leftWheel,AaFLeftWheelVeer);
  152.                 WheelSetVeer(rightWheel,AaFRightWheelVeer);
  153.                 show();
  154.                 doSometingelse();

  155.     }
  156. }
  157. void AaFDelay(int t)
  158. {
  159.    timeCounter=t;
  160.    while(timeCounter);
  161. }
  162. void doSometingelse()
  163. {
  164.         AaFDelay(100);
  165.         if(DirectionOfMotion==0)
  166.         {
  167.            DirectionOfMotion=1;
  168.         }
  169.         else if(DirectionOfMotion==1)
  170.         {
  171.            DirectionOfMotion=0;
  172.         }
  173.         //Other
  174.         //TurnCar(left);
  175.         //TurnCar(left);
  176.         CarStatus=1;
  177. }
  178. void show()
  179. {
  180.         if(LCDtimeCounter==0)
  181.         {
  182.                 LCDtimeCounter=100;
  183.                 showLeft();
  184.                 showRight();
  185. ……………………

  186. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
智能红外循迹小车,包括完整码盘测速,蔽障,等功能完美实现,库函数编写,便于阅读.rar (293.78 KB, 下载次数: 182)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:344895 发表于 2018-7-18 10:16 | 只看该作者
为什么有错啊
回复

使用道具 举报

板凳
ID:267330 发表于 2018-10-22 16:39 | 只看该作者
在下载不了啊
回复

使用道具 举报

地板
ID:267330 发表于 2018-10-24 09:42 | 只看该作者
复制,学习,别人的经验
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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