1、巡线小车源程序( MSP430,PID)首先是各种宏定义的头文件cppview plaincopy “myhead.h“ #ifndef xxxx_xxx #define xxxx_xxx #define P 43 #define I 0.01 #define D 100 #define LeftMotorTurnNeg P3OUT|=BIT0;P3OUT #define LeftMotorTurnPos P3OUTP3OUT|=BIT1; #define RightMotorTurnNeg P3OUT|=BIT3;P3OUT #define RightMotorTurnPos P3OUTP3
2、OUT|=BIT2; #define LeftPwmOut TACCR1 #define RightPwmOut TACCR2 #define F 10000 #define T (8000000/F) #define BASIC 100 #define simpletime (unsigned int)5 #define LEFT 100 #define RIGHT 100 #endif 接下来就是点击驱动的头文件 cppview plaincopy “Motor.h“ #ifndef Motor #define Motor #include “myhead.h“ #include “msp
3、430x14x.h“ void MotorLeft(float left) LeftMotorTurnPos/正转 if(left100)left=100; if(left100)right=100; if(right=0;i-); while(IFG1 BCSCTL2=SELM_2+DIVM_0+SELS+DIVS_0;/SMCLK=MCLK=XT2;DIV=0; float PID_Cal(float error) float derror=error-lasterror;/微分 df sumerror+=error;/积分 lasterror=error; return P*error+
4、I*sumerror+D*derror; float Sensor_Read()/左边传感器压线往左偏 static float ans=0; float sum=0; int i,sensor=0,num=0; sensor=P5IN;/3.7 5.7 5.6 5.5.5.0 sensor=(P3IN for(i=0;i9;i+) if(!(sensor num+; if(num!=0 if(ans0) P6OUT=0x80; P6OUT|=(int)(-(ans-0.5); else P6OUT=(int)(ans+0.5); return ans;/运行找正常 void AvoidDet
5、ection(float left,float right)/壁障函数 int sensor=0; MotorLeft(left); MotorRight(right); delay(200); do MotorLeft(right); MotorRight(left); sensor=P3IN/3.7 5.7 5.6 5.5.5.0 sensor=(sensor1)|P5IN; while(sensor=0x1ff);/当在空白的时候,一只往里面拐。 void main( void ) / Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; Crystal(); IO_inti(); timer_inti(); PID_inti(); P6OUT=0XF0; while(1) if(flag) DriveMotor(PID_Cal(Sensor_Read(); #pragma vector=TIMERB0_VECTOR _interrupt void timer_bo() flag=!flag; #pragma vector=PORT1_VECTOR _interrupt void port_1() AvoidDetection(LEFT,RIGHT);