Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.71 KB | None | 0 0
  1. #pragma config(Sensor, S1,     touch,          sensorEV3_Touch)
  2. #pragma config(Sensor, S4,     Ultra,          sensorEV3_Ultrasonic)
  3. #pragma config(Motor,  motorA,          right,         tmotorEV3_Large, PIDControl, reversed, driveRight, encoder)
  4. #pragma config(Motor,  motorD,          left,          tmotorEV3_Large, PIDControl, reversed, driveLeft, encoder)
  5. //*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
  6.  
  7. const int ScreenHeight =127;    //USED FOR SIMULATION
  8. const int ScreenWidth  =177;
  9.  
  10. typedef struct{
  11.     int NorthWall;
  12.     int EastWall;
  13.     int SouthWall;
  14.     int WestWall;
  15. }Cell;
  16.  
  17. Cell Grid[4][6];
  18.  
  19. // Start Facing North
  20. int RobotDirection=0; // 0=North, 1=East, 2=South, 3=West
  21.  
  22. // Start in the 0,0 Cell // CHANGE BASED ON GIVEN POSITION
  23. int StartPosRow=0; // Starting position
  24. int StartPosCol=0;
  25.  
  26. int CurrentPosRow=StartPosRow; // Starting position //CHANGES BASED ON ALGORITHM
  27. int CurrentPosCol=StartPosCol;
  28.  
  29. int TargetPosRow=3; //"CHEESE" End Position (based on given)
  30. int TargetPosCol=0;
  31.  
  32.  
  33. void GridInit();
  34. void WallGen();
  35. void GridDraw();
  36. void DrawBot();
  37. void DisplayStartandEnd();
  38.  
  39.  
  40. task main()
  41. {
  42.     if(getUSDistance(Ultra) < 10){
  43.         motor[right] = 100;
  44.         ]
  45.  
  46.  
  47.     GridInit();
  48.     WallGen();
  49.    
  50.     while( (CurrentPosRow!=TargetPosRow) || (CurrentPosCol!=TargetPosCol)){
  51.         int temp= Solver();
  52.         GridDraw();
  53.         DisplayStartandEnd();
  54.         DrawBot();
  55.         sleep(1000);
  56.         eraseDisplay();
  57.     }
  58.  
  59.     while(true){
  60.         displayCenteredTextLine(5,"MAZE SOLVED !!");
  61.         sleep(500);
  62.         eraseDisplay();
  63.         sleep(500);
  64.     }
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71. //=====================================================================     //Initializes the values of the grid (struct) to 0 -> no wall, 1 - > wall
  72. void GridInit()
  73. {
  74.     for(int i=0;i<4;i++)
  75.         {
  76.         for(int j=0;j<6;j++)
  77.             {
  78.             Grid[i][j].NorthWall=0;
  79.             Grid[i][j].EastWall=0;
  80.             Grid[i][j].WestWall=0;
  81.             Grid[i][j].SouthWall=0;
  82.             }
  83.         }
  84. }
  85. //===================================================================== Row/Col
  86. void WallGen()
  87. {
  88.     int i=0;
  89.     int j=0;
  90.  
  91.     for(i=0;i<4;i++)
  92.     {                   //Creates the boundary of the grid, Left and Ride
  93.         Grid[i][0].WestWall=1;
  94.         Grid[i][5].EastWall=1;
  95.     }
  96.  
  97.     for(j=0;j<6;j++)
  98.     {                   //Creates the boundary of the grid, Top and Bottom
  99.         Grid[0][j].SouthWall=1;
  100.         Grid[3][j].NorthWall=1;
  101.     }
  102.  
  103.     Grid[0][0].NorthWall =1;  Grid[1][0].SouthWall =1;      //WE "DESIGN" THE GRID BASED ON THE GIVEN ONE DURING TEST DAY [ROW][COLUMN]
  104.     Grid[0][1].NorthWall =1;  Grid[1][1].SouthWall =1;
  105.     Grid[0][3].EastWall  =1;  Grid[0][4].WestWall  =1;
  106.     Grid[1][2].EastWall  =1;  Grid[1][3].WestWall  =1;
  107.     Grid[1][3].EastWall  =1;  Grid[1][4].WestWall  =1;
  108.     Grid[1][4].EastWall  =1;  Grid[1][5].WestWall  =1;
  109.     Grid[1][5].NorthWall =1;  Grid[2][5].SouthWall  =1;
  110.     Grid[3][0].EastWall  =1;  Grid[3][1].WestWall  =1;
  111.     Grid[3][4].SouthWall =1;  Grid[2][4].NorthWall  =1;
  112.  
  113.     for(j=1;j<4;j++){                   //???
  114.         Grid[2][j].NorthWall=1;
  115.         Grid[2][j].SouthWall=1;
  116.         Grid[3][j].SouthWall=1;
  117.         Grid[1][j].NorthWall=1;
  118.     }
  119. }
  120. //=====================================================================
  121. void GridDraw(){    //Used for simulation/Emulator
  122.     int XStart=0;       //(x,y) of start/end
  123.     int YStart=0;
  124.     int XEnd  =0;
  125.     int YEnd  =0;
  126.     for(int i=0;i<4;i++){
  127.         for(int j=0;j<6;j++){
  128.             if(Grid[i][j].NorthWall==1){
  129.                     XStart= j   *ScreenWidth/6;
  130.                     YStart=(i+1)*ScreenHeight/4;
  131.                     XEnd  =(j+1)*ScreenWidth/6;
  132.                     YEnd  =(i+1)*ScreenHeight/4;
  133.                     drawLine(XStart,YStart,XEnd,YEnd);
  134.             }
  135.             if (Grid[i][j].EastWall==1){
  136.                     XStart=(j+1)*ScreenWidth/6;
  137.                     YStart=(i)*ScreenHeight/4;
  138.                     XEnd  =(j+1)*ScreenWidth/6;
  139.                     YEnd  =(i+1)*ScreenHeight/4;
  140.                     drawLine(XStart,YStart,XEnd,YEnd);
  141.             }
  142.             if (Grid[i][j].WestWall==1){
  143.                     XStart= j   *ScreenWidth/6;
  144.                     YStart=(i)*ScreenHeight/4;
  145.                     XEnd  =(j)*ScreenWidth/6;
  146.                     YEnd  =(i+1)*ScreenHeight/4;
  147.                     drawLine(XStart,YStart,XEnd,YEnd);
  148.             }
  149.             if(Grid[i][j].SouthWall==1){
  150.                 XStart= j   *ScreenWidth/6;
  151.                 YStart=(i)*ScreenHeight/4;
  152.                 XEnd  =(j+1)*ScreenWidth/6;
  153.                 YEnd  =(i)*ScreenHeight/4;
  154.                 drawLine(XStart,YStart,XEnd,YEnd);
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. //=====================================================================
  161. void DrawBot(){ //Used for simulation/Emulator
  162.     int RobotXpixelPos=0;
  163.     int RobotYpixelPos=0;
  164.  
  165.     if(CurrentPosCol==0){
  166.             RobotXpixelPos=ScreenWidth/12;
  167.     }
  168.     else{
  169.         RobotXpixelPos=(2*CurrentPosCol+1)*ScreenWidth/12;
  170.     }
  171.  
  172.     if(CurrentPosRow==0){
  173.             RobotYpixelPos=ScreenHeight/8;
  174.     }
  175.     else{
  176.         RobotYpixelPos=(2*CurrentPosRow+1)*ScreenHeight/8;
  177.     }
  178.  
  179.     switch(RobotDirection){
  180.             case 0: displayStringAt(RobotXpixelPos,RobotYpixelPos,"^"); break; // Facing North
  181.             case 1: displayStringAt(RobotXpixelPos,RobotYpixelPos,">"); break; // Facing East
  182.             case 2: displayStringAt(RobotXpixelPos,RobotYpixelPos,"V"); break; // Facing South
  183.             case 3: displayStringAt(RobotXpixelPos,RobotYpixelPos,"<"); break; // Facing West
  184.             default: break;
  185.     }
  186. }
  187. //===================================================================== //We program this
  188. int Solver()
  189. {
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. }
  197.  
  198. //=====================================================================
  199. void DisplayStartandEnd(){ //Ending pios
  200.     int XpixelPos=0;
  201.     int YpixelPos=0;
  202.  
  203.     if(StartPosCol==0){
  204.             XpixelPos=ScreenWidth/12;
  205.     }
  206.     else{
  207.         XpixelPos=(2*StartPosCol+1)*ScreenWidth/12;
  208.     }
  209.  
  210.     if(StartPosRow==0){
  211.             YpixelPos=ScreenHeight/8;
  212.     }
  213.     else{
  214.         YpixelPos=(2*StartPosRow+1)*ScreenHeight/8;
  215.     }
  216.     displayStringAt(XpixelPos,YpixelPos,"S");
  217.  
  218.     if(TargetPosCol==0){
  219.             XpixelPos=ScreenWidth/12;
  220.     }
  221.     else{
  222.         XpixelPos=(2*TargetPosCol+1)*ScreenWidth/12;
  223.     }
  224.  
  225.     if(TargetPosRow==0){
  226.             YpixelPos=ScreenHeight/8;
  227.     }
  228.     else{
  229.         YpixelPos=(2*TargetPosRow+1)*ScreenHeight/8;
  230.     }
  231.     displayStringAt(XpixelPos,YpixelPos,"E");
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement