首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使Finch对象跟随一个对象

使Finch对象跟随一个对象
EN

Stack Overflow用户
提问于 2016-02-24 13:03:11
回答 1查看 1.6K关注 0票数 0

这里的类文件在我的Java代码中,芬奇机器人名为myF。

我需要让myF做以下工作:

  1. 点击一次,移动到放置在它前面的物体,带有喙色(myF.setLED.)物体静止时为红色,而物体移动时为绿色。
  2. 芬奇机器人应该跟随物体的周围,它的喙亮红色,如果物体停止,绿色,如果该物体移动。
  3. 在移动的时候,芬奇应该嗡嗡作响,但不要太大声。( myF.buzz(Buzz,BuzzDuration) )

然而,我的代码没有正常工作,我知道有一些错误,但我不知道如何继续多次尝试。请看下面的代码。任何帮助都将不胜感激。

代码语言:javascript
复制
import edu.cmu.ri.createlab.terk.robot.finch.Finch;

public class Main {


//Variable declaration.

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 = -50;
static int turnRight = -50;

static int time = 0;
static int tappedCount = 0;


//Initialization of Finch   

static Finch myF = new Finch();


//Main method

public static void main(String[] args) 


//TAP FINCH ONCE FOR IT TO DO EITHER OF THE FOLLOWING:

//Turn red and follow stationary object
//Turn green and follow an object in motion
//Do nothing if no object is in front of Finch




{


    //while (myF.isFinchUpsideDown() == false)
{

    if

    (myF.isTapped() == true && myF.isObstacle() == true)

{

    {
    //Set Beak LED to Green if object in front is moving. 
    // *******need to add a conditional statement for whether obstacle    in front of finch is moving.

    myF.setLED(R,0,0);
    //myF.setWheelVelocities(velocityLeft,velocityRight);
    myF.buzz(Buzz, BuzzDuration);

        if (myF.isObstacleRightSide() == false)
        {
            myF.setWheelVelocities(0, 255);
        }
            else if (myF.isObstacleLeftSide() == false)
            {
            myF.setWheelVelocities(255, 0);
            }
                else if (myF.isObstacle() == true)
                {

                    myF.stopWheels();


                }

    }

}

    //Beak supposed to be RED if object in front is stationary, Finch should move towards object.

else {

//      if
    //***beak should stay off unless object is placed ahead of it and/or is moving, respectively
//      (myF.isTapped() == false) 

    {   
        myF.setLED(0,0,0);  
        myF.setWheelVelocities(0,0);  
        //myF.quit();
        //myF.stopWheels();
        tappedCount = 0;
    }
}
}
myF.quit();

//  FollowObjectAround();


//RUN AGAIN i.e. "the Move method" whilst running the first algorithms above. 

//      boolean RunAgain = true;
//
//      while(RunAgain) {
//                  
//          Move();
//        
//      }


} //main(String[] args) ends here.

//FIRST PART SUCCESSFUL
//Next part: follow object incorporating corresponding beak colour



//public Main() {

public static void FollowObjectAround() {

while (true){    

//This begins the movement of the finch  

//if (myF.isObstacleLeftSide() == false && myF.isObstacleRightSide() == false || myF.isObstacle() == false)

//LED colours are red, green and blue
//myF.setLED(R,0,0);
//myF.setWheelVelocities(velocityLeft, velocityRight); 
//^^
//here, the robot runs straight into the object and keeps going.
//might be best to have the finch stop when it reaches the object, proceeding movement if that object moves also.


//Triggers the RunAgain function to true so that the program does not stop in one run so that the Finch will continue to move

boolean RunAgain = true;

while(RunAgain) {

//Calling of the Move method for the Finch movements.

Move();

while

(myF.isTapped()==true && myF.isTapped()==true)
//if (myF.isTapped()==true && myF.isTapped()==true)
{
myF.setLED(0,0,0);   
myF.stopWheels();
//break;
}

} // I just added this brace, wasn't there before.

}

//Inside the while, RunAgain loop , there is a conditional statement that makes the program terminate if the Finch has been tapped twice

//if (myF.isTapped()==true && myF.isTapped()==true)
//            {
//                break;
//            }
    }
  //}
//}






// Method for Finch movements

public static void Move() {

if (myF.isObstacleRightSide() == false && myF.isObstacleLeftSide() == false && myF.isObstacle() == true)

{
   MoveStraight();
}

else if (myF.isObstacleRightSide() == false && myF.isObstacleLeftSide() == true)

{
   MoveLeft();
}

else if (  myF.isObstacleRightSide() == true && myF.isObstacleLeftSide() == false)

{
   MoveRight();
}

else if (myF.isObstacleRightSide()==true && myF.isObstacleLeftSide()==true)
{
   StopMoving();

}

}

//=====================================================================================================


//If the finch is moving straight, the light will be green and both of the wheels will move at 150

public static void MoveStraight()

{
  myF.setLED(0, G, 0);

  myF.setWheelVelocities(velocityLeft, velocityRight);

  myF.buzz(Buzz, BuzzDuration);
}

public static void MoveLeft()

{
//If the finch is moving left, the light will be green, the left wheel will move at -50 and the right wheel will move at 150
myF.setLED(0, G, 0);

  myF.setWheelVelocities(turnLeft, velocityRight);

  myF.buzz(Buzz, BuzzDuration);

}

public static void MoveRight()

//If the finch is moving right, the light will be green the left wheel will move at 150 and the right wheel will move at -50
{
  myF.setLED(0, G, 0);

  myF.setWheelVelocities(velocityLeft, turnRight);

  myF.buzz(Buzz, BuzzDuration);

}

public static void StopMoving()

//if the Finch is not moving, the colour of the light will be red and the buzzing will stop
{
  myF.setLED(R, 0 , 0);

  myF.stopWheels();

  myF.buzz(Buzz, BuzzDuration);

}



}
EN

回答 1

Stack Overflow用户

发布于 2016-02-24 18:27:45

//while (myF.isFinchUpsideDown() == false) {

"{“这里是错的,而这里

{ //将Beak LED设置为绿色,如果前面的物体正在移动。// *需要添加一个条件语句,说明芬奇前面的障碍是否在移动。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35602994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档