近十个学校的智能小车比赛程序(无保留),要的来拿

近十个学校的比赛程序(无保留),你懂得。非常经典,做智能车的不容错过噢。


好的内容都是回复可见的




程序1.
  1. #include <hidef.h>      /* common defines and macros */
  2. #include <mc9s12dg128.h>     /* derivative information */
  3. #pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
  4. /*************************************************************
  5. 摄像头数据
  6. ***************************************************************/
  7. unsigned int AbsoluteTime; // 系统时钟,用于闭环调速
  8. unsigned int Picture1[10][4]={{600,600,0,40},{600,600,0,60},{600,600,0,80},
  9.                               {600,600,0,100},{600,600,0,120},{600,600,0,140},
  10.                               {600,600,0,160},{600,600,0,180},{600,600,0,200},
  11.                               {600,600,0,220}};
  12. unsigned int Picturewrflag[10];    //图像数据真伪判别标志
  13. int PictureCoordinate[10]={0,0,0,0,0,0,0,0,0,0};//图像坐标
  14. unsigned int FieldCNT;
  15. int RowCNT;
  16. int CapFlag;
  17. long coordinate1,coordinate2,line_wideth;
  18. int se = 0;
  19. int PMCNT,NextPMCNT;
  20. int curvature;//曲率
  21. int ServoMotorPWM;
  22. int i;
  23. int TurningLine;
  24. /*********************************************************************
  25. PID数据
  26. ***********************************************************************/
  27. static int CurrentSpeed=0,DstSpeed=90;
  28. static int waittime=0;
  29.         int speed_0=0;
  30.         int e=0,e_1=0;
  31.         int ie=0;
  32.         int Kp=35;
  33.         int Kd=0;
  34.         int Ki=10;//5;
  35.         int PACN0_i;
  36.         int SpeedMin=90,SpeedMax=400;
  37.         char a;
  38. /***********************************************************************
  39. 路径记忆数据
  40. ************************************************************************/
  41. int length=0;
  42. int bendstraightMemory[100];//直道弯道数据存储
  43. /*********************************************************************
  44. 摄像头函数
  45. *********************************************************************/
  46. void ETCInit(void);
  47. void CCDInit(void);
  48. void PWMInit(void);
  49. void SystemInit(void);
  50. void PictureDispose(void);
  51. /*********************************************************************
  52. PID函数
  53. **********************************************************************/
  54. void SpeedPID(void);
  55. static void RTIInit(void);
  56. void SetSpeed(void);
  57. /***********************************************************************
  58. 路径记忆函数
  59. ************************************************************************/
  60. void JourneyMemory(void);
  61. /***********************************************************************/
  62. void main(void) {
  63.         DisableInterrupts;
  64.         SystemInit();       
  65.         EnableInterrupts;   
  66.   for(;;) {
  67.   EnableInterrupts;
  68.   JourneyMemory();
  69.   PictureDispose();
  70.   }
  71. }
  72. /************************************************************************
  73. 系统初始化区域
  74. **************************************************************************/
  75.   void SystemInit(void)
  76. {        AbsoluteTime = 0; // 初始化系统时钟
  77.         DDRB = 0xff; // 初始化PORTB,用于数据输出
  78.         FieldCNT = 0; // 场计数器初始化
  79.         ETCInit();
  80.         PWMInit();
  81.         RTIInit();
  82.   }
  83.   static void RTIInit(void) {
  84.   /* setup of the RTI interrupt frequency */
  85.   /* adjusted to get 1 millisecond (10.24 ms) with 16 MHz oscillator */
  86.   RTICTL = 0x64; /* set RTI prescaler */
  87.   CRGINT = 0x80; /* enable RTI interrupts */  
  88.   }
  89.   void ETCInit(void)
  90.   {   
  91.    TCTL4_EDG0B=0;
  92.    TCTL4_EDG0A=1; //0 channel Capture on rising edges only
  93.    TCTL3_EDG7B=1;
  94.    TCTL3_EDG7A=0; //7 channel Capture on falling edges only
  95.    TIOS_IOS0=0; //0 channel input compare
  96.    TIOS_IOS7=0; //7 channel input compare  
  97.         TIOS_IOS4 = 0; // 视频输入捕获
  98.         TIOS_IOS5 = 0; // 行同步
  99.         TIOS_IOS6 = 0; // 场同步
  100.         TSCR1_TEN = 1; // 使能计数
  101.         TSCR1_TFFCA = 1; // 捕获中断、比较中断标志位通过读取数据和写入数据清除
  102.         PACTL=0x40;   //16位累加器A使能 ,下降沿工作
  103. PBCTL_PBEN=1;//16位累加器B使能
  104.         TC4 = TC4; // 清除中断标志位
  105.         TC5 = TC5;
  106.         TC6 = TC6;
  107.         CCDInit();
  108.   }
  109.   **********************************
  110. 函数名称:CCDInit
  111.   功能描述:图像空间初始化
  112.   ********************************
  113.  
  114.   void PWMInit(void) {
  115.         PWME = 0x00;              // 禁止所有的PWM输出通道
  116.         PWMPOL = 0xFF;     // 所有的PWM输出高电平有效
  117.         PWMCLK = 0x00;         // Clock A && Clock B is the clock source for PWM channel 0~7
  118.         PWMPRCLK = 0x03;         // bus clock:24M;Clock A--8分频,Clock B--0分频
  119.         PWMCAE = 0x00;         // 所有的PWM输出为左对齐方式输出
  120.         PWMCTL = 0xFF;         // 将两个8位的PWM输出通道组合成一个16位的PWM输出通
  121.         PWMCNT01 = 0xFF;         // PWM计数器清零
  122.         PWMCNT23 = 0xFF;
  123.         PWMCNT45 = 0xFF;
  124.         PWMCNT67 = 0xFF;
  125.         PWMPER01 = 60000; // 驱动前轮舵机,周期20ms,频率50Hz
  126.         PWMPER23 = 2400; //周期0.1ms,频率10kHz
  127.         PWMPER67 = 2400;
  128.         // 设置占空比初始值
  129.         PWMDTY01 = 4200//舵机要求PWM高电平宽度0.5ms~2.5ms对应计数器数值为3800~4450~5150右
  130.         PWMDTY23 =1660;
  131.         PWMDTY67 =0;               
  132.         PWME = 0xFF;         // 所有PWM通道使能
  133.         }
  134.  
  135.   
  136.   
  137.   
  138.   
  139.          EnableInterrupts;
  140.    }
  141. /**************************************************************************
  142. 速度PID控制区域
  143. ***************************************************************************
  144.   interrupt void Real_Time_Interrupt(void)
  145.   {
  146.         AbsoluteTime++; // 运行系统时钟
  147.   SpeedPID();   
  148.   PORTB=(char)(PACN10);
  149.         CRGFLG = 0x80;         // 清中断标志
  150.                 EnableInterrupts;
  151.   }
  152. /*******************************************************************/
  153.   void SpeedPID(void)
  154.   {
  155.    PACN0_i=PACN10;
  156.           CurrentSpeed = PACN0_i;//码盘输入引脚PT0
  157.         PACN10=0;
  158.         e_1=e;
  159.         e=CurrentSpeed-DstSpeed;//-CurrentSpeed;
  160.         if(e<-20){
  161.         PWMDTY67 =0;          
  162.         PWMDTY23 =2400;
  163.         }
  164.         else if(e>20){
  165.         PWMDTY23=0;
  166.         PWMDTY67=2400;
  167.         }
  168.         else
  169.         {
  170.         ie+=e;
  171.         speed_0=speed_0-(Kp*e/10)-(int)(Ki*ie/1000)+Kd*(e-e_1);
  172.   SetSpeed();
  173.         }
  174.   }
  175.   void SetSpeed(void)
  176.   { if(speed_0>2400)
  177.   speed_0=2400;
  178.         PWMDTY67 =0;          
  179.         PWMDTY23 =speed_0;         
  180.   }
  181.  
  182.  
复制代码

 
 
 

文件到原文下载,原文出自:https://bbs.usoftchina.com/thread-205436-1-1.html

  • 34
    点赞
  • 295
    收藏
    觉得还不错? 一键收藏
  • 67
    评论
评论 67
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值