Finch机器人追踪对象

时间:2016-02-24 18:07:13

标签: java object detection tracing

我正在写一个程序,我必须让我的雀科机器人跟随一个物体。我已经写好了,一旦它被轻拍在顶部,它就会通过感知周围的物体开始。一旦我敲击顶部并覆盖传感器,就应该打开一盏红灯,它将沿着覆盖的传感器方向跟随物体。当它在移动时,它应该发出嗡嗡声并且从红色变为绿色。当我停止移动物体时,雀科应该将它的鼻子变回红色并等待它被轻敲两次以结束程序或直到物体被移动以使其继续跟随。但是,当我点击它时,没有任何反应。

这是我的代码:



import edu.cmu.ri.createlab.terk.robot.finch.Finch;

public class FollowingAnObject {

    static Finch myF = new Finch();

    public static void main(String[] args) {
        myF = new Finch();
    }

    public FollowingAnObject() {
        while (true) {
            //This begins the movement of the finch  
            if (myF.isTapped() == true && myF.isObstacleLeftSide() == true && myF.isObstacleRightSide() == true && myF.isObstacle() == true) {
                //LED colours are red, green and blue
                myF.setLED(R, 0, 0);
                myF.setWheelVelocities(velocityLeft, velocityRight);

                //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 the Move method for the Finch movements
                    Move();
                    //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 all of the 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();
        }
    }

    //All of the variables have been declared
    static int Buzz = 300;
    static int BuzzDuration = 12;

    static int R = 250;
    static int G = 250;

    static int velocityLeft = 150;
    static int velocityRight = 150;

    static int turnLeft = -50;
    static int turnRight = -50;

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




0 个答案:

没有答案