Log in

View Full Version : Java Autonomous Periodic


mmolo
20-01-2014, 17:05
Hey everyone, my team is just starting to code our robot in Java. We have some decent Java experience, but we have a question about how autonomousPeriodic is ran. Basically, since the Periodic methods are going to be called every ~20ms if we have code like this:

public void autonomousPeriodic() {
drive.drive(0.5, 0.0);
Timer.delay(3000);
drive.drive(0.0, 0.0);
piston.set(true);
Timer.delay(2000);
piston.set(false);
}


Will this code be called every 20ms and attempt to drive forward and set the pistons 50 times per second?

If so I'm guessing we would just have to wrap it in an if statement that will set a boolean that it's checking to false as soon as it runs, so it only runs the first time.

Thanks in advance!

joelg236
20-01-2014, 17:07
Use initAutonomous() instead to only have it run once. Your code will not run 50 times per second, but it will loop back every time the method finishes (until autonomous is complete)

mmolo
20-01-2014, 17:12
Ah, thank you. That would make more sense.

otherguy
20-01-2014, 21:08
In the Iterative robot project (http://team2168.org/javadoc/edu/wpi/first/wpilibj/IterativeRobot.html). The AutonomousInit() method is called once, each time the auto mode is entered (this is triggered by the field). It is meant to aid in initializing variables/components to known states.

Putting code in this method which delays for any significant length of time is not what it was designed for. I've take a look through the source code (http://team2168.org/javadoc/src-html/edu/wpi/first/wpilibj/IterativeRobot.html#line.41)for the IterativeRobot class, and I can't find anything indicating that it won't work (that's not saying that it will). Just know that the Timer.delay() method should never be used in any of the periodic methods within the IterativeRobot project.

The SimpleRobot (http://team2168.org/javadoc/edu/wpi/first/wpilibj/SimpleRobot.html)class, on the other hand, will work perfectly fine with such delays in the auto/teleop methods. You might consider using that class instead if you anticipate needing to use delays in other areas of your code.