巡线车程序(完整版) 1 #ifndef _Macro.h_ 2 #define _Macro.h_ 3 #include
__delay_cycles((long)(CPU_F*(double)x/1000000.0)) 12 13 #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) 14 #define PC 20 // 比例放大系数 15 #define IC 0 //积分放大系数 16 #define DC 85 //大系数 17 #define LEFTOUT TACCR1 18 #define RIGHTOUT TACCR2 19 #define SensorIn P5IN 20 #define F 5000//5000hz 21 #define Period (8000000/F) 22 #define EnableLeftPos P3OUT|=BIT1 23 #define UnenableLeftPos P3OUT&=~BIT1 24 25 #define EnableLeftNeg P3OUT|=BIT0 26 #define UnenableLeftNeg P3OUT&=~BIT0 27 28 #define EnableRightPos P3OUT|=BIT2 29 #define UnenableRightPos P3OUT&=~BIT2 30 31 #define EnableRightNeg P3OUT|=BIT3 32 #define UnenableRightNeg P3OUT&=~BIT3 33 34 #define Basic_Left 100//百分之八十 35 #define Basic_Right 100//Basic_Left 36 #define MAX (100) 37 #define MIN (-100) 38 #define foreward 1 39 #define backward 0
40 #define max_speed 100 41 #define min_speed -100 42 #define key 0 43 44 45 #define left_1 1 46 #define left_2 2 47 #define left_3 3 48 #define left_4 4 49 #define left_5 5 50 #define left_6 6 51 #define left_7 7//右直角 52 53 #define right_1 -1 54 #define right_2 -2 55 #define right_3 -3 56 #define right_4 -4 57 #define right_5 -5 58 #define right_6 -6 59 #define right_7 -7//左直角 60 #endif
[cpp]view plaincopy 61 #include \62 #include \63 void Motorstop() 64 { 65 LEFTOUT=0; 66 RIGHTOUT=0; 67 } 68 void MotorLeft(int speed,int direction) 69 { 70 if(speed>max_speed)speed=max_speed; 71 if(direction==backward)//反转 72 { 73 EnableLeftNeg; 74 UnenableLeftPos; 75 }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 else if(direction==foreward)//正转 {
EnableLeftPos; UnenableLeftNeg; }
LEFTOUT=Period/100*speed; }
void MotorRight(int speed,int direction) {
if(speed>max_speed)speed=max_speed;
if(direction==backward)//反转 {
EnableRightNeg; UnenableRightPos; }
else if(direction==foreward)//正转 {
EnableRightPos; UnenableRightNeg; }
RIGHTOUT=Period/100*speed; }
void MotorDrive(int PIDout) {
int speedleft,speedright; speedleft=Basic_Left PIDout; speedright=Basic_Right-PIDout;
if(speedleft<0)
MotorLeft(speedleft,backward);//反转 else MotorLeft(speedleft,foreward);//正转
if(speedright<0)
MotorRight(speedright,backward);//反转 else MotorRight(speedright,foreward);//正转 }
void Rangle(float angle) {
// TBCTL|=TBCLR;
TBCCR1=LMAX (unsigned int)(angle*one);