"

2013 FRC Java API

"

edu.wpi.first.wpilibj
Class Counter

java.lang.Object
  extended by edu.wpi.first.wpilibj.SensorBase
      extended by edu.wpi.first.wpilibj.Counter
All Implemented Interfaces:
CounterBase, LiveWindowSendable, PIDSource, Sendable
Direct Known Subclasses:
GearTooth

public class Counter
extends SensorBase
implements CounterBase, LiveWindowSendable, PIDSource

Class for counting the number of ticks on a digital input channel. This is a general purpose class for counting repetitive events. It can return the number of counts, the period of the most recent cycle, and detect when the signal being counted has stopped by supplying a maximum cycle time.


Nested Class Summary
static class Counter.Mode
          Mode determines how and what the counter counts
 
Nested classes/interfaces inherited from interface edu.wpi.first.wpilibj.CounterBase
CounterBase.EncodingType
 
Nested classes/interfaces inherited from interface edu.wpi.first.wpilibj.PIDSource
PIDSource.PIDSourceParameter
 
Field Summary
 
Fields inherited from class edu.wpi.first.wpilibj.SensorBase
kAnalogChannels, kAnalogModules, kDigitalChannels, kPwmChannels, kRelayChannels, kSolenoidChannels, kSolenoidModules, kSystemClockTicksPerMicrosecond
 
Constructor Summary
Counter()
          Create an instance of a counter where no sources are selected.
Counter(AnalogTrigger trigger)
          Create an instance of a Counter object.
Counter(CounterBase.EncodingType encodingType, DigitalSource upSource, DigitalSource downSource, boolean inverted)
          Create an instance of a Counter object.
Counter(DigitalSource source)
          Create an instance of a counter from a Digital Input.
Counter(int channel)
          Create an instance of a Counter object.
Counter(int slot, int channel)
          Create an instance of a Counter object.
 
Method Summary
 void clearDownSource()
          Disable the down counting source to the counter.
 void clearUpSource()
          Disable the up counting source to the counter.
 void free()
          Free the resources used by this object
 int get()
          Read the current counter value.
 boolean getDirection()
          The last direction the counter value changed.
 double getDistance()
          Read the current scaled counter value.
 double getPeriod()
          Get the Period of the most recent count.
 double getRate()
          Get the current rate of the Counter.
 int getSamplesToAverage()
          Get the Samples to Average which specifies the number of samples of the timer to average when calculating the period.
 String getSmartDashboardType()
          Live Window code, only does anything if live window is activated.
 boolean getStopped()
          Determine if the clock is stopped.
 ITable getTable()
          
 void initTable(ITable subtable)
          Initializes a table for this sendable object.
 double pidGet()
          Get the result to use in PIDController
 void reset()
          Reset the Counter to zero.
 void setDistancePerPulse(double distancePerPulse)
          Set the distance per pulse for this counter.
 void setDownSource(AnalogTrigger analogTrigger, AnalogTriggerOutput.Type triggerType)
          Set the down counting source to be an analog trigger.
 void setDownSource(DigitalSource source)
          Set the source object that causes the counter to count down.
 void setDownSource(int channel)
          Set the down counting source to be a digital input channel.
 void setDownSource(int slot, int channel)
          Set the down counting source to be a digital input slot and channel.
 void setDownSourceEdge(boolean risingEdge, boolean fallingEdge)
          Set the edge sensitivity on a down counting source.
 void setExternalDirectionMode()
          Set external direction mode on this counter.
 void setMaxPeriod(double maxPeriod)
          Set the maximum period where the device is still considered "moving".
 void setPIDSourceParameter(PIDSource.PIDSourceParameter pidSource)
          Set which parameter of the encoder you are using as a process control variable.
 void setPulseLengthMode(double threshold)
          Configure the counter to count in up or down based on the length of the input pulse.
 void setReverseDirection(boolean reverseDirection)
          Set the Counter to return reversed sensing on the direction.
 void setSamplesToAverage(int samplesToAverage)
          Set the Samples to Average which specifies the number of samples of the timer to average when calculating the period.
 void setSemiPeriodMode(boolean highSemiPeriod)
          Set Semi-period mode on this counter.
 void setUpdateWhenEmpty(boolean enabled)
          Select whether you want to continue updating the event timer output when there are no samples captured.
 void setUpDownCounterMode()
          Set standard up / down counting mode on this counter.
 void setUpSource(AnalogTrigger analogTrigger, AnalogTriggerOutput.Type triggerType)
          Set the up counting source to be an analog trigger.
 void setUpSource(DigitalSource source)
          Set the source object that causes the counter to count up.
 void setUpSource(int channel)
          Set the upsource for the counter as a digital input channel.
 void setUpSource(int slot, int channel)
          Set the up source for the counter as digital input channel and slot.
 void setUpSourceEdge(boolean risingEdge, boolean fallingEdge)
          Set the edge sensitivity on an up counting source.
 void start()
          Start the Counter counting.
 void startLiveWindowMode()
          Start having this sendable object automatically respond to value changes reflect the value on the table.
 void stop()
          Stop the Counter.
 void stopLiveWindowMode()
          Stop having this sendable object automatically respond to value changes.
 void updateTable()
          Update the table for this sendable object with the latest values.
 
