Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Using TimerTasks (http://www.chiefdelphi.com/forums/showthread.php?t=82970)

Geek 2.0 18-02-2010 08:21

Using TimerTasks
 
Is it a good idea to create TimerTasks to take care of updating certain portions of the robot, such as the drive train or the ball handling system? For instance, if there is something that needs to be called regularly (like setting the Jaguars for motors or checking limit switches, etc.) just set a TimerTask to call it at a 20ms period so that we don't have to worry about updating.

I have them working, but I'm wondering if there are any disadvantages that I'm missing, and also hoping to help anyone who wants to try.

Lord_Jeremy 18-02-2010 22:31

Re: Using TimerTasks
 
It sounds to me like you're redoing what IterativeRobot does. Maybe I missed something but you're saying you want certain things to only happen periodically? I would just use IterativeRobot and change the update period or use counters to call your functions every X cycles, since the teleopPeriodic frequency is known.

Geek 2.0 18-02-2010 23:16

Re: Using TimerTasks
 
Well, we're already doing that, but I set up a TimerTask because it's just cleaner, because you don't need to make sure your update functions are at the end of your iterative loop or whatever. Are there any downsides to using a TimerTask?

Lord_Jeremy 19-02-2010 00:19

Re: Using TimerTasks
 
Eh, I don't know of any, but you should probably hold out for someone with more know-how...

Jared Russell 19-02-2010 08:16

Re: Using TimerTasks
 
We use TimerTasks extensively in our code this year. Every mechanism that has any "background" tasks like filters or PID controllers gets one. In my opinion it helps to keep your robot's "main loop" uncluttered.

Geek 2.0 20-02-2010 11:04

Re: Using TimerTasks
 
Thanks for the reassurance.

Here's another question... If I use Timer.delay() from the WPILibJ in a TimerTask, does that still pause ALL of your code? I wouldn't think so, because it only sleeps the current thread (as I understand it... I'm not sure).

Here's the delay() function in the Timer Class:

Code:

    public static void delay(final double seconds) {
        try {
            Thread.sleep((long) (seconds * 1e3));
        } catch (final InterruptedException e) {
        }
    }

Thanks!

ericarseneau 22-02-2010 17:03

Re: Using TimerTasks
 
It only pauses the calling Thread. Although you do need to keep in mind that this type of delay does not mean that something will occurr every seconds of time. It means that the something will occur AFTER seconds of time has gone by. This means that if your code runs for a long time, you may not get the period you might be expecting.

JasonStern 23-02-2010 10:39

Re: Using TimerTasks
 
Quote:

Originally Posted by ericarseneau (Post 926469)
... does not mean that something will occurr every seconds of time.

You can call Timer.scheduleAtFixedRate if you want something to be run at a fixed period.

Timer.schedule will run something at a fixed delay instead.

Check out the javadocs for more info: http://www.wbrobotics.com/javadoc/java/util/Timer.html

-Jason


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

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