找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1283|回复: 0
收起左侧

电脑鼠迷宫程序

[复制链接]
ID:714730 发表于 2020-10-25 00:06 | 显示全部楼层 |阅读模式
冠军完美版 能顺利运行
51hei.png

单片机源程序如下:
  1. #include "..\config.h"
  2. #include "Mouse_Drive.h"

  3. /*********************************************************************************************************
  4. *当小车进入时,以小车的前方为north,右方为east,下方为south,左方为west.作为迷宫的绝对方向
  5. *对应已经定义的绝对方向:     up,right,down,left.
  6. **********************************************************************************************************/
  7. #define NORTH      1
  8. #define EAST       2
  9. #define SOUTH      4
  10. #define WEST       8
  11. #define PASSED     16
  12. #define MARK       32
  13. #define SJUDGE     64
  14. #define SPECIAL    128

  15. //#define SEARCHSPEED        100                                //95,100

  16. /*********************************************************************************************************
  17.   定义全局变量
  18. *********************************************************************************************************/
  19. INT8U             GucMouseDir                     = UP;                 /*  保存电脑鼠当前方向          */
  20. MAZECOOR          GmcMouse                        = {0,0};              /*  保存电脑鼠当前位置坐标      */
  21. INT8U             GucMapBlock[MAZETYPE][MAZETYPE] = {0};                /*  GucMapBlock[x][y]           */

  22. static MOTOR   GmRight  = {MOTOR_STOP, MOTOR_GO_AHEAD, 0, 0, 0};        /*  定义并初始化右电机状态      */
  23. static MOTOR   GmLeft   = {MOTOR_STOP, MOTOR_GO_AHEAD, 0, 0, 0};        /*  定义并初始化左电机状态      */
  24.                                                                         
  25. static volatile INT8U GucMouseState = STOP;                             /*  保存电脑鼠当前运行状态      */
  26. static INT32U GulAccelTable[300]    = {0}; //300                             /*  电机加减速各阶段定时器值    */
  27. static INT32S GlMaxSpeed            = SEARCHSPEED;                               /*  保存允许运行的最大速度      */

  28. static volatile INT8U GucDistance[5]    = {0};                          /*  记录传感器状态              */

  29. INT8U  GWallMap[MAXSIZE];
  30. INT8U  GDistanceMap[MAXSIZE]={255};
  31. INT8U GStartChange =0;        
  32. static INT8U GLCoorflag =0;         /* 允许在转弯时,是否检测左边  */
  33. static INT8U GRCoorflag =0;         /* 允许在转弯时,是否检测右边  */
  34. INT8U BlankNum =0;

  35. INT8U GCurPosition  =LEFT_START_NUM;  
  36. static INT8U  GSetflag =0;
  37. static INT8U  zb=0,zs=0,zg=0;

  38. INT8U GEndNum =LEFT_END_NUM ;           /*终点坐标*/
  39. INT8U GStartNum=LEFT_START_NUM ;

  40. static INT8U  GConformflag =0;     /*  终点坐标确认标志  */

  41. INT8U Gmazetype = 8;

  42. static INT8U GoLong = 0;

  43. INT8U straigtOneflag = 0;
  44. INT8U PreTurnRflag = 0;
  45. INT8U PreTurnLflag = 0;
  46. INT8U PreTurnBflag = 0;
  47. INT8U TurnRLflag = 0;

  48. INT8U  GCountInfo[100]={0x87,0x81,0x41,0x81,0x41,0x81,0x41,0x81,0x41,0x81,
  49.                        0x82,0x41,0x81,0x84,0,0x41,0x81,0x41,0x83,0x81,0x02,0};

  50. INT8U turnback_first_step=0;
  51. INT8U turnback_secend_step=0;
  52. INT8U  first_step=0;
  53. INT8U Spurt_num_count =1;

  54. INT8U  left_flag=0;
  55. INT8U  right_flag=0;

  56. INT8U Lturnflag =0;
  57. INT8U Rturnflag =0;
  58. INT8U spurtflag=0;


  59. void SeveralStep(INT8U step);
  60. void updateCPostion(void);





  61. /*********************************************************************************************************
  62. ** Function name:       myDelay
  63. ** Descriptions:        软件延时
  64. ** input parameters:    延时时间,单位为us
  65. ** output parameters:   无
  66. ** Returned value:      无
  67. *********************************************************************************************************/
  68. void delayMs(INT32U ulTime)
  69. {
  70.     INT32U i;
  71.     while (ulTime--) {
  72.       for (i = 0; i < (FCCLK / 1000 / 3); i++);
  73.     }
  74. }
  75. /*********************************************************************************************************
  76. ** Function name:       lftMtCtrl
  77. ** Descriptions:        左步进电机驱动时序
  78. ** input parameters:    GmLeft.cDir :电机运行方向
  79. ** output parameters:   无
  80. ** Returned value:      无
  81. *********************************************************************************************************/
  82. void  lftMtCtrl (void)
  83. {
  84.     static INT8U ucRunStep = 0;                                         /*  保存电机当前位置            */
  85.    
  86.     switch (GmLeft.cDir) {
  87.     case MOTOR_GO_AHEAD:                                                /*  向前步进                    */
  88.         if (++ucRunStep >= 8) {
  89.             ucRunStep = 0;
  90.         }
  91.         break;
  92.     case MOTOR_GO_BACK:                                                 /*  向后步进                    */
  93.         if (--ucRunStep >= 8) {                                         /*  溢出                        */
  94.             ucRunStep = 7;
  95.         }
  96.         break;
  97.     case MOTORPAUSE:
  98.         break;
  99.     default:
  100.         break;
  101.     }
  102.    
  103.     switch (ucRunStep) {                                                /*  执行步进电机时序            */
  104.     case 0:
  105.         MT_LFT_STEP1_SET();
  106.         break;
  107.     case 1:
  108.         MT_LFT_STEP2_SET();
  109.         break;
  110.     case 2:
  111.         MT_LFT_STEP3_SET();
  112.         break;
  113.     case 3:
  114.         MT_LFT_STEP4_SET();
  115.         break;
  116.     case 4:
  117.         MT_LFT_STEP5_SET();
  118.         break;
  119.     case 5:
  120.         MT_LFT_STEP6_SET();
  121.         break;
  122.     case 6:
  123.         MT_LFT_STEP7_SET();
  124.         break;
  125.     case 7:
  126.         MT_LFT_STEP8_SET();
  127.         break;
  128.     default:
  129.         break;
  130.     }
  131. }


  132. /*********************************************************************************************************
  133. ** Function name:       rgtMtCtrl
  134. ** Descriptions:        右步进电机驱动时序
  135. ** input parameters:    无
  136. ** output parameters:   无
  137. ** Returned value:      无
  138. *********************************************************************************************************/
  139. void  rgtMtCtrl (void)
  140. {
  141.     static INT8U ucRunStep = 0;                                         /*  保存电机当前位置            */
  142.    
  143.     switch (GmRight.cDir) {
  144.     case MOTOR_GO_AHEAD:                                                /*  向前步进                    */
  145.         if (--ucRunStep >= 8) {                                         /*  溢出                        */
  146.             ucRunStep = 7;
  147.         }
  148.         break;
  149.     case MOTOR_GO_BACK:                                                 /*  向后步进                    */
  150.         if (++ucRunStep >= 8) {
  151.             ucRunStep = 0;
  152.         }
  153.         break;
  154.         case MOTORPAUSE:
  155.         break;
  156.     default:
  157.         break;
  158.     }
  159.    
  160.     switch (ucRunStep) {                                                /*  执行步进电机时序            */
  161.     case 0:
  162.         MT_RGT_STEP1_SET();
  163.         break;
  164.     case 1:
  165.         MT_RGT_STEP2_SET();
  166.         break;
  167.     case 2:
  168.         MT_RGT_STEP3_SET();
  169.         break;
  170.     case 3:
  171.         MT_RGT_STEP4_SET();
  172.         break;
  173.     case 4:
  174.         MT_RGT_STEP5_SET();
  175.         break;
  176.     case 5:
  177.         MT_RGT_STEP6_SET();
  178.         break;
  179.     case 6:
  180.         MT_RGT_STEP7_SET();
  181.         break;
  182.     case 7:
  183.         MT_RGT_STEP8_SET();
  184.         break;
  185.     default:
  186.         break;
  187.     }
  188. }


  189. /*********************************************************************************************************
  190. ** Function name:       lftMtSpeedCtrl
  191. ** Descriptions:        左电机速度调节
  192. ** input parameters:    无
  193. ** output parameters:   无
  194. ** Returned value:      无
  195. *********************************************************************************************************/
  196. void lftMtSpeedCtrl (void)
  197. {
  198.     INT32S lDownPusle;
  199.    
  200.     lDownPusle = GmLeft.ulPulse - GmLeft.ulPulseCtr;                    /*  统计电机还剩余的步数        */
  201.     if (lDownPusle <= GmLeft.lSpeed) {
  202.         GmLeft.lSpeed--;
  203.     } else {                                                            /*  非减速区间,则加速到最大值  */
  204.         if (GmLeft.lSpeed < GlMaxSpeed) {
  205.             GmLeft.lSpeed++;
  206.         }
  207.     }
  208.     if (GmLeft.lSpeed < 0) {                                            /*  设置速度下限                */
  209.         GmLeft.lSpeed = 0;
  210.     }
  211.    
  212.     TIMER0_LOAD_SET(GulAccelTable[GmLeft.lSpeed]);                     /*  设置定时时间                 */
  213. }

  214. /*********************************************************************************************************
  215. ** Function name:       rgtMtSpeedCtrl
  216. ** Descriptions:        右电机速度调节
  217. ** input parameters:    无
  218. ** output parameters:   无
  219. ** Returned value:      无
  220. *********************************************************************************************************/
  221. void rgtMtSpeedCtrl (void)
  222. {
  223.     INT32S lDownPusle;
  224.    
  225.     lDownPusle = GmRight.ulPulse - GmRight.ulPulseCtr;                  /*  统计电机还剩余的步数        */
  226.     if (lDownPusle <= GmRight.lSpeed) {
  227.         GmRight.lSpeed--;
  228.     } else {                                                            /*  非减速区间,则加速到最大值  */
  229.         if (GmRight.lSpeed < GlMaxSpeed) {
  230.             GmRight.lSpeed++;
  231.         }/* else {
  232.             GmRight.lSpeed--;
  233.         }*/
  234.     }
  235.     if (GmRight.lSpeed < 0) {                                           /*  设置速度下限                */
  236.         GmRight.lSpeed = 0;
  237.     }

  238.     TIMER1_LOAD_SET(GulAccelTable[GmRight.lSpeed]);                     /*  设置定时时间                */

  239. }
  240. /*********************************************************************************************************
  241. ** Function name:       __speedContrR
  242. ** Descriptions:        右电机速度调节
  243. ** input parameters:    无
  244. ** output parameters:   无
  245. ** Returned value:      无
  246. *********************************************************************************************************/
  247. void __speedContrR_C (void)
  248. {
  249.     INT32S iDPusle;
  250.     if(Lturnflag ||Rturnflag );
  251.      //   GmRight.iSpeed = 60;
  252.     else
  253.     {
  254.       iDPusle =  GmRight.ulPulse+30 -  GmRight.ulPulseCtr;                 /*  统计电机还剩余的步数        */
  255.       if (iDPusle <=  GmRight.lSpeed) {
  256.            GmRight.lSpeed--;
  257.       } else {                                                            /*  非减速区间,则加速到最大值  */
  258.           if ( GmRight.lSpeed <  GlMaxSpeed)
  259.           {
  260.                GmRight.lSpeed++;
  261.           }
  262.           else
  263.           {
  264.                GmRight.lSpeed--;
  265.           }       
  266.       }
  267.     }
  268.     if (  GmRight.lSpeed < 0) {                                         /*  设置速度下限                */
  269.           GmRight.lSpeed = 0;
  270.     }
  271.     //__GmRight.iSpeed = 50;////////////////////////////////调试添加/////////////////////
  272.     TIMER1_LOAD_SET(GulAccelTable[GmRight.lSpeed]);
  273. }
  274. /*********************************************************************************************************
  275. ** Function name:       __speedContrL
  276. ** Descriptions:        左电机速度调节
  277. ** input parameters:    无
  278. ** output parameters:   无
  279. ** Returned value:      无
  280. *********************************************************************************************************/
  281. void __speedContrL_C (void)
  282. {
  283.     INT32S iDPusle;
  284.     if(Lturnflag ||Rturnflag );           //??????
  285.     else
  286.     {
  287.       iDPusle =  GmLeft.ulPulse +30 -  GmLeft.ulPulseCtr;                   /*  统计电机还剩余的步数        */
  288.       if (iDPusle <=  GmLeft.lSpeed) {
  289.            GmLeft.lSpeed--;
  290.       } else {                                                            /*  非减速区间,则加速到最大值  */
  291.           if (  GmLeft.lSpeed < GlMaxSpeed) {
  292.                 GmLeft.lSpeed++;
  293.           }
  294.       }
  295.     }
  296.     if (  GmLeft.lSpeed < 0) {                                          /*  设置速度下限                */
  297.           GmLeft.lSpeed = 0;
  298.     }
  299.    
  300.     // GmLeft.iSpeed = 50;////////////////////////////////调试添加/////////////////////
  301.    TIMER0_LOAD_SET(GulAccelTable[GmLeft.lSpeed]); /*  设置定时时间                */
  302. }
  303. /*********************************************************************************************************
  304. ** Function name:       timer0Isr
  305. ** Descriptions:        Timer0中断服务函数
  306. ** input parameters:    GmLeft.cState :驱动步进电机的时序状态
  307. **                      GmLeft.cDir   :步进电机运动的方向
  308. ** output parameters:   无
  309. ** Returned value:      无
  310. *********************************************************************************************************/
  311. void timer0Isr(void)
  312. {
  313.     static INT8S ucCnt1 = 0, ucCnt2 = 0;                                /*  修正姿态计数变量            */
  314.     TIMER0_INT_CLR();                                                   /*  清除中断标志                */

  315.     switch (GmLeft.cState) {        
  316.     case MOTOR_STOP:                                                    /*  停止,同时清零速度和脉冲值  */
  317.         GmLeft.lSpeed     = 0;
  318.         GmLeft.ulPulse    = 0;
  319.         GmLeft.ulPulseCtr = 0;
  320.         break;        
  321.     case WAITONE_STEP:                                                  /*  暂停一步                    */
  322.         GmLeft.cState     = MOTOR_RUN;
  323.         break;
  324.     case MOTOR_RUN:                                                     /*  电机运行                    */
  325.         if (GucMouseState == GO_AHEAD) {                                /*  根据传感器状态微调电机位置  */
  326.             if (GucDistance[__FRONTR] &&                                 /*  前右方有挡板                */\
  327.                (GucDistance[__FRONTL]==0)) {                              /*  前左方无挡板                */
  328.                 if (ucCnt1 == 1) {
  329.                     GmLeft.cState = WAITONE_STEP;                       /*  使下一步暂停                */
  330.                 }
  331.                 if (++ucCnt1 > 3) {                                      // 3步暂停一步               
  332.                     ucCnt1 = 0;
  333.                 }
  334.                                
  335.                                
  336.                                
  337.             } else {
  338.                 ucCnt1 = 0;
  339.             }
  340.             if ((GucDistance[__LEFT] == 1) &&                             /*  左远方有挡板               */\
  341.                (GucDistance[__RIGHT] == 0)) {                             /*  右方无挡板                  */
  342.                 if(ucCnt2 == 3) {
  343.                     GmLeft.cState = WAITONE_STEP;                       /*  使下一步暂停                */
  344.                 }
  345.             if (++ucCnt2 > 6) {                                  // 6步暂停一步                 
  346.                     ucCnt2 = 0;
  347.                 }
  348.                                

  349.                                
  350.                                
  351.             } else {
  352.                 ucCnt2  = 0;
  353.             }
  354.         }
  355.         lftMtCtrl();                                                    /*  电机驱动程序                */
  356.         break;
  357.     default:
  358.         break;
  359.     }
  360.     /*
  361.      *  是否完成任务判断
  362.      */
  363.     if (GmLeft.cState != MOTOR_STOP) {
  364.         GmLeft.ulPulseCtr++;                                            /*  运行脉冲计数                */
  365.         if(spurtflag)
  366.            __speedContrL_C();                                                    /*  电机驱动程序                */
  367.                 else
  368.                         lftMtSpeedCtrl();                                               /*  速度调节                    */                                              /*  速度调节                    */
  369.         if (GmLeft.ulPulseCtr >= GmLeft.ulPulse) {
  370.             GmLeft.cState      = MOTOR_STOP;
  371.             GmLeft.ulPulseCtr  = 0;
  372.             GmLeft.ulPulse     = 0;
  373.             GmLeft.lSpeed      = 0;
  374.         }
  375.     }
  376. }


  377. /*********************************************************************************************************
  378. ** Function name:       timer1Isr
  379. ** Descriptions:        Timer1中断服务函数
  380. ** input parameters:    无
  381. ** output parameters:   无
  382. ** Returned value:      无
  383. *********************************************************************************************************/
  384. void timer1Isr(void)
  385. {
  386.     static INT8S ucCnt1 = 0, ucCnt2 = 0;                                /*  修正姿态计数变量            */
  387.     TIMER1_INT_CLR();                                                   /*  清除中断标志                */
  388.    
  389.     switch (GmRight.cState) {
  390.     case MOTOR_STOP:                                                     /*  停止,同时清零速度和脉冲值  */
  391.         GmRight.lSpeed     = 0;
  392.         GmRight.ulPulse    = 0;
  393.         GmRight.ulPulseCtr = 0;
  394.         break;

  395.     case WAITONE_STEP:                                                   /*  暂停一步                    */
  396.         GmRight.cState     = MOTOR_RUN;
  397.         break;
  398.     case MOTOR_RUN:                                                     /*  电机运行                    */
  399.         if (GucMouseState == GO_AHEAD) {                                /*  根据传感器状态微调电机位置  */
  400.             if (GucDistance[__FRONTL] &&                                 /*  前左方有挡板                */\
  401.                (GucDistance[__FRONTR]==0)) {                              /*  前右方无挡板                */
  402.                 if (ucCnt1 == 1) {
  403.                     GmRight.cState = WAITONE_STEP;                      /*  使下一步暂停                */
  404.                 }
  405.                 if (++ucCnt1 > 3) {                                 // 3步暂停一步            
  406.                     ucCnt1 = 0;
  407.                 }

  408.             } else {
  409.                 ucCnt1 = 0;
  410.             }
  411.             if ((GucDistance[__RIGHT] == 1) &&                           /*  右远方有挡板               */\
  412.                (GucDistance[__LEFT] == 0)) {                              /*  左方无挡板                  */
  413.                 if(ucCnt2 == 3) {
  414.                     GmRight.cState = WAITONE_STEP;                      /*  使下一步暂停                */
  415.                 }
  416.                 if (++ucCnt2 > 6) {                                     //6步暂停一步              
  417.                     ucCnt2 = 0;
  418.                 }
  419.             } else {
  420.                 ucCnt2  = 0;
  421.             }
  422.         }
  423.         rgtMtCtrl();                                                    /*  推动电机转动一步            */
  424.         break;

  425.     default:
  426.         break;
  427.     }
  428.     /*
  429.      *  是否完成任务判断
  430.      */
  431.     if (GmRight.cState != MOTOR_STOP) {
  432.         GmRight.ulPulseCtr++;                                           /*  运行脉冲计数                */
  433.         if(spurtflag)
  434.             __speedContrR_C();                  /*  速度调节                    */
  435.                 else
  436.                         rgtMtSpeedCtrl();                                               /*  速度调节                    */                                              /*  速度调节                    */
  437.         if (GmRight.ulPulseCtr >= GmRight.ulPulse) {
  438.             GmRight.cState      = MOTOR_STOP;
  439.             GmRight.ulPulseCtr  = 0;
  440.             GmRight.ulPulse     = 0;
  441.             GmRight.lSpeed      = 0;
  442.         }
  443.     }
  444. }
  445. /*********************************************************************************************************
  446. ** Function name:       irSend
  447. ** Descriptions:        红外发射启动函数
  448. ** input parameters:    ucIndex: 红外发射索引号
  449. **                      ulFreq : 红外载波频率
  450. ** output parameters:   无
  451. ** Returned value:      无
  452. *********************************************************************************************************/
  453. void  irSend (INT8U ucIndex, INT32U ulFreq)
  454. {
  455.     switch (ucIndex) {
  456.     case FRONT_SIDE_PWM_INDEX:
  457.         PWM_RESET();
  458.         PWM_FRQ_SET(ulFreq);
  459.         FRONTSIDE_PWM_PULSE_SET(FPCLK / ulFreq / 2);
  460.         PWM_START();
  461.         FRONTSIDE_PWM_EN();
  462.         break;
  463.     case DIG_PWM_INDEX:
  464.         PWM_RESET();
  465.         PWM_FRQ_SET(ulFreq);
  466.         DIG_PWM_PULSE_SET(FPCLK / ulFreq / 2);
  467.         PWM_START();
  468.         DIG_PWM_EN();
  469.         break;
  470.     default:
  471.         break;
  472.     }
  473. }
  474. /*********************************************************************************************************
  475. ** Function name:       irCheck
  476. ** Descriptions:        红外线传感器检测。
  477. ** input parameters:    无
  478. ** output parameters:   无
  479. ** Returned value:      无
  480. *********************************************************************************************************/
  481. void irCheck (void)
  482. {
  483.     static INT8U ucState = 0;
  484.     static INT32U ulIRCheck;
  485.    
  486.     switch (ucState) {
  487.     case 0:
  488.         irSend(FRONT_SIDE_PWM_INDEX, 32200);                            /*  探测左右两侧近距            */
  489.         irSend(DIG_PWM_INDEX, 35000); //35000                                  /*  驱动斜角上的传感器检测      */
  490.         break;
  491.     case 1:
  492.         ulIRCheck = IR_READ();                                          /*  读取传感器状态              */
  493.         FRONTSIDE_PWM_DIS();                                            /*  禁止PWM发生器2              */
  494.         DIG_PWM_DIS();                                                  /*  禁止PWM发生器1              */
  495.         
  496.         if (ulIRCheck & IR_SIDE_LFT) {
  497.             GucDistance[__LEFT]   &= 0xfd;
  498.         } else {
  499.             GucDistance[__LEFT]   |= 0x02;
  500.         }

  501.         if (ulIRCheck & IR_DIG_LFT) {
  502.             GucDistance[__FRONTL]  = 0x00;
  503.         } else {
  504.             GucDistance[__FRONTL]  = 0x01;
  505.         }
  506.         
  507.         if (ulIRCheck & IR_DIG_RGT) {
  508.             GucDistance[__FRONTR]  = 0x00;
  509.         } else {
  510.             GucDistance[__FRONTR]  = 0x01;
  511.         }
  512.         if (ulIRCheck & IR_SIDE_RGT) {
  513.             GucDistance[__RIGHT]  &= 0xfd;
  514.         } else {
  515.             GucDistance[__RIGHT]  |= 0x02;
  516.         }
  517.         break;
  518.     case 2:
  519.         irSend(FRONT_SIDE_PWM_INDEX, 36000);                            /*  驱动检测左前右三个方向远距  */
  520.         
  521.         break;
  522.     case 3:
  523.         ulIRCheck = IR_READ();                                          /*  读取传感器状态              */
  524.         FRONTSIDE_PWM_DIS();                                            /*  禁止PWM发生器2              */
  525.         break;
  526.     case 4:
  527.         irSend(FRONT_SIDE_PWM_INDEX, 36000);                            /*  重复检测左前右三个方向远距  */
  528.         break;
  529.     case 5:
  530.         ulIRCheck &= IR_READ();                                         /*  读取传感器状态              */
  531.         FRONTSIDE_PWM_DIS();                                            /*  禁止PWM发生器2              */

  532.         
  533.         if (ulIRCheck & IR_SIDE_LFT) {
  534.             GucDistance[__LEFT]  &= 0xfe;
  535.         } else {
  536.             GucDistance[__LEFT]  |= 0x01;
  537.         }
  538.         if (ulIRCheck & IR_FRONT) {
  539.             GucDistance[__FRONT] &= 0xfe;
  540.         } else {
  541.             GucDistance[__FRONT] |= 0x01;
  542.         }
  543.         if (ulIRCheck & IR_SIDE_RGT) {
  544.             GucDistance[__RIGHT] &= 0xfe;
  545.         } else {
  546.             GucDistance[__RIGHT] |= 0x01;
  547.         }
  548.         break;
  549.     default:
  550.         break;
  551.     }
  552.     if (++ucState >= 6) {
  553.         ucState = 0;
  554.     }
  555. }

  556. /*********************************************************************************************************
  557. ** Function name:       mouseTurnback_C
  558. ** Descriptions:        后转
  559. ** input parameters:    无
  560. ** output parameters:   无
  561. ** Returned value:      无
  562. *********************************************************************************************************/
  563. void mouseTurnback_C(void)
  564. {
  565.     while (GmLeft.cState  != MOTOR_STOP);
  566.     while (GmRight.cState != MOTOR_STOP);
  567.     delayMs(1);

  568.     GucMouseState   = TURN_BACK;
  569.     GmRight.cDir    = MOTOR_GO_BACK;
  570.     GmRight.ulPulse = 85;//162 * 10;

  571.    
  572.     GmLeft.cDir     = MOTOR_GO_AHEAD;
  573.     GmLeft.ulPulse  = 85;//162 * 10;

  574.     GmLeft.cState   = MOTOR_RUN;
  575.     GmRight.cState  = MOTOR_RUN;
  576.    
  577.     GucMouseDir = (GucMouseDir + 2) % 4;                                /*  方向标记                    */
  578.    
  579.     while (GmRight.cState != MOTOR_STOP);
  580.     while (GmLeft.cState  != MOTOR_STOP);
  581.     delayMs(2);

  582. }
  583. /*********************************************************************************************************
  584. ** Function name:       mazeSearch
  585. ** Descriptions:        迷宫搜索,若发现分支路或者前方有挡板则退出
  586. ** input parameters:    无  
  587. ** output parameters:   无
  588. ** Returned value:      无
  589. *********************************************************************************************************/
  590. void mazeSearch(void)
  591. {
  592.     INT8S cL = 0, cR = 0;
  593.         INT32U temp1 = 0;
  594.         INT8U num = 0;
  595.         INT8U Gonum = 0;
  596.         INT8U DelayFlag = 0;
  597.        
  598.     /*
  599.      *  设定运行任务
  600.      */

  601.        
  602.     GucMouseState   = GO_AHEAD;
  603.         GlMaxSpeed = SEARCHSPEED;
  604.     GmRight.cDir  = MOTOR_GO_AHEAD;
  605.     GmLeft.cDir   = MOTOR_GO_AHEAD;
  606.         GmLeft.ulPulseCtr = 0;
  607.         GmRight.ulPulseCtr = 0;
  608.     GmRight.ulPulse = MAZETYPE * ONEBLOCK;
  609.     GmLeft.ulPulse  = MAZETYPE * ONEBLOCK;
  610.     GmRight.cState  = MOTOR_RUN;
  611.     GmLeft.cState   = MOTOR_RUN;
  612.    
  613.        
  614.         if(GLCoorflag || GRCoorflag)   /* 有转弯  */
  615.     {
  616.           GLCoorflag =0;
  617.           GRCoorflag =0;
  618.           DelayFlag  =1;
  619.       GoLong = 0;
  620.           num=20;
  621.           Gonum = 0;
  622.     }
  623.    
  624.         else { // 没有转弯  
  625.          
  626.       if(GoLong == 1){             //如果上一次跳出过MazeSearch
  627.           GoLong = 0;
  628.           Gonum = 20;
  629.           num = 0;
  630.       }
  631.       else{                        //一直在该函数内,没有跳出过,直走
  632.           num = 0;
  633.           Gonum = 0;
  634.       }                       
  635.     }

  636.     while (GmLeft.cState != MOTOR_STOP)
  637.         {
  638.                 if ((GmLeft.ulPulseCtr+num) >= (ONEBLOCK + Gonum))                          // 判断是否走完一格      
  639.                 {
  640.                         GmLeft.ulPulse    -= (ONEBLOCK + Gonum);
  641.                         GmRight.ulPulse   -= (ONEBLOCK + Gonum);
  642.                         GmLeft.ulPulse    += num;
  643.                         GmRight.ulPulse   += num;

  644.                         updateCPostion();                                  //更新当前坐标            
  645.                         wallCheck();

  646.                         GmLeft.ulPulseCtr = 0;
  647.                         GmRight.ulPulseCtr = 0;
  648.                         GmRight.ulPulse = MAZETYPE * ONEBLOCK;
  649.                         GmLeft.ulPulse  = MAZETYPE * ONEBLOCK;
  650.                         num =0;                                                    // num 清零
  651.                         Gonum = 0;
  652.                 }
  653.                
  654.                                

  655.                
  656.         if (GucDistance[__FRONT]) {                                       /*  前方有墙                    */
  657.             /*
  658.              *  设置继续前进的步数,以实现能停止在单元格中心的目的
  659.              */
  660.             GmRight.ulPulse = GmRight.ulPulseCtr + 74;//70,80,83,76
  661.             GmLeft.ulPulse  = GmLeft.ulPulseCtr  + 74;
  662.             /*
  663.              *  反复检测前方传感器的状态,以消除误判
  664.              */
  665.                         //GmLeft.cState = GmRight.cState = MOTOR_STOP;
  666.                         //delayMs(5000);
  667.                        
  668.                         //tempF = 35;
  669.                         if(PreTurnBflag == 1){
  670.                                 PreTurnBflag = 0;
  671.                                 while(GucDistance[__FRONT]) {
  672.                                         if((GmLeft.ulPulseCtr + 40) > GmLeft.ulPulse) {                        //48
  673.                                                 goto End;                                           /*  跳出循环,跳到程序结束处    */
  674.                                         }
  675.                                 }
  676.                         }
  677.                        
  678.         /*                if(PreTurnLflag || PreTurnRflag){
  679.                                 PreTurnLflag = 0;
  680.                                 PreTurnRflag = 0;
  681.                                 while (GucDistance[__FRONT]) {
  682.                                         if((GmLeft.ulPulseCtr + 35) > GmLeft.ulPulse) {                        //48
  683.                                                 goto End;                                    
  684.                                         }
  685.                                 }
  686.                                
  687.                         }*/
  688.                        
  689.             while (GucDistance[__FRONT]) {
  690.                 if ((GmLeft.ulPulseCtr + 40) > GmLeft.ulPulse) {                        //48
  691.                     goto End;                                           /*  跳出循环,跳到程序结束处    */
  692.                 }
  693.             }
  694.             /*
  695.              *  程序执行到此步表明出现误判,则重新设定电机运行任务
  696.              */            

  697.             GmRight.ulPulse = MAZETYPE * ONEBLOCK;
  698.             GmLeft.ulPulse  = MAZETYPE * ONEBLOCK;
  699.         }
  700.        
  701.                 if(DelayFlag)
  702.                 {
  703.                         DelayFlag = 0;
  704.                         temp1= GmRight.ulPulseCtr;
  705.                         while(GmRight.ulPulseCtr <= (temp1 + 24));                        //                        if(GucDistance[__LEFT] == 0)
  706.                                 cL = 1;
  707.                         if(GucDistance[__RIGHT] == 0)
  708.                                 cR = 1;
  709.                 }
  710.                
  711.         if (cL) {                                                       /*  是否允许检测左边            */
  712.             if ((GucDistance[__LEFT] & 0x01) == 0){                      /*  左边有支路                  */
  713.                 /*
  714.                  *  设置继续前进的步数,以实现能停止在单元格中心的目的
  715.                  */
  716.                 GmRight.ulPulse = GmRight.ulPulseCtr + 74;//82
  717.                 GmLeft.ulPulse  = GmLeft.ulPulseCtr  + 74;
  718.                 /*
  719.                  *  反复检测前方传感器的状态,以消除误判
  720.                  */       
  721.                                 //tempL = 37;                       
  722.                                
  723.                 while ((GucDistance[__LEFT] & 0x01) == 0) {
  724.                     if ((GmLeft.ulPulseCtr + 40) > GmLeft.ulPulse) {                //53
  725.                         GoLong = 1;
  726.                                                 goto End;                                       /*  跳出循环,跳到程序结束处    */
  727.                     }
  728.                 }
  729.                 /*
  730.                  *  程序执行到此步表明出现误判,则重新设定电机运行任务
  731.                  */
  732.                 GmRight.ulPulse = MAZETYPE * ONEBLOCK;
  733.                 GmLeft.ulPulse  = MAZETYPE * ONEBLOCK;
  734.             }
  735.         } else {                                                        /*  左边有墙时开始允许检测左边  */
  736.             if ( GucDistance[__LEFT] & 0x01) {
  737.                 cL = 1;
  738.             }
  739.         }
  740.                
  741.                
  742.         if (cR) {                                                       /*  是否允许检测右边            */
  743.             if ((GucDistance[__RIGHT] & 0x01) == 0) {                     /*  右边有支路                  */
  744.                 /*
  745.                  *  设置继续前进的步数,以实现能停止在单元格中心的目的
  746.                  */
  747.                 GmRight.ulPulse = GmRight.ulPulseCtr + 74;
  748.                 GmLeft.ulPulse  = GmLeft.ulPulseCtr  + 74;
  749.                 /*
  750.                  *  反复检测前方传感器的状态,以消除误判
  751.                  */
  752.                                 //tempR = 37;
  753.                        
  754.                 while ((GucDistance[__RIGHT] & 0x01) == 0) {
  755.                     if ((GmLeft.ulPulseCtr + 40) > GmLeft.ulPulse) {                        //53
  756.                         GoLong = 1;
  757.                                                 goto End;                                       /*  跳出循环,跳到程序结束处    */
  758.                     }
  759.                 }
  760.                 /*
  761.                  *  程序执行到此步表明出现误判,则重新设定电机运行任务
  762.                  */
  763.                 GmRight.ulPulse = MAZETYPE * ONEBLOCK;
  764.                 GmLeft.ulPulse  = MAZETYPE * ONEBLOCK;
  765.             }
  766.         } else {
  767.             if ( GucDistance[__RIGHT] & 0x01) {                           /*  右边有墙时开始允许检测右边  */
  768.                 cR = 1;
  769.             }
  770.         }
  771.     }
  772. End:
  773.         PreTurnBflag = 0;
  774.         PreTurnLflag = 0;
  775.         PreTurnRflag = 0;
  776.         updateCPostion();
  777.         wallCheck();       
  778. }

  779. /*********************************************************************************************************
  780. ** Function name:       mouseTurnright
  781. ** Descriptions:        右转
  782. ** input parameters:    无
  783. ** output parameters:   无
  784. ** Returned value:      无
  785. *********************************************************************************************************/
  786. void mouseTurnright(void)
  787. {

  788.        
  789.     GRCoorflag = 1;                                                /*右转的标志*/
  790.         PreTurnBflag = 0;
  791.         PreTurnRflag = 1;
  792.         PreTurnLflag = 0;
  793.     GucMouseState  = TURN_RIGHT;                                 

  794.     GmLeft.lSpeed      = SEARCHSPEED;
  795.   
  796.     GmRight.cDir    = MOTORPAUSE;                                /*抱死状态*/
  797.    
  798.       
  799.     GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
  800.     GmRight.ulPulse = MAZETYPE * ONEBLOCK;
  801.     GmLeft.ulPulseCtr = 0;
  802.     GmRight.ulPulseCtr  = 0;

  803.        
  804.     while(GmLeft.ulPulseCtr <= 78);                //88
  805.        

  806.     GmRight.cDir    = MOTOR_GO_AHEAD;  
  807.          // GmLeft.lSpeed = GmRight.lSpeed = 30;
  808.     GucMouseDir     = (GucMouseDir + 1) % 4;  /*  方向标记  */  
  809.    
  810.    

  811.     /*
  812.      *  设定运行任务
  813.      */
  814.     GucMouseState   = GO_AHEAD;
  815.     GlMaxSpeed      =  SEARCHSPEED/4;

  816.     GmRight.cDir    = MOTOR_GO_AHEAD;
  817.     GmLeft.cDir     = MOTOR_GO_AHEAD;
  818.     GmRight.ulPulseCtr  = 0;
  819.     GmLeft.ulPulseCtr   = 0;
  820.    
  821.     GmLeft.lSpeed = SEARCHSPEED/4 - 3;//40
  822.     GmRight.lSpeed = SEARCHSPEED/4 - 3;
  823.     GmLeft.ulPulse   =   ONEBLOCK;
  824.     GmRight.ulPulse  =   ONEBLOCK;
  825.     GmLeft.cState  = MOTOR_RUN;
  826.     GmRight.cState   = MOTOR_RUN;

  827.    //GmLeft.lSpeed =  GmRight.lSpeed;   

  828. }


  829. /*********************************************************************************************************
  830. ** Function name:       mouseTurnleft
  831. ** Descriptions:        左转
  832. ** input parameters:    无
  833. ** output parameters:   无
  834. ** Returned value:      无
  835. *********************************************************************************************************/
  836. void mouseTurnleft(void)
  837. {       
  838.     GLCoorflag  = 1;
  839.         PreTurnBflag = 0;
  840.         PreTurnRflag = 0;
  841.         PreTurnLflag = 1;
  842.        
  843.         GucMouseState    = TURN_LEFT;
  844.    
  845.         GmRight.lSpeed      = SEARCHSPEED;

  846.     GmLeft.cDir     = MOTORPAUSE;
  847.    
  848.       
  849.     GmRight.ulPulse =  MAZETYPE * ONEBLOCK;
  850.     GmLeft.ulPulse = MAZETYPE * ONEBLOCK;
  851.     GmRight.ulPulseCtr  = 0;
  852.     GmLeft.ulPulseCtr   = 0;
  853.    
  854.        
  855.     while(GmRight.ulPulseCtr<=78);                //88


  856.     GmLeft.cDir     = MOTOR_GO_AHEAD;
  857.         //GmLeft.lSpeed = GmRight.lSpeed = 30;
  858.         GucMouseDir     = (GucMouseDir + 3) % 4;  /*  方向标记   */  

  859.     /*
  860.      *  设定运行任务
  861.      */
  862.     GucMouseState   = GO_AHEAD;
  863.     GlMaxSpeed      =SEARCHSPEED / 4;
  864.     GmRight.cDir    = MOTOR_GO_AHEAD;
  865.     GmLeft.cDir     = MOTOR_GO_AHEAD;
  866.     GmLeft.ulPulseCtr  = 0;
  867.     GmRight.ulPulseCtr = 0;
  868.    
  869.     GmLeft.lSpeed = SEARCHSPEED/4-3;//40
  870.     GmRight.lSpeed =SEARCHSPEED/4-3;
  871.    
  872.     GmLeft.ulPulse   =  ONEBLOCK;
  873.     GmRight.ulPulse  =  ONEBLOCK;
  874.     GmRight.cState  = MOTOR_RUN;
  875.     GmLeft.cState   = MOTOR_RUN;
  876.    // GmRight.lSpeed = GmLeft.lSpeed;

  877. }


  878. /*********************************************************************************************************
  879. ** Function name:       mouseTurnback
  880. ** Descriptions:        后转
  881. ** input parameters:    无
  882. ** output parameters:   无
  883. ** Returned value:      无
  884. *********************************************************************************************************/
  885. void mouseTurnback(void)
  886. {
  887.         if((GucMouseTask == BACK) && (hy == 1)){
  888.                 //GmLeft.ulPulse = 40;
  889.                 //GmRight.ulPulse =40;
  890.                 GmLeft.ulPulseCtr = 0;
  891.                 GmRight.ulPulseCtr = 0;
  892.                 GmLeft.cState = MOTOR_RUN;
  893.                 GmRight.cState = MOTOR_RUN;
  894.                 GmLeft.cDir = MOTOR_GO_AHEAD;
  895.                 GmRight.cDir = MOTOR_GO_AHEAD;
  896.         }
  897.     /*
  898.      *  等待停止
  899.      */
  900.     while (GmLeft.cState  != MOTOR_STOP);
  901.     while (GmRight.cState != MOTOR_STOP);
  902.     delayMs(50);
  903.        
  904.     GucMouseState   = TURN_BACK;
  905.     /*
  906.      *  开始后转
  907.      */
  908.     GmRight.cDir    = MOTOR_GO_BACK;
  909.     GmRight.ulPulse = 85;//90
  910.    
  911.     GmLeft.cDir     = MOTOR_GO_AHEAD;
  912.     GmLeft.ulPulse  = 85;
  913.        
  914.         PreTurnBflag = 1;
  915.         PreTurnRflag = 0;
  916.         PreTurnLflag = 0;
  917.    
  918.     GmLeft.cState   = MOTOR_RUN;
  919.     GmRight.cState  = MOTOR_RUN;
  920.        
  921.         GucMouseDir = (GucMouseDir + 2) % 4;
  922.     /*
  923.      *  等待后转完成
  924.      */
  925.     while (GmLeft.cState  != MOTOR_STOP);
  926.     while (GmRight.cState != MOTOR_STOP);
  927.         delayMs(50);
  928.        
  929. /*        if(hy == 1){
  930.                 GmLeft.ulPulse = 20;
  931.                 GmRight.ulPulse = 20;
  932.                 GmLeft.cState = MOTOR_RUN;
  933.                 GmRight.cState = MOTOR_RUN;
  934.                 GmLeft.cDir = MOTOR_GO_AHEAD;
  935.                 GmRight.cDir = MOTOR_GO_AHEAD;
  936.         }
  937. */
  938.        
  939. }
  940. /*********************************************************************************************************
  941. ** Function name:       mouseTurnright_C
  942. ** Descriptions:        右转
  943. ** input parameters:    无
  944. ** output parameters:   无
  945. ** Returned value:      无
  946. *********************************************************************************************************/
  947. void mouseTurnright_C(void)
  948. {
  949.     INT8U tempnum=0;
  950.         Rturnflag = 1;
  951.        
  952.         if(BlankNum!=0)
  953.         {
  954.                 if (BlankNum==1)
  955.                 {
  956.                         tempnum=125;  //125 ..//120
  957.                 }
  958.                 else tempnum=125;  //133
  959.         }       
  960.         else  tempnum=125;

  961.     GucMouseState  = TURN_RIGHT;
  962.         GmLeft.ulPulse  = GmLeft.ulPulseCtr  + tempnum;//133;  //140 ??
  963.         GmLeft.lSpeed   = 80;//80;    //150
  964.    
  965.     GmRight.ulPulse = GmRight.ulPulseCtr + 21;//24;  //  30
  966.     GmRight.lSpeed  = 12;//7;    //12
  967.    
  968.    
  969.     GmRight.cDir    = MOTOR_GO_AHEAD;
  970.     GmLeft.cDir     = MOTOR_GO_AHEAD;
  971.    
  972.     GmRight.cState  = MOTOR_RUN;
  973.     GmLeft.cState   = MOTOR_RUN;
  974.    
  975.     //while (GmRight.cState  != __MOTORSTOP);
  976.     while ( GmLeft.ulPulseCtr<=(GmLeft.ulPulse-30));  // 30
  977.    
  978.    
  979.       GucMouseDir     = (GucMouseDir + 1) % 4;  /*  方向标记  */
  980.     //while (GmLeft.cState  != __MOTORSTOP);
  981.       GmRight.ulPulse = MAZETYPE * ONEBLOCK;
  982.       GmLeft.ulPulse  = MAZETYPE * ONEBLOCK;  
  983.    
  984.    GmRight.ulPulseCtr = 0;
  985.    GmLeft.ulPulseCtr  = 0;  
  986.    GmRight.ulPulse = GmRight.ulPulseCtr + 10;

  987.     GmRight.cState  = MOTOR_RUN;
  988.     GmLeft.cState   = MOTOR_RUN;
  989.     GmLeft.lSpeed = GmRight.lSpeed +10 ;
  990.    

  991.      Rturnflag = 0;
  992.      

  993.    
  994.    if(turnback_secend_step==1)//转180度的第二个弯
  995.        SeveralStep(12);
  996.    else{
  997.      if(turnback_first_step==1)   //转180度的第一个弯
  998.        SeveralStep(55);//60
  999.      else
  1000.        SeveralStep(34);
  1001.    }
  1002. }

  1003. /*********************************************************************************************************
  1004. ** Function name:      SeveralStep
  1005. ** Descriptions:      
  1006. ** input parameters:    执行几步
  1007. ** output parameters:   无
  1008. ** Returned value:      无
  1009. *********************************************************************************************************/
  1010. void SeveralStep(INT8U step)
  1011. {
  1012.       GucMouseState   = GO_AHEAD;
  1013.     GlMaxSpeed      =  53;
  1014.     GmRight.cDir    = MOTOR_GO_AHEAD;
  1015.     GmLeft.cDir     = MOTOR_GO_AHEAD;
  1016.    
  1017.    GmRight.ulPulseCtr = 0;
  1018.    GmLeft.ulPulseCtr  = 0;  
  1019.     GmRight.ulPulse = step+10;//__GmRight.uiPulse + cNBlock * ONEBLOCK - 6;
  1020.     GmLeft.ulPulse  = step+10;//__GmLeft.uiPulse  + cNBlock * ONEBLOCK - 6;
  1021.     GmRight.cState  = MOTOR_RUN;
  1022.     GmLeft.cState   = MOTOR_RUN;
  1023.    
  1024.    
  1025.     if((turnback_first_step==1)||(turnback_secend_step==1))
  1026.     {
  1027.       GmRight.lSpeed=40;
  1028.       GmLeft.lSpeed=40;  
  1029.     }
  1030.     else
  1031.     {
  1032.       GmRight.lSpeed=50;
  1033.       GmLeft.lSpeed=50;      
  1034.     }
  1035.     if (GucDistance[__FRONT]) {                                   /*  前方有墙,则跳出程序        */
  1036.           if((GmRight.ulPulse - GmRight.ulPulseCtr)<=10)
  1037.           {
  1038.             GmRight.ulPulse = GmRight.ulPulseCtr + 8;
  1039.             GmLeft.ulPulse  = GmLeft.ulPulseCtr  + 8;
  1040.           }
  1041.           else
  1042.           {
  1043.             GmRight.ulPulse = GmRight.ulPulseCtr + 18;
  1044.             GmLeft.ulPulse  = GmLeft.ulPulseCtr  + 18;
  1045.           }
  1046.           while (GucDistance[ __FRONT]) {
  1047.                 if ((GmLeft.ulPulseCtr + 10) > GmLeft.ulPulse) {
  1048.                     goto SeveralEnd;
  1049.                 }
  1050.                 else
  1051.                 {
  1052.                     GmRight.ulPulse = step+8;
  1053.                     GmLeft.ulPulse  = step+8;
  1054.                 }
  1055.                   
  1056.             }
  1057.         }   
  1058.    
  1059.     while(GmLeft.ulPulseCtr+10 < GmLeft.ulPulse);
  1060.     while(GmRight.ulPulseCtr+10 < GmRight.ulPulse);

  1061. SeveralEnd:;
  1062.    
  1063. }


  1064. /*********************************************************************************************************
  1065. ** Function name:       mouseTurnleft_C
  1066. ** Descriptions:        左转
  1067. ** input parameters:    无
  1068. ** output parameters:   无
  1069. ** Returned value:      无
  1070. *********************************************************************************************************/
  1071. void mouseTurnleft_C(void)
  1072. {

  1073.     INT8U tempnum=0;
  1074.         Lturnflag = 1;
  1075.        
  1076.         if(BlankNum!=0)
  1077.         {
  1078.                 if (BlankNum==1)
  1079.                 {
  1080.                         tempnum=125;   //125  ,120
  1081.                 }
  1082.                 else tempnum=125;   //133
  1083.         }       
  1084.         else  tempnum=125;   //131
  1085.    
  1086.     GucMouseState   = TURN_LEFT;
  1087.     GmRight.ulPulse  = GmRight.ulPulseCtr  + tempnum;//133;  //140
  1088.     GmRight.lSpeed   = 80;//80;//150;   
  1089.    
  1090.     GmLeft.ulPulse = GmLeft.ulPulseCtr + 21;//24;  // 30
  1091.     GmLeft.lSpeed  = 12;//7;//9;
  1092.    
  1093.    
  1094.     GmLeft.cDir    = MOTOR_GO_AHEAD;
  1095.     GmRight.cDir     = MOTOR_GO_AHEAD;
  1096.    
  1097.     GmRight.cState  = MOTOR_RUN;
  1098.     GmLeft.cState   = MOTOR_RUN;
  1099.    
  1100.     //while (GmRight.cState  != __MOTORSTOP);
  1101.     while ( GmRight.ulPulseCtr<=(GmRight.ulPulse-30));  // 30
  1102.     //while (GmLeft.cState  != __MOTORSTOP);
  1103.    
  1104.      GmRight.lSpeed  =  GmLeft.lSpeed + 10;
  1105.      

  1106.    GmRight.ulPulseCtr = 0;
  1107.    GmLeft.ulPulseCtr  = 0;  
  1108.    GmLeft.ulPulse = GmRight.ulPulseCtr + 10;

  1109.     GmRight.cState  = MOTOR_RUN;
  1110.     GmLeft.cState   = MOTOR_RUN;
  1111.     //while(keyCheck() == false);
  1112.     GucMouseDir     = (GucMouseDir + 3) % 4;  /*  方向标记                    */  
  1113.   
  1114.    GmRight.ulPulseCtr = 0;
  1115.    GmLeft.ulPulseCtr  = 0;  
  1116.    
  1117.    Lturnflag = 0;
  1118.   /*
  1119.    if(turnback_first_step==1)   //转180度的第一个弯
  1120.        SeveralStep(55);//60
  1121.     else if(turnback_secend_step==1)//转180度的第二个弯
  1122.        SeveralStep(10);

  1123.     else
  1124.        SeveralStep(30);
  1125. */
  1126.    if(turnback_secend_step==1)//转180度的第二个弯
  1127.        SeveralStep(12);
  1128.    else{
  1129.      if(turnback_first_step==1)   //转180度的第一个弯
  1130.        SeveralStep(55);//60
  1131.      else
  1132.        SeveralStep(34);
  1133.    }      
  1134. }
  1135. /*********************************************************************************************************
  1136. ** Function name:       冲刺时候用的mouseGoahead_C
  1137. ** Descriptions:        前进N格
  1138. ** input parameters:    iNblock: 前进的格数
  1139. ** output parameters:   无
  1140. ** Returned value:      无
  1141. *********************************************************************************************************/
  1142. void mouseGoahead_C (INT8S  nBlock)
  1143. {
  1144.     INT8U num=0;
  1145.     INT8U cNBlock=0,Block=0;
  1146.     INT8U coner=0;
  1147.     INT8U blacknum =0;
  1148.     INT8U first_step_Back =0;

  1149.     INT8S cL = 0, cR = 0,left_first_flag=1,right_first_flag=1;

  1150.     static INT8U step_count=0;      
  1151.    
  1152.     step_count++;
  1153.    
  1154.     if((coner==2)&&((GCountInfo[step_count]&0xc0)==0x80))//(GCountInfo[step_count]==0x81))
  1155.       turnback_first_step=1;
  1156.     if((coner==1)&&((GCountInfo[step_count]&0xc0)==0x40))//(GCountInfo[step_count]==0x41))
  1157.       turnback_first_step=1;
  1158.    
  1159.     if(first_step==1)
  1160.     {   first_step_Back =1;
  1161.             num=10;
  1162.         first_step=0;
  1163.     }
  1164.     else
  1165.         num=70;
  1166.     cNBlock=nBlock&0x0f;
  1167.     Block=cNBlock;
  1168.     coner=nBlock&0xc0;
  1169.     coner>>=6;
  1170.    
  1171.     blacknum = nBlock&0x30;
  1172.     blacknum >>=4;
  1173.    
  1174.     /*
  1175.      *  设定运行任务
  1176.      */
  1177.    
  1178.     GucMouseState   = GO_AHEAD;
  1179.     GmRight.cDir    = MOTOR_GO_AHEAD;
  1180.     GmLeft.cDir     = MOTOR_GO_AHEAD;
  1181.    
  1182.    
  1183.     if(Block>=4)
  1184.     {   
  1185.            
  1186.        if(Spurt_num_count  == 1)
  1187.           GlMaxSpeed   =  230;         //230
  1188.        if(Spurt_num_count  == 2)  
  1189.           GlMaxSpeed    =  250;        //250
  1190.     }
  1191.     if(Block==3)
  1192.     {
  1193.                 if (Spurt_num_count  == 1)
  1194.                         GlMaxSpeed    =  200;
  1195.                 if (Spurt_num_count  == 2)
  1196.                         GlMaxSpeed    =  240;
  1197.         }
  1198.     if(blacknum)
  1199.     {  if(Spurt_num_count  == 1)
  1200.          GlMaxSpeed     =  200;
  1201.        if(Spurt_num_count  == 2)
  1202.           GlMaxSpeed    =  230;
  1203.     }
  1204.     else{
  1205.    
  1206.        if(Spurt_num_count  == 1)
  1207.          GlMaxSpeed     =  MAXSPEED;
  1208.        if(Spurt_num_count  == 2)
  1209.           GlMaxSpeed    =  MAXSPEED2;
  1210.    
  1211.     }
  1212. //        GlMaxSpeed      =   MAXSPEED;  
  1213. //   GmRight.cDir    = MOTOR_GO_AHEAD;
  1214. //   GmLeft.cDir     = MOTOR_GO_AHEAD;
  1215.    GmRight.ulPulseCtr = 0;
  1216.    GmLeft.ulPulseCtr  = 0;  
  1217.     GmRight.ulPulse = cNBlock * ONEBLOCK-15 ;//GmRight.ulPulse + cNBlock * ONEBLOCK - 6;
  1218.     GmLeft.ulPulse  = cNBlock * ONEBLOCK-15 ;//GmLeft.ulPulse  + cNBlock * ONEBLOCK - 6;
  1219.     GmRight.cState  = MOTOR_RUN;
  1220.     GmLeft.cState   = MOTOR_RUN;
  1221.    
  1222.    GmLeft.lSpeed = GmRight.lSpeed;   /*********调试加***********/
  1223.     switch(coner)
  1224.     {
  1225.       case 0: left_flag=0;
  1226.               right_flag=0;
  1227.               break;
  1228.       case 1: left_flag=1;
  1229.               right_flag=0;
  1230.               break;
  1231.       case 2: left_flag=0;
  1232.              right_flag=1;
  1233.              break;
  1234.       default:
  1235.         break;
  1236.     }
  1237.    
  1238.         /*if(Block==3)
  1239.     {   
  1240.        if(Spurt_num_count  == 1)
  1241.           GlMaxSpeed    =  200;                        //200
  1242.        if(Spurt_num_count  == 2)
  1243.           GlMaxSpeed    =  240;          //240
  1244.       
  1245.     }
  1246.     if(Block>3)
  1247.     {   
  1248.            
  1249.        if(Spurt_num_count  == 1)
  1250.           GlMaxSpeed    =  250;                //250
  1251.        if(Spurt_num_count  == 2)
  1252.           GlMaxSpeed    =  280;     //280
  1253.     }*/
  1254.   
  1255.     if(cNBlock==1)
  1256.     {
  1257.       if(GucMouseTask == BACKTOSTART1  && first_step_Back == 1){
  1258.          if(GmLeft.ulPulse   <= 45 ||GmRight.ulPulse   <= 45 ){
  1259.             cR=1;
  1260.             cL=1;
  1261.             first_step_Back = 0;
  1262.          }
  1263.          else{
  1264.             cR=0;
  1265.             cL=0;
  1266.          }
  1267.        
  1268.       }
  1269.       else{
  1270.             cR=1;
  1271.             cL=1;
  1272.       }
  1273.     }
  1274.    
  1275.     if(coner==0)
  1276.     {
  1277.                 if(Block==1)
  1278.                 {
  1279.                         GmLeft.ulPulse  -= 55;  //45
  1280.                         GmRight.ulPulse -= 55;  //45
  1281.                 }
  1282.                 else
  1283.                 {
  1284.                         GmLeft.ulPulse  -= 68;//
  1285.                         GmRight.ulPulse -= 68;//60;
  1286.                 }
  1287.     }
  1288.    
  1289.     while (GmLeft.cState != MOTOR_STOP)
  1290.     {
  1291.         if(left_first_flag==1)
  1292.         {
  1293.           if ((GmLeft.ulPulseCtr+num) >= ONEBLOCK)
  1294.           {                          /*  判断是否走完一格            */
  1295.               GmLeft.ulPulse    -= ONEBLOCK;
  1296.               GmLeft.ulPulse    += num;
  1297.               GmLeft.ulPulseCtr -= ONEBLOCK;
  1298.               GmLeft.ulPulseCtr += num;
  1299.               cNBlock--;
  1300.               if(blacknum)
  1301.                      blacknum--;
  1302.               left_first_flag=0;
  1303.          }
  1304.         }
  1305.         else   if (GmLeft.ulPulseCtr >= ONEBLOCK)
  1306.           {                          /*  判断是否走完一格    */
  1307.               GmLeft.ulPulse    -= ONEBLOCK;
  1308.               GmLeft.ulPulseCtr -= ONEBLOCK;
  1309.               
  1310.               cNBlock--;
  1311.               if(blacknum)
  1312.                      blacknum--;
  1313.           }
  1314.         if(right_first_flag==1)
  1315.         {
  1316.           if ((GmRight.ulPulseCtr+num) >= ONEBLOCK) {                         /*  判断是否走完一格            */
  1317.               GmRight.ulPulse    -= ONEBLOCK;
  1318.               GmRight.ulPulse    += num;
  1319.               GmRight.ulPulseCtr -= ONEBLOCK;
  1320.               GmRight.ulPulseCtr += num;
  1321.               right_first_flag=0;
  1322.           }
  1323.         }
  1324.         else  if (GmRight.ulPulseCtr >= ONEBLOCK) {                         /*  判断是否走完一格            */
  1325.             GmRight.ulPulse    -= ONEBLOCK;
  1326.             GmRight.ulPulseCtr -= ONEBLOCK;

  1327.         }
  1328.         if (GucDistance[__FRONT]) {                                   /*  前方有墙,则跳出程序        */
  1329.             GmRight.ulPulse = GmRight.ulPulseCtr + 21;                                       
  1330.             GmLeft.ulPulse  = GmLeft.ulPulseCtr  + 21;                //30
  1331.             while (GucDistance[ __FRONT]) {
  1332.                 if ((GmLeft.ulPulseCtr + 10) > GmLeft.ulPulse) {               
  1333.                     goto End;
  1334.                 }
  1335.             }
  1336.         }
  1337.         if(Block==1)
  1338.         {   if(Spurt_num_count  == 1){
  1339.          
  1340.                         GmRight.lSpeed =  53;//70;//53;//33;
  1341.                         GmLeft.lSpeed  =  53;//70;//53;//33;
  1342.                         GlMaxSpeed     =  65;//80;//65;//40;
  1343.         }
  1344.         if(Spurt_num_count  == 2)
  1345.         {
  1346.                         GmRight.lSpeed   =  53;//60;//53;//33;
  1347.                         GmLeft.lSpeed    =  53;//60;//53;//33;
  1348.                         GlMaxSpeed       =  65;//70;//65;//40;
  1349.         }
  1350.         }
  1351.         if(Block >=2 && cNBlock<2)
  1352.         {
  1353.            if(Spurt_num_count  == 1){
  1354.          
  1355.             GmRight.lSpeed =  70;//70;//60; //70
  1356.             GmLeft.lSpeed  =  70;//70;//60;
  1357.             GlMaxSpeed     =  85;//85;//85;//70;           
  1358.            }
  1359.            if(Spurt_num_count  == 2){
  1360.                                 GmRight.lSpeed  =  80;//80;//70;//60; //70
  1361.                                 GmLeft.lSpeed   =  80;//80;//70;//60;
  1362.                                 GlMaxSpeed      =  85;//95;//85;//70;
  1363.             
  1364.            }
  1365.            
  1366.          
  1367.         }
  1368.         if (cNBlock <= 2) {
  1369.          
  1370.             if (cL&&left_flag) {                                                   /*  是否允许检测左边            */
  1371.             
  1372.                 if ((GucDistance[ __LEFT] & 0x01) == 0) {             /*  左边有支路,跳出程序        */
  1373.                     GmRight.ulPulse = GmRight.ulPulseCtr + 20;
  1374.                     GmLeft.ulPulse  = GmLeft.ulPulseCtr  + 20;
  1375.                     while ((GucDistance[ __LEFT] & 0x01) == 0) {
  1376.                         if ((GmLeft.ulPulseCtr + 8) > GmLeft.ulPulse)   //12
  1377.                                                 {                     
  1378.                             goto End;
  1379.                         }
  1380.                     }
  1381.                 }
  1382.             } else {                                                    /*  左边有墙时开始允许检测左边  */
  1383.                 if ( (GucDistance[ __LEFT] & 0x01)&&(cNBlock<2)) {
  1384.                     cL = 1;
  1385.                 }
  1386.             }
  1387.             if (cR&&right_flag) {                                                   /*  是否允许检测右边            */

  1388.                 if ((GucDistance[__RIGHT] & 0x01) == 0) {             /*  右边有支路,跳出程序        */
  1389.                     GmRight.ulPulse = GmRight.ulPulseCtr + 20;
  1390.                     GmLeft.ulPulse  = GmLeft.ulPulseCtr  + 20;
  1391.                     while ((GucDistance[ __RIGHT] & 0x01) == 0) {
  1392.                         if ((GmLeft.ulPulseCtr + 8) > GmLeft.ulPulse)   //12
  1393.                                                 {

  1394.                             goto End;
  1395.                         }
  1396.                     }
  1397.                 }
  1398.             } else {                  //修改过
  1399.                 if ( (GucDistance[__RIGHT] & 0x01)&&(cNBlock<2)) {                   /*  右边有墙时开始允许检测右边  */
  1400.                     cR = 1;
  1401.                 }
  1402.             }
  1403.         }
  1404.     }
  1405.     /*
  1406.      *  设定运行任务,让电脑鼠走到支路的中心位置
  1407.      */
  1408. End:   
  1409.   if(right_flag)
  1410.     mouseTurnright_C();
  1411.   if(left_flag)
  1412.     mouseTurnleft_C();
  1413.   left_flag=0;
  1414.   right_flag=0;
  1415.   turnback_first_step=0;
  1416.   turnback_secend_step=0;
  1417.   
  1418.   if((coner==2)&&(GCountInfo[step_count]==0x81))
  1419.       turnback_secend_step=1;   
  1420.   if((coner==1)&&(GCountInfo[step_count]==0x41))
  1421.       turnback_secend_step=1;
  1422.   
  1423.   BlankNum=(GCountInfo[step_count]&0x30)>>4;  //加的。。。计算
  1424.   
  1425. }
  1426. /*********************************************************************************************************
  1427. ** Function name:       keyCheck
  1428. ** Descriptions:        读取按键
  1429. ** input parameters:    无
  1430. ** output parameters:   无
  1431. ** Returned value:      true:  按键已按下
  1432. **                      false: 按键未按下
  1433. *********************************************************************************************************/
  1434. BOOLEAN keyCheck (void)
  1435. {
  1436.     if (KEY_PRESS()) {                                                  /*  按键按下                    */
  1437.         delayMs(10);                                                    /*  延时10ms消抖动              */
  1438.         if (KEY_PRESS()) {                                              /*  按键按下                    */
  1439.             delayMs(10);                                                /*  延时10ms消抖动              */
  1440.             while(!KEY_PRESS());                                        /*  等待按键释放                */
  1441.             return (true);
  1442.         }
  1443.     }
  1444.     return (false);
  1445. }

  1446. /*********************************************************************************************************
  1447. ** Function name:       keyCheck_C
  1448. ** Descriptions:        读取按键
  1449. ** input parameters:    无
  1450. ** output parameters:   无
  1451. ** Returned value:      true:  按键已按下
  1452. **                      false: 按键未按下
  1453. *********************************************************************************************************/
  1454. BOOLEAN keyCheck_C (void)
  1455. {
  1456.     if (ZLG7298_KEY_READ()){                                                  /*  按键按下                    */
  1457.         delayMs(10);                                                    /*  延时10ms消抖动              */
  1458.         if (ZLG7298_KEY_READ()) {                                              /*  按键按下                    */
  1459.             delayMs(10);                                                /*  延时10ms消抖动              */
  1460.             while(!ZLG7298_KEY_READ());                                        /*  等待按键释放                */
  1461.             return (true);
  1462.         }
  1463.     }
  1464.     return (false);
  1465. }

  1466. /*********************************************************************************************************
  1467. ** Function name:       sysTickIsr
  1468. ** Descriptions:        定时中断扫描。
  1469. ** input parameters:    无
  1470. ** output parameters:   无
  1471. ** Returned value:      无
  1472. *********************************************************************************************************/
  1473. void sysTickIsr(void)
  1474. {
  1475.     static INT32S lLft = 0, lRgt = 0;
  1476.    
  1477.     /*
  1478.      *  如果左电机长时间停止,则断电
  1479.      */
  1480.     if (GmLeft.cState == MOTOR_STOP) {
  1481.         lLft++;
  1482.     } else {
  1483.         lLft = 0;
  1484.     }
  1485.     if (lLft >= 500) {
  1486.         MT_LFT_OFF();
  1487.     }
  1488.     /*
  1489.      *  如果右电机长时间停止,则断电
  1490.      */
  1491.     if (GmRight.cState == MOTOR_STOP) {
  1492.         lRgt++;
  1493.     } else {
  1494.         lRgt = 0;
  1495.     }
  1496.     if (lRgt >= 500) {
  1497.         MT_RGT_OFF();
  1498.     }
  1499.     /*
  1500.      *  红外线检测
  1501.      */
  1502.     irCheck();
  1503. }


  1504. /*********************************************************************************************************
  1505. ** Function name:       __wallCheck
  1506. ** Descriptions:        根据传感器检测结果判断是否存在墙壁,并保存
  1507. ** input parameters:    无
  1508. ** output parameters:   无
  1509. ** Returned value:      无(已经调试)
  1510. ** atention:    ucMap低四位从右到左,依次对应迷宫的(前右下左)(即north=1,east=2,south=4,west=8)
  1511. *********************************************************************************************************/
  1512. void wallCheck (void)
  1513. {
  1514.     INT8U ucMap = 0;
  1515.    
  1516.     ucMap &= ~MOUSEWAY_B;                         /*    初始化为后方无Wall,有墙壁为1,无墙为0  */

  1517.     if (GucDistance[__LEFT]  & 0x01)            /*  __GucDistance[__LEFT]为1,说明左边传感器检测到有wall!(真)*/
  1518.      {         
  1519.          ucMap |=  MOUSEWAY_L;                     /*  将相对方向上的信息转化为绝对信息保存                      */
  1520.      }   
  1521.      if (GucDistance[__FRONT] & 0x01)
  1522.      {
  1523.          ucMap |=  MOUSEWAY_F;
  1524.      }
  1525.      if (GucDistance[__RIGHT] & 0x01)
  1526.      {
  1527.         ucMap |=  MOUSEWAY_R;
  1528.      }
  1529.       GWallMap[GCurPosition]  = ucMap;             /*   将墙壁信息保存    */
  1530.       
  1531.       GWallMap[GCurPosition] |= PASSED;           /*  经过的单元格标记  */
  1532.       
  1533.       if(ucMap==0x00)   /* 十字路口 */
  1534.       {
  1535.           GWallMap[GCurPosition] |= SPECIAL;   
  1536.       }
  1537.      if(ucMap)
  1538.      {
  1539.           if( ucMap & NORTH)   /*  该单元格上方有墙 ,上面一单元格下方有墙  */
  1540.           {
  1541.                   if((GCurPosition+1)%STEPNUM)    /*  上方单元格不能对north更新 */
  1542.                   {
  1543.                       GWallMap[GCurPosition + 1] |= SOUTH;
  1544.                   }   
  1545.           }
  1546.          
  1547.           if( ucMap & EAST)   /*  该单元格右方有墙 ,右面一单元格左方有墙  */
  1548.           {
  1549.                   if(GCurPosition<WALLNUM)      /*  右方单元格不能对east更新 */
  1550.                   GWallMap[GCurPosition + STEPNUM] |= WEST;   
  1551.           }
  1552.           if( ucMap & SOUTH)   /*  该单元格下方有墙 ,下面一单元格上方有墙  */
  1553.           {
  1554.                    if(GCurPosition%STEPNUM)        /*  下方单元格不能对south更新 */
  1555.                    GWallMap[GCurPosition - 1] |= NORTH;   
  1556.           }
  1557.           if( ucMap & WEST)   /*  该单元格左方有墙 ,左面一单元格右方有墙  */
  1558.           {
  1559.                   if(GCurPosition>(STEPNUM-1))          /*  左方单元格不能对West更新 */
  1560.                   GWallMap[GCurPosition -STEPNUM] |= EAST;   
  1561.           }
  1562.      }


  1563. }


  1564. /*********************************************************************************************************
  1565. ** Function name:       voltageDetect
  1566. ** Descriptions:        电压检测,检测结果在7289 EX BOARD 上显示出来
  1567. ** input parameters:    无
  1568. ** output parameters:   无
  1569. ** Returned value:      无
  1570. *********************************************************************************************************/
  1571. void volDetect (void)
  1572. {
  1573.     INT32U i;
  1574.     INT32U ulAdcData;

  1575.     ADC_SMP_ON();                                                       /* 立即转换                     */
  1576.     while (!ADC_DATA_READY_FLG());                                      /* 读取AD0STAT的通道5的Done     */

  1577.     ulAdcData = 0;                                                      /* 第一次转换结果丢弃           */
  1578.     for (i = 0; i < 8; i ++) {                                          /* 连续8次采样                  */
  1579.         ADC_SMP_ON();                                                   /* 立即转换                     */
  1580.         while (!ADC_DATA_READY_FLG());                                  /* 读取AD0STAT的通道5的Done     */
  1581.         ulAdcData += ADC_DATA_GET();

  1582.     }
  1583.     ulAdcData >>= 3;                                                    /* 外部电阻分压,相当于/8       */
  1584.     ulAdcData = ulAdcData * (3300 * 3) / ((1 << 12) - 1);               /*  计算电池电压值(mV)          */

  1585.     zlg7289Download(0,6,1,(ulAdcData / 1000));                          /*  显示电压值整数部分,单位V   */
  1586.     zlg7289Download(0,7,0,(ulAdcData % 1000 ) / 100);                   /*  显示电压值小数部分,单位V   */
  1587. }
  1588. /*********************************************************************************************************
  1589. ** Function name:       sensorInit
  1590. ** Descriptions:        传感器控制初始化
  1591. ** input parameters:    无
  1592. ** output parameters:   无
  1593. ** Returned value:      无
  1594. *********************************************************************************************************/
  1595. void  sensorInit (void)
  1596. {
  1597.     IR_PIN_MODE_IN();                                                   /* 红外检测引脚配置为输入引脚   */
  1598.    
  1599.     PWM1TCR  = 0x02;                                                    /* 复位PWM1定时器               */
  1600.     PWM1TC   = 0;                                                       /* 定时器设置为0                */
  1601.     PWM1PR   = 0;                                                       /* 时钟不分频                   */
  1602.     PWM1MCR  = 0x02;                                                    /* 设置PWMMR0匹配后复位PWMTC,  */
  1603.                                                                         /* 并产生中断标志               */
  1604.     PWM1MR0  = FPCLK / 38000;                                           /* 38KHz频率                    */
  1605.     PWM1MR1  = FPCLK / 38000 / 2;
  1606.     PWM1MR2  = FPCLK / 38000 / 2;

  1607.     PWM1PCR  &= ~((1 << 9) | (1 << 10));                                /* 禁止输出                     */
  1608.     PWM1TCR  = 0x05;                                                    /* 启动定时器                   */
  1609. }
  1610. /*********************************************************************************************************
  1611. ** Function name:       stepMotorIint
  1612. ** Descriptions:        步进电机控制初始化
  1613. ** input parameters:    无
  1614. ……………………

  1615. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有程序51hei提供下载:
电脑鼠迷宫程序(冠军组,完美版).rar (488.92 KB, 下载次数: 23)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表