Hey Guys,
I decided to get autonomous to work this year and ran into the problem of an unlimited run time despite a set stopping point.
Can someone check this out and find the problem? I honestly don’t know what the problem is.
I may be overlooking something simple, but I am clearly not seeing it.
public void autonomousInit() {
// schedule the autonomous command (example)
// if (autonomousCommand != null) autonomousCommand.start();
//autonomousCommand.start();
// Reset timer to 0sec
edu.wpi.first.wpilibj.Timer myTimer = new edu.wpi.first.wpilibj.Timer();
myTimer.reset();
// Start timer
myTimer.start();
// move(-0.5,3.0);
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
// link -https://www.reddit.com/r/FRC/comments/3zcc2d/timer_not_working/
// If is has been less than 2 seconds since autonomous started, drive forwards
edu.wpi.first.wpilibj.Timer myTimer = new edu.wpi.first.wpilibj.Timer();
if(myTimer.get() < 2.0){
Robot.drivetraintank.setLeft_Back(0.5);
Robot.drivetraintank.setLeft(0.5);
Robot.drivetraintank.setRight(0.5);
Robot.drivetraintank.setRight_Back(0.5);
}
// If more than 2 seconds have elapsed, stop driving and turn off the timer
else {
Robot.drivetraintank.setLeft_Back(0.5);
Robot.drivetraintank.setLeft(0.5);
Robot.drivetraintank.setRight(0.5);
Robot.drivetraintank.setRight_Back(0.5);
myTimer.stop();
}
}
// Scheduler.getInstance().run();
In your else block, you don’t seem to be stopping the motors. Try changing all those calls to set 0.
Also, I noticed that you’re using the command based framework (based on the code you left commented out).
Why not make an autonomous command and use the internal timer of each command to run your autonomous? Something like this:
class AutoCommand extends Command {
public AutoCommand(){
}
public void init(){
setTimeout(2); // here's where you set the internal timer of commands
}
public void execute(){
Robot.drivetraintank.setLeft_Back(0.5);
Robot.drivetraintank.setLeft(0.5);
Robot.drivetraintank.setRight(0.5);
Robot.drivetraintank.setRight_Back(0.5);
}
public boolean isFinished(){
return isTimedOut(); // this ends the command once the 2 seconds you set in init() are up
}
// in the last 2 methods, clean up by zeroing out the motors
// this ensures that motor output stops once the command ends or is cancelled
public void end(){
Robot.drivetraintank.setLeft_Back(0);
Robot.drivetraintank.setLeft(0);
Robot.drivetraintank.setRight(0);
Robot.drivetraintank.setRight_Back(0);
}
public void interrupted(){
Robot.drivetraintank.setLeft_Back(0);
Robot.drivetraintank.setLeft(0);
Robot.drivetraintank.setRight(0);
Robot.drivetraintank.setRight_Back(0);
}
}
Then in your robot class
Command autoCommand = new AutoCommand();
public void autonomousInit(){
autoCommand.start();
}
public void autonomousPeriodic(){
Scheduler.getInstance().run();
}
public void teleopInit(){
// it's always good practice to do this, in case you make some change to your auto command that ends up taking too long to run
// we put code in interrupted() to handle this case
autoCommand.cancel();
}
Either of the two posts above should put you down the path of solving the issue.
The code you have now (in addition to not actually stopping the motors in the else) is creating a new timer every time autonomousPeriodic is called. This timer is never started so it will return 0. You can either create the timer once as ‘heuristics’ has suggested or scrap this and use a command with setTimout() and isTimedOut() as ‘euhlmann’ has suggested.