Finch机器人追踪对象问题 [英] Finch robot tracing object problem

查看:130
本文介绍了Finch机器人追踪对象问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的代码



I am writing a program where I have to make my finch robot follow an object. I have written it so that the finch will begin by sensing objects around it once it has been tapped on the top. As soon as I tap the top and cover the sensors, a red light is supposed to turn on and it will follow the object in the direction of the covered sensor. While it is moving, it should be making a buzzing sound and have the light change from red to green. When I stop moving the object, the finch is supposed to turn it's nose back to red and wait until it is either tapped twice to end the program or until the object is moved for it to continue following. However, when I tap it, nothing happens.

Here is my code

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





我的尝试:



有些人说我应该做以下

myF.setWheelVelocities(velocityLeft,turnRight,4000);

myF.buzz(Buzz,BuzzDuration,4000);

这是错的

if(myF.isTapped()== true& ;& myF.isTapped()== true)

但是我不知道写什么?!?!



What I have tried:

some people said i should make the following
myF.setWheelVelocities(velocityLeft, turnRight ، 4000);
myF.buzz(Buzz, BuzzDuration ، 4000);
and this is wrong
if (myF.isTapped() == true && myF.isTapped() == true)
but i dont know what to write ?!?!

推荐答案

您好,



我没有和Finch合作,但我在编程方面经验不足。所以我稍微重构了你的代码以使其可读并添加了一些TODO注释。这可能会对你有所帮助。





Hello,

I have not worked with Finch but I have little experience in programming. So I have refactored your code a little bit to make it readable and added few TODO comments. This may help you.


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

public class FollowAnObjectSolution {

	// TODO - Please use proper variable names 
	Finch myF = new Finch();
	int Buzz = 300;
    int BuzzDuration = 12;

    int R = 250;
    int G = 250;

    int velocityRight = 150;
    int velocityLeft = 150;

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

	public FollowAnObjectSolution() {    }
	
	public void StartObserving()
	{
		while (true) {

				// TODO - The logic below looks very disturbed. Please simplify it
				if (myF.isTapped() == true && 
myF.isObstacleLeftSide() == true && 
myF.isObstacleRightSide() == true && 
myF.isObstacle() == true) {

					myF.setLED(R, 0, 0);
					myF.setWheelVelocities(velocityLeft, velocityRight);


					boolean RunAgain = true;

					while (RunAgain) {

						Move();

						// TODO - Find Some method which will say tapped twice or not because the code doesn't seems right.
						if (myF.isTapped() == true && myF.isTapped() == true) {
							break;
						}
					}
				}
			}
	}

    public static void main(String[] args) {
        new FollowAnObjectSolution().StartObserving();
    }    

	private 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();
        }
    }

	private void MoveStraight() {
        myF.setLED(0, G, 0);
        myF.setWheelVelocities(velocityLeft, velocityRight);
        myF.buzz(Buzz, BuzzDuration);
    }

    private void MoveLeft() {
		myF.setLED(0, G, 0);
        myF.setWheelVelocities(turnLeft, velocityRight);
        myF.buzz(Buzz, BuzzDuration);

    }

    private void MoveRight()
    {
        myF.setLED(0, G, 0);
        myF.setWheelVelocities(velocityLeft, turnRight);
        myF.buzz(Buzz, BuzzDuration);

    }

    private void StopMoving()
    {
        myF.setLED(R, 0, 0);
        myF.stopWheels();
        myF.buzz(Buzz, BuzzDuration);
    }
}


这篇关于Finch机器人追踪对象问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