Methods inherited from class edu.wpi.first.wpilibj.SensorBase
checkAnalogChannel, checkAnalogModule, checkDigitalChannel, checkDigitalModule, checkPWMChannel, checkPWMModule, checkRelayChannel, checkRelayModule, checkSolenoidChannel, checkSolenoidModule, getDefaultAnalogModule, getDefaultDigitalModule, getDefaultSolenoidModule, setDefaultAnalogModule, setDefaultDigitalModule, setDefaultSolenoidModule
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Counter

public Counter()
Create an instance of a counter where no sources are selected. Then they all must be selected by calling functions to specify the upsource and the downsource independently.


Counter

public Counter(DigitalSource source)
Create an instance of a counter from a Digital Input. This is used if an existing digital input is to be shared by multiple other objects such as encoders.

Parameters:
source - the digital source to count

Counter

public Counter(int channel)
Create an instance of a Counter object. Create an up-Counter instance given a channel. The default digital module is assumed.

Parameters:
channel - the digital input channel to count

Counter

public Counter(int slot,
               int channel)
Create an instance of a Counter object. Create an instance of an up-Counter given a digital module and a channel.

Parameters:
slot - The cRIO chassis slot for the digital module used
channel - The channel in the digital module

Counter

public Counter(CounterBase.EncodingType encodingType,
               DigitalSource upSource,
               DigitalSource downSource,
               boolean inverted)
Create an instance of a Counter object. Create an instance of a simple up-Counter given an analog trigger. Use the trigger state output from the analog trigger.

Parameters:
encodingType - which edges to count
upSource - first source to count
downSource - second source for direction
inverted - true to invert the count

Counter

public Counter(AnalogTrigger trigger)
Create an instance of a Counter object. Create an instance of a simple up-Counter given an analog trigger. Use the trigger state output from the analog trigger.

Parameters:
trigger - the analog trigger to count
Method Detail

free

public void free()
Description copied from class: SensorBase
Free the resources used by this object

Overrides:
free in class SensorBase

setUpSource

public void setUpSource(int slot,
                        int channel)
Set the up source for the counter as digital input channel and slot.

Parameters:
slot - the location of the digital module to use
channel - the digital port to count

setUpSource

public void setUpSource(int channel)
Set the upsource for the counter as a digital input channel. The slot will be the default digital module slot.

Parameters:
channel - the digital port to count

setUpSource

public void setUpSource(DigitalSource source)
Set the source object that causes the counter to count up. Set the up counting DigitalSource.

Parameters:
source - the digital source to count

setUpSource

public void setUpSource(AnalogTrigger analogTrigger,
                        AnalogTriggerOutput.Type triggerType)
Set the up counting source to be an analog trigger.

Parameters:
analogTrigger - The analog trigger object that is used for the Up Source
triggerType - The analog trigger output that will trigger the counter.

setUpSourceEdge

