如何从传感器类中的方法获取机器人的方向?

问题描述 投票:0回答:1

我正在使用LRV(最近最少访问)算法制作一个程序。基本上,我设计了机器人遍历网格(这是一个二维字符数组)的算法。机器人在遍历网格时检查每个单元格是否为

EMPTY
(由“O”定义)、
OCCUPIED
(由“S”定义)或
BLOCKED
(由“X”定义)。这些单元格只能被称为传感器的对象占据(它有自己的类)。无法遍历
BLOCKED
单元格。每次机器人必须移动时,它都会收到来自传感器的方向。因此,一开始,机器人将被放置在网格上,它会放下一个传感器并从中获取方向,或者从预先存在的传感器中获取方向。

现在我已经解释了我的程序,我的具体问题是, 我有一个 Sensor 类,它有一个返回 INT 的

getVisitingDirection
方法。 我每个方向都有一个计数器(北、南、东、西,类型为 INT) 这是课程。

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)
{}

}

现在我陷入困境的是 getVisitingDirection,我尝试使用 if 语句来检查网格的左上角(坐标 0,0),是的,就是这样。 我希望该方法为机器人提供一个方向,然后增加该方向的计数器。 即使在这里理解这个概念也确实很困难。 任何帮助将不胜感激! 谢谢 瓦伦

java sensors robotics
1个回答
3
投票

我在伪代码中放置了一个函数,它应该可以让您走上正确的道路。

// 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;
}

请注意,您必须创建 checkIfUpIsBlocked + 方法。

上面的接缝非常好。
您可能希望通过枚举更改 int 字段,因为它们更易于阅读并且不易出现人为错误。

如何用 int 数字返回路线?
您可以使用二进制逻辑并返回单个 int 来表示多个方向。

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
© www.soinside.com 2019 - 2024. All rights reserved.