|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Java wait Function
What we want to do is to have our solenoids to open and then after like 0.5 seconds have it close automatically.
This is our code so far: Code:
if (j_shooting.getRawButton(2)){ //FORWARD - BUTTON A
flag = 1;
}else if (j_shooting.getRawButton(1)){ //BACKWARD - BUTTON X
cyl_shoot.set(DoubleSolenoid.Value.kForward);
}else {
cyl_shoot.set(DoubleSolenoid.Value.kOff);
}
if (flag == 1){
cyl_shoot.set(DoubleSolenoid.Value.kReverse);
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
cyl_shoot.set(DoubleSolenoid.Value.kForward);
flag = 0;
}
So my question is, is the code i have right now good and if not what other options is there to have the pistons open, pause for 0.5 seconds, and then close. thanks |
|
#2
|
||||
|
||||
|
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
}
}
|
|
#3
|
||||
|
||||
|
Re: Java wait Function
Timer.delay() is supposed to leave outputs set where they are as well, but I've had more success using Timer.getFPGATimestamp() in conjunction with a loop.
|
|
#4
|
||||
|
||||
|
Re: Java wait Function
Quote:
Our team had similar requirements surrounding our catapult (do X, wait half a second, then do Y). Originally the programmers implemented this using Timer.delay(). The result was that when the drivers tried to perform the action while moving, but then stopped moving or changed directions during the action, the robot would keep moving. Though .5 seconds doesn't sound like much, it is plenty of time for your robot to go just a little too far and injure someone, break something, or get you a penalty for going too far outside the arena. Use Timer.delay() with caution. |
|
#5
|
||||
|
||||
|
Re: Java wait Function
^This.
Year in and year out I see programmers bumping up against this concept. I think it's probably fair to say that some (many?) of them receive advice on how to fix the problem, but never really understand why there was a problem and why the fix fixed it. Does anyone have a link to a clear, simple, well-written tutorial on concurrent processing for FRC that covers threads, state machines, and interrupts? Last edited by Ether : 01-03-2014 at 13:18. |
|
#6
|
|||
|
|||
|
Re: Java wait Function
This is what I have done.
Inside op control: Code:
if(INSERT SOMETHING HERE) {
new Thread() {
public void run() {
//Code to run here:
Thread.sleep(time in ms);
//More code
}.start()
}
}
This has worked well for me. Hope this helps! Matt |
|
#7
|
||||
|
||||
|
Re: Java wait Function
how does the thread get killed?
|
|
#8
|
|||
|
|||
|
Re: Java wait Function
My understanding is that the JVM handles it. From here, it seems like letting the JVM handle could introduce race conditions if you access the same data from multiple threads like that...
Alex Brinister |
|
#9
|
||||
|
||||
|
Re: Java wait Function
ah. looked at it too fast. The thread completes normally when run() returns.
|
|
#10
|
|||
|
|||
|
Re: Java wait Function
Is safety configured for the drive base? I'd hope that the Java implementation would halt the drive motors when wait() or breakpoints delay for more than 100ms.
Greg McKaskle |
|
#11
|
|||
|
|||
|
Re: Java wait Function
I think this is only enforced if you have the Motor Safety Helper enabled. This feature is optional in both the C++ and Java libraries and can be disabled by the programmer. It is generally better to keep it enabled but to extend the timeout period using the SetTimeout() function in whatever motor controller class you choose to use (Talon, Victor, CANJaguar, Jaguar, etc).
Alex Brinister |
|
#12
|
|||
|
|||
|
Re: Java wait Function
What I intended to ask was whether the Motor Safety Helper was enabled when the robot motors ran for several seconds during the sleep() call.
Greg McKaskle |
|
#13
|
|||
|
|||
|
Re: Java wait Function
I have not turned motor safety helper on or off. So what ever it defaults to is what it is set to and it still works for me.
|
|
#14
|
|||
|
|||
|
Re: Java wait Function
Does the default result in zombie robots? I'd hope not. It should take effort to produce a zombie robot.
Also, if you are turning off the safety config, please ensure that everyone running the robot knows how to disable or estop the robot. Personally, I'd only turn it off when the robot is on blocks and I want the motors to keep running while I'm at a breakpoint. Greg McKaskle |
|
#15
|
||||
|
||||
|
Re: Java wait Function
Greg,
I haven't done any real testing, but it does not appear that any sort of safety kicks in after 100ms in Java. Our robot definitely moved for more than 100ms with our sleep() calls. You made me curious, though, so I started poking around. The Jaguar class extends SafePWM, which indeed defaults to disabling safety: Code:
void initSafePWM() {
m_safetyHelper = new MotorSafetyHelper(this);
m_safetyHelper.setExpiration(0.0);
m_safetyHelper.setSafetyEnabled(false);
}
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|