View Single Post
  #6   Spotlight this post!  
Unread 07-02-2017, 16:27
mmaunu's Avatar
mmaunu mmaunu is offline
Registered User
FRC #2485 (W.A.R. Lords)
Team Role: Mentor
 
Join Date: Mar 2013
Rookie Year: 2010
Location: San Diego, CA
Posts: 94
mmaunu is a jewel in the roughmmaunu is a jewel in the roughmmaunu is a jewel in the roughmmaunu is a jewel in the rough
Re: Timer.Delay stops motor movement?

Quote:
Originally Posted by Robodawgs2221 View Post
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.    

}
__________________
2016 Carson Division Industrial Design Award
2016 San Diego and Orange County, Engineering Inspiration Award
2014 Las Vegas (Winners with 987, 2478; Excellence in Engineering)
2014 San Diego (Finalists with 987, 3250; Quality Award)
2013 Inland Empire (Winners with 1538, 968; Excellence in Engineering Award)
2013 San Diego (Finalists with 2984, 4322; Creativity Award)
2012 Las Vegas (Finalists with 2034, 3187; Quality Award)
Reply With Quote