Help! Robot Programming

This is a discussion on Help! Robot Programming within the C Programming forums, part of the General Programming Boards category; Hey, im doing my first programming subject and i really need help with an assignment. I have to write a ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Question Help! Robot Programming

    Hey, im doing my first programming subject and i really need help with an assignment.

    I have to write a simulator program for a robot designed to move packages around in a warehouse environment.

    The map specifies the size, shape and initial positions of all the packages in the environment, as well as the positions of the walls, and the initial position and orientation of the robot. The walls will be represented by the "star" character *. The robot will be represented by the characters ^, v, < or >, depending on which direction it is facing.

    I can only give the robot 4 commands:
    L turn left
    R turn right
    F (try to) move forward
    P print the state of the environment

    When it executes an L or R instruction, the robot remains in the same location and only its orientation changes. When it executes the F instruction, the robot attempts to move a single step in whichever direction it is facing.


    Right now i can get the robot to scan the environment and print out what it sees. But i can't get my robot to move.. can anyone give me any pointers??

    heres the code i have so far.

    Code:
    #include <stdio.h>
    
    #define ROBOT       26
    #define WALL        27
    #define NONE        -1
    
    #define MAX_ROWS    80
    #define MAX_COLS    80
    
    #define EAST         0
    #define NORTH        1
    #define WEST         2
    #define SOUTH        3
    
    void scan_state(
                     int *pnrows,
                     int  ncols[MAX_ROWS],
                     int  object[MAX_ROWS][MAX_COLS],
                     int *pdirection
    	       );
    
    void print_state(
                     int nrows,
                     int ncols[MAX_ROWS],
                     int object[MAX_ROWS][MAX_COLS],
                     int direction
    	        );
    
    int main( void )
    {
      int nrows;                      // number of rows in environment
      int ncols[MAX_ROWS];            // number of columns in each row
      int object[MAX_ROWS][MAX_COLS]; // object at each location
      int direction;                  // which way the robot is facing
    
      // scan the state of the environment
      scan_state( &nrows, ncols, object,&direction );
    
      // print the state of the environment
      print_state( nrows, ncols, object, direction );
    
      return 0;
    }
    
    void scan_state(
                     int *pnrows,
                     int  ncols[MAX_ROWS],
                     int object[MAX_ROWS][MAX_COLS],
                     int *pdirection
                   )
    {
      int ch;
      int r,c,rr,rc;
    
      r = 0;
      c = -1;
    
      while (( ch = getchar() ) != '(' ) {
        c++;
        if ( ch == '\n' ) {
          ncols[r++] = c;
          c = -1;
        }
        else if ( ch == '*' ) {
          object[r][c] = WALL;
        }
        else if ((ch >='A' )&&( ch <= 'Z')){
           object [r][c]= ch - 'A';  
        }
        else if ( ch == '^' ) {
          object [r][c] = ROBOT;
          *pdirection = NORTH;
        }
        else if ( ch == '>' ) {
          object [r][c] = ROBOT;
          *pdirection = EAST;
        }
        else if ( ch == 'v' ) {
          object [r][c] = ROBOT;
          *pdirection = SOUTH;
        }
        else if ( ch == '<' ) {
          object [r][c] = ROBOT;
          *pdirection = WEST;
        }
        else {
          object [r][c] = NONE;
        }
      }
      
      //give the robot its own cordinates.
      if (object[r][c] == ROBOT ) {
      object[rr][rc] = ROBOT;
      }
      
      while (( ch = getchar() ) != ')' ) {
        c++;
        if ( ch == '\n' ) {
          ncols[r++] = c;
          c = -1;
        }
        else if ( ch == 'L' ) {
          if ( *pdirection == SOUTH ) {
              *pdirection = EAST;
          }
          else {
            *pdirection++;
          }
        }
        else if (ch == 'R' ) {
          if ( *pdirection == EAST ) {
               *pdirection = SOUTH;
          }
          else {
            *pdirection--;
          }
        }
      }
    
     *pnrows = r;
    }
    
    
    void print_state(
                     int nrows,
                     int ncols[MAX_ROWS],
                     int object[MAX_ROWS][MAX_COLS],
                     int direction
                    )
    {
    int r,c;
    
    for( r=0; r < nrows ; r++ ) {
       for( c=0; c < ncols[r]; c++ ) {
          if (object[r][c] == WALL) {
          printf( "*" );
          }
          if (object[r][c] == NONE) {
          printf( " " );
          }
          if ((object [r][c]>= 0)&&(object [r][c]<= 25)){
             printf("%c",'A'+object [r][c]);
          }
    
          if ( object[r][c] == ROBOT ) {
            if (direction == NORTH) {
              printf("^");
            }
            if (direction == EAST) {
              printf(">");
            }
            if (direction == SOUTH) {
              printf("v");
            }
            if (direction == WEST) {
              printf("<");
            }
          }
    
       }
       printf( "\n" );
    }
    
    
    }
    Thanks a lot.. i know its a crapload to read but any help is appreciated..

    cheers

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop making new accounts to post the same exact thing, or go find your classmate and work the problem out between you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    i echo quazah's response, but try using a function to move the robot based on a condition.

    Code:
    // if  ( robot has checked area NORTH ->(then) MOVE SOUTH ) 
    {
        // pass a pointer to a new function perhaps?
    }
    EDIT: I thought homework questions were prohibited from the boards anyway

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just a thought:

    Every box you try to move, you'll need to find the depth of the box. Although you'll be pushing on the side of the box facing the robot, the BACK of the box will be the determining factor in how far the box can be pushed.

    When the back of the box hits up against a wall, then the whole push must make no more progress, at least until the robot has been re-positioned so it can push the box in a new direction. Then the new back of the box will determine if a push in that direction, can succeed.

    In programs like this, working out what you need to do in pseudocode, with pencil and paper, is a real time-saver. Here, I recommend working the same way to solve the problems that remain with your program.

    Edit: OK, I see your request for help with the robot's movement. Here's how I'd do it:

    1) You have a global CD = robot's current direction
    2) Use a switch statement to then determine which way the robot should turn, in psuedocode, something like:

    Code:
    switch cd
       CD is UP:
        if keypress is L then CD is LEFT
        else if keypress is R then CD is RIGHT
        else if keypress is D then CD is DOWN
        else if keypress is U then call your Move(UP) function and make the move.
    
       CD is RIGHT
        if keypress is L then CD is UP
        else if keypress is R then call Move(RIGHT) and make the move
        else if keypress is D then CD is DOWN 
        else if keypress is U then CD is UP
    
    etc.
    After changing the CD, you may want to call a TURN(CD) function to show the robot has actually turned, on the screen with a change of direction to the robot's char.

    Move() must check ahead that the robot's grid at that point (x,y), is not a wall, or a box. If it's a wall, the robot just stops, if it's a box, the robot calls PushBox(), to see if the box can be pushed one point in the grid.

    Hope that gives you some helpful idea's. If you're learning C, I wouldn't get all caught up with using pointers and unions and such. Try to keep your program at your level, or just a bit more. Making a program too complicated is just the opposite of what you want to do. KISS will help with the problem solving needed.


    Adak
    Last edited by Adak; 09-27-2006 at 07:23 AM.

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Homework QUESTIONS are fine, if a person is stumped and has 90% of the code done and needs help with the last little bit, most of us on here would be glad to help. We have all gotten stuck on programs before, and can relate. Now people asking us to do their home (IE always asking for 'code examples') are in violation.

    The OP did say that he was having trouble with the moving functions...

    So far it looks like you haven't attempted movement or the colission system. So what you are going to want to do is determin what the difference in the cols and rows are when you go a certain direction... then you will have to modify the state array accordingly. You will also need to check for an object in the robots way before performing the move, but after determining the direction you wish to go.

    Good luck, and please try things more before asking questions, so we can give more specific help.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ah! I see you're the same robot poster we had, before!

    Well, I guess we can't have a determined poster go unrewarded. I'm now onto the computer with my C on it, so I'll take a look at your program, in more detail, today.

    I'm looking at the environment in the robot.txt file, and I'm just wondering if the top of the robot's environment should be flat, or sloped with an opening on the left upper corner. I couldn't view it in notepad, so I'm using Wordpad to see it. Is that correct?

    I'll post back later today when the din dies down for Wednesday.

    You know this program is a lot like the game of "Sokoban". I had forgotten that game.

    One question for you. The robot can push one box. Can he push a box which has been pushed up against another box (but not against a wall)?


    Adak

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Quote Originally Posted by Adak

    One question for you. The robot can push one box. Can he push a box which has been pushed up against another box (but not against a wall)?


    Adak
    Yes he can push multiple boxes, and the boxes can be different, e.g there could be a line of boxes:

    Code:
    *************************
    *                       *
    *      >[A][C][B]       *
    *                       *
    *************************
    
    pushing forward would give
    
    *************************
    *                       *
    *      .  >[A][C][B]    *
    *                       *
    *************************
    Last edited by Gizzle; 09-27-2006 at 08:45 AM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's the kind of robot I need for playing Sokoban!!

    (you can get a free Sokoban (1.66), @ download.com, btw)

    We really don't need to keep any track of the different sized boxes, just push them around, however we please, as long as they don't bump up against a wall, right?

    And you don't have to push the boxes into a goal area to win the game?

    Just be able to follow the commands and display it on the screen. OK.

    Adak
    Last edited by Adak; 09-27-2006 at 10:20 AM.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well at least this effort is considerably more advanced than the other post, which seems to have just been dumped on the board.

    > object[rr][rc] = ROBOT;
    What values are stored in rr and cc at this point in the code?

    > But i can't get my robot to move
    Like
    Code:
    move ( object, &rpos, &cpos );
    Another useful function would be, from the robots current position, scan in the direction the robot is looking for a NONE. If all you "see" is objects and a wall, then you can't push the boxes.

    Then write a function to move a row of boxes one space in the direction of movement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Gizzle:

    OK, if you're still working with this program, and since you "showed me yours", I'll "show you mine".

    I won't deal with large re-writes of the code you posted up, because it's just against my religion to wade through someone else's code with superflous stuff, which I strongly suspect you didn't write, and don't fully understand, either.

    So, let's start with something new and to the point. This isn't a full program, but it will become one, although perhaps slightly different than your assignment. Very close, though.

    Code:
    # include <stdio.h>
    
    int cd, cp; /* cd=current direction, cp= current position */
    
    int main(void)  {
       char move;
       int i, quit;
       GetEnv();                    /* get the set up environment */
    
       do  {                           /* start the "game' loop */
         PrintEnv();                /* print current environment */
         move = GetMove();   /* get the move or q to quit, and bring it back here */
         MakeMove(move);    /* definitely our trickiest function, imo */
       }while( move != 'q');  /* our "game" loop end */
    
       return 0;
    }
    MakeMove may need to be broken up into sub-functions, because it does so much. Pseudocode for a portion of it, is next

    I'm going to break thus up into a few posts at least, so you have time to read and ask any questions.

    Adak

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Firstly i truly appreciate your help Adak, thanks mate.

    I understand what you are doing, your code has a few more functions than mine with functions for getting environment and the instructions, and printing them out respectively.

    Btw i did write up that code.

    I've been working on it and i can make my robot turn left and right now. I used a crapload of IF statements. Ill show you what i did to make it turn.. but my if statements for moving forward don't relaly work.

    Code:
      
      //Give the robot its own cordinates.
      if (object[r][c] == ROBOT ) {
      r_r=r;
      r_c=c;
      }
    
      while (( ch = getchar() ) != ')' ) {
        c++;
        if ( ch == '\n' ) {
          ncols[r++] = c;
          c = -1;
        }
        if ( ch == 'L' ) {
          if ( *pdirection == EAST ) {
              *pdirection = NORTH;
          }
          else if ( *pdirection == NORTH ) {
              *pdirection = WEST;
          }
          else if ( *pdirection == WEST ) {
              *pdirection = SOUTH;
          }
          else if ( *pdirection == SOUTH ) {
              *pdirection = EAST;
          }
        }
        if (ch == 'R' ) {
          if ( *pdirection == EAST ) {
               *pdirection = SOUTH;
          }
          else if ( *pdirection == SOUTH ) {
               *pdirection = WEST;
          }
          else if ( *pdirection == WEST ) {
               *pdirection = NORTH;
          }
          else if ( *pdirection == NORTH ) {
               *pdirection = EAST;
          }
        }
        if (ch == 'F' ) {
          if ( *pdirection == EAST ) {
            object[r_r+1][r_c] = ROBOT;
    
          }
          if ( *pdirection == SOUTH ) {
            object[r_r][r_c-1] = ROBOT;
    
          }
          if ( *pdirection == WEST ) {
            object[r_r-1][r_c] = ROBOT;
    
          }
          if ( *pdirection == NORTH ) {
            object[r_r][r_c+1] = ROBOT;
    
          }
        }
    
      }
    I'm not too sure what im doing wrong. But something gives me the idea that its got to do with the beginning of my scan_state function when i did

    Code:
      r = 0;
      c = -1;
    Would this have forced my point to be fixed..? I'm not sure..

    Thanks heaps.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can simplify your turns.
    Code:
    enum dir { NORTH, EAST, SOUTH, WEST };
    ...
    /* left */
    if( ch == 'L' )
    {
        *pdir = *pdir == NORTH ? WEST : *pdir - 1;
    }
    /* right */
    if( ch == 'R' )
    {
        *pdir = *pdir == WEST ? NORTH : *pdir + 1;
    }
    As to moving, are you just setting every square they move to, to ROBOT? Or should you be setting it as "empty" when you move them out of it?

    You also don't appear to be initializing most of your variables. I suspect if you compile with warnings on you'll be seeing some things like "possibly used without being initialized" scattered here and there.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The whole NORTH SOUTH EAST WEST thing is where I knew I'd need to write my own code.

    IIRC, the instructions for the robot will be Left or Right, and Forward - and Print, and that's ALL. No directions, so why add them in?

    On the robot environment, I'd ALWAYS use a centered area, like so:
    0 1 2 3 4 5 6 7 8 9 A B
    =================
    9 9 9 9 9 9 9 9 9 9 9 9
    9 0 0 0 0 0 0 0 0 0 0 9
    9 0 ^ 0 B B B 0 0 0 0 9
    9 0 0 A A 0 0 0 0 0 0 9
    9 0 0 A A 0 0 0 0 0 0 9
    9 9 9 9 9 9 9 9 9 9 9 9

    So the #9 gives you a border you can detect easily, and your robot goes on columns 1 through 10 or 1 thru 80 in your case. (the 9 could be any value you like, clearly).

    In your program, be sure you have your array set up and printing correctly before moving onto move. Otherwise it will be far more difficult to finish the program, imo.

    I would start with a small array, as small as practical - just enough to see that the array is loading right, displaying right, and then we'll start on the moving.

    More later,

    Adak

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak
    The whole NORTH SOUTH EAST WEST thing is where I knew I'd need to write my own code.

    IIRC, the instructions for the robot will be Left or Right, and Forward - and Print, and that's ALL. No directions, so why add them in?
    They're not there for the robot necessarily. They're there so you the programmer can plot through it easily. Foreward relative to what? What coordinate am I supposed to be incrementing or decrementing here? That's why we use compass directions. Because they're easy to map to a grid and for movement. The robot doesn't need to know anything about the directions.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah
    They're not there for the robot necessarily. They're there so you the programmer can plot through it easily. Foreward relative to what? What coordinate am I supposed to be incrementing or decrementing here? That's why we use compass directions. Because they're easy to map to a grid and for movement. The robot doesn't need to know anything about the directions.


    Quzah.
    You're NOT a Sokoban player!!

    You can work DIRECTLY with the input from the user, and every movement is relative to the current position of the robot, of course.

    Perhaps I'm too compass oriented, but on my screen, West is on my right hand side, but using these robot directions, West would have to be East, and of course, East would have to be West.

    That doesn't HELP!

    Adak

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Continuous Navigation while X for a Robot
    By Creighton in forum C Programming
    Replies: 3
    Last Post: 06-29-2009, 08:38 PM
  2. New Problem with the Robot output!
    By tx1988 in forum C++ Programming
    Replies: 0
    Last Post: 04-18-2009, 04:02 PM
  3. Generate keypresses like the Robot class in java
    By samel_tvom in forum Game Programming
    Replies: 3
    Last Post: 10-06-2008, 05:37 AM
  4. Help with moving robot in a maze
    By Killahkode in forum C Programming
    Replies: 2
    Last Post: 09-25-2006, 10:51 AM
  5. Replies: 5
    Last Post: 09-08-2001, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21