View Single Post
  #2   Spotlight this post!  
Unread 28-02-2014, 22:47
Domenic Rodriguez's Avatar
Domenic Rodriguez Domenic Rodriguez is offline
Registered User
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Grove City, PA
Posts: 213
Domenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura about
Re: Java wait Function

Thread.sleep() causes the current thread to pause for a time. If you call this from the main thread, the robot will be unresponsive for those 500 ms while the thread sleeps. You could avoid this by moving the solenoid code to a separate thread.

A simpler option that I often use is to record a timestamp at the start and then subtract that timestamp from future readings to get the elapsed time.

Example:
Code:
double startTime;

if (j_shooting.getRawButton(2)) {
    // This will repeatedly be set until you let go of the button
    startTime = System.currentTimeMillis(); 
} else if (j_shooting.getRawButton(1))
    // This will override, but not reset, the timer
    cyl_shoot.set(DoubleSolenoid.Value.kForward);
} else {
    // Check the time to see if the pistons should be...
    if (System.currentTimeMillis() - startTime < 0.5) {
        cyl_shoot.set(DoubleSolenoid.Value.kReverse);  // ...extended, or...
    } else {
        cyl_shoot.set(DoubleSolenoid.Value.kForward);  // ..retracted
    }
}
This way you aren't locking up the rest of your program while you wait for the pistons to retract.
__________________

LuNaTeCs - Learning Under Nurturing Adults Teaching Engineering Concepts and Skills - Small and Mighty!

FRC 316 LuNaTeCs - Student (2011-2014), Lead Programmer (2011-2014), Team Captain (2013-2014), Operator (2013), Drive Coach (2014), Mentor (2015-????)
'11 Philly Regional Finalists, '13 Chestnut Hill Finalists, '13 Lenape Champions, '13 Archimedes Division, '14 Chestnut Hill Champions, '14 Lenape Champions
FTC 7071 EngiNerds - Founding Advisor (2013-2014) | FRC 5420 Velocity - Founding Advisor (2015)
Grove City College Class of '18, Electrical/Computer Engineering (B.S.E.E)

Reply With Quote