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