⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 last.txt

📁 飞思卡儿智能汽车竞赛主程序 包含刹车程序和pmw模块程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/*-------------------------------------------------------------------------------------------------------*-

  main.c(v1.01)
  
  -------------------------------------------------------------------------------------------------------
  
  -this program is used for controlling cruisecar.
  
-*-------------------------------------------------------------------------------------------------------*/

  #include <hidef.h>             /* common defines and macros */
  #include <mc9s12dg128.h>       /* derivative information */
  #pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
  
/*------------------------------------public variable----------------------------------------------------*/
           int mk;
           int ek;
           int ek_1;           
           int ppk;
           int pik;
           int pdk;
           int pik_1;
           int pk;
           int pk_1;
           
         
  unsigned int check_v;//小车规定速度(即我们要求小车所跑的速度)
  byte sam_atd_g[15];   //道路检测值
  unsigned int table[29];   //方向表
  byte front1;          //转向值1
  byte front;           //转向值
  unsigned  int cnt1;//直线判断累加位   
  unsigned int cnt2;//左弯道判断累加位
  unsigned int cnt3;//右弯道判断累加位
  byte flag1;//直道判断标志位
  byte flag2;//左弯道判断标志位
  byte flag3;//右弯道判断标志位
  unsigned int bend_fl;//弯道刹车标志及累加位
  byte wait;//WAIT作用在于使小车在进入弯道刹车以后但未经过直道前不再刹车
  unsigned int dripwm;//PID内用的上限值
  unsigned int dripwm1;//PID内用的下限值
  unsigned int clock;//刹车延时累加向量
  byte clock_f;//是否进入刹车延时标志
  unsigned int strait[3];//直线各区域传感器所对应速度
  unsigned int bend[5];//弯道各区域传感器所对应速度
  int clock_limit;//刹车延时上限值
  //int kp;
  //int ki;
  //int kd;
/*_____________________________________________________________________________________________________*/    
  #define kp 30
  #define ki 1
  #define kd 3
  #define limit1 153  //电压比较值(即黑线电压大于此值,白线小于此值)
  #define uplimit 224
  #define downlimit 20
  //#define strait 16
  //#define bend 13

  
/*.......................................................................................................*/

/*.......................................................................................................*/ 
     void crg_init(void);   //锁相环初始化 notes:busclock is 20MHz.// the init function is used for setting core clock(40MHz).        
     void atd_init(void); // AD转换初始化the init function is prepare for using ATD_sub_block. // notes:       
     void pwm_init(void);//PWM信号初始化 the init function is prepare for using PWM_sub_block.       
     void rti_init(void);     // the init function is prepare for using RTI_sub_block.
     void ect_init(void);//记数初始化 the init function is prepare for using ECT_sub_block.
     void table_init(void);//方向表初始化函数
     void chang_f(void); //方向改变函数
     void c_comp(void);  //方向判断函数
     void sam_position(void);  //道路数据采集函数
     void check_bend(void);// 直道检测
     void check_bend1(void);//左弯道检测
     void check_bend2(void); //右弯道检测
     void pid_c(unsigned int v);  //PID控制函数
     void c_comp1(void);//方向判断函数1(刹车时用)
     void check_va(void);//速度值选择位函数   
/*------------------------------------------MAIN---------------------------------------------------------*/     
     void main(void)
  {
       DDRA=0x00;
       //strait=17;
       //bend=12;
       flag1=1;
       bend_fl=1;
       check_va();
       table_init();              
       crg_init();// the function crg_init() set the frequence of MCU core is 40MHz.                              
       atd_init(); // the function atd_init() is used for preparing for ATD sampling.                               
       pwm_init();        
       ect_init();        
       EnableInterrupts 
                                     
       for(;;)
       {
         
           if(flag2==1||flag3==1){     //如果检测到弯道则刹车     
              if(bend_fl==2){            //如果刹车标志为2则刹车
                  if(mk>=11){  //如果速度大于某值则刹车 
                       if(wait==0){
                       PWMDTY7=0x00;
                       clock_f=1;                                                       
                      }  
                  }  
              }
           }
          sam_position();
          if(clock_f==1){
            if(clock<clock_limit){
            c_comp1(); 
            }else{
            clock=0;                                                       
            clock_f=0;
            bend_fl=3;
            wait=1;
            }
          }
          if(clock_f==0){
          c_comp(); 
          }
      }  
        
   }
