智能小車 尋跡、測速
單片機源程序如下:
- #include "stm32f10x.h"
- #include "CommonTypeDefine.h"
- #include "Wheel.h"
- #include <stdio.h>
- #include "LCD.h"
- int DELAY1[2]={5,60};
- static WheelVeer AaFLeftWheelVeer=positiveDirection;
- static WheelVeer AaFRightWheelVeer=positiveDirection;
- static char DirectionOfMotion=0; //運動方向 0正走 1反走
- static char AvoidanceStatus=0; //0表示沒有在避障1、2、3、4、5、6、7分別代表避障的7個階段
- static char BarrierDirection=0; //0表示沒有在避障1、2分別代表在左邊和右邊
- int AaFWheelVeerArray1[8]={2,0,-2,0,-2,0,2,0};
- int AaFWheelVeerArray2[8]={-2,0,2,0,2,0,-2,0};
- static char CarStatus=1;
- static int timeCounter=0;
- static int LCDtimeCounter=100;
- void AaFDelay(int);
- void doSometingelse(void);
- void show(void);
- void showLeft(void);
- void showRight(void);
- void CarControlInit()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
- //LCD相關配置
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE);
- LCD_Init();
- LCD_Clr();
- //蔽障中斷口線
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- //小車尋跡口線配置
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_7|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00000);
- if (SysTick_Config(SystemCoreClock /100))
- {
- while (1);
- }
- NVIC_SetPriority(SysTick_IRQn, 0x0);
- WheelInit(positiveDirection,(char)255);
- }
- void AvoidanceAndFollow()
- {
- if(AvoidanceStatus==0)
- {
- if(DirectionOfMotion==0)
- {
- if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)==RESET) //左邊有障礙物
- {
- AvoidanceStatus=8;
- BarrierDirection=1;
- }
- else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)==RESET) //右邊有障礙物
- {
- AvoidanceStatus=8;
- BarrierDirection=2;
- }
- else if((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)==RESET)&&(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==RESET)&&(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==RESET))
- {
- CarStatus=0;
- AaFLeftWheelVeer=stop;
- AaFRightWheelVeer=stop;
- }
- else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)==RESET) //黑線壓在右邊
- {
- AaFLeftWheelVeer=positiveDirection;
- AaFRightWheelVeer=stop;
- }
- else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==RESET) //黑線壓在左邊
- {
- AaFLeftWheelVeer=stop;
- AaFRightWheelVeer=positiveDirection;
- }
- else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==RESET) //黑線壓在中間
- {
- AaFLeftWheelVeer=positiveDirection;
- AaFRightWheelVeer=positiveDirection;
- }
- }
- else if(DirectionOfMotion==1)
- {
- if((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==RESET)&&(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==RESET)&&(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==RESET))
- {
- CarStatus=0;
- AaFLeftWheelVeer=stop;
- AaFRightWheelVeer=stop;
- }
- else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==RESET) //黑線壓在右邊
- {
- AaFLeftWheelVeer=reverseDirection;
- AaFRightWheelVeer=stop;
- }
- else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==RESET) //黑線壓在左邊
- {
- AaFLeftWheelVeer=stop;
- AaFRightWheelVeer=reverseDirection;
- }
- else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==RESET) //黑線壓在中間
- {
- AaFLeftWheelVeer=reverseDirection;
- AaFRightWheelVeer=reverseDirection;
- }
- }
- }
- timeCounter--;
- }
- void CarControl()
- {
- while(1)
- {
- while(CarStatus==1)
- {
- if(AvoidanceStatus>0)
- {
- if(BarrierDirection==1)
- {
- TurnCar(AaFWheelVeerArray1[8-AvoidanceStatus]);
- }
- else if(BarrierDirection==2)
- {
- TurnCar(AaFWheelVeerArray2[8-AvoidanceStatus]);
- }
- if(AvoidanceStatus==5)
- {
- AaFDelay(120);
- }
- else
- {
- AaFDelay(DELAY1[AvoidanceStatus%2]);
- }
- AvoidanceStatus--;
- AaFDelay(2);
- }
- else
- {
- WheelSetVeer(leftWheel,AaFLeftWheelVeer);
- WheelSetVeer(rightWheel,AaFRightWheelVeer);
- AaFDelay(1);
- }
- show();
- }
- WheelSetVeer(leftWheel,AaFLeftWheelVeer);
- WheelSetVeer(rightWheel,AaFRightWheelVeer);
- show();
- doSometingelse();
- }
- }
- void AaFDelay(int t)
- {
- timeCounter=t;
- while(timeCounter);
- }
- void doSometingelse()
- {
- AaFDelay(100);
- if(DirectionOfMotion==0)
- {
- DirectionOfMotion=1;
- }
- else if(DirectionOfMotion==1)
- {
- DirectionOfMotion=0;
- }
- //Other
- //TurnCar(left);
- //TurnCar(left);
- CarStatus=1;
- }
- void show()
- {
- if(LCDtimeCounter==0)
- {
- LCDtimeCounter=100;
- showLeft();
- showRight();
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
智能紅外循跡小車,包括完整碼盤測速,蔽障,等功能完美實現,庫函數編寫,便于閱讀.rar
(293.78 KB, 下載次數: 183)
2018-4-6 23:59 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|