Quote:
Originally Posted by Robodawgs2221
This seemed to fix my problem. Thanks.
I'm trying to make some basic autonomous codes to modify later. We're using an Iterative model, and if there's any way you could think to make the code a little bit cleaner/more efficient, or even just some tips on transferring from RobotC to Java, that would be amazing 
|
If you are going to use IterativeRobot, you do not want to have calls to Timer.delay(). In the iterative model, your autonomousPeriodic() method will get called for you every 20ms (roughly). You want to write code that quickly does something and then returns, only to be executed 20ms later. You could use timing and the iterative model like so (though this is not how we code our autonomous):
Code:
import edu.wpi.first.wpilibj.Timer;
public class Robot extends IterativeRobot {
private double autoStartTime;
public void autonomousInit() {
autoStartTime = Timer.getFPGATimestamp();
//other stuff as needed
}
public void autonomousPeriodic() {
double currTime = Timer.getFPGATimestamp();
double timeElapsed = currTime - autoStartTime;
if( timeElapsed < 2 ) {
//put code here...whatever you want to do for the first 2 seconds
arcadeDrive.drive(1, 0);
}
else if( timeElapsed < 5) {
//put code here that you want to do for the next 3 seconds
arcadeDrive.drive(0.75, 0.6);
}
//etc.
}
//the rest of your Robot class
}
Hope that makes sense and/or helps. That could all be in a case statement as well:
Code:
public void autonomousPeriodic() {
double currTime = Timer.getFPGATimestamp();
double timeElapsed = currTime - autoStartTime;
switch( AUTO_TYPE ) {
case LEFT:
if( timeElapsed < 2 ) {
//put code here...whatever you want to do for the first 2 seconds
arcadeDrive.drive(1, 0);
}
else if( timeElapsed < 5) {
//put code here that you want to do for the next 3 seconds
arcadeDrive.drive(0.75, 0.6);
}
//etc.
break;
case RIGHT:
//etc.
}