Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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


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 ?!?!
Posted
Updated 26-Feb-16 3:58am
v2

1 solution

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.


C#
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);
    }
}
 
Share this answer
 
Comments
Member 12351379 25-Feb-16 10:45am    
i still have errors even if i make a solution for the twise tapped , can i send the file to you ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900