我必须制作代码,使得Finch机器人在通过水龙头激活后跟踪物体,并且当它被轻敲两次时程序退出。我无法理解如何做到这一点。现在,我试图让它在机器人被激活之后(它将在while(x == 1)代码之后),每次点击机器人时它会向'tappedCount'添加1。
然而,由于程序将始终在其他'while'循环内工作(在if语句中检查对象的位置,因为Finch的性质,其中一个将始终激活。这意味着:
if(myf.isTapped())
{
tappedCount++;
}
if(tappedCount==2)
{
System.out.println("Exiting Finch");
System.exit(0);
myf.quit();
}
代码永远不会有机会运行。
有没有人建议如何让它运行?或者当机器人被轻敲两次时退出程序的另一种方法会很棒。谢谢。
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class FinchCode {
static long instant1;
static long instant2;
static long instant3;
static long instant4;
static long duration;
static long duration1;
static int x = 0;
static int Buzz = 300;
static int BuzzDuration = 1200;
static int R = 250;
static int G = 250;
static int velocityLeft = 150;
static int velocityRight = 150;
static int turnLeft = -100;
static int turnRight = -100;
static int time = 0;
static int tappedCount = 0;
static int millis;
public static void main(String[] args) throws InterruptedException{
Finch myf = new Finch();
while(!myf.isBeakDown())
{
if(myf.isTapped()&& x==0)
{
if(myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
{
x = 1;
myf.setLED(R,0,0);
myf.stopWheels();
}
}
while(x==1)
{
if(myf.isTapped())
{
tappedCount++;
}
if(tappedCount==2)
{
System.out.println("Exiting Finch");
System.exit(0);
myf.quit();
}
if(!myf.isObstacleLeftSide()&&!myf.isObstacleRightSide())
{
while(!myf.isObstacleLeftSide()&&!myf.isObstacleRightSide())
{
myf.setLED(0,G,0);
myf.setWheelVelocities(velocityLeft, velocityRight);
}
System.out.println("Forward");
}
if(!myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
{
while(!myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
{
myf.setLED(0,G,0);
myf.setWheelVelocities(velocityLeft, turnRight);
}
System.out.println("Right");
}
if(myf.isObstacleLeftSide()&&!myf.isObstacleRightSide())
{
while(myf.isObstacleLeftSide()&&!myf.isObstacleRightSide())
{
myf.setLED(0,G,0);
myf.setWheelVelocities(turnLeft, velocityRight);
}
System.out.println("Left");
}
if(myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
{
while(myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
{
myf.setLED(R,0,0);
myf.stopWheels();
}
System.out.println("Stop");
}
}
}
}
答案 0 :(得分:1)
boolean move = false;
if(myf.isTapped())
{
move = true;
}
while(move)
{
//Code keeping the finch moving
//check at the end of loop if the tapp has occured yet
//if so reset to false, and the while loop will exit
if(myf.isTapped())
{
move = false;
}
}
//Exit program and print your stuff
System.out.println("Exiting Finch");
System.exit(0);
myf.quit();