void timeCycle(void) { //计算绝对角度 E1_lastAbsPulseCnt=E1_absPulseCnt; E1_absPulseCnt= E1_pulseCnt%4000; if (E1_absPulseCnt>2000) E1_absPulseCnt-=4000; else if(E1_absPulseCnt<-2000) E1_absPulseCnt+=4000; //每毫秒定时任务 if(nowTime!=TimeBase) { nowTime = TimeBase; E1_pulseInMs = E1_absPulseCnt- E1_pulseInLastMs; E1_pulseInLastMs = E1_absPulseCnt; E2_interval=E2_pulseCnt-E2_lastPulseCnt; E2_lastPulseCnt=E2_pulseCnt; // if (nowTime%5==0) // { // UART_sendlong(UCA1,E1_pulseCnt+1000000L); // UART_sendstr(UCA1," "); // UART_sendlong(UCA1,E2_pulseCnt+1000000L); // UART_sendstr(UCA1," "); // } if ((!UCA1_GET_CHAR(&command))||(abs(E2_pulseCnt)>10000)) { Motor_config(0); while(1); } } }
//基本任务2 unsigned char function_2(void) { timeCycle(); Motor_config(750); while(nowTime+100>TimeBase); Motor_config(-200); while(nowTime+1000>TimeBase) { timeCycle(); if (!E1_absPulseCnt) { Motor_config(0); return 0; } } Motor_config(0); return 1; }
//基本任务1 unsigned char function_1(void) { Motor_config(400); while(1) { timeCycle(); if (E2_pulseCnt>48) { Motor_config(-400); } else if(E2_pulseCnt<-48) { Motor_config(400); } if (abs(E1_absPulseCnt)<1000) { Motor_config(0); return 0; } } }
void Traction_init() { P1DIR &=~BIT0; P1DIR &=~BIT1; P1DIR &=~BIT2; P1DIR &=~BIT3; P1IES |= BIT0; P1IES &=~BIT1; P1IES &=~BIT2; P1IES &=~BIT3; P1IFG=0; // P1IE |=BIT0+BIT1+BIT2+BIT3; Motor_config(LEAST+200,LEAST+200,LEAST+200,LEAST+200); }
__interrupt void P1ISR(void) { switch(P1IV) { case 0: break; case 2: if ((xianshi!=0)&&(xianshi+250>TimeBase)) { return; } if (P1IN & BIT1) { Motor_config(0,0,0,0); // while(1); } else { Motor_config(LEAST+300,LEAST+300,LEAST,LEAST); if (turn!=10) { turn=10; } } break; case 4: if (P1IN & BIT0) { Motor_config(LEAST,LEAST,LEAST+300,LEAST+300); if (turn!=5) { turn=5; xianshi=TimeBase; } } else { Motor_config(0,0,0,0); // while(1); } break; /* case 6: if ((xianshi!=0)&&(xianshi+250>TimeBase)) { return; } Motor_config(LEAST+150,LEAST+150,LEAST-250,LEAST-250); if (turn!=-5) { turn=-5; } break; case 8: Motor_config(LEAST+300,LEAST+300,-LEAST,-LEAST); if (turn!=-10) { turn=-10; xianshi=TimeBase; } break; */ } }
unsigned char function_6(void) { while(1) { //计算绝对角度 E1_lastAbsPulseCnt=E1_absPulseCnt; E1_absPulseCnt= E1_pulseCnt%4000; if (E1_absPulseCnt>2000) E1_absPulseCnt-=4000; else if(E1_absPulseCnt<-2000) E1_absPulseCnt+=4000; //每毫秒定时任务 if(nowTime!=TimeBase) { nowTime = TimeBase; E1_pulseInMs = E1_absPulseCnt- E1_pulseInLastMs; E1_pulseInLastMs = E1_absPulseCnt; E2_interval=E2_pulseCnt-E2_lastPulseCnt; E2_lastPulseCnt=E2_pulseCnt; // if (nowTime%5==0) // { // UART_sendlong(UCA1,E1_pulseCnt+1000000L); // UART_sendstr(UCA1," "); // UART_sendlong(UCA1,E2_pulseCnt+1000000L); // UART_sendstr(UCA1," "); // } if ((!UCA1_GET_CHAR(&command))||(abs(E2_pulseCnt)>10000)) { Motor_config(0); while(1); } } if ((abs(E1_absPulseCnt)<500)&&(abs(E1_absPulseCnt)>2)) { RP.inAuto=1; // PID_setMode(&RP,AUTOMATIC); if(!PID_compute(&RP)) { speed+=RP.myOutput; speed-=E2_interval; if (speed>0) speedSign = 1; else if (speed<0) speedSign = -1; else speedSign = 0; if (abs(speed)>900) speed=speedSign*900; if (speedSign>0) Motor_config(speedSign*400+speed/3+10); else Motor_config(speedSign*400+speed/3+10); // if (RP.myOutput>0) // Motor_config(450+RP.myOutput/3-E2_pulseCnt/2); // else if (RP.myOutput<-0) // Motor_config(-450+RP.myOutput/3-E2_pulseCnt/2); // else // Motor_config(0); } } else { speed=0; speedSign=0; Motor_config(0); RP.inAuto=0; // PID_setMode(&RP,MANUAL); } } }