0

クラス ファイルはこちら私の Java コードでは、Finch ロボットの名前は myF です。

myF に次のことをさせる必要があります。

  1. 1 回タップすると、オブジェクトの前に置かれたオブジェクトに向かって移動します。くちばしの色 (myF.setLED..) は、オブジェクトが静止している場合は赤、オブジェクトが動いている場合は緑です。

  2. Finch ロボットは、オブジェクトが停止するとくちばしが赤色に点灯し、オブジェクトが移動すると緑色に点灯して、オブジェクトを追跡する必要があります。

  3. 動いている間、フィンチはブーンという音を立てるべきですが、大きすぎてはいけません。( myF.buzz(Buzz, BuzzDuration)

ただし、私のコードは正常に機能していません。いくつかの間違いがあることはわかっていますが、何度も試してみましたが、続行する方法がわかりません。以下の私のコードを見てください。どんな助けでも大歓迎です。

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);

}



}
4

1 に答える 1