public void setUpSourceEdge(boolean risingEdge,
                            boolean fallingEdge)
Set the edge sensitivity on an up counting source. Set the up source to either detect rising edges or falling edges.

Parameters:
risingEdge - true to count rising edge
fallingEdge - true to count falling edge

clearUpSource

public void clearUpSource()
Disable the up counting source to the counter.


setDownSource

public void setDownSource(int channel)
Set the down counting source to be a digital input channel. The slot will be set to the default digital module slot.

Parameters:
channel - the digital port to count

setDownSource

public void setDownSource(int slot,
                          int channel)
Set the down counting source to be a digital input slot and channel.

Parameters:
slot - the location of the digital module to use
channel - the digital port to count

setDownSource

public void setDownSource(DigitalSource source)
Set the source object that causes the counter to count down. Set the down counting DigitalSource.

Parameters:
source - the digital source to count

setDownSource

public void setDownSource(AnalogTrigger analogTrigger,
                          AnalogTriggerOutput.Type triggerType)
Set the down counting source to be an analog trigger.

Parameters:
analogTrigger - The analog trigger object that is used for the Down Source
triggerType - The analog trigger output that will trigger the counter.

setDownSourceEdge

public void setDownSourceEdge(boolean risingEdge,
                              boolean fallingEdge)
Set the edge sensitivity on a down counting source. Set the down source to either detect rising edges or falling edges.

Parameters:
risingEdge - true to count the rising edge
fallingEdge - true to count the falling edge

clearDownSource

public void clearDownSource()
Disable the down counting source to the counter.


setUpDownCounterMode

public void setUpDownCounterMode()
Set standard up / down counting mode on this counter. Up and down counts are sourced independently from two inputs.


setExternalDirectionMode

public void setExternalDirectionMode()
Set external direction mode on this counter. Counts are sourced on the Up counter input. The Down counter input represents the direction to count.


setSemiPeriodMode

public void setSemiPeriodMode(boolean highSemiPeriod)
Set Semi-period mode on this counter. Counts up on both rising and falling edges.

Parameters:
highSemiPeriod - true to count up on both rising and falling

setPulseLengthMode

public void setPulseLengthMode(double threshold)
Configure the counter to count in up or down based on the length of the input pulse. This mode is most useful for direction sensitive gear tooth sensors.

Parameters:
threshold - The pulse length beyond which the counter counts the opposite direction. Units are seconds.

start

public void start()
Start the Counter counting. This enables the counter and it starts accumulating counts from the associated input channel. The counter value is not reset on starting, and still has the previous value.

Specified by:
start in interface CounterBase

get

public int get()
Read the current counter value. Read the value at this instant. It may still be running, so it reflects the current value. Next time it is read, it might have a different value.

Specified by:
get in interface CounterBase
Returns:
the count

getDistance

public double getDistance()
Read the current scaled counter value. Read the value at this instant, scaled by the distance per pulse (defaults to 1).

Returns:

reset

public void reset()
Reset the Counter to zero. Set the counter value to zero. This doesn't effect the running state of the counter, just sets the current value to zero.

Specified by:
reset in interface CounterBase

stop

public void stop()
Stop the Counter. Stops the counting but doesn't effect the current value.

Specified by:
stop in interface CounterBase

setMaxPeriod

public void setMaxPeriod(double maxPeriod)
Set the maximum period where the device is still considered "moving". Sets the maximum period where the device is considered moving. This value is used to determine the "stopped" state of the counter using the GetStopped method.

Specified by:
setMaxPeriod in interface CounterBase
Parameters:
maxPeriod - The maximum period where the counted device is considered moving in seconds.

setUpdateWhenEmpty

public void setUpdateWhenEmpty(boolean enabled)
Select whether you want to continue updating the event timer output when there are no samples captured. The output of the event timer has a buffer of periods that are averaged and posted to a register on the FPGA. When the timer detects that the event source has stopped (based on the MaxPeriod) the buffer of samples to be averaged is emptied. If you enable the update when empty, you will be notified of the stopped source and the event time will report 0 samples. If you disable update when empty, the most recent average will remain on the output until a new sample is acquired. You will never see 0 samples output (except when there have been no events since an FPGA reset) and you will likely not see the stopped bit become true (since it is updated at the end of an average and there are no samples to average).

