Finch 障碍物传感器在 java 中不起作用

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

我是 Finch 和 Java 代码的新手,我正在尝试通过传感器使 Finch 向左或向右移动。希望有人能帮忙。

if(suzie.isFinchLevel()) {
        suzie.saySomething("Moving forward");
        suzie.sleep(1000);
        while (!suzie.isObstacle()){
        suzie.setWheelVelocities(100,100);
        if(suzie.isObstacleLeftSide()){ //turn right
            suzie.setWheelVelocities(100,0,2000);
        }
        else if (suzie.isObstacleRightSide()){
            suzie.setWheelVelocities(0,100,2000); //turn left
        }   
    }   
 }   
java robotics finch
1个回答
1
投票

看来您尚未实际访问传感器的值。请记住,您必须首先访问传感器的值(通过 getObstacleSensors(); 方法)。另请记住,此方法以布尔数组形式返回每个传感器的值。请参阅javadoc:

getObstacleSensors 公共 boolean[] getObstacleSensors()

以 2 元素布尔数组的形式返回两个障碍物传感器的值。左侧传感器是第 0 个元素,右侧传感器是第 1 个元素。

返回:2 元素数组中左右障碍物传感器的值


我最近做了一些与您尝试使用按钮动作侦听器的输入执行的操作类似的操作。在实现它之前,我编写了一个名为“obstacleAvoidance”的方法,机器人在遇到障碍时实际上会执行该方法;此方法接受 Finch 对象(即 suzie)作为参数。这使得代码[相对]不那么混乱。执行此任务的代码(在操作侦听器中)可能如下所示:

私有类 ButtonListener 实现 ActionListener {

  public void actionPerformed( ActionEvent e ) {
    //Get the finchbot's obstacle sensors and store them in an array
    boolean [] sensors = suzie.getObstacleSensors();

    //Check to see if either of the ficnhbot's sensors 
    //detect an obstacle. This is enclosed in a while loop which is
    //broken if either sensor returns false (detects an obstacle)
    while (sensors [0] == false && sensors[1] == false)
    {
    // Sets the Action text field
   System.out.println( "Performing Action..." );

    // This method tells the robot to perform an action
    command.performAction(suzie, -255, -85);
    }

    //Otherwise, perform obstacle avoidance maneuver
    command.obtacleAvoidance(suzie);
}

}

我希望这对您来说是一个良好的起点......

T

© www.soinside.com 2019 - 2024. All rights reserved.