




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
頭文件/*************************************************************************************************//*--———-——--——--—--—-———-——————---——-文件名:Bace.h—----————---——-—————-———————-—-—-——-——*//*-——--—-------———------—-—-—--—-功能:Base.c對(duì)應(yīng)頭文件--—---—--—-——-—--—-————-—-—--—---*//*************************************************************************************************///結(jié)構(gòu)體定義在頭文件中voiddelay(intdelaytime); //延時(shí)大約3usvoidlongdelay(intdelaytime); //長延時(shí)大約3ms,沒有細(xì)算,本應(yīng)該用定時(shí)器的,O(∩_∩)O哈哈~unsignedcharMax(intbijiao[],unsignedcharn);//最大值,返回?cái)?shù)組下標(biāo)unsignedcharMin(intbijiao[],unsignedcharn);//最小值,返回?cái)?shù)組下標(biāo)intMidnum(intbijiao[],unsignedcharn);//中值,返回中間數(shù)下標(biāo)voidMaxtoMin(intbijiao[],charflag[],intn);intAverage(intbijiao[],unsignedcharn);//求中間值intabs(inti);intPIDcalc1(intPcoe,intIcoe,intDcoe,intError,intLastError,intPrevError,intSumError,intiError_max);//位置pid用于驅(qū)動(dòng)舵機(jī);//位置式PID用于驅(qū)動(dòng)舵機(jī)intPIDcalc2(intPcoeS,intIcoeS,intDcoeS,intSetPoint,intCurrentPoint,intLastErrorS,intPrevErrorS,intPrevPrevErrorS,intIterm_max);//增量式PID用于驅(qū)動(dòng)馬達(dá)currentpoint當(dāng)前測量值/*************************************************************************************************//*—-—-----—-----——--——————-——-———-文件名:SureLocation.h----———-----—--——--——-—-————--——-—-*//*-—--—---———----—-——-—-------——功能SureLocation。c對(duì)應(yīng)頭文件——---------—-——-—-——-—---—-—--—*//*************************************************************************************************//*--—-—-----——-———————--———---——--以下內(nèi)部文件調(diào)用————-——---———-----—-—--——-----—--——*/#ifndef_H_SURELOCATION_ #define_H_SURELOCATION_intBackLocationtest(void);/*———-—---————————-——---——--——---—以下外部文件調(diào)用———-—-----——————-—-—--—---———-———--*/intSureLocationTest(void);intWandaotest(void);intCrosstest(void);#endif/*************************************************************************************************//*-—---—-—-———-—-————-—--—--———-——文件名:HardWare.h---—————--——-—---————————--——-—----*//*—---——--—-------——--—--—--—-—-功能HardWare。c對(duì)應(yīng)頭文件———--—--—-——-—------——-————----*//*************************************************************************************************/#ifndef_H_HARDWARE_ #define_H_HARDWARE_/*-——————-—---—-—-----——---—-——-——以下內(nèi)部函數(shù)調(diào)用--—-———--——-—-—-—-——————————----—-—-—-—-———----*/voidCLK_init(void);voidECT_init(void);voidIO_init(void);voidPWM_init(void);voidAD_init(void);/*---—-———---—-—---———-—--——--——--以下外部函數(shù)調(diào)用-——--———-—-----—--—--—-———————-—------———-——-——*/voidinit(void);unsignedcharGetATD0(unsignedcharch);#endif/*************************************************************************************************//*-————-——-———————-—--——-—---—-——-文件名:HardWareDriver。h—————————-—-—————-—---—--—-——-—--—-*//*--———-——--—------—-——————----—功能HardWareDriver。c對(duì)應(yīng)頭文件--—--—-——-——-———--—----—-—-———-*//*************************************************************************************************/#ifndef_H_HARDWAREDRIVER_ #define_H_HARDWAREDRIVER_/*-——-—-—-——--——-—--——-——-—-—-----以下外部函數(shù)調(diào)用—--—-—--—————---———-—-—--—---—---—---—-——---——-*/voidMOD_init(void);voidLEDtest(void);voidXianshi(unsignedcharsta);#endif/*************************************************************************************************//*-——-—-——-—————--——-—--——---————--—文件名:Control。h--————--———————--——--——————--—-—-——--—*//*-———--—-—————-—-——--—--------功能:Control.c對(duì)應(yīng)頭文件-—-———-———-—-———-————-—---—-———————*//*************************************************************************************************//*----———-—--——-——-—-—-—--———-———-以下內(nèi)部文件調(diào)用————-—-—-—---—--—--——--—-—---—--——-*/#ifndef_H_CONTROL_ #define_H_CONTROL_voidduojicontrol(void);voidupv(intvj);voiddownv(intvj);intSpeedWanted(void);voidControlMotor(intuin);voidMotorControl(void);/*--——--—--———----—-—-—-—-———-————以下外部文件調(diào)用——--——-——---————--———-————--—---—--*/voidControl(void);#endif/*************************************************************************************************//*---—--—--——-—-—---—----——---—-文件名:Sensor。h———---—--——-—————-—-—----———---——-————-——-—-——-——*//*—----—------————-——-----—-—功能:Sensor.c對(duì)應(yīng)頭文件-—--—-——-—-——--——-—----——-——---————---——-—-——*//*************************************************************************************************//*-—--—--—-——------—-———————--——以下內(nèi)部文件調(diào)用--—-——----———-——--——--—-—--—--——--—-——--——————-——*/voidGetADdata(inttestevetime);voidGetordinarydata(void);voidADdataOk(void);/*---—-—--———-——-----—--——--—--—以下外部文件調(diào)用—---——---——--——-—--———---—---—-————---—————--——-—*/voidProcess(void);/*———-——-———————--————-—-—--——功能包括其它所有頭文件的文件——--——--——----—-—-—-—--——---—-*//*************************************************************************************************/#ifndef_H_INCLUDE_ #define_H_INCLUDE_#include”Sensor。h”//#include”LCD。h"#include"Surelocation。h"#include”Control。h”#include”base。h”#include"HardWare.h”#include"Hardwaredriver。h"#include”MC9S12XS128.h”/*-----——--—-—-----———-以下傳感器整定參數(shù)--—--——-——-————--—-—--—-—-—--—*/#defineFRONTNUM8//前排傳感器數(shù)4#defineTESTTIME4//每次采集每個(gè)傳感器讀取的數(shù)值數(shù)目#defineHISTORYNUM10//歷史數(shù)據(jù)保存量#defineMIDYUZHI3//中間兩傳感器迷失后的和#defineZHONGJIANYUZHI35//線在兩個(gè)傳感器中間和某個(gè)傳感器之下時(shí)的閾值#defineZHONGJIAN210//用來檢測車子是否在跑到中心(賽車中心距離賽道中心八厘米)中間兩傳感器的兩單峰函數(shù)疊加為單峰函數(shù)峰值250#defineADYUZHI10//AD數(shù)據(jù)校驗(yàn)閾值#defineLOCYUZHI68//跨區(qū)閾值#defineCOE1/*-—-------——---—-—--—-以下電機(jī)整定參數(shù)——-—-———---—-—-——---———-—-—---*/#definePWMMOTOT800//電機(jī)PWM周期;//電機(jī)PWM周期1.6ms,電機(jī)周期隨便只關(guān)心占空比。//電機(jī)PWM周期1.6ms#defineUOUTMAX700 //最大正向電壓#defineUOUTMIN400 //最大反向電壓/*——--—-——-—----—-———--以下舵機(jī)整定參數(shù)—---—-——----—-——----—-—--—-———*/#definePWMDT30000//舵機(jī)PWM周期20ms#defineCENTRAL2100//舵機(jī)PWM中心占空1.53ms,轉(zhuǎn)動(dòng)極限duojizhankong=750#defineMOSTLEFT2850//舵機(jī)最左邊位置#defineMOSTRIGHT1350//舵機(jī)最右邊位置/*—--—————-—-——------—-速度控制相關(guān)宏定義--—---———-----——----—-—--——-—-*/#definePIDYushe(450+speedset*7)#definePIDYushe1(450+speedset*7)//(450+speedset*7)#definePIDYushe2(550+speedset*10)//(450+speedset*7)#defineSTOPCOUNT120 //檢測到速度異常(很?。┩\囁璧睦奂訑?shù)目#defineSTOPSAFTMIN 30 //啟動(dòng)防撞需要達(dá)到的最小速度值#defineSPEEDMAX25//最高速度#defineSPEEDMIN12//最低速度/*——-———-—-——-——---—--—LCD相關(guān)宏定義——---—--—--——--—-———-——--—-———*///#defineRSPORTA_PA5//#defineENPORTA_PA3#endif程序/*************************************************************************************************//*—-—-—-—-——-—--———-—————-—-----——-—-文件名:Bace。c----———-————--——-——-----——-—--—---———-*//*—--—-—---—-———--————-————-——-功能:基礎(chǔ)功能,延時(shí)和數(shù)據(jù)處理-———-—---——-——————-———-—-——*//*************************************************************************************************/#include"Include。h" //包括延時(shí)、數(shù)據(jù)算術(shù)處理等voiddelay(intdelaytime) {//軟件延時(shí),24M總線頻率時(shí)延時(shí)時(shí)間約為10us inti;unsignedcharj; for(i=0;i〈delaytime;i++) for(j=0;j〈24;j++) ; } voidlongdelay(intdelaytime) {//軟件延時(shí),24M總線頻率時(shí)延時(shí)時(shí)間約為1ms inti; for(i=0;i<delaytime;i++) delay(100); }/*---———-—---—--——-—————--—-——求數(shù)組中中間值函數(shù)———---—-——--—————-——-—--—--—--—*/ //函數(shù)返回值unsignedcharbijiao[n/2];//輸入?yún)?shù)intbijiao[],unsignedcharn//求中值,bijiao[]為原始數(shù)據(jù),n為數(shù)據(jù)量/*—-—-—-—-———-—-———————--——--—-————--—-———-—————---—---—--————----------—--——-—--—-*/ intMidnum(intbijiao[],unsignedcharn) { inti,j,temp; if(n==1) returnbijiao[0]; if(n==2) return(bijiao[0]+bijiao[1])/2; for(j=1;j〈n;j++) for(i=0;i<n-j;i++) { if(bijiao[i]〈bijiao[i+1]) { temp=bijiao[i]; bijiao[i]=bijiao[i+1]; bijiao[i+1]=temp; } } if(n%2) returnbijiao[(n—1)/2]; else return(bijiao[n/2]+bijiao[n/2—1])/2; } /*-—-———---—-———-——————-——--—-—求數(shù)組中最大值函數(shù)——---—-—---————---——-—-——--——--*/ //函數(shù)返回值unsignedchartemp//輸入?yún)?shù)intbijiao[],unsignedcharn//求最大值,bijiao[]為原始數(shù)據(jù),n為數(shù)據(jù)量/*—--——-—————-—--—--—-——-———-————-----—---————--————-—-———-—--—---—----———-—————--—*/ unsignedcharMax(intbijiao[],unsignedcharn) {unsignedchari,temp=0; for(i=0;i〈n;i++) {if(bijiao[temp]〈bijiao[i])temp=i; } return(temp); } /*———-————-———--—-—-——--——-————求數(shù)組平均值函數(shù)—-——————-——--—---——------------*/ //函數(shù)返回值數(shù)組平均值//輸入?yún)?shù)intbijiao[],unsignedcharn//求平均值,bijiao[]為原始數(shù)據(jù),n為數(shù)據(jù)量/*-—-—-—-—------——-—-----—--——-—----——---—————————-————----——-—-————----———---——--—*/ intAverage(intbijiao[],unsignedcharn)//建立了一個(gè)原來數(shù)組的副本,否則應(yīng)該是指針*int { unsignedchari=0; inttemp=0; for(i=0;i<n;i++) { temp+=bijiao[i]; } return(temp/n); } /*—-——--————-——--——-—-——-—--——-求數(shù)組中最小值函數(shù)---—-—-——---—————-—---—--——---—*/ //函數(shù)返回值unsignedchartemp//輸入?yún)?shù)intbijiao[],unsignedcharn//求最小值bijiao[]為原始數(shù)據(jù),n為數(shù)據(jù)量/*-—---———----------—---—-——-—-----------—-—--—-—————-—-—-——--—-——-—-—----—-———-—-—*/unsignedcharMin(intbijiao[],unsignedcharn) { unsignedchari,temp=0; for(i=0;i〈n;i++) {if(bijiao[temp]〉bijiao[i]) temp=i;} return(temp); }//得到數(shù)組前n個(gè)元素中最小一個(gè)數(shù)的下標(biāo) /*-----—-—---—-—--—--———-——---—--—-—數(shù)組排序按下標(biāo)————---—--—-—--——-—-—--——-———--—-----*///函數(shù)返回值void//輸入?yún)?shù)intbijiao[],intflag[],intn//參數(shù)bijiao[]原始數(shù)據(jù),flag[]排序后的標(biāo)號(hào)數(shù)組,n數(shù)組長度/*——-—-—--------—-—--—————-—-——-———--—----—--—-————----——---—-——--————---—-—-—-—----——-———*/voidMaxtoMin(intbijiao[],intflag[],intn) { inttemp[10]={0}; inti; if(n〉10) n=10; for(i=0;i〈n;i++) temp[i]=bijiao[i]; for(i=0;i<n;i++) { flag[i]=Max(temp,n); temp[flag[i]]=1;//給一個(gè)很小的數(shù)值 } }//將數(shù)組bijiao[]中數(shù)字從小到大排列的下標(biāo)存入數(shù)組flag/*---————-----———-———-————-————————-絕對(duì)值函數(shù)-----—————-—---—————-——---——---——-—-—*///函數(shù)返回值int//輸入?yún)?shù)int//返回輸入數(shù)字的絕對(duì)值/*---—--—-———-—-—---—-——-——-—----—--——-—————-----------—-—--—-—------—-—————-—-————————---*/intabs(inti){intj;if(i〉=0)j=i;elsej=0—i;return(j);}/*-————---—-—--——-—-—————------—--—-PID定義部分-————-———--—--——-——-—-———-————---———-*///函數(shù)返回值int//輸入?yún)?shù)intbijiao[],intflag[],intn//參數(shù)bijiao[]原始數(shù)據(jù),flag[]排序后的標(biāo)號(hào)數(shù)組,n數(shù)組長度/*-——-——---—----—----———--—-—---—-——-——-—-—---------—————--———------—--—-—-————-—-—-—---——*/intPIDcalc1(intPcoe,intIcoe,intDcoe,intError,intLastError,intPrevError,intSumError,intiError_max)//位置pid用于驅(qū)動(dòng)舵機(jī){intdError,iError,pError;PrevError=LastError;LastError=Error;SumError+=Error;dError=Dcoe*(LastError—PrevError);iError=Icoe*(SumError);pError=Pcoe*Error;if(iError〉iError_max)iError=0;return(pError+iError+dError);}intPIDcalc2(intPcoeS,intIcoeS,intDcoeS,intSetPoint,intCurrentPoint,intLastErrorS,intPrevErrorS,intPrevPrevErrorS,intIterm_max)//增量式PID用于驅(qū)動(dòng)馬達(dá)currentpoint當(dāng)前測量值{intPterm,Iterm,Dterm;PrevPrevErrorS=PrevErrorS;PrevErrorS=LastErrorS;LastErrorS=SetPoint-CurrentPoint;Pterm=PcoeS*(LastErrorS-PrevErrorS);Iterm=IcoeS*LastErrorS;Dterm=IcoeS*(LastErrorS-2*PrevErrorS+PrevPrevErrorS);if(Iterm>Iterm_max)Iterm=0;return(Pterm+Iterm+Dterm);}/**************************************************************************************************//*--———--——-———-----—-——--——--—飛思卡爾杯智能小車大賽省賽版本---—-—-----—---——-—————-—--————-—*//*——-----——---———-——-—-—-—----制作人:張霖張凱淇張陽---—————--—---——-—--——--—————-—--——————-—*//**************************************************************************************************//**************************************************************************************************//*————---————————-———--————--—---——-—文件名:main。c——--—-—----—-——-----—---————————---—---*//*----—--—---——————--—-——-—-—-功能主函數(shù)和定時(shí)器所在,核心文件—-——————--——-——-—--——--—-—*//**************************************************************************************************/#include<hidef。h〉/*commondefinesandmacros*/#include"derivative。h”/*derivative—specificdefinitions*/#include"Include。h”externintADdataokflag;//AD數(shù)據(jù)有效externintCrossflag;externintWandaoflag;externintduojizhankong;externintmodeflag;intforecast[15];//彎道識(shí)別intTingcheflag;//停車標(biāo)志位intLINE_V;intTC0count;unsignedintf1,f2;intstartline=0;unsignedintspeed;unsignedintmodecount;/*—----——--—---—---—-———--—-———-—-—-主函數(shù)---———-———-—-—————-—————-————-———----—*/ //main.c:Startprogramhere。.。/*—--—-—-—--——-—--—--—------—-----—--—-———-——----——--——-———---—--——-——--——————-—-*/voidmain(void){DisableInterrupts;//longdelay(2000);//2s延時(shí)init(); PTM_PTM0=1;TC0=11320;//54msmodeflag=0;LEDtest();EnableInterrupts;//離線調(diào)試的時(shí)候如果不禁止此項(xiàng)就會(huì)因?yàn)?8號(hào)中斷而中止程序執(zhí)行 while(1) { MOD_init(); /*if(PORTA_PA1==0)//干簧管掃描 { longdelay(1);//機(jī)械開關(guān)防抖 startline++;if(startline==3)Tingcheflag=1; } if(Tingcheflag) { while(1){ DisableInterrupts; PWMDTY23=2400; }調(diào)試禁止*/ }} //#pragmaCODE_SEG__NEAR_SEGNON_BANKED//禁止隨便用近調(diào)用#pragmaCODE_SEGNON_BANKEDvoidinterrupt8timer0(void){DisableInterrupts;TFLG1&=0x01;PACTL_PAEN=0;TC0=(TC0+1125)%65525;//定時(shí)6ms則1125f1=PACNT;//先把PACNT值存進(jìn)變量中再在實(shí)時(shí)調(diào)試時(shí)讀取否則PACNT分高低位讀取會(huì)出錯(cuò)誤/*--——--——-———-—-——---此參數(shù)需整定—-—--——-—-—-——---—--———--——--—-————-—————-*/speed=f1*720/1260;//(f1/200*6*10^(—3))*(40/105)*0.18速度單位:厘米/秒/*輪周長180mm傳動(dòng)比105/40車速2m/s時(shí)車輪轉(zhuǎn)速11r/s編碼器轉(zhuǎn)速29。17r/s脈沖數(shù)5833/s脈沖周期0.1714ms每次脈沖數(shù)35個(gè)*/PITTF_PTF=1;//清中斷標(biāo)志位PACTL_PAEN=1;PACNT=0x00;//計(jì)數(shù)器歸零Process();Control();EnableInterrupts;}//#pragmaCODE_SEG__NEAR_SEGNON_BANKED#pragmaCODE_SEGNON_BANKEDvoidinterrupt24externinterrupt(void){DisableInterrupts;//PIEJ=0x00;longdelay(1);//干簧管是機(jī)械開關(guān)要防抖以后的干簧管要加電容防抖PIFJ=0x80;startline++;if(startline==3)Tingcheflag=1;EnableInterrupts;}/*************************************************************************************************//*—--—----————-——-—-——--——----——文件名:SureLocation。c—————---——-—-—-———-—--——-—--—------—---——--*//*—-———--——-——-———-—-——-—-——-功能:確定前后兩排黑線相對(duì)位置———--—---————-———————-—-————----—---——*//*************************************************************************************************/#include"Include。h"externintADdatahis[HISTORYNUM][FRONTNUM];//傳感器歷史數(shù)值,0為目前值externintADdata[FRONTNUM];//傳感器數(shù)據(jù)最終值externcharADdataOkflag;intlocation;//主位置/*--———---—--————-———---—-——---主位置檢測——--—--—--—--———-----—--—-—--*/ //函數(shù)返回值int//輸入?yún)?shù)void//獲取前排位置/*---—————-——--———————--——————-—-—-————-—-——--—-——-————-—--———-——————-—-—-—---—*/intSureLocationTest(void){charloctemp,loc;loctemp=Max(ADdata,F(xiàn)RONTNUM);//大區(qū)區(qū)段if(loctemp==7&&ADdata[6]<35){loc=0b10000000;if(ADdata[7]>25){if(ADdata[7]>39)location=4;elselocation=5;}else{if(ADdata[7]〉4){if(ADdata[7]〉14)location=6;elselocation=7;}else{if(ADdata[7]〉1)location=8;elselocation=9;}}}if((loctemp==6&&ADdata[7]>39)||(loctemp==7&&ADdata[6]>35)){loc=0b01000000;if((loctemp==6)&&ADdata[6]〈44){if(ADdata[6]〉36)location=1;elselocation=0;}}if(loctemp==3)location=0;if(loctemp==2&&ADdata[1]〈29){loc=0b00010000;if(ADdata[2]〉35)location=-1;elselocation=0;}if(loctemp==3&&ADdata[3]>40){loc=0b00001000;location=—2;}if(loctemp==0&&ADdata[1]〉34){loc=0b00000100;if(ADdata[0]〉53)location=—4;elselocation=-3;}if(loctemp==0&&ADdata[1]〈34){loc=0b00000100;if(ADdata[0]〉43){if(ADdata[0]>55)location—5;elselocation=—6;}else{if(ADdata[0]〉32)location=—7;else{if(ADdata[0]>20)location=—8;elselocation=-9;}}}/*if((ADdata[3]+ADdata[4])<MIDYUZHI){if(ADdata[0]〉A(chǔ)Ddata[7])location=—10;elselocation=10;}*/return(location);}/*************************************************************************************************//*———--——---——----————-—--———--———文件名:HardWare。c--——--——————————--------—---——-——--*//*——-—-—-—-—--—-—---———-—-—--————-—-功能硬件初始化--——-—--—-————--———---———-—--—--—-———-—-*//*************************************************************************************************/#include"Include.h"intPWMMOTOTA;//電機(jī)占空比externintLINE_V;/*—————---—------—-—-—-—---—-————--—系統(tǒng)時(shí)鐘初始化-—-------—--——--—--------—-——--———--———-*/voidCLK_init(void){CLKSEL=0x00;//初始時(shí)還沒有PLLPLLCTL=0xe1;//時(shí)鐘監(jiān)控使能,PLL允許,VCOCLK調(diào)頻2%,禁止快速蘇醒,禁止自給時(shí)鐘,偽停止下禁止實(shí)時(shí)中斷和看門狗SYNR=2;REFDV=1;//PLLCLK=2*OSCCLK*(SYNR+1/((REFDV+1))=48M;asmNOP;asmNOP;asmNOP;//等待鎖相環(huán)穩(wěn)定while(!(CRGFLG&0x08));//鎖相環(huán)穩(wěn)定標(biāo)志位CLKSEL=0x80;//時(shí)鐘選擇寄存器,BusClock=PLLCLK/2,停止模式下振蕩器、系統(tǒng)時(shí)鐘、鎖相環(huán)、內(nèi)核時(shí)鐘、看門狗繼續(xù)工作}/*---—————--—--———--——--———--------—ECT初始化—---———-———————--—-—--——---——-—————----—*/voidECT_init(void){TIOS=0x0f;//通道0~4輸出比較,5~7輸入捕捉TSCR2=0x07;//TOF被置位時(shí)禁止中斷,通道7輸出后不復(fù)位,f=24/128=0。1875MHzTCTL3=0x55;//通道IC7~4捕捉上升沿;(TCTL2=0x00,OC與引腳斷開)PACTL=0x40;//脈沖累加器A使能,當(dāng)PT7檢測到上升沿后計(jì)數(shù)器加1,用分頻因子定義時(shí)鐘,禁止累加器溢出中斷TIE=0x01;//OC通道各位引起中斷,自由計(jì)時(shí)器溢出不引起中斷PACNT=0x00;TCNT=0x00;TSCR1=0x80;//定時(shí)器允許,WAIT模式繼續(xù)工作,F(xiàn)REEZE模式繼續(xù)工作,定時(shí)器標(biāo)志不快速清除}/*----————-——-—-——-—-----—---———-———IO口初始化———---——--—-———--—--——--———--—-———--———-*/voidIO_init(void){DDRA=0xff;PORTA=0xff;DDRA=0x00;//PORTA做輸入口//PUCR_PUPAE=1;//A口上拉電阻DDRB=0xff;//PORTB做LED燈PORTB=0xff;//DDRT=0xff;//PTT=0x00;//初始拉低DDRT=0x00;//T口輸入PERT=0xff;//T口加pulldevice防抖PPST=0xff;//T口下拉電阻DDRP=0xff;//PORTP做PWM輸出PTP=0xff;DDRM_DDRM0=1;//PTM0做輸出口,1=正開0=倒車PTM_PTM0=0;DDRJ=0xff;PTJ=0xff;//PTJ0初始值為高PERJ=0xff;DDRJ=0x00;//PTJ0做輸入口,接受干簧管中斷PPSJ=0x00;//PTJ7捕捉下降沿中斷PIEJ=0X80;//中斷使能//RDR0AD0=0xff;//AD口即使傳感器不工作時(shí)也有值認(rèn)為是電磁干擾加下拉電阻RDRP=0xff;//PUCR=0x40;//BKGDEKAB都是用上拉電阻//PUCR對(duì)于輸入拉高RDRIV對(duì)于輸出拉低}/*-———-—-—-—--—-————-———-—----——--—-PWM初始化—--———-—--—-------——-----——-—-—--—---—--*/voidPWM_init(void){PWME=0x00;//初始禁止PWMPWMPRCLK=0x44;//預(yù)分頻BUSCLOK/16=1。5MHzPWMCLK=0x00;//0、1使用CLOCKA2、3使用CLOCKBPWMCTL=0x30;//通道0123級(jí)聯(lián)PWMPOL=0xff;//初始低電平PWMCAE=0x00;//波形左對(duì)齊PWMPER01=PWMDT;//舵機(jī)周期PWMDTY01=CENTRAL;//舵機(jī)中心占空比PWMPER23=PWMMOTOT;//電機(jī)周期PWMDTY23=260;//電機(jī)初始中心占空比PWME=0xff;}/*—--————--—-————-—--—---——-—---——--AD模塊初始化—--—-----——---————-—-—--——---—-—-——-————*/voidAD_init(void){ATD0CTL0=0x07;//AD轉(zhuǎn)換序列長度為8ATD0CTL1=0x00;//允許指定AD口做外部觸發(fā)輸入口,8位結(jié)果,采樣前采樣電容不放電1ATD0CTL2=0x40;//標(biāo)志寄存器不快速清零,停止模式不運(yùn)行,禁止外部觸發(fā),禁止轉(zhuǎn)換完成中斷和比較成功中斷ATD0CTL3=0xc0;//結(jié)果右對(duì)齊,非FIFO模式,凍結(jié)模式繼續(xù)轉(zhuǎn)換轉(zhuǎn)換長度4ATD0CTL4=0x44;//第二階段采用8個(gè)AD周期(第一階段2個(gè)AD周期),f=fbus/2*(4+1),4。16ms轉(zhuǎn)換一次ATD0CTL5=0x30;//連續(xù)多通道轉(zhuǎn)換,從通道0開始ATD0DIEN=0x00;//禁用數(shù)字輸入緩沖?}/*--———————————-——————-—-——-—--———--獲取AD轉(zhuǎn)換初始數(shù)據(jù)-------———-——--——-—---———-—-———--—--—---*/unsignedcharGetATD0(unsignedcharch){bytead=0;//while(!ATD0STAT0_SCF);switch(ch){case0:ad=ATD0DR0L;break;case1:ad=ATD0DR1L;break;case2:ad=ATD0DR2L;break;case3:ad=ATD0DR3L;break;case4:ad=ATD0DR4L;break;case5:ad=ATD0DR5L;break;case6:ad=ATD0DR6L;break;case7:ad=ATD0DR7L;break;}returnad;}/*-----——-———-—---———--—--—---————-—總初始化--—-———————-—————---—-—-——-—---————---———-——*/voidinit(void){CLK_init();ECT_init();IO_init();PWM_init();AD_init();}/*************************************************************************************************//*——-------———--———---—---—-—--—---—文件名:HardDriver.c—-—-—--—-————--———---———--—-—---*//*—-———-————---——-----—--——-—----——功能1:初始化系統(tǒng)參數(shù)-—----—-—---—---——--——----—--——-*//*—---—-—--—--——-—--—-—--————--—--———功能2:采樣底層函數(shù)——-—---—---——-————————-—--—--—-—-*//*************************************************************************************************/#include"Include.h"externintLINE_V;intlevelflag;//A口數(shù)值intwandaoflag;//賽道識(shí)別策略/*---——-——---———-----—--—-----—————速度模式初始化--———-—---—-—----—----—-—————---—*/ //函數(shù)返回值void//輸入?yún)?shù)/*--—————-—--——————----—-——--—-——-----—-—-——————--—-—-—----——--——---——-—--—-—-—--—--*/voidMOD_init(void){levelflag=PORTA;switch(levelflag&0b00010101)//低3位確定速度基本檔位{case0b00000000: LINE_V=200; break; case0b00000001: LINE_V=212; break; case0b00000100: LINE_V=250; break; case0b00000101: LINE_V=288; break; case0b00010000: LINE_V=300; break; case0b00010001: LINE_V=328; break; case0b00010101: LINE_V=350;break; default:LINE_V=300;break;}switch(levelflag&0b01000000){case0b01000000:wandaoflag=1;//彎道case0b00000000:wandaoflag=0;//直道}}/*-——---——--——-----————-———--—-——-LED指示燈顯示—-—-——---—----—--——--——--——--—-——*/ //函數(shù)返回值void//輸入?yún)?shù)unsignedcharsta/*——-—----———---——-—-——-—-—---——--———-—-----—----————--—-—--—----——---—----—-————-—-*/voidXianshi(unsignedcharsta){switch(sta){case1:PORTB=0x01;case2:PORTB=0x02;case3:PORTB=0x04;case4:PORTB=0x08;case5:PORTB=0x10;case6:PORTB=0x20;case7:PORTB=0x40;case8:PORTB=0x80;}}/*-——-—-——-——-——————-——--—-——-————LED指示燈測試-——----————————--——-—-—-—-——-————*/ //函數(shù)返回值void//輸入?yún)?shù)void/*-——-———-----------—————------------———-——-----———-——-—-—-—--—-—------——-—-——————-—*/voidLEDtest(void){Xianshi(1);longdelay(30);Xianshi(2);longdelay(30);Xianshi(3);longdelay(30);Xianshi(4);longdelay(30);Xianshi(5);longdelay(30);Xianshi(6);longdelay(30);Xianshi(7);longdelay(30);Xianshi(8);longdelay(30);}/*************************************************************************************************//*—--—-—--—-——————--—-----—-—----———文件名:Control。c——---—---—————-----——-———-————--———-—-*//*————-——---—-————--———----——--功能:確定舵機(jī)和電機(jī)輸出量——-—-——------—-———-——---—----—--———*//*************************************************************************************************/#include"Include.h”externintWandaoflag;//彎道檢測標(biāo)志位externintCrossflag;//交叉道檢測externcharstartlineflag;//起跑線檢測intSafeStopEnableFlag;//防撞允許標(biāo)志位,當(dāng)速度超過STOPSAFTMIN時(shí)啟動(dòng)允許externintTingcheflag;externintlocation;externintmodecount;externintforecast[15];externintwandaoflag;/*—---———————-—-——---——--——-—--—舵機(jī)參數(shù)-———-—-—--———--—---——-————-——*/intCarlocation=0;//小車位置intPiancha;intduojizhankong;intdirectionlost[3]={0,0,0};//方向偏差intduojiPcoe=0;intduojiIcoe=0;intduojiDcoe=0;intduojiLastError=0;intduojiPrevError=0;intduojiSumError=0;intduojiiError_max=0;/*—--—--———--—-—---—-——--——-—驅(qū)動(dòng)電機(jī)參數(shù)---—-—-————--————-—----——--—*/intI_Term_max=10; //積分限幅值intI_Term_min=—10; //積分限幅值intspeedset=50; //速度設(shè)定值,這里給的初值關(guān)系不大intspeedlost[3]={0,0,0}; //速度偏差intumotor=0; //上一次的電機(jī)電壓輸intuout=0;externintspeed; //當(dāng)前速度(可以不是全局變量,但為調(diào)試方便externintLINE_V;externintPWMMOTOTA;unsignedcharlastStrategy=1; //bang—bang為1,pid為0,初始為bang-bangintBANGBANGtoPID=10; //Bang-BangToPID閾值intPIDtoBANGBANG=10 ; //PIDToBang-Bang閾值intlast_PIDV;//最后一次PID增量intspeedzero; //用于撞車后停車intWAN_V=100;intmotorPcoe=0;intmotorIcoe=0;intmotorDcoe=0;intmotorLastError=0;intmotorPrevError=0;intmotorPrevPrevError=0;intmotorSumError=0;intmotoriError_max=0;intPIDmotor=0;//當(dāng)前馬達(dá)PID增量intmodeflag;//賽道模式/*--—--—-————-----——-——-————---舵機(jī)控制主程序—----—-——-—-—--—-——-——-—--*/ //函數(shù)返回值void//輸入?yún)?shù)void/*—----—--—--—-————------—--——-———----—--————-—-——-——-—--—-—-—-————--—--—-*/voidduojicontrol(void){Carlocation=SureLocationTest();//小車位置量 /*if(modeflag==3) { duojiPcoe=—65;duojiIcoe=0;duojiDcoe=0; }*/ //else //{ duojiPcoe=41;duojiIcoe=0;duojiDcoe=47; //}Piancha=PIDcalc1(duojiPcoe,duojiIcoe,duojiDcoe,Carlocation,duojiLastError,duojiPrevError,duojiSumError,duojiiError_max);duojizhankong=CENTRAL—Piancha;if(duojizhankong〈MOSTRIGHT)duojizhankong=MOSTRIGHT;if(duojizhankong>MOSTLEFT)duojizhankong=MOSTLEFT;PWMDTY01=duojizhankong;//占空比=((PWMPER-duojizhankong)/PWMPER)*100%}intspeedwanted(void){/*intwanted;if(Tingcheflag==1)return(0);else{wanted=LINE_V-(abs(Carlocation)/10)*(abs(Carlocation)/10);return(wanted);}調(diào)試禁止*/intspeedtemp;if(location〉=4)modeflag=2;elsemodeflag=1;if(modeflag==1)speedtemp=LINE_V;if(modeflag==2)speedtemp=LINE_V+65;return(speedtemp);}voidControlMotor(intuin){ //if(uin〉0) inttemp; temp=uin;PWMDTY23=temp; //PTM_PTM0=1;//電機(jī)正轉(zhuǎn) /*elseif(uin〈0) { PWMDTY23=uin; PTM_PTM0=0;}//倒車 else { PWMDTY23=0; PTM_PTM0=1; }*/}voidMotorControl(void){intbangzhi;intP_Term;intI_Term;intD_Term;speedset=20;speedlost[2]=speedlost[1]; speedlost[1]=speedlost[0]; speedlost[0]=(speedset-speed); /*if(lastStrategy) bangzhi=10; else bangzhi=PIDtoBANGBANG;//棒棒與pid之間的閾值*//* if(speedlost[0]〉10) { uout=UOUTMAX; speedlost[2]=0; speedlost[1]=0; lastStrategy=1; last_PIDV=0; } elseif(speedlost[0]〈—10) { uout=UOUTMIN; speedlost[2]=0; speedlost[1]=0; lastStrategy=1; last_PIDV=0; }//bangbang算法 else {*/ motorPcoe=200;motorIcoe=0;motorDcoe=0; /*uout=PIDcalc2(motorPcoe,motorIcoe,motorDcoe,speedset,speed,motorLastError,motorPrevError,motorPrevPrevError,motoriError_max); uout=last_PIDV+uout; uout=PWMMOTOT—uout; lastStrategy=0;*/ P_Term=motorPcoe*(speedlost[0]-speedlost[1]);//比例環(huán)節(jié); D_Term=motorDcoe*(speedlost[0]—2*speedlost[1]+speedlost[2]);//微分環(huán)節(jié) I_Term=motorIcoe*speedlost[0];//積分環(huán)節(jié) if(I_Term〉I_Term_max) //積分分離(限幅) I_Term=I_Term_max; elseif(I_Term〈I_Term_min) // 積分分離(限幅) I_Term=I_Term_min; PIDmotor=P_Term+I_Term+D_Term; last_PIDV=PIDmotor; uout=umotor+P_Term+I_T
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 胃竇部幽門螺桿菌活檢檢測情況的臨床分析
- 《似是故人來》訪談節(jié)目漢英交替?zhèn)髯g實(shí)踐報(bào)告
- 2024年江西景德鎮(zhèn)陶瓷大學(xué)招聘筆試真題
- 小班安全教育防中暑
- 二零二五年度大數(shù)據(jù)分析與應(yīng)用合伙投資協(xié)議書合同
- 2025年度校園兼職煮飯人員健康安全協(xié)議
- 2025年度餐飲行業(yè)廚師與餐飲集團(tuán)老板聯(lián)營合同
- 二零二五年度電商平臺(tái)收益共享合同
- 2025年度特色餐飲店員工勞動(dòng)聘請合同
- 2025年度電商平臺(tái)合同封面圖片設(shè)計(jì)及用戶體驗(yàn)提升合同
- 光伏工程施工組織設(shè)計(jì)
- 貫入法檢測砌體灰縫砂漿強(qiáng)度原始記錄
- 反激式開關(guān)電源電子數(shù)據(jù)表格(自帶公式)
- 物業(yè)二次裝修管理培訓(xùn)課件
- 城市雕塑藝術(shù)工程量清單計(jì)價(jià)定額2020版
- 2004年科龍電器案例分析
- 公司股權(quán)激勵(lì)方案(絕對(duì)干貨)PPT幻燈片課件(46頁P(yáng)PT)
- 土建工程監(jiān)理實(shí)施細(xì)則(完整版)
- 詩詞接龍(飛花令)PPT
- 肝癌的介入治療及護(hù)理ppt課件
- 竇桂梅刷子李教學(xué)《刷子李》教學(xué)設(shè)計(jì).doc
評(píng)論
0/150
提交評(píng)論