org.team751.framework
Class RobotTask

java.lang.Object
  extended by java.lang.Thread
      extended by org.team751.framework.RobotTask
All Implemented Interfaces:
java.lang.Runnable

public abstract class RobotTask
extends java.lang.Thread

A thread that contains a robot task. Operations in this task can safely block without freezing the robot.

Note that the methods in this class are called as frequently as possible. You must add a call to Thread.sleep(long) or Timer.delay(double) to prevent this task from running continuously and causing 100% CPU usage.

Author:
Sam Crow

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static float kMinTaskTime
          The minimum time, in seconds, that a task (disabled(), autonomous(), or teleop()) is expected to take.
private  CompetitionState state
          The current phase of competition
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
RobotTask()
           
 
Method Summary
protected  void autonomous()
          The robot task handler calls this method as frequently as possible while the robot is in autonomous mode.
protected  void checkInterrupt()
          Check if the thread has been interrupted.
protected  void disabled()
          The robot task handler calls this method as frequently as possible while the robot is disabled.
 void run()
           
(package private)  void setState(CompetitionState state)
          Change this task's competition state If the new state is different from the current state, this method will call interrupt() to ensure that it knows about the new state.
protected  void teleop()
          The robot task handler calls this method as frequently as possible while the robot is in teleoperated mode.
private  void timingWarning(java.lang.String methodName)
          Print a warning to System.err telling the user that a task didn't take long enough to complete and probably lacks a call to Thread.sleep(long) or Timer.delay(double).
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

state

private volatile CompetitionState state
The current phase of competition


kMinTaskTime

public static final float kMinTaskTime
The minimum time, in seconds, that a task (disabled(), autonomous(), or teleop()) is expected to take. If one of those method calls takes less than this time, it is likely that the programmer didn't use Thread.sleep(long) or Timer.delay(double) to prevent continous task operation and 100% CPU usage. In this event, a warning will be printed to System.err .

See Also:
Constant Field Values
Constructor Detail

RobotTask

public RobotTask()
Method Detail

run

public final void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

setState

final void setState(CompetitionState state)
Change this task's competition state If the new state is different from the current state, this method will call interrupt() to ensure that it knows about the new state.

Parameters:
state - The new state to set

disabled

protected void disabled()
                 throws java.lang.InterruptedException
The robot task handler calls this method as frequently as possible while the robot is disabled.

Throws:
java.lang.InterruptedException - If the task has been interrupted. You don't need to worry about this. The robot task handler will deal with it.

autonomous

protected void autonomous()
                   throws java.lang.InterruptedException
The robot task handler calls this method as frequently as possible while the robot is in autonomous mode.

Throws:
java.lang.InterruptedException - If the task has been interrupted. You don't need to worry about this. The robot task handler will deal with it.

teleop

protected void teleop()
               throws java.lang.InterruptedException
The robot task handler calls this method as frequently as possible while the robot is in teleoperated mode.

Throws:
java.lang.InterruptedException - If the task has been interrupted. You don't need to worry about this. The robot task handler will deal with it.

checkInterrupt

protected final void checkInterrupt()
                             throws java.lang.InterruptedException
Check if the thread has been interrupted. An interrupt happens when the competition state changes. Robot code should call this method in between iterations whenever doing anything that could take some time (except Thread.sleep() or anything else that throws an InterruptedException) to ensure that it responds quickly to state changes.

If this method is called and that task has been interrupted, the task will immediately return up to RobotTask.run() and the state will be checked again.

If you need to clean up after a process in the event that it's interrupted, do this:
 // Do some operation
 try {
        checkInterrupt();
 } catch (InterruptedException e) {
        // Clean up
        throw e; // Always re-throw so that the state will be rechecked
 }
 

Throws:
java.lang.InterruptedException - If the task has been interrupted. You don't need to worry about this. The robot task handler will deal with it.

timingWarning

private void timingWarning(java.lang.String methodName)
Print a warning to System.err telling the user that a task didn't take long enough to complete and probably lacks a call to Thread.sleep(long) or Timer.delay(double).

Parameters:
methodName - The name of the method that was called, not including parenthesis. For example, "disabled", "autonomous", or "teleop".