Parameters:
enabled - true to continue updating

getStopped

public boolean getStopped()
Determine if the clock is stopped. Determine if the clocked input is stopped based on the MaxPeriod value set using the SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the device (and counter) are assumed to be stopped and it returns true.

Specified by:
getStopped in interface CounterBase
Returns:
Returns true if the most recent counter period exceeds the MaxPeriod value set by SetMaxPeriod.

getDirection

public boolean getDirection()
The last direction the counter value changed.

Specified by:
getDirection in interface CounterBase
Returns:
The last direction the counter value changed.

setReverseDirection

public void setReverseDirection(boolean reverseDirection)
Set the Counter to return reversed sensing on the direction. This allows counters to change the direction they are counting in the case of 1X and 2X quadrature encoding only. Any other counter mode isn't supported.

Parameters:
reverseDirection - true if the value counted should be negated.

getPeriod

public double getPeriod()
Get the Period of the most recent count. Returns the time interval of the most recent count. This can be used for velocity calculations to determine shaft speed.

Specified by:
getPeriod in interface CounterBase
Returns:
the time beteween the last two ticks in seconds

getRate

public double getRate()
Get the current rate of the Counter. Read the current rate of the counter accounting for the distance per pulse value. The default value for distance per pulse (1) yields units of pulses per second.

Returns:
The rate in units/sec

setSamplesToAverage

public void setSamplesToAverage(int samplesToAverage)
Set the Samples to Average which specifies the number of samples of the timer to average when calculating the period. Perform averaging to account for mechanical imperfections or as oversampling to increase resolution.

Parameters:
samplesToAverage - The number of samples to average from 1 to 127.

getSamplesToAverage

public int getSamplesToAverage()
Get the Samples to Average which specifies the number of samples of the timer to average when calculating the period. Perform averaging to account for mechanical imperfections or as oversampling to increase resolution.

Returns:
SamplesToAverage The number of samples being averaged (from 1 to 127)

setDistancePerPulse

public void setDistancePerPulse(double distancePerPulse)
Set the distance per pulse for this counter. This sets the multiplier used to determine the distance driven based on the count value from the encoder. Set this value based on the Pulses per Revolution and factor in any gearing reductions. This distance can be in any units you like, linear or angular.

Parameters:
distancePerPulse - The scale factor that will be used to convert pulses to useful units.

setPIDSourceParameter

public void setPIDSourceParameter(PIDSource.PIDSourceParameter pidSource)
Set which parameter of the encoder you are using as a process control variable. The counter class supports the rate and distance parameters.

Parameters:
pidSource - An enum to select the parameter.

pidGet

public double pidGet()
Description copied from interface: PIDSource
Get the result to use in PIDController

Specified by:
pidGet in interface PIDSource
Returns:
the result to use in PIDController

getSmartDashboardType

public String getSmartDashboardType()
Live Window code, only does anything if live window is activated.

Specified by:
getSmartDashboardType in interface Sendable
Returns:
the string representation of the named data type that will be used by the smart dashboard for this sendable

initTable

public void initTable(ITable subtable)
Initializes a table for this sendable object.

Specified by:
initTable in interface Sendable
Parameters:
subtable - The table to put the values in.

getTable

public ITable getTable()

Specified by:
getTable in interface Sendable
Returns:
the table that is currently associated with the sendable

updateTable

public void updateTable()
Update the table for this sendable object with the latest values.

Specified by:
updateTable in interface LiveWindowSendable

startLiveWindowMode

public void startLiveWindowMode()
Start having this sendable object automatically respond to value changes reflect the value on the table.

Specified by:
startLiveWindowMode in interface LiveWindowSendable

stopLiveWindowMode

public void stopLiveWindowMode()
Stop having this sendable object automatically respond to value changes.

Specified by:
stopLiveWindowMode in interface LiveWindowSendable

"

2013 FRC Java API

"

"
For updated information see the Java FRC site
"