First Year Java Autonomous Help

Hello,
This year, I am switching my team from Labview to Java. One issue I’ve had with the transition is how to program Autonomous methods in Java. I have tried to write a method that will move the robot backwards for a set amount of time, at a set speed. When I try to run it, the drive motors kick the bot back for a second and then stop instead of continuing to roll back. Any help would be appreciated.
Code:

public void moveBackward(double time, double motorPower){
Hardware.chassis.tankDrive(-motorPower, -motorPower);
Timer.delay(time);
Hardware.chassis.tankDrive(0, 0);
}

Your input is good, but try this for the method:

double last = Timer.getFPGATimestamp
while get timestamp-last<time
Set motor power
Reset motor power

Try disabling motorSafety as well.

Thanks, what method would I use to get the timestamp?
*Never mind, I found it

When you set up a project in Java, you choose what base class to use, Simple Robot or Iterative Robot (or command, which is based on iterative). There are descriptions of each here: http://wpilib.screenstepslive.com/s/3120/m/7912/l/130578-choosing-a-base-class

In LabVIEW, autonomous is called at the start of autonomous and stopped at the end of autonomous. You can put delays in autonomous in LabVIEW. Teleop, on the other hand, is called every 20ms. You can’t put any delays in teleop or you will shut down communication.

In Java (and C++), Simple Robot behaves like LabVIEW’s autonomous, for both teleop and autonomous. Iterative Robot behaves like LabVIEW’s teleop, for both teleop and autonomous.

If you are using iterative robot, you can’t use the long delay or cgmv123’s while loop. Instead use an if statement that checks the time and either turns on or off the motor and doesn’t do anything that takes a long time.

If you are using the command templates, it’s very easy to make a command that sets a timeout in init, runs the motor and checks the time in execute, and stops the motor in finish.

Another possible problem is the motor safety. By default, robotDrive needs to be called every 0.1 seconds or else it will shut off the motor. This is important because if your code crashes, it makes sure the robot stops, rather then keep running the last speed. In your code, robotDrive wouldn’t get called for the length of your delay. cgmv123’s code works around this by calling very often in the while loop.