Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   What is the point of timer.delay? (http://www.chiefdelphi.com/forums/showthread.php?t=103072)

guoruiwu1994 17-02-2012 09:39

What is the point of timer.delay?
 
It delays the code by the specified amount of time, but I'm not sure what the benefits of that are.

NS_Radication 17-02-2012 11:55

Re: What is the point of timer.delay?
 
The Timer.delay(); function allows the robot to activate a value and reset.

For example:

driveTrain.tankDrive(motor1, motor2);
Timer.delay(0.005);

This allows the drivetrain to refresh the motors at 5 ms(aka stop on a dime!)

OR

if(joy1.getRawButton(1))
{
move_motor = true;
}
Timer.delay(0.5);

if(move_motor == true)
{
motor.set(1.0);
}
Timer.delay(0.005);

This allows button 1 to change position only after every half second if the code changes. So the button held will not change if the button is hit again for 0.5 seconds.

Hope this helped.

P.S. The placement of the Timer.delay() is very important because having a drive train after a time greater than, for example, .15 seconds, will start to see a very small twitch in activating the motors and cause it to slow down and start up at intervals. It can get weird!

Happy Competition!

Lalaland1125 18-02-2012 11:06

Re: What is the point of timer.delay?
 
Just double checking, won't timer.delay also pretty much freeze the thread that the robot is running under, so if you are running a single threaded robot the robot will completely freeze for that amount of time?

EricVanWyk 18-02-2012 11:12

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by Lalaland1125 (Post 1129457)
Just double checking, won't timer.delay also pretty much freeze the thread that the robot is running under, so if you are running a single threaded robot the robot will completely freeze for that amount of time?

Almost. It will pause the thread it is called from, allowing your other threads to take that time to do stuff. Think of it as a way of saying "I'm good for the next N milliseconds, its someone else's turn."

Ether 18-02-2012 11:25

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by Lalaland1125 (Post 1129457)
Just double checking, won't timer.delay also pretty much freeze the thread that the robot is running under,

Yes, if by "freeze the thread" you mean "nothing else in that thread will execute until the delay expires".

Quote:

so if you are running a single threaded robot the robot will completely freeze for that amount of time?
Nothing else in that single thread will execute until the delay expires. So if that's what you mean by "the robot will completely freeze for that amount of time", then yes.



Herbblood 18-02-2012 12:36

Re: What is the point of timer.delay?
 
Yeah, with the delay the entire program stops until the delay is over.

Ether 18-02-2012 12:57

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by Herbblood (Post 1129491)
Yeah, with the delay the entire program stops until the delay is over.

For the benefit of students reading this thread, as a blanket statement without qualifiers, that is untrue.

If the delay is implemented as blocked waiting (rather than busy waiting) it halts only the thread in which it is located. Other threads continue to execute. Even if the delay is implemented as busy waiting, other threads will execute IF they are of greater priority, or if they are of equal priority AND time-slicing is enabled.




wireties 18-02-2012 15:06

Quote:

Originally Posted by Ether (Post 1129510)
If the delay is implemented as blocked waiting (rather than busy waiting) it halts only the thread in which it is located. Other threads continue to execute. Even if the delay is implemented as busy waiting, other threads will execute IF they are of greater priority, or if they are of equal priority AND time-slicing is enabled.

That is really close. VxWorks has the following task states: executing, ready, blocked, delayed or any of these things in combination with suspended or stopped. I suspect the delay method simply puts the task into a delayed state. The "busy waiting" thing is not done by the OS. I think ether is referring to something commonly called a spinlock - that could be the case with this delay method but it is nothing to do with the OS.

Ether 18-02-2012 16:21

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by wireties (Post 1129582)
That is really close. VxWorks has the following task states: executing, ready, blocked, delayed or any of these things in combination with suspended or stopped. I suspect the delay method simply puts the task into a delayed state. The "busy waiting" thing is not done by the OS. I think ether is referring to something commonly called a spinlock - that could be the case with this delay method but it is nothing to do with the OS.

I guess I could have been clearer. None of my comments were referring to the OS. Here's an expanded clumsy-reading version of my post, with nothing left to the reader's imagination:

If the delay is implemented as blocked waiting (used generically here to include blocked waiting for a timer) (rather than busy waiting aka "spinning", either by the programmer or by a call to an intrinsic function of the language that the programmer is using or by a library routine that the programmer calls either directly or indirectly) it halts only the thread in which it is located. Other threads continue to execute. Even if the delay is implemented (see previous exansion of "implemented") as busy waiting (aka "spinning"), other threads will execute IF they are of greater priority, or if they are of equal priority AND time-slicing is enabled.



Sunstroke 19-02-2012 00:42

Re: What is the point of timer.delay?
 
I believe that in Squawk (the Java VM on the cRIO), all threads are artificial constructs in the VM.

I mean that in the sense that the Squawk system will not use the cRIO's built in threading capabilities, it just kind of pretends to use them.

Also, Timer.delay() is implemented with Thread.sleep() and so it will NOT prevent other threads from running while it is asleep. However, the thread that calls Timer.delay(...) will wait until that time has passed.

Ether 19-02-2012 00:47

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by Sunstroke (Post 1129982)
the Squawk system will not use the cRIO's built in threading capabilities

Just a point of clarification: the cRIO doesn't have a "built-in" threading capability. It uses a third-party RTOS, vxWorks.



gixxy 19-02-2012 14:29

Re: What is the point of timer.delay?
 
I know that if you put in a Timer.delay() it will delay EVERYTHING. I put in a 4 second delay on a command to stop it from being called too often, but during that 4 seconds I couldn't do anything, the DriveTrain,our BallFeed belt. everything was frozen.

Ether 19-02-2012 14:36

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by gixxy (Post 1130196)
I know that if you put in a Timer.delay() it will delay EVERYTHING. I put in a 4 second delay on a command to stop it from being called too often, but during that 4 seconds I couldn't do anything, the DriveTrain,our BallFeed belt. everything was frozen.

Did you read posts 4, 5, 7, 8, 9, and 10 about threads ?



gixxy 19-02-2012 22:35

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by Ether (Post 1130204)
Did you read posts 4, 5, 7, 8, 9, and 10 about threads ?



We are multithreaded. Which is why this is confusing me.

Ether 19-02-2012 22:40

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by gixxy (Post 1130535)
We are multithreaded.

Quote:

Originally Posted by gixxy (Post 1130196)
I know that if you put in a Timer.delay() it will delay EVERYTHING. I put in a 4 second delay on a command to stop it from being called too often, but during that 4 seconds I couldn't do anything, the DriveTrain,our BallFeed belt. everything was frozen.

Are you saying that if you add a Timer.delay() to one thread (let's call it "Thread A") it will stop execution of all other threads?

Is Thread A higher priority than the other threads?

or

Is Thread A the same priority as the other threads, and you have time-slicing disabled?



NS_Radication 19-02-2012 22:45

Re: What is the point of timer.delay?
 
What exactly is a thread and there can be multiple to change what delays or not.

This is confusing due to a lack of certain terminology.:confused:

Ether 19-02-2012 22:47

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by NS_Radication (Post 1130544)
What exactly is a thread

http://en.wikipedia.org/wiki/Thread_%28computing%29



Chiller 19-02-2012 22:50

Re: What is the point of timer.delay?
 
Threads are amazing!

Sunstroke 20-02-2012 03:15

Re: What is the point of timer.delay?
 
Quote:

Originally Posted by gixxy (Post 1130196)
I know that if you put in a Timer.delay() it will delay EVERYTHING. I put in a 4 second delay on a command to stop it from being called too often, but during that 4 seconds I couldn't do anything, the DriveTrain,our BallFeed belt. everything was frozen.

The command system is actually not multithreaded. There is one thread that goes around and constantly calls the initialize/execute/... on each running command. So using Timer.delay in a command is a terrible idea.

However, you can use the WaitCommand in a command group, or you can use the timeSinceInitialized() method built into each command to do some timing based code.


All times are GMT -5. The time now is 11:27.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi