javasensorsrobotics

How to get a direction for a robot from a method in a sensor class?


I am making a program using the LRV(Least recently visited) Algorithm. Basically, I design the algorithm for a robot to traverse through a grid (which is a 2D char array). The robot whilst traversing the grid checks whether each cell is either EMPTY (defined by 'O'), OCCUPIED ( defined by 'S' ) or BLOCKED (defined by 'X'). The cells can only be occupied by an object known as Sensor (this has its own class). BLOCKED cells cannot be traversed on. Each time the robot must move, it receives a direction from the sensor. So in the beginning the robot would be placed on the grid and it would drop a sensor and get a direction from it, or get a direction from a pre-existing sensor.

Now that I've explained my program, my specific question is, I have a class Sensor that has a getVisitingDirection method that returns INT. I have a counter for each direction (North, South, East and West of type INT) Here is the class.

package ITI1121A;
public class Sensor {

private int cColumns;
private int cRows;
private int North;
private int South;
private int West;
private int East;

public Sensor(int sX, int sY) { 

cColumns = sX;
cRows = sY;
South= -1;
North = -1;
West = -1;
East = -1;

}
/* ADD YOUR CODE HERE */
public int getX ()
{return cColumns;}
public int getY ()
{return cRows;}

public int getVisitingDirection(GridMap g1)
  boolean temp;
{
  if(cRows==0){
  //top row
    if(cColumns==0){
    temp=g1.isCellBlocked(cColumns+1,cRows);
    if (temp=false){
    return West++;
    }

    }

  }

}

public void increaseCounter(int direction)
{}

}

Now where I am stuck is at getVisitingDirection, I've tried to make if statements to check the top left edge of the grid ( coordinates 0,0) and yeah that's about it. I want the method to give a direction to the robot and then increase the counter of that direction. Having real difficulty even getting the concept here. Any help will be highly appreciated! Thanks Varun


Solution

  • I've put a function in pseudo-code that should set you in the right path.

    // lets assume binary code where 0000 represents top, right, bottom, left 
    // (0011 would mean can go bottom or left)
    public int getVisitingDirection()
    {
        String tmpBinary = "b"; // to initialize the field
    
        // check if can go up
        tmpBinary += (cCollums>0&&checkIfUpIsBlocked()) "1" : "0";
        // TODO check if can go right (compare with size + 1)
        // check if can go bottom (compare with size +1 )
        // check if can go left (check if > 0)
    
        // when this runs tmpBinary will be in the form of a binary representation
        // this will be passed to the robot that can then chooses where to go
        // 1111 would mean that all squares are clear to go
        // 1101 would mean top, right and left
        // etc...
    
    }
    
    private boolean checkIfUpIsBlocked()
    {
        // TODO must check if the cell is blocked
        return true;
    }
    

    Do notice that you have to create the checkIfUpIsBlocked + methods.

    On top of that seams pretty good.
    You may want to change the int fields by enums as they are easier to read and less prone to human errors.

    How to return directions with an int number?
    You can use the binary logic and return a single int to represent multiple directions.

    0000 (int 0)  => no possible direction
    0001 (int 1)  => left direction
    0010 (int 2)  => bottom direction
    0011 (int 3)  => left and bottom
    0100 (int 4)  => right direction
    (...)
    1111 (int 15) => all directions possible