冠軍完美版 能順利運行
51hei.png (11.7 KB, 下載次數: 49)
下載附件
2020-10-25 02:52 上傳
單片機源程序如下:
- #include "..\config.h"
- #include "Mouse_Drive.h"
- /*********************************************************************************************************
- *當小車進入時,以小車的前方為north,右方為east,下方為south,左方為west.作為迷宮的絕對方向
- *對應已經定義的絕對方向: up,right,down,left.
- **********************************************************************************************************/
- #define NORTH 1
- #define EAST 2
- #define SOUTH 4
- #define WEST 8
- #define PASSED 16
- #define MARK 32
- #define SJUDGE 64
- #define SPECIAL 128
- //#define SEARCHSPEED 100 //95,100
- /*********************************************************************************************************
- 定義全局變量
- *********************************************************************************************************/
- INT8U GucMouseDir = UP; /* 保存電腦鼠當前方向 */
- MAZECOOR GmcMouse = {0,0}; /* 保存電腦鼠當前位置坐標 */
- INT8U GucMapBlock[MAZETYPE][MAZETYPE] = {0}; /* GucMapBlock[x][y] */
-
- static MOTOR GmRight = {MOTOR_STOP, MOTOR_GO_AHEAD, 0, 0, 0}; /* 定義并初始化右電機狀態 */
- static MOTOR GmLeft = {MOTOR_STOP, MOTOR_GO_AHEAD, 0, 0, 0}; /* 定義并初始化左電機狀態 */
-
- static volatile INT8U GucMouseState = STOP; /* 保存電腦鼠當前運行狀態 */
- static INT32U GulAccelTable[300] = {0}; //300 /* 電機加減速各階段定時器值 */
- static INT32S GlMaxSpeed = SEARCHSPEED; /* 保存允許運行的最大速度 */
- static volatile INT8U GucDistance[5] = {0}; /* 記錄傳感器狀態 */
- INT8U GWallMap[MAXSIZE];
- INT8U GDistanceMap[MAXSIZE]={255};
- INT8U GStartChange =0;
- static INT8U GLCoorflag =0; /* 允許在轉彎時,是否檢測左邊 */
- static INT8U GRCoorflag =0; /* 允許在轉彎時,是否檢測右邊 */
- INT8U BlankNum =0;
- INT8U GCurPosition =LEFT_START_NUM;
- static INT8U GSetflag =0;
- static INT8U zb=0,zs=0,zg=0;
- INT8U GEndNum =LEFT_END_NUM ; /*終點坐標*/
- INT8U GStartNum=LEFT_START_NUM ;
- static INT8U GConformflag =0; /* 終點坐標確認標志 */
- INT8U Gmazetype = 8;
- static INT8U GoLong = 0;
- INT8U straigtOneflag = 0;
- INT8U PreTurnRflag = 0;
- INT8U PreTurnLflag = 0;
- INT8U PreTurnBflag = 0;
- INT8U TurnRLflag = 0;
- INT8U GCountInfo[100]={0x87,0x81,0x41,0x81,0x41,0x81,0x41,0x81,0x41,0x81,
- 0x82,0x41,0x81,0x84,0,0x41,0x81,0x41,0x83,0x81,0x02,0};
- INT8U turnback_first_step=0;
- INT8U turnback_secend_step=0;
- INT8U first_step=0;
- INT8U Spurt_num_count =1;
- INT8U left_flag=0;
- INT8U right_flag=0;
- INT8U Lturnflag =0;
- INT8U Rturnflag =0;
- INT8U spurtflag=0;
- void SeveralStep(INT8U step);
- void updateCPostion(void);
- /*********************************************************************************************************
- ** Function name: myDelay
- ** Descriptions: 軟件延時
- ** input parameters: 延時時間,單位為us
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void delayMs(INT32U ulTime)
- {
- INT32U i;
- while (ulTime--) {
- for (i = 0; i < (FCCLK / 1000 / 3); i++);
- }
- }
- /*********************************************************************************************************
- ** Function name: lftMtCtrl
- ** Descriptions: 左步進電機驅動時序
- ** input parameters: GmLeft.cDir :電機運行方向
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void lftMtCtrl (void)
- {
- static INT8U ucRunStep = 0; /* 保存電機當前位置 */
-
- switch (GmLeft.cDir) {
- case MOTOR_GO_AHEAD: /* 向前步進 */
- if (++ucRunStep >= 8) {
- ucRunStep = 0;
- }
- break;
- case MOTOR_GO_BACK: /* 向后步進 */
- if (--ucRunStep >= 8) { /* 溢出 */
- ucRunStep = 7;
- }
- break;
- case MOTORPAUSE:
- break;
- default:
- break;
- }
-
- switch (ucRunStep) { /* 執行步進電機時序 */
- case 0:
- MT_LFT_STEP1_SET();
- break;
- case 1:
- MT_LFT_STEP2_SET();
- break;
- case 2:
- MT_LFT_STEP3_SET();
- break;
- case 3:
- MT_LFT_STEP4_SET();
- break;
- case 4:
- MT_LFT_STEP5_SET();
- break;
- case 5:
- MT_LFT_STEP6_SET();
- break;
- case 6:
- MT_LFT_STEP7_SET();
- break;
- case 7:
- MT_LFT_STEP8_SET();
- break;
- default:
- break;
- }
- }
- /*********************************************************************************************************
- ** Function name: rgtMtCtrl
- ** Descriptions: 右步進電機驅動時序
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void rgtMtCtrl (void)
- {
- static INT8U ucRunStep = 0; /* 保存電機當前位置 */
-
- switch (GmRight.cDir) {
- case MOTOR_GO_AHEAD: /* 向前步進 */
- if (--ucRunStep >= 8) { /* 溢出 */
- ucRunStep = 7;
- }
- break;
- case MOTOR_GO_BACK: /* 向后步進 */
- if (++ucRunStep >= 8) {
- ucRunStep = 0;
- }
- break;
- case MOTORPAUSE:
- break;
- default:
- break;
- }
-
- switch (ucRunStep) { /* 執行步進電機時序 */
- case 0:
- MT_RGT_STEP1_SET();
- break;
- case 1:
- MT_RGT_STEP2_SET();
- break;
- case 2:
- MT_RGT_STEP3_SET();
- break;
- case 3:
- MT_RGT_STEP4_SET();
- break;
- case 4:
- MT_RGT_STEP5_SET();
- break;
- case 5:
- MT_RGT_STEP6_SET();
- break;
- case 6:
- MT_RGT_STEP7_SET();
- break;
- case 7:
- MT_RGT_STEP8_SET();
- break;
- default:
- break;
- }
- }
- /*********************************************************************************************************
- ** Function name: lftMtSpeedCtrl
- ** Descriptions: 左電機速度調節
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void lftMtSpeedCtrl (void)
- {
- INT32S lDownPusle;
-
- lDownPusle = GmLeft.ulPulse - GmLeft.ulPulseCtr; /* 統計電機還剩余的步數 */
- if (lDownPusle <= GmLeft.lSpeed) {
- GmLeft.lSpeed--;
- } else { /* 非減速區間,則加速到最大值 */
- if (GmLeft.lSpeed < GlMaxSpeed) {
- GmLeft.lSpeed++;
- }
- }
- if (GmLeft.lSpeed < 0) { /* 設置速度下限 */
- GmLeft.lSpeed = 0;
- }
-
- TIMER0_LOAD_SET(GulAccelTable[GmLeft.lSpeed]); /* 設置定時時間 */
- }
- /*********************************************************************************************************
- ** Function name: rgtMtSpeedCtrl
- ** Descriptions: 右電機速度調節
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void rgtMtSpeedCtrl (void)
- {
- INT32S lDownPusle;
-
- lDownPusle = GmRight.ulPulse - GmRight.ulPulseCtr; /* 統計電機還剩余的步數 */
- if (lDownPusle <= GmRight.lSpeed) {
- GmRight.lSpeed--;
- } else { /* 非減速區間,則加速到最大值 */
- if (GmRight.lSpeed < GlMaxSpeed) {
- GmRight.lSpeed++;
- }/* else {
- GmRight.lSpeed--;
- }*/
- }
- if (GmRight.lSpeed < 0) { /* 設置速度下限 */
- GmRight.lSpeed = 0;
- }
- TIMER1_LOAD_SET(GulAccelTable[GmRight.lSpeed]); /* 設置定時時間 */
- }
- /*********************************************************************************************************
- ** Function name: __speedContrR
- ** Descriptions: 右電機速度調節
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void __speedContrR_C (void)
- {
- INT32S iDPusle;
- if(Lturnflag ||Rturnflag );
- // GmRight.iSpeed = 60;
- else
- {
- iDPusle = GmRight.ulPulse+30 - GmRight.ulPulseCtr; /* 統計電機還剩余的步數 */
- if (iDPusle <= GmRight.lSpeed) {
- GmRight.lSpeed--;
- } else { /* 非減速區間,則加速到最大值 */
- if ( GmRight.lSpeed < GlMaxSpeed)
- {
- GmRight.lSpeed++;
- }
- else
- {
- GmRight.lSpeed--;
- }
- }
- }
- if ( GmRight.lSpeed < 0) { /* 設置速度下限 */
- GmRight.lSpeed = 0;
- }
- //__GmRight.iSpeed = 50;////////////////////////////////調試添加/////////////////////
- TIMER1_LOAD_SET(GulAccelTable[GmRight.lSpeed]);
- }
- /*********************************************************************************************************
- ** Function name: __speedContrL
- ** Descriptions: 左電機速度調節
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void __speedContrL_C (void)
- {
- INT32S iDPusle;
- if(Lturnflag ||Rturnflag ); //??????
- else
- {
- iDPusle = GmLeft.ulPulse +30 - GmLeft.ulPulseCtr; /* 統計電機還剩余的步數 */
- if (iDPusle <= GmLeft.lSpeed) {
- GmLeft.lSpeed--;
- } else { /* 非減速區間,則加速到最大值 */
- if ( GmLeft.lSpeed < GlMaxSpeed) {
- GmLeft.lSpeed++;
- }
- }
- }
- if ( GmLeft.lSpeed < 0) { /* 設置速度下限 */
- GmLeft.lSpeed = 0;
- }
-
- // GmLeft.iSpeed = 50;////////////////////////////////調試添加/////////////////////
- TIMER0_LOAD_SET(GulAccelTable[GmLeft.lSpeed]); /* 設置定時時間 */
- }
- /*********************************************************************************************************
- ** Function name: timer0Isr
- ** Descriptions: Timer0中斷服務函數
- ** input parameters: GmLeft.cState :驅動步進電機的時序狀態
- ** GmLeft.cDir :步進電機運動的方向
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void timer0Isr(void)
- {
- static INT8S ucCnt1 = 0, ucCnt2 = 0; /* 修正姿態計數變量 */
- TIMER0_INT_CLR(); /* 清除中斷標志 */
- switch (GmLeft.cState) {
- case MOTOR_STOP: /* 停止,同時清零速度和脈沖值 */
- GmLeft.lSpeed = 0;
- GmLeft.ulPulse = 0;
- GmLeft.ulPulseCtr = 0;
- break;
- case WAITONE_STEP: /* 暫停一步 */
- GmLeft.cState = MOTOR_RUN;
- break;
- case MOTOR_RUN: /* 電機運行 */
- if (GucMouseState == GO_AHEAD) { /* 根據傳感器狀態微調電機位置 */
- if (GucDistance[__FRONTR] && /* 前右方有擋板 */\
- (GucDistance[__FRONTL]==0)) { /* 前左方無擋板 */
- if (ucCnt1 == 1) {
- GmLeft.cState = WAITONE_STEP; /* 使下一步暫停 */
- }
- if (++ucCnt1 > 3) { // 3步暫停一步
- ucCnt1 = 0;
- }
-
-
-
- } else {
- ucCnt1 = 0;
- }
- if ((GucDistance[__LEFT] == 1) && /* 左遠方有擋板 */\
- (GucDistance[__RIGHT] == 0)) { /* 右方無擋板 */
- if(ucCnt2 == 3) {
- GmLeft.cState = WAITONE_STEP; /* 使下一步暫停 */
- }
- if (++ucCnt2 > 6) { // 6步暫停一步
- ucCnt2 = 0;
- }
-
-
-
- } else {
- ucCnt2 = 0;
- }
- }
- lftMtCtrl(); /* 電機驅動程序 */
- break;
- default:
- break;
- }
- /*
- * 是否完成任務判斷
- */
- if (GmLeft.cState != MOTOR_STOP) {
- GmLeft.ulPulseCtr++; /* 運行脈沖計數 */
- if(spurtflag)
- __speedContrL_C(); /* 電機驅動程序 */
- else
- lftMtSpeedCtrl(); /* 速度調節 */ /* 速度調節 */
- if (GmLeft.ulPulseCtr >= GmLeft.ulPulse) {
- GmLeft.cState = MOTOR_STOP;
- GmLeft.ulPulseCtr = 0;
- GmLeft.ulPulse = 0;
- GmLeft.lSpeed = 0;
- }
- }
- }
- /*********************************************************************************************************
- ** Function name: timer1Isr
- ** Descriptions: Timer1中斷服務函數
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void timer1Isr(void)
- {
- static INT8S ucCnt1 = 0, ucCnt2 = 0; /* 修正姿態計數變量 */
- TIMER1_INT_CLR(); /* 清除中斷標志 */
-
- switch (GmRight.cState) {
- case MOTOR_STOP: /* 停止,同時清零速度和脈沖值 */
- GmRight.lSpeed = 0;
- GmRight.ulPulse = 0;
- GmRight.ulPulseCtr = 0;
- break;
- case WAITONE_STEP: /* 暫停一步 */
- GmRight.cState = MOTOR_RUN;
- break;
- case MOTOR_RUN: /* 電機運行 */
- if (GucMouseState == GO_AHEAD) { /* 根據傳感器狀態微調電機位置 */
- if (GucDistance[__FRONTL] && /* 前左方有擋板 */\
- (GucDistance[__FRONTR]==0)) { /* 前右方無擋板 */
- if (ucCnt1 == 1) {
- GmRight.cState = WAITONE_STEP; /* 使下一步暫停 */
- }
- if (++ucCnt1 > 3) { // 3步暫停一步
- ucCnt1 = 0;
- }
- } else {
- ucCnt1 = 0;
- }
- if ((GucDistance[__RIGHT] == 1) && /* 右遠方有擋板 */\
- (GucDistance[__LEFT] == 0)) { /* 左方無擋板 */
- if(ucCnt2 == 3) {
- GmRight.cState = WAITONE_STEP; /* 使下一步暫停 */
- }
- if (++ucCnt2 > 6) { //6步暫停一步
- ucCnt2 = 0;
- }
- } else {
- ucCnt2 = 0;
- }
- }
- rgtMtCtrl(); /* 推動電機轉動一步 */
- break;
- default:
- break;
- }
- /*
- * 是否完成任務判斷
- */
- if (GmRight.cState != MOTOR_STOP) {
- GmRight.ulPulseCtr++; /* 運行脈沖計數 */
- if(spurtflag)
- __speedContrR_C(); /* 速度調節 */
- else
- rgtMtSpeedCtrl(); /* 速度調節 */ /* 速度調節 */
- if (GmRight.ulPulseCtr >= GmRight.ulPulse) {
- GmRight.cState = MOTOR_STOP;
- GmRight.ulPulseCtr = 0;
- GmRight.ulPulse = 0;
- GmRight.lSpeed = 0;
- }
- }
- }
- /*********************************************************************************************************
- ** Function name: irSend
- ** Descriptions: 紅外發射啟動函數
- ** input parameters: ucIndex: 紅外發射索引號
- ** ulFreq : 紅外載波頻率
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void irSend (INT8U ucIndex, INT32U ulFreq)
- {
- switch (ucIndex) {
- case FRONT_SIDE_PWM_INDEX:
- PWM_RESET();
- PWM_FRQ_SET(ulFreq);
- FRONTSIDE_PWM_PULSE_SET(FPCLK / ulFreq / 2);
- PWM_START();
- FRONTSIDE_PWM_EN();
- break;
- case DIG_PWM_INDEX:
- PWM_RESET();
- PWM_FRQ_SET(ulFreq);
- DIG_PWM_PULSE_SET(FPCLK / ulFreq / 2);
- PWM_START();
- DIG_PWM_EN();
- break;
- default:
- break;
- }
- }
- /*********************************************************************************************************
- ** Function name: irCheck
- ** Descriptions: 紅外線傳感器檢測。
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void irCheck (void)
- {
- static INT8U ucState = 0;
- static INT32U ulIRCheck;
-
- switch (ucState) {
- case 0:
- irSend(FRONT_SIDE_PWM_INDEX, 32200); /* 探測左右兩側近距 */
- irSend(DIG_PWM_INDEX, 35000); //35000 /* 驅動斜角上的傳感器檢測 */
- break;
- case 1:
- ulIRCheck = IR_READ(); /* 讀取傳感器狀態 */
- FRONTSIDE_PWM_DIS(); /* 禁止PWM發生器2 */
- DIG_PWM_DIS(); /* 禁止PWM發生器1 */
-
- if (ulIRCheck & IR_SIDE_LFT) {
- GucDistance[__LEFT] &= 0xfd;
- } else {
- GucDistance[__LEFT] |= 0x02;
- }
- if (ulIRCheck & IR_DIG_LFT) {
- GucDistance[__FRONTL] = 0x00;
- } else {
- GucDistance[__FRONTL] = 0x01;
- }
-
- if (ulIRCheck & IR_DIG_RGT) {
- GucDistance[__FRONTR] = 0x00;
- } else {
- GucDistance[__FRONTR] = 0x01;
- }
- if (ulIRCheck & IR_SIDE_RGT) {
- GucDistance[__RIGHT] &= 0xfd;
- } else {
- GucDistance[__RIGHT] |= 0x02;
- }
- break;
- case 2:
- irSend(FRONT_SIDE_PWM_INDEX, 36000); /* 驅動檢測左前右三個方向遠距 */
-
- break;
- case 3:
- ulIRCheck = IR_READ(); /* 讀取傳感器狀態 */
- FRONTSIDE_PWM_DIS(); /* 禁止PWM發生器2 */
- break;
- case 4:
- irSend(FRONT_SIDE_PWM_INDEX, 36000); /* 重復檢測左前右三個方向遠距 */
- break;
- case 5:
- ulIRCheck &= IR_READ(); /* 讀取傳感器狀態 */
- FRONTSIDE_PWM_DIS(); /* 禁止PWM發生器2 */
-
- if (ulIRCheck & IR_SIDE_LFT) {
- GucDistance[__LEFT] &= 0xfe;
- } else {
- GucDistance[__LEFT] |= 0x01;
- }
- if (ulIRCheck & IR_FRONT) {
- GucDistance[__FRONT] &= 0xfe;
- } else {
- GucDistance[__FRONT] |= 0x01;
- }
- if (ulIRCheck & IR_SIDE_RGT) {
- GucDistance[__RIGHT] &= 0xfe;
- } else {
- GucDistance[__RIGHT] |= 0x01;
- }
- break;
- default:
- break;
- }
- if (++ucState >= 6) {
- ucState = 0;
- }
- }
- /*********************************************************************************************************
- ** Function name: mouseTurnback_C
- ** Descriptions: 后轉
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void mouseTurnback_C(void)
- {
- while (GmLeft.cState != MOTOR_STOP);
- while (GmRight.cState != MOTOR_STOP);
- delayMs(1);
- GucMouseState = TURN_BACK;
- GmRight.cDir = MOTOR_GO_BACK;
- GmRight.ulPulse = 85;//162 * 10;
-
- GmLeft.cDir = MOTOR_GO_AHEAD;
- GmLeft.ulPulse = 85;//162 * 10;
- GmLeft.cState = MOTOR_RUN;
- GmRight.cState = MOTOR_RUN;
-
- GucMouseDir = (GucMouseDir + 2) % 4; /* 方向標記 */
-
- while (GmRight.cState != MOTOR_STOP);
- while (GmLeft.cState != MOTOR_STOP);
- delayMs(2);
- }
- /*********************************************************************************************************
- ** Function name: mazeSearch
- ** Descriptions: 迷宮搜索,若發現分支路或者前方有擋板則退出
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void mazeSearch(void)
- {
- INT8S cL = 0, cR = 0;
- INT32U temp1 = 0;
- INT8U num = 0;
- INT8U Gonum = 0;
- INT8U DelayFlag = 0;
-
- /*
- * 設定運行任務
- */
-
- GucMouseState = GO_AHEAD;
- GlMaxSpeed = SEARCHSPEED;
- GmRight.cDir = MOTOR_GO_AHEAD;
- GmLeft.cDir = MOTOR_GO_AHEAD;
- GmLeft.ulPulseCtr = 0;
- GmRight.ulPulseCtr = 0;
- GmRight.ulPulse = MAZETYPE * ONEBLOCK;
- GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
- GmRight.cState = MOTOR_RUN;
- GmLeft.cState = MOTOR_RUN;
-
-
- if(GLCoorflag || GRCoorflag) /* 有轉彎 */
- {
- GLCoorflag =0;
- GRCoorflag =0;
- DelayFlag =1;
- GoLong = 0;
- num=20;
- Gonum = 0;
- }
-
- else { // 沒有轉彎
-
- if(GoLong == 1){ //如果上一次跳出過MazeSearch
- GoLong = 0;
- Gonum = 20;
- num = 0;
- }
- else{ //一直在該函數內,沒有跳出過,直走
- num = 0;
- Gonum = 0;
- }
- }
- while (GmLeft.cState != MOTOR_STOP)
- {
- if ((GmLeft.ulPulseCtr+num) >= (ONEBLOCK + Gonum)) // 判斷是否走完一格
- {
- GmLeft.ulPulse -= (ONEBLOCK + Gonum);
- GmRight.ulPulse -= (ONEBLOCK + Gonum);
- GmLeft.ulPulse += num;
- GmRight.ulPulse += num;
- updateCPostion(); //更新當前坐標
- wallCheck();
- GmLeft.ulPulseCtr = 0;
- GmRight.ulPulseCtr = 0;
- GmRight.ulPulse = MAZETYPE * ONEBLOCK;
- GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
- num =0; // num 清零
- Gonum = 0;
- }
-
-
-
- if (GucDistance[__FRONT]) { /* 前方有墻 */
- /*
- * 設置繼續前進的步數,以實現能停止在單元格中心的目的
- */
- GmRight.ulPulse = GmRight.ulPulseCtr + 74;//70,80,83,76
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 74;
- /*
- * 反復檢測前方傳感器的狀態,以消除誤判
- */
- //GmLeft.cState = GmRight.cState = MOTOR_STOP;
- //delayMs(5000);
-
- //tempF = 35;
- if(PreTurnBflag == 1){
- PreTurnBflag = 0;
- while(GucDistance[__FRONT]) {
- if((GmLeft.ulPulseCtr + 40) > GmLeft.ulPulse) { //48
- goto End; /* 跳出循環,跳到程序結束處 */
- }
- }
- }
-
- /* if(PreTurnLflag || PreTurnRflag){
- PreTurnLflag = 0;
- PreTurnRflag = 0;
- while (GucDistance[__FRONT]) {
- if((GmLeft.ulPulseCtr + 35) > GmLeft.ulPulse) { //48
- goto End;
- }
- }
-
- }*/
-
- while (GucDistance[__FRONT]) {
- if ((GmLeft.ulPulseCtr + 40) > GmLeft.ulPulse) { //48
- goto End; /* 跳出循環,跳到程序結束處 */
- }
- }
- /*
- * 程序執行到此步表明出現誤判,則重新設定電機運行任務
- */
- GmRight.ulPulse = MAZETYPE * ONEBLOCK;
- GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
- }
-
- if(DelayFlag)
- {
- DelayFlag = 0;
- temp1= GmRight.ulPulseCtr;
- while(GmRight.ulPulseCtr <= (temp1 + 24)); // if(GucDistance[__LEFT] == 0)
- cL = 1;
- if(GucDistance[__RIGHT] == 0)
- cR = 1;
- }
-
- if (cL) { /* 是否允許檢測左邊 */
- if ((GucDistance[__LEFT] & 0x01) == 0){ /* 左邊有支路 */
- /*
- * 設置繼續前進的步數,以實現能停止在單元格中心的目的
- */
- GmRight.ulPulse = GmRight.ulPulseCtr + 74;//82
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 74;
- /*
- * 反復檢測前方傳感器的狀態,以消除誤判
- */
- //tempL = 37;
-
- while ((GucDistance[__LEFT] & 0x01) == 0) {
- if ((GmLeft.ulPulseCtr + 40) > GmLeft.ulPulse) { //53
- GoLong = 1;
- goto End; /* 跳出循環,跳到程序結束處 */
- }
- }
- /*
- * 程序執行到此步表明出現誤判,則重新設定電機運行任務
- */
- GmRight.ulPulse = MAZETYPE * ONEBLOCK;
- GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
- }
- } else { /* 左邊有墻時開始允許檢測左邊 */
- if ( GucDistance[__LEFT] & 0x01) {
- cL = 1;
- }
- }
-
-
- if (cR) { /* 是否允許檢測右邊 */
- if ((GucDistance[__RIGHT] & 0x01) == 0) { /* 右邊有支路 */
- /*
- * 設置繼續前進的步數,以實現能停止在單元格中心的目的
- */
- GmRight.ulPulse = GmRight.ulPulseCtr + 74;
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 74;
- /*
- * 反復檢測前方傳感器的狀態,以消除誤判
- */
- //tempR = 37;
-
- while ((GucDistance[__RIGHT] & 0x01) == 0) {
- if ((GmLeft.ulPulseCtr + 40) > GmLeft.ulPulse) { //53
- GoLong = 1;
- goto End; /* 跳出循環,跳到程序結束處 */
- }
- }
- /*
- * 程序執行到此步表明出現誤判,則重新設定電機運行任務
- */
- GmRight.ulPulse = MAZETYPE * ONEBLOCK;
- GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
- }
- } else {
- if ( GucDistance[__RIGHT] & 0x01) { /* 右邊有墻時開始允許檢測右邊 */
- cR = 1;
- }
- }
- }
- End:
- PreTurnBflag = 0;
- PreTurnLflag = 0;
- PreTurnRflag = 0;
- updateCPostion();
- wallCheck();
- }
- /*********************************************************************************************************
- ** Function name: mouseTurnright
- ** Descriptions: 右轉
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void mouseTurnright(void)
- {
-
- GRCoorflag = 1; /*右轉的標志*/
- PreTurnBflag = 0;
- PreTurnRflag = 1;
- PreTurnLflag = 0;
- GucMouseState = TURN_RIGHT;
- GmLeft.lSpeed = SEARCHSPEED;
-
- GmRight.cDir = MOTORPAUSE; /*抱死狀態*/
-
-
- GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
- GmRight.ulPulse = MAZETYPE * ONEBLOCK;
- GmLeft.ulPulseCtr = 0;
- GmRight.ulPulseCtr = 0;
-
- while(GmLeft.ulPulseCtr <= 78); //88
-
- GmRight.cDir = MOTOR_GO_AHEAD;
- // GmLeft.lSpeed = GmRight.lSpeed = 30;
- GucMouseDir = (GucMouseDir + 1) % 4; /* 方向標記 */
-
-
- /*
- * 設定運行任務
- */
- GucMouseState = GO_AHEAD;
- GlMaxSpeed = SEARCHSPEED/4;
- GmRight.cDir = MOTOR_GO_AHEAD;
- GmLeft.cDir = MOTOR_GO_AHEAD;
- GmRight.ulPulseCtr = 0;
- GmLeft.ulPulseCtr = 0;
-
- GmLeft.lSpeed = SEARCHSPEED/4 - 3;//40
- GmRight.lSpeed = SEARCHSPEED/4 - 3;
- GmLeft.ulPulse = ONEBLOCK;
- GmRight.ulPulse = ONEBLOCK;
- GmLeft.cState = MOTOR_RUN;
- GmRight.cState = MOTOR_RUN;
- //GmLeft.lSpeed = GmRight.lSpeed;
- }
- /*********************************************************************************************************
- ** Function name: mouseTurnleft
- ** Descriptions: 左轉
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void mouseTurnleft(void)
- {
- GLCoorflag = 1;
- PreTurnBflag = 0;
- PreTurnRflag = 0;
- PreTurnLflag = 1;
-
- GucMouseState = TURN_LEFT;
-
- GmRight.lSpeed = SEARCHSPEED;
- GmLeft.cDir = MOTORPAUSE;
-
-
- GmRight.ulPulse = MAZETYPE * ONEBLOCK;
- GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
- GmRight.ulPulseCtr = 0;
- GmLeft.ulPulseCtr = 0;
-
-
- while(GmRight.ulPulseCtr<=78); //88
- GmLeft.cDir = MOTOR_GO_AHEAD;
- //GmLeft.lSpeed = GmRight.lSpeed = 30;
- GucMouseDir = (GucMouseDir + 3) % 4; /* 方向標記 */
- /*
- * 設定運行任務
- */
- GucMouseState = GO_AHEAD;
- GlMaxSpeed =SEARCHSPEED / 4;
- GmRight.cDir = MOTOR_GO_AHEAD;
- GmLeft.cDir = MOTOR_GO_AHEAD;
- GmLeft.ulPulseCtr = 0;
- GmRight.ulPulseCtr = 0;
-
- GmLeft.lSpeed = SEARCHSPEED/4-3;//40
- GmRight.lSpeed =SEARCHSPEED/4-3;
-
- GmLeft.ulPulse = ONEBLOCK;
- GmRight.ulPulse = ONEBLOCK;
- GmRight.cState = MOTOR_RUN;
- GmLeft.cState = MOTOR_RUN;
- // GmRight.lSpeed = GmLeft.lSpeed;
- }
- /*********************************************************************************************************
- ** Function name: mouseTurnback
- ** Descriptions: 后轉
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void mouseTurnback(void)
- {
- if((GucMouseTask == BACK) && (hy == 1)){
- //GmLeft.ulPulse = 40;
- //GmRight.ulPulse =40;
- GmLeft.ulPulseCtr = 0;
- GmRight.ulPulseCtr = 0;
- GmLeft.cState = MOTOR_RUN;
- GmRight.cState = MOTOR_RUN;
- GmLeft.cDir = MOTOR_GO_AHEAD;
- GmRight.cDir = MOTOR_GO_AHEAD;
- }
- /*
- * 等待停止
- */
- while (GmLeft.cState != MOTOR_STOP);
- while (GmRight.cState != MOTOR_STOP);
- delayMs(50);
-
- GucMouseState = TURN_BACK;
- /*
- * 開始后轉
- */
- GmRight.cDir = MOTOR_GO_BACK;
- GmRight.ulPulse = 85;//90
-
- GmLeft.cDir = MOTOR_GO_AHEAD;
- GmLeft.ulPulse = 85;
-
- PreTurnBflag = 1;
- PreTurnRflag = 0;
- PreTurnLflag = 0;
-
- GmLeft.cState = MOTOR_RUN;
- GmRight.cState = MOTOR_RUN;
-
- GucMouseDir = (GucMouseDir + 2) % 4;
- /*
- * 等待后轉完成
- */
- while (GmLeft.cState != MOTOR_STOP);
- while (GmRight.cState != MOTOR_STOP);
- delayMs(50);
-
- /* if(hy == 1){
- GmLeft.ulPulse = 20;
- GmRight.ulPulse = 20;
- GmLeft.cState = MOTOR_RUN;
- GmRight.cState = MOTOR_RUN;
- GmLeft.cDir = MOTOR_GO_AHEAD;
- GmRight.cDir = MOTOR_GO_AHEAD;
- }
- */
-
- }
- /*********************************************************************************************************
- ** Function name: mouseTurnright_C
- ** Descriptions: 右轉
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void mouseTurnright_C(void)
- {
- INT8U tempnum=0;
- Rturnflag = 1;
-
- if(BlankNum!=0)
- {
- if (BlankNum==1)
- {
- tempnum=125; //125 ..//120
- }
- else tempnum=125; //133
- }
- else tempnum=125;
- GucMouseState = TURN_RIGHT;
- GmLeft.ulPulse = GmLeft.ulPulseCtr + tempnum;//133; //140 ??
- GmLeft.lSpeed = 80;//80; //150
-
- GmRight.ulPulse = GmRight.ulPulseCtr + 21;//24; // 30
- GmRight.lSpeed = 12;//7; //12
-
-
- GmRight.cDir = MOTOR_GO_AHEAD;
- GmLeft.cDir = MOTOR_GO_AHEAD;
-
- GmRight.cState = MOTOR_RUN;
- GmLeft.cState = MOTOR_RUN;
-
- //while (GmRight.cState != __MOTORSTOP);
- while ( GmLeft.ulPulseCtr<=(GmLeft.ulPulse-30)); // 30
-
-
- GucMouseDir = (GucMouseDir + 1) % 4; /* 方向標記 */
- //while (GmLeft.cState != __MOTORSTOP);
- GmRight.ulPulse = MAZETYPE * ONEBLOCK;
- GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
-
- GmRight.ulPulseCtr = 0;
- GmLeft.ulPulseCtr = 0;
- GmRight.ulPulse = GmRight.ulPulseCtr + 10;
- GmRight.cState = MOTOR_RUN;
- GmLeft.cState = MOTOR_RUN;
- GmLeft.lSpeed = GmRight.lSpeed +10 ;
-
- Rturnflag = 0;
-
-
- if(turnback_secend_step==1)//轉180度的第二個彎
- SeveralStep(12);
- else{
- if(turnback_first_step==1) //轉180度的第一個彎
- SeveralStep(55);//60
- else
- SeveralStep(34);
- }
- }
-
- /*********************************************************************************************************
- ** Function name: SeveralStep
- ** Descriptions:
- ** input parameters: 執行幾步
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void SeveralStep(INT8U step)
- {
- GucMouseState = GO_AHEAD;
- GlMaxSpeed = 53;
- GmRight.cDir = MOTOR_GO_AHEAD;
- GmLeft.cDir = MOTOR_GO_AHEAD;
-
- GmRight.ulPulseCtr = 0;
- GmLeft.ulPulseCtr = 0;
- GmRight.ulPulse = step+10;//__GmRight.uiPulse + cNBlock * ONEBLOCK - 6;
- GmLeft.ulPulse = step+10;//__GmLeft.uiPulse + cNBlock * ONEBLOCK - 6;
- GmRight.cState = MOTOR_RUN;
- GmLeft.cState = MOTOR_RUN;
-
-
- if((turnback_first_step==1)||(turnback_secend_step==1))
- {
- GmRight.lSpeed=40;
- GmLeft.lSpeed=40;
- }
- else
- {
- GmRight.lSpeed=50;
- GmLeft.lSpeed=50;
- }
- if (GucDistance[__FRONT]) { /* 前方有墻,則跳出程序 */
- if((GmRight.ulPulse - GmRight.ulPulseCtr)<=10)
- {
- GmRight.ulPulse = GmRight.ulPulseCtr + 8;
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 8;
- }
- else
- {
- GmRight.ulPulse = GmRight.ulPulseCtr + 18;
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 18;
- }
- while (GucDistance[ __FRONT]) {
- if ((GmLeft.ulPulseCtr + 10) > GmLeft.ulPulse) {
- goto SeveralEnd;
- }
- else
- {
- GmRight.ulPulse = step+8;
- GmLeft.ulPulse = step+8;
- }
-
- }
- }
-
- while(GmLeft.ulPulseCtr+10 < GmLeft.ulPulse);
- while(GmRight.ulPulseCtr+10 < GmRight.ulPulse);
-
- SeveralEnd:;
-
- }
- /*********************************************************************************************************
- ** Function name: mouseTurnleft_C
- ** Descriptions: 左轉
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void mouseTurnleft_C(void)
- {
- INT8U tempnum=0;
- Lturnflag = 1;
-
- if(BlankNum!=0)
- {
- if (BlankNum==1)
- {
- tempnum=125; //125 ,120
- }
- else tempnum=125; //133
- }
- else tempnum=125; //131
-
- GucMouseState = TURN_LEFT;
- GmRight.ulPulse = GmRight.ulPulseCtr + tempnum;//133; //140
- GmRight.lSpeed = 80;//80;//150;
-
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 21;//24; // 30
- GmLeft.lSpeed = 12;//7;//9;
-
-
- GmLeft.cDir = MOTOR_GO_AHEAD;
- GmRight.cDir = MOTOR_GO_AHEAD;
-
- GmRight.cState = MOTOR_RUN;
- GmLeft.cState = MOTOR_RUN;
-
- //while (GmRight.cState != __MOTORSTOP);
- while ( GmRight.ulPulseCtr<=(GmRight.ulPulse-30)); // 30
- //while (GmLeft.cState != __MOTORSTOP);
-
- GmRight.lSpeed = GmLeft.lSpeed + 10;
-
-
- GmRight.ulPulseCtr = 0;
- GmLeft.ulPulseCtr = 0;
- GmLeft.ulPulse = GmRight.ulPulseCtr + 10;
- GmRight.cState = MOTOR_RUN;
- GmLeft.cState = MOTOR_RUN;
- //while(keyCheck() == false);
- GucMouseDir = (GucMouseDir + 3) % 4; /* 方向標記 */
-
- GmRight.ulPulseCtr = 0;
- GmLeft.ulPulseCtr = 0;
-
- Lturnflag = 0;
- /*
- if(turnback_first_step==1) //轉180度的第一個彎
- SeveralStep(55);//60
- else if(turnback_secend_step==1)//轉180度的第二個彎
- SeveralStep(10);
- else
- SeveralStep(30);
- */
- if(turnback_secend_step==1)//轉180度的第二個彎
- SeveralStep(12);
- else{
- if(turnback_first_step==1) //轉180度的第一個彎
- SeveralStep(55);//60
- else
- SeveralStep(34);
- }
- }
- /*********************************************************************************************************
- ** Function name: 沖刺時候用的mouseGoahead_C
- ** Descriptions: 前進N格
- ** input parameters: iNblock: 前進的格數
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void mouseGoahead_C (INT8S nBlock)
- {
- INT8U num=0;
- INT8U cNBlock=0,Block=0;
- INT8U coner=0;
- INT8U blacknum =0;
- INT8U first_step_Back =0;
- INT8S cL = 0, cR = 0,left_first_flag=1,right_first_flag=1;
- static INT8U step_count=0;
-
- step_count++;
-
- if((coner==2)&&((GCountInfo[step_count]&0xc0)==0x80))//(GCountInfo[step_count]==0x81))
- turnback_first_step=1;
- if((coner==1)&&((GCountInfo[step_count]&0xc0)==0x40))//(GCountInfo[step_count]==0x41))
- turnback_first_step=1;
-
- if(first_step==1)
- { first_step_Back =1;
- num=10;
- first_step=0;
- }
- else
- num=70;
- cNBlock=nBlock&0x0f;
- Block=cNBlock;
- coner=nBlock&0xc0;
- coner>>=6;
-
- blacknum = nBlock&0x30;
- blacknum >>=4;
-
- /*
- * 設定運行任務
- */
-
- GucMouseState = GO_AHEAD;
- GmRight.cDir = MOTOR_GO_AHEAD;
- GmLeft.cDir = MOTOR_GO_AHEAD;
-
-
- if(Block>=4)
- {
-
- if(Spurt_num_count == 1)
- GlMaxSpeed = 230; //230
- if(Spurt_num_count == 2)
- GlMaxSpeed = 250; //250
- }
- if(Block==3)
- {
- if (Spurt_num_count == 1)
- GlMaxSpeed = 200;
- if (Spurt_num_count == 2)
- GlMaxSpeed = 240;
- }
- if(blacknum)
- { if(Spurt_num_count == 1)
- GlMaxSpeed = 200;
- if(Spurt_num_count == 2)
- GlMaxSpeed = 230;
- }
- else{
-
- if(Spurt_num_count == 1)
- GlMaxSpeed = MAXSPEED;
- if(Spurt_num_count == 2)
- GlMaxSpeed = MAXSPEED2;
-
- }
- // GlMaxSpeed = MAXSPEED;
- // GmRight.cDir = MOTOR_GO_AHEAD;
- // GmLeft.cDir = MOTOR_GO_AHEAD;
- GmRight.ulPulseCtr = 0;
- GmLeft.ulPulseCtr = 0;
- GmRight.ulPulse = cNBlock * ONEBLOCK-15 ;//GmRight.ulPulse + cNBlock * ONEBLOCK - 6;
- GmLeft.ulPulse = cNBlock * ONEBLOCK-15 ;//GmLeft.ulPulse + cNBlock * ONEBLOCK - 6;
- GmRight.cState = MOTOR_RUN;
- GmLeft.cState = MOTOR_RUN;
-
- GmLeft.lSpeed = GmRight.lSpeed; /*********調試加***********/
- switch(coner)
- {
- case 0: left_flag=0;
- right_flag=0;
- break;
- case 1: left_flag=1;
- right_flag=0;
- break;
- case 2: left_flag=0;
- right_flag=1;
- break;
- default:
- break;
- }
-
- /*if(Block==3)
- {
- if(Spurt_num_count == 1)
- GlMaxSpeed = 200; //200
- if(Spurt_num_count == 2)
- GlMaxSpeed = 240; //240
-
- }
- if(Block>3)
- {
-
- if(Spurt_num_count == 1)
- GlMaxSpeed = 250; //250
- if(Spurt_num_count == 2)
- GlMaxSpeed = 280; //280
- }*/
-
- if(cNBlock==1)
- {
- if(GucMouseTask == BACKTOSTART1 && first_step_Back == 1){
- if(GmLeft.ulPulse <= 45 ||GmRight.ulPulse <= 45 ){
- cR=1;
- cL=1;
- first_step_Back = 0;
- }
- else{
- cR=0;
- cL=0;
- }
-
- }
- else{
- cR=1;
- cL=1;
- }
- }
-
- if(coner==0)
- {
- if(Block==1)
- {
- GmLeft.ulPulse -= 55; //45
- GmRight.ulPulse -= 55; //45
- }
- else
- {
- GmLeft.ulPulse -= 68;//
- GmRight.ulPulse -= 68;//60;
- }
- }
-
- while (GmLeft.cState != MOTOR_STOP)
- {
- if(left_first_flag==1)
- {
- if ((GmLeft.ulPulseCtr+num) >= ONEBLOCK)
- { /* 判斷是否走完一格 */
- GmLeft.ulPulse -= ONEBLOCK;
- GmLeft.ulPulse += num;
- GmLeft.ulPulseCtr -= ONEBLOCK;
- GmLeft.ulPulseCtr += num;
- cNBlock--;
- if(blacknum)
- blacknum--;
- left_first_flag=0;
- }
- }
- else if (GmLeft.ulPulseCtr >= ONEBLOCK)
- { /* 判斷是否走完一格 */
- GmLeft.ulPulse -= ONEBLOCK;
- GmLeft.ulPulseCtr -= ONEBLOCK;
-
- cNBlock--;
- if(blacknum)
- blacknum--;
- }
- if(right_first_flag==1)
- {
- if ((GmRight.ulPulseCtr+num) >= ONEBLOCK) { /* 判斷是否走完一格 */
- GmRight.ulPulse -= ONEBLOCK;
- GmRight.ulPulse += num;
- GmRight.ulPulseCtr -= ONEBLOCK;
- GmRight.ulPulseCtr += num;
- right_first_flag=0;
- }
- }
- else if (GmRight.ulPulseCtr >= ONEBLOCK) { /* 判斷是否走完一格 */
- GmRight.ulPulse -= ONEBLOCK;
- GmRight.ulPulseCtr -= ONEBLOCK;
- }
- if (GucDistance[__FRONT]) { /* 前方有墻,則跳出程序 */
- GmRight.ulPulse = GmRight.ulPulseCtr + 21;
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 21; //30
- while (GucDistance[ __FRONT]) {
- if ((GmLeft.ulPulseCtr + 10) > GmLeft.ulPulse) {
- goto End;
- }
- }
- }
- if(Block==1)
- { if(Spurt_num_count == 1){
-
- GmRight.lSpeed = 53;//70;//53;//33;
- GmLeft.lSpeed = 53;//70;//53;//33;
- GlMaxSpeed = 65;//80;//65;//40;
- }
- if(Spurt_num_count == 2)
- {
- GmRight.lSpeed = 53;//60;//53;//33;
- GmLeft.lSpeed = 53;//60;//53;//33;
- GlMaxSpeed = 65;//70;//65;//40;
- }
- }
- if(Block >=2 && cNBlock<2)
- {
- if(Spurt_num_count == 1){
-
- GmRight.lSpeed = 70;//70;//60; //70
- GmLeft.lSpeed = 70;//70;//60;
- GlMaxSpeed = 85;//85;//85;//70;
- }
- if(Spurt_num_count == 2){
- GmRight.lSpeed = 80;//80;//70;//60; //70
- GmLeft.lSpeed = 80;//80;//70;//60;
- GlMaxSpeed = 85;//95;//85;//70;
-
- }
-
-
- }
- if (cNBlock <= 2) {
-
- if (cL&&left_flag) { /* 是否允許檢測左邊 */
-
- if ((GucDistance[ __LEFT] & 0x01) == 0) { /* 左邊有支路,跳出程序 */
- GmRight.ulPulse = GmRight.ulPulseCtr + 20;
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 20;
- while ((GucDistance[ __LEFT] & 0x01) == 0) {
- if ((GmLeft.ulPulseCtr + 8) > GmLeft.ulPulse) //12
- {
- goto End;
- }
- }
- }
- } else { /* 左邊有墻時開始允許檢測左邊 */
- if ( (GucDistance[ __LEFT] & 0x01)&&(cNBlock<2)) {
- cL = 1;
- }
- }
- if (cR&&right_flag) { /* 是否允許檢測右邊 */
- if ((GucDistance[__RIGHT] & 0x01) == 0) { /* 右邊有支路,跳出程序 */
- GmRight.ulPulse = GmRight.ulPulseCtr + 20;
- GmLeft.ulPulse = GmLeft.ulPulseCtr + 20;
- while ((GucDistance[ __RIGHT] & 0x01) == 0) {
- if ((GmLeft.ulPulseCtr + 8) > GmLeft.ulPulse) //12
- {
- goto End;
- }
- }
- }
- } else { //修改過
- if ( (GucDistance[__RIGHT] & 0x01)&&(cNBlock<2)) { /* 右邊有墻時開始允許檢測右邊 */
- cR = 1;
- }
- }
- }
- }
- /*
- * 設定運行任務,讓電腦鼠走到支路的中心位置
- */
- End:
- if(right_flag)
- mouseTurnright_C();
- if(left_flag)
- mouseTurnleft_C();
- left_flag=0;
- right_flag=0;
- turnback_first_step=0;
- turnback_secend_step=0;
-
- if((coner==2)&&(GCountInfo[step_count]==0x81))
- turnback_secend_step=1;
- if((coner==1)&&(GCountInfo[step_count]==0x41))
- turnback_secend_step=1;
-
- BlankNum=(GCountInfo[step_count]&0x30)>>4; //加的。。。計算
-
- }
- /*********************************************************************************************************
- ** Function name: keyCheck
- ** Descriptions: 讀取按鍵
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: true: 按鍵已按下
- ** false: 按鍵未按下
- *********************************************************************************************************/
- BOOLEAN keyCheck (void)
- {
- if (KEY_PRESS()) { /* 按鍵按下 */
- delayMs(10); /* 延時10ms消抖動 */
- if (KEY_PRESS()) { /* 按鍵按下 */
- delayMs(10); /* 延時10ms消抖動 */
- while(!KEY_PRESS()); /* 等待按鍵釋放 */
- return (true);
- }
- }
- return (false);
- }
- /*********************************************************************************************************
- ** Function name: keyCheck_C
- ** Descriptions: 讀取按鍵
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: true: 按鍵已按下
- ** false: 按鍵未按下
- *********************************************************************************************************/
- BOOLEAN keyCheck_C (void)
- {
- if (ZLG7298_KEY_READ()){ /* 按鍵按下 */
- delayMs(10); /* 延時10ms消抖動 */
- if (ZLG7298_KEY_READ()) { /* 按鍵按下 */
- delayMs(10); /* 延時10ms消抖動 */
- while(!ZLG7298_KEY_READ()); /* 等待按鍵釋放 */
- return (true);
- }
- }
- return (false);
- }
- /*********************************************************************************************************
- ** Function name: sysTickIsr
- ** Descriptions: 定時中斷掃描。
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void sysTickIsr(void)
- {
- static INT32S lLft = 0, lRgt = 0;
-
- /*
- * 如果左電機長時間停止,則斷電
- */
- if (GmLeft.cState == MOTOR_STOP) {
- lLft++;
- } else {
- lLft = 0;
- }
- if (lLft >= 500) {
- MT_LFT_OFF();
- }
- /*
- * 如果右電機長時間停止,則斷電
- */
- if (GmRight.cState == MOTOR_STOP) {
- lRgt++;
- } else {
- lRgt = 0;
- }
- if (lRgt >= 500) {
- MT_RGT_OFF();
- }
- /*
- * 紅外線檢測
- */
- irCheck();
- }
- /*********************************************************************************************************
- ** Function name: __wallCheck
- ** Descriptions: 根據傳感器檢測結果判斷是否存在墻壁,并保存
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無(已經調試)
- ** atention: ucMap低四位從右到左,依次對應迷宮的(前右下左)(即north=1,east=2,south=4,west=8)
- *********************************************************************************************************/
- void wallCheck (void)
- {
- INT8U ucMap = 0;
-
- ucMap &= ~MOUSEWAY_B; /* 初始化為后方無Wall,有墻壁為1,無墻為0 */
- if (GucDistance[__LEFT] & 0x01) /* __GucDistance[__LEFT]為1,說明左邊傳感器檢測到有wall!(真)*/
- {
- ucMap |= MOUSEWAY_L; /* 將相對方向上的信息轉化為絕對信息保存 */
- }
- if (GucDistance[__FRONT] & 0x01)
- {
- ucMap |= MOUSEWAY_F;
- }
- if (GucDistance[__RIGHT] & 0x01)
- {
- ucMap |= MOUSEWAY_R;
- }
- GWallMap[GCurPosition] = ucMap; /* 將墻壁信息保存 */
-
- GWallMap[GCurPosition] |= PASSED; /* 經過的單元格標記 */
-
- if(ucMap==0x00) /* 十字路口 */
- {
- GWallMap[GCurPosition] |= SPECIAL;
- }
- if(ucMap)
- {
- if( ucMap & NORTH) /* 該單元格上方有墻 ,上面一單元格下方有墻 */
- {
- if((GCurPosition+1)%STEPNUM) /* 上方單元格不能對north更新 */
- {
- GWallMap[GCurPosition + 1] |= SOUTH;
- }
- }
-
- if( ucMap & EAST) /* 該單元格右方有墻 ,右面一單元格左方有墻 */
- {
- if(GCurPosition<WALLNUM) /* 右方單元格不能對east更新 */
- GWallMap[GCurPosition + STEPNUM] |= WEST;
- }
- if( ucMap & SOUTH) /* 該單元格下方有墻 ,下面一單元格上方有墻 */
- {
- if(GCurPosition%STEPNUM) /* 下方單元格不能對south更新 */
- GWallMap[GCurPosition - 1] |= NORTH;
- }
- if( ucMap & WEST) /* 該單元格左方有墻 ,左面一單元格右方有墻 */
- {
- if(GCurPosition>(STEPNUM-1)) /* 左方單元格不能對West更新 */
- GWallMap[GCurPosition -STEPNUM] |= EAST;
- }
- }
- }
- /*********************************************************************************************************
- ** Function name: voltageDetect
- ** Descriptions: 電壓檢測,檢測結果在7289 EX BOARD 上顯示出來
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void volDetect (void)
- {
- INT32U i;
- INT32U ulAdcData;
- ADC_SMP_ON(); /* 立即轉換 */
- while (!ADC_DATA_READY_FLG()); /* 讀取AD0STAT的通道5的Done */
- ulAdcData = 0; /* 第一次轉換結果丟棄 */
- for (i = 0; i < 8; i ++) { /* 連續8次采樣 */
- ADC_SMP_ON(); /* 立即轉換 */
- while (!ADC_DATA_READY_FLG()); /* 讀取AD0STAT的通道5的Done */
- ulAdcData += ADC_DATA_GET();
- }
- ulAdcData >>= 3; /* 外部電阻分壓,相當于/8 */
- ulAdcData = ulAdcData * (3300 * 3) / ((1 << 12) - 1); /* 計算電池電壓值(mV) */
- zlg7289Download(0,6,1,(ulAdcData / 1000)); /* 顯示電壓值整數部分,單位V */
- zlg7289Download(0,7,0,(ulAdcData % 1000 ) / 100); /* 顯示電壓值小數部分,單位V */
- }
- /*********************************************************************************************************
- ** Function name: sensorInit
- ** Descriptions: 傳感器控制初始化
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void sensorInit (void)
- {
- IR_PIN_MODE_IN(); /* 紅外檢測引腳配置為輸入引腳 */
-
- PWM1TCR = 0x02; /* 復位PWM1定時器 */
- PWM1TC = 0; /* 定時器設置為0 */
- PWM1PR = 0; /* 時鐘不分頻 */
- PWM1MCR = 0x02; /* 設置PWMMR0匹配后復位PWMTC, */
- /* 并產生中斷標志 */
- PWM1MR0 = FPCLK / 38000; /* 38KHz頻率 */
- PWM1MR1 = FPCLK / 38000 / 2;
- PWM1MR2 = FPCLK / 38000 / 2;
- PWM1PCR &= ~((1 << 9) | (1 << 10)); /* 禁止輸出 */
- PWM1TCR = 0x05; /* 啟動定時器 */
- }
- /*********************************************************************************************************
- ** Function name: stepMotorIint
- ** Descriptions: 步進電機控制初始化
- ** input parameters: 無
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有程序51hei提供下載:
電腦鼠迷宮程序(冠軍組,完美版).rar
(488.92 KB, 下載次數: 23)
2020-10-25 00:06 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|