/*-------------------------------------------------------------------------------------------------------*/
/*-------------------------------------------CRG_INIT-----------------------------------------------------*/

 // the function crg_init() set the frequence of MCU core is 40MHz.
 // notes: busclock is 20MHz.
       
 void crg_init(void) 
   {
         SYNR=0x02;                    // mcu core clock 40MHz(bus clock 20MHz).
         REFDV=0x01;
         while((CRGFLG & 0x08)==0 )    // wait for PLL clock stabilization
        {
         }
         CLKSEL |=0x80;                // select PLL clock.
    }

/*-------------------------------------------PWM_INIT-----------------------------------------------------*/

 // the function pwm_init() is used for preparing for the two motors work.   
 // notes: see port.h for details of pins connection.
 
  void pwm_init(void) 
    { 
         PWMCTL=0x40;      // select PWM(4)5,PWM7 channel.                  
         DDRP=0xFF;        // for PTP1,PTP2,PTP3 output enable.
         PTP=0x04;         // for PTP1(IN1)=0,PTP2(D2)=1,PTP3(D1)=0.
         PWMPRCLK=0x33;    // A_CLK(busclk/8).B_CLK(busclk/8)
         PWMSCLB=0x26;
         PWMSCLA=0x26;
         PWMPOL=0xA2;       // PWM45 select A_CLK,PWM7 select SB_CLK      
         PWMCLK=0x82;
         PWMCNT4=0x00;
         PWMCNT5=0x00;
         PWMCNT7=0x00;
         PWMCNT1=0x00;   
         PWMPER45=0x7530;  // init_period(20ms)
         PWMPER7=0xC3;
         PWMPER1=0xC3;
         PWMCAE=0x7D;
         PWMDTY7=0x00;
         PWMDTY1=0x80;
         PWMDTY45=2070;    // init_pulse(us)         
         PWME=0xA0;     // for PWM7,PWM(4)5 output enable.
    }
      
/*--------------------------------------------ATD_INIT-------------------------------------------------*/
  
  // the function atd_init() is used for preparing for sampling the information the front of sensor(15).     
 // notes: see port.h for details of pins connection.
 
    void atd_init(void)
    {
     __asm{
        LDAA #$C0
        STAA ATD0CTL2
        STAA ATD1CTL2
        LDAA #$02
        STAA ATD0CTL3
        STAA ATD1CTL3
        LDAA #$8A
		STAA ATD0CTL4
		STAA ATD1CTL4
		LDAA #$B0
      	STAA ATD0CTL5	      
        LDAA #$B0
        STAA ATD1CTL5
        }
    }
    
    
/*--------------------------------------------RTI_INIT--------------------------------------------------*/

 // the function rti_init() is used for preparing for supporting the real time interrupt information    
 //notes:
 
   void rti_init(void) 
   {
        RTICTL=0x71;    // 16MHz/1*2^16=244/sec(ATD sample rate).
        CRGINT |=0x80;  // enable rti.
   }
   
/*---------------------------------------------ECT_INIT---------------------------------------------------*/

  // the function ect_init() is used for prepariong for measuring speed of cruisecar.
  // notice:
     void ect_init(void)
     {
          __asm{
          LDAA #$01			 ;//;Set the rising endge for PT0设置PT0口作为上升沿有效
          STAA TCTL4       
     	  LDD  #$0000		 ;//;计数器清零
          STD  PACN10     
          LDAA  #$40		 ;//;Enable  the PACB  脉冲累加器使能
          STAA PBCTL                            
;/*--------------------------------------------------------------MDC--------------------------------------*/                        
          LDD  #60000		 ;//;load the value for timer 模减计数器清零
          STD  MCCNT           	
          LDAA #$07			; //;模减计数器控制寄存器设置
          STAA TSCR2                          
          LDAA #$F7			; //;Set the MDC.Interrupt enable;Set the mode for onece
          STAA MCCTL 		 ;//;Return the present value while read;MDC enable;16div  
          }      
     }
/*-------------------------------------------------TABLE_INIT--------------------------------------------*/
    void table_init(void){//the value of  chuangganqi 不同区域的传感器给予不同的转向值,且赋值间隔逐渐递减.
        int i;
      int value=2070;     // 小车转向中点时的赋值
      for(i=14;i>=10;i--){//-6*15
        table[i]=value;
        value-=20;
      }
       value=1950;
      for(i=9;i>=5;i--){  //-5*30
        table[i]=value;
        value-=40;
      }
      value=1750;
      for(i=4;i>=0;i--){   
        table[i]=value;
        value-=35;
      }
       value=2070;
      for(i=14;i<=18;i++){ //+6*15
        table[i]=value;
        value+=20;
      }
         value=2190;
      for(i=19;i<=23;i++){ //+5*30
        table[i]=value;
        value+=40;
      }
         value=2390;
         for(i=24;i<=28;i++){
          table[i]=value;
          value+=35;
         }
     }
