中级会员
- 积分
- 292
- 威望
- 270
- 贡献
- 16
- 兑换币
- 0
- 注册时间
- 2009-12-23
- 在线时间
- 3 小时
|
本帖最后由 chenrunshe_007 于 2010-1-23 14:07 编辑
- #include <hidef.h>
- #include <mc9s12dg128.h>
- #pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
- //=========================public variable=====================
- //-----------------------turning variable------------------
- unsigned char sam_g[15]; //道路检测值
- unsigned int angle_data; //舵机转角
- int car_positn; //赛车当前位置参数
- int pre_positn;
- unsigned int black_sensor_number; //检测到黑线的传感器个数
- int positn_temp[10];
- unsigned int t=0;
- //---------------------speed variable---------------------
- unsigned char dir_flag; //方向标志,为1表示检测到有效路径,可以给驱动力
- unsigned char brake_flag; //刹车标志位 判断当前是否需要刹车
- unsigned int car_driver; //驱动力控制
- unsigned int pulse_count; //速度检测 统计脉冲个数
- unsigned int ideal_speed; //车的理想速度
- unsigned int times; //丢失黑线的次数
- int speed_error; //理想与实际速度偏差值
- int pre_error; //速度PID 前一次的速度误差值 ideal_speed- pulse_count
- int pre_d_error; //速度PID 前一次的速度误差之差 d_error-pre_d_error
- int pk; //速度PID值
- //---------------------start_line variable-------------------
- unsigned char start_line_acc; //统计检测起跑线次数
- unsigned char finish_flag; //起跑线标志位,为1表示检测到起跑线3次
- //----------------------dis_play variable--------------------
- unsigned int start_flag,start_count;
- //---------------------table-------------------------
- unsigned char speed_table11[13]={270,260,250,240,200,180,180,180,170,140,140,100,90}; //15.0s
- unsigned char speed_table21[13]= {25,24,23,20,19,17,17,17,15,12,11,10,9}; //15.0s
- unsigned char speed_table12[13]= {29,28,27,26,25,20,20,20,19,17,15,10,9}; //15.0s
- unsigned char speed_table22[13]= {27,26,25,24,20,18,18,18,17,14,14,10,9}; //15.0s
- unsigned int circle; //控制赛车跑几圈停车
- #define kp 2000//2000
- #define ki 5//5
- #define kd 10//10
- #define Angle_Center 4344 //舵机中心位置
- #define lose_limit 30000 //丢失黑线后 滑翔时间
- void data_init(void);
- void crg_init(void); // 锁相环初始化
- void pwm_init(void); // PWM信号初始化
- void ect_init(void); // ECT初始化
- void sam_position(void); //读结果
- void check_start(void); //起跑线检测函数
- void car_position(void); //计算car_positn
- void angle(void); //计算转角
- void speed(void); //计算速度
- void driver(void); //驱动
- void pre_start(void);
- void delay(void);
- void found_start(void);
- void stop(void);
- void pid(void);
- unsigned int absolute(int);
- //========================main loop============================
- void main(void)
- {
- data_init(); //设置基本数据
- crg_init(); //锁向环初始化
- ect_init(); //ECT
- pwm_init(); //初始化PWM
- pre_start();
- EnableInterrupts;
- for(;;)
- {
- sam_position(); //读采样值
- check_start(); //检测起跑线
- car_position(); //计算car_positn
- angle(); //计算转角
- speed(); //计算速度
- driver(); //拐弯 驱动
- }
- }
- //--------------------data_init--------------------
- void data_init(void)
- {
- start_line_acc=0;
- finish_flag=0;
- DDRA=0X00;
- DDRB=0X00;
- times=0;
- }
- //-------------------pre_start------------------
- void pre_start(void)
- {
- unsigned int i;
- PWMDTY01=Angle_Center;
- PWMDTY67=0;
- for(i=0;i<10;i++) delay();
- PWMDTY23=0;
- }
- //----------------------crg_init-------------------
- void crg_init(void)
- {
- SYNR=0x02;
- REFDV=0x01;
- while((CRGFLG & 0x08)==0 );
- CLKSEL =0x80;
- }
- //--------------------pwm_init------------------------
- void pwm_init(void)
- {
- PWMCTL=0xB0; // 设置通道76、32、10级连
- PWME=0x00; // 通道禁止输出;
- PWMPRCLK=0x12;//预分频:A_CLK=busclk/2^2=6M B_CLK=BUSCLK/2^1=12M
- PWMSCLA=0x01; //SA_CLK=A_CLK/(2*1)==3MHz
- PWMSCLB=0X01; //SB_CLK=B_CLK/(2*1)==6MHz
- PWMPOL=0x8A; //极性选择起始为高电平;
- PWMCLK=0x8A; //PWM01 选择 SA_CLK PWM23 67选择SB_CLK
- PWMCNT0=0x00;
- PWMCNT1=0x00;
- PWMCNT2=0x00;
- PWMCNT3=0x00;
- PWMCNT6=0x00;
- PWMCNT7=0x00;
- PWMPER01=60000; // 周期==(1/3M)*(60000)=20ms
- PWMPER23=10000; // F=6M/10000==600Hz
- PWMPER67=10000; // F=6M/10000==600Hz
- PWMCAE=0x00; //左对齐方式
- PWME=0x82; // 通道1,7输出使能;
- }
- //-----------------------ect_init-------------------------
- void ect_init(void)
- {
- TCTL4=0x01; // Set the rising endge for PT0.
- PACN10=0x0000;
- PBCTL=0x40; //pt0 and pt1 级联成16位计数器
- MCCNT=60000; //60000*24M/16=40ms
- MCCTL=0xC7;
- TSCR1=0x10;
- }
- void sam_position(void)
- {
- sam_g[1]= PORTA_PA4;
- sam_g[2]= PORTA_PA3;
- sam_g[3]= PORTA_PA2;
- sam_g[4]= PORTA_PA1;
- sam_g[5]= PORTA_PA0;
- sam_g[6]= PORTB_PB0;
- sam_g[7]= PORTB_PB1;
- sam_g[8]= PORTB_PB2;
- sam_g[9]= PORTB_PB3;
- sam_g[10]= PORTB_PB4;
- sam_g[11]= PORTB_PB5;
- sam_g[12]= PORTB_PB6;
- sam_g[13]= PORTB_PB7;
- }
- //----------------------check_start---------------------
- void check_start(void)
- {
- unsigned char i,j=0;
- start_flag=0;
- for(i=1;i<14;i++)
- if(sam_g^sam_g[i+1])
- j++;
- if(j>=4)
- {
- if(sam_g[5] &&((!sam_g[4])&&(!sam_g[6])) &&((!sam_g[3])&&(!sam_g[7]))&&(sam_g[1]&&sam_g[10])
- ) start_flag=1;
- else if(sam_g[5]&&sam_g[6] &&((!sam_g[4])&&(!sam_g[7]))&&((!sam_g[3])&&(!sam_g[8])) &&(sam_g[1]&&sam_g[10])
- ) start_flag=1;
- else if( sam_g[6] &&((!sam_g[5])&&(!sam_g[7])) &&((!sam_g[4])&&(!sam_g[8])) &&(sam_g[1]&&sam_g[11])
- ) start_flag=1;
- else if( sam_g[6]&&sam_g[7] &&((!sam_g[5])&&(!sam_g[8])) &&((!sam_g[4])&&(!sam_g[9])) &&(sam_g[2]&&sam_g[11])
- ) start_flag=1;
- else if( sam_g[7] &&((!sam_g[6])&&(!sam_g[8])) &&((!sam_g[5])&&(!sam_g[9])) &&(sam_g[2]&&sam_g[12])
- ) start_flag=1;
- else if( sam_g[7]&&sam_g[8] &&((!sam_g[6])&&(!sam_g[9])) &&((!sam_g[5])&&(!sam_g[10])) &&(sam_g[3]&&sam_g[12])
- ) start_flag=1;
- else if( sam_g[8] &&((!sam_g[7])&&(!sam_g[9])) &&((!sam_g[6])&&(!sam_g[10])) &&(sam_g[3]&&sam_g[13])
- ) start_flag=1;
- else if( sam_g[8]&&sam_g[9]&&((!sam_g[7])&&(!sam_g[10])) &&((!sam_g[6])&&(!sam_g[11])) &&(sam_g[4]&&sam_g[13])
- ) start_flag=1;
- else if( sam_g[9] &&((!sam_g[8])&&(!sam_g[10])) &&((!sam_g[7])&&(!sam_g[11])) &&(sam_g[4]&&sam_g[13])
- ) start_flag=1;
- }
- if(start_flag)
- art_count++;
- else
- art_count=0;
- if(start_count==2)
- {
- found_start();
- start_count=0;
- }
- if(start_line_acc==2)
- {
- finish_flag=1;
- }
- }
- //--------------------------car_position------------------------
复制代码 |
|