Reputation: 1
I am new to finch and Java code and I'm trying to make finch moves either left or right by its sensors. Hopefully someone can help.
if(suzie.isFinchLevel()) {
suzie.saySomething("Moving forward");
suzie.sleep(1000);
while (!suzie.isObstacle()){
suzie.setWheelVelocities(100,100);
if(suzie.isObstacleLeftSide()){ //turn right
suzie.setWheelVelocities(100,0,2000);
}
else if (suzie.isObstacleRightSide()){
suzie.setWheelVelocities(0,100,2000); //turn left
}
}
}
Upvotes: -1
Views: 817
Reputation: 11
It appears that you have not actually accessed the vlaues of the sensors. Remember the you must first access the values (via the getObstacleSensors(); method) of the sensors. Also recall that this method returns the value of each sensor as a boolean array. See the javadoc:
getObstacleSensors public boolean[] getObstacleSensors()
Returns the value of both obstacle sensors as 2 element boolean array. The left sensor is the 0th element, and the right sensor is the 1st element.
Returns: The values of left and right obstacle sensors in a 2 element array
I did something recently similar to what you're trying to do using input from the button actionlisteners. Before implementing it, I wrote a method called "obstacleAvoidance" that the bot would actually execute when an obstacle is encountered; this method accepts a Finch object (i.e. suzie) as an argument. This made the code [relatively] less cluttered. The code to perform this task (in the action listener) could appear something like this:
private class ButtonListener implements ActionListener {
public void actionPerformed( ActionEvent e ) {
//Get the finchbot's obstacle sensors and store them in an array
boolean [] sensors = suzie.getObstacleSensors();
//Check to see if either of the ficnhbot's sensors
//detect an obstacle. This is enclosed in a while loop which is
//broken if either sensor returns false (detects an obstacle)
while (sensors [0] == false && sensors[1] == false)
{
// Sets the Action text field
System.out.println( "Performing Action..." );
// This method tells the robot to perform an action
command.performAction(suzie, -255, -85);
}
//Otherwise, perform obstacle avoidance maneuver
command.obtacleAvoidance(suzie);
}
}
I hope this serves as a good starting point for you...
T
Upvotes: 1