/*____________________________________________check_va____________________________________________________*/
/*以PORTA即A口连接拨码开关来选择速度 */
    void check_va(void){
      int i;
      unsigned int value1;
      byte open; 
      open=PORTA;
      switch(open){
        case 0x00:
        clock_limit=22000;//刹车时间值
        value1=17;     //速度
        for(i=0;i<=2;i++){
           strait[i]=value1;
           value1--;
        }
        for(i=0;i<=4;i++){
             bend[i]=value1;
             value1--;
        }
        break;
        case 0x01:
        clock_limit=20000;
        value1=16;
        for(i=0;i<=2;i++){
           strait[i]=value1;
           value1--;
        }
        for(i=0;i<=4;i++){
             bend[i]=value1;
             value1--;
        }
        break;
        case 0x04:
        clock_limit=18000;
        value1=15;
        for(i=0;i<=2;i++){
           strait[i]=value1;
           value1--;
        }
        for(i=0;i<=4;i++){
             bend[i]=value1;
             value1--;
        }
        break;
        case 0x10:
        clock_limit=28000;
        value1=18;
        for(i=0;i<=2;i++){
           strait[i]=value1;
           value1--;
        }
        for(i=0;i<=4;i++){
             bend[i]=value1;
             value1--;
        }
        break;
        case 0x40:
        clock_limit=32000;
        value1=19;
        for(i=0;i<=2;i++){
           strait[i]=value1;
           value1--;
        }
        for(i=0;i<=4;i++){
             bend[i]=value1;
             value1--;
        }
        break;
        //case 0x01:strait=17;bend=11;break;
        //case 0x04:strait=16;bend=12;break;
        //case 0x10:strait=15;bend=12;break;
        //case 0x40:strait=15;bend=13;break;
        //case 0x05:strait=14;bend=12;break;
        /*case 0x11:kp=30;ki=2;kd=2;break;
        case 0x41:kp=30;ki=2;kd=4;break;        
        case 0x14:kp=20;ki=2;kd=5;break;
        case 0x44:kp=30;ki=3;kd=1;break;
        case 0x50:kp=30;ki=3;kd=2;break;
        case 0x51:kp=30;ki=3;kd=3;break;
        case 0x54:kp=30;ki=3;kd=4;break;
        case 0x55:kp=30;ki=0;kd=0;break; */
  }           
 } 
/*-----------------------------------------------C_COMP---------------------------------------------------*/
/*将临界电压(LIMIT1)与检测电压(SAM_ATD_G[I])比较,判断是哪个传感器处于黑线之上.通过小车位置来对
  小车速度作出调整(即改变CHECK_V,FRONT等的值) */
 void c_comp(void) 
     {
      if(limit1<sam_atd_g[0]){
         if(limit1<sam_atd_g[1]){
         check_v=strait[0];
                  
         check_bend();
         front=13;
         chang_f();
         }                                                 
         if(limit1<sam_atd_g[8]){
         check_v=strait[0];
                 
         check_bend();
         front=15;
         chang_f();
         }
         check_v=strait[0];
                 
         check_bend();
         front=14;
         chang_f();
        } else if(limit1<sam_atd_g[1]){
        if(limit1<sam_atd_g[2]){
        check_v=strait[1];
          
          check_bend();
          front=11;
          chang_f();
        }
        check_v=strait[1];
               
        check_bend();
        front=12;               
        chang_f();
        }else if(limit1<sam_atd_g[8]){
        if(limit1<sam_atd_g[9]){
        check_v=strait[1];
       
          check_bend(); 
          front=17;
          chang_f();
        }
        check_v=strait[1];
                
        check_bend();
        front=16;
        chang_f();
        }else if(limit1<sam_atd_g[2]){
        if(limit1<sam_atd_g[3]){
        check_v=bend[0];
        
        check_bend1();
        front=9; 
        chang_f();
        }
        check_v=strait[2];
        
        check_bend();
        front=10;
        chang_f();
        }else if(limit1<sam_atd_g[9]){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -