|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Re: Java Threads
Quote:
Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Solenoid;
public class Pulser implements Runnable {
Solenoid firstSolenoid = new Solenoid(1);
public void run() {
while (true) {
try {
firstSolenoid.set(true);
Thread.sleep(1000); // sleep thread for 1 second
firstSolenoid.set(false);
Thread.sleep(1000); // sleep thread for another second
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
wait() causes the thread to sleep until another thread issues a notify() or notifyAll() command. It doesn't set a specific time to wait for. You might try replacing the sleep() function with Timer.delay(), I think that would get rid of the try..catch statements. Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
public class Pulser implements Runnable {
Solenoid firstSolenoid = new Solenoid(1);
public void run() {
while (true) {
firstSolenoid.set(true);
Timer.delay(1);
firstSolenoid.set(false);
Timer.delay(1);
}
}
}
Code:
Pulser pulser = new Pulser(); new Thread(pulser).start(); Last edited by wdell : 22-02-2011 at 20:51. |
|
#2
|
||||
|
||||
|
Re: Java Threads
Quote:
How is Timer.delay() implemented? |
|
#3
|
|||
|
|||
|
Re: Java Threads
Quote:
Code:
public static void delay(final double seconds) {
try {
Thread.sleep((long) (seconds * 1e3));
} catch (final InterruptedException e) {
}
}
|
|
#4
|
|||
|
|||
|
Re: Java Threads
We ran a neat little experiment (at least to me) to understand how the underlying threads were being executed.
Set the motor speed to one value- and then immediately set it to another value. What we ended up with is a 'pulse' - sometimes it would hold continuous values... othertimes it would slow/speed up. I had hoped it would help drive some points in with the programmers. |
|
#5
|
||||
|
||||
|
Re: Java Threads
Quote:
|
|
#6
|
|||
|
|||
|
Re: Java Threads
Quote:
If they executed linearly and in order, you'd have either no pulse (since the first set would be overridden when the code exited the loop) or a constant pulse rate since both calls were made in sequence, one after another, with no delays other than what the scheduler created. Which mean the scheduler was pausing the robot.main classes and running things in the background- interrupt, so to speak, the main thread transparently. The students didn't (and still don't, I think) understand the implications of this. They think it just runs and runs and runs... they never see the pauses because they aren't presented to them. One of the biggest issues I've had in trying to teach (and even some adults) is that just because you put two lines next to each other in the code- when dealing with hardware- you never know for certain (unless you're VERY careful) that they will execute properly. So it is always best to design with that thought in mind.... |
|
#7
|
||||
|
||||
|
Re: Java Threads
Quote:
Someone said it was not time-based, but rather based on number of bytecodes executed. Is this correct, and can anyone link to a document where this is discussed? I'm referring to the 2011 FRC Java Framework implementation here specifically. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|