RoboJDETM v2.0

com.ridgesoft.robotics
Class AnalogShaftEncoder

java.lang.Object
  extended by java.lang.Thread
      extended by com.ridgesoft.robotics.AnalogShaftEncoder
All Implemented Interfaces:
DirectionListener, ShaftEncoder, Runnable

public class AnalogShaftEncoder
extends Thread
implements ShaftEncoder, DirectionListener

Implements an incremental shaft encoder using a photoreflive or break-beam sensor to track the position of a wheel.

This class is designed to interface to an infrared photoreflective sensor, such as a Fairchild QRB1134 sensor, to sense the position of a wheel. Using its own thread, this class polls the sensor signal, counting the transitions between high and low values which occur each time the edge of a wheel spoke passes in front of the sensor. Because this class will count both edges of each spoke, the change in the counter value for each full rotation of the wheel will be equal to twice the number of spokes in the wheel.

By implementing the DirectionListener interface, this class is able to listen for changes in direction of the motor driving the wheel, enabling it to keep track of which direction the wheel is rotating. By counting up when the wheel rotates forward and counting down when the wheel rotates backward, this class is able to track the absolute position of the wheel as moves forward and backward.

Knowing the position of the wheels and the geometry of the robot, a program can keep track of the robot's position. This class can be used in conjuntion with the OdometricLocalizer class for position tracking.

This class implements a simple filter (hysterisis) to ensure small changes of the sensor signal due to noise will not be counted. This is accomplished by using separate thresholds to detect high to low signal transitions and low to high signal transitions.

This class uses its own thread to periodically poll the sensor signal. The accuracy of the encoder depends on the thread priority and polling period being set correctly.

This thread should normally be given a high priority. Running this thread at too low a priority may cause the encoder to miss signal transitions and, therefore, miscount. The priority should be higher than all other threads that depend on this encoder. Otherwise, the dependent threads could prevent this encoder from being accurate, undermining their own accuracy.

The polling period should be chosen such that the sensor is polled at atleast twice the frequency at which the encoder is expected to count. The polling period can be easily calculated knowing the maximum rotational speed of the wheel and the number of encoder counts per wheel rotation. Polling too frequently consumes computing power, leaving less computational power for other tasks.

See Also:
OdometricLocalizer

Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
AnalogShaftEncoder(AnalogInput input, int lowThreshold, int highThreshold, int period, int threadPriority)
          Constructs an AnalogShaftEncoder object.
 
Method Summary
 int getCounts()
          Gets the current value of the encoder counter.
 int getRate()
          Not supported.
 void run()
          Encoder thread's run method which polls the sensor and maintains the encoder's counter.
 String toString()
          Returns a String containing the decimal value of the encoder counter.
 void updateDirection(boolean isForward)
          Listener method to enable the encoder to know whether it should increment or decrement each time it senses a transition of the sensor signal.
 
Methods inherited from class java.lang.Thread
activeCount, currentThread, dumpStack, getName, getPriority, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, setDaemon, setName, setPriority, sleep, start, yield
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait
 

Constructor Detail

AnalogShaftEncoder

public AnalogShaftEncoder(AnalogInput input,
                          int lowThreshold,
                          int highThreshold,
                          int period,
                          int threadPriority)
Constructs an AnalogShaftEncoder object.

Parameters:
input - AnalogInput to which the sensor is attached
lowThreshold - value below which the sensor signal be considered to be low
highThreshold - value above which the sensor signal is considered to be high
period - sensor polling period in milliseconds
threadPriority - priority of this thread
Method Detail

getCounts

public int getCounts()
Gets the current value of the encoder counter.

Specified by:
getCounts in interface ShaftEncoder
Returns:
current counter value

getRate

public int getRate()
Not supported.

Specified by:
getRate in interface ShaftEncoder
Returns:
always returns 0

run

public void run()
Encoder thread's run method which polls the sensor and maintains the encoder's counter.

Do not call this method directly.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

toString

public String toString()
Returns a String containing the decimal value of the encoder counter.

Overrides:
toString in class Thread
Returns:
string

updateDirection

public void updateDirection(boolean isForward)
Listener method to enable the encoder to know whether it should increment or decrement each time it senses a transition of the sensor signal.

Specified by:
updateDirection in interface DirectionListener
Parameters:
isForward - true if the counter should increment, false if it should decrement

RoboJDETM v2.0

Copyright (c) 2003-2009 RidgeSoft, LLC. All rights reserved.
www.ridgesoft.com