冠军完美版 能顺利运行
单片机源程序如下:
- #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)
|