Package trclib.dataprocessor
Class TrcUtil
java.lang.Object
trclib.dataprocessor.TrcUtil
This class contains platform independent utility methods. All methods in this class are static. It is not
necessary to instantiate this class to call its methods.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
This interface provides the method to get data of the specified type. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final double
static final double
static final double
static final double
static final double
static final double
static final double
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic double
applyDeadband
(double value, double deadband) This method checks if the given value is within the deadband range.static double
average
(double... nums) This method calculates and returns the average of the numbers in the given array.static int
bytesToInt
(byte data) This method converts a byte into an integer.static int
bytesToInt
(byte low, byte high) This method combines two bytes into an integer.static int
bytesToInt
(byte data1, byte data2, byte data3, byte data4) This method combines two bytes into an integer.static short
bytesToShort
(byte low, byte high) This method combines two bytes into a short.static double
clipRange
(double value) This method clips the given value to the range between -1.0 and 1.0.static double
clipRange
(double value, double limit) This method clips the given value to the range limited by -limit as the low limit and limit as the high limit.static double
clipRange
(double value, double lowLimit, double highLimit) This method clips the given value to the range limited by the given low and high limits.static int
clipRange
(int value, int lowLimit, int highLimit) This method clips the given value to the range limited by the given low and high limits.static org.apache.commons.math3.linear.RealMatrix
createCCWRotationMatrix
(double angle) Create a rotation matrix that will rotate a point counter-clockwise about the origin by a specific number of degrees.static org.apache.commons.math3.linear.RealMatrix
createCWRotationMatrix
(double angle) Create a rotation matrix that will rotate a point clockwise about the origin by a specific number of degrees.static org.apache.commons.math3.linear.RealVector
createVector
(double... vector) Create anArrayRealVector
with the supplied numbers.static boolean
inRange
(double value, double low, double high) This method checks if the given value is within the specified range inclusive.static boolean
inRange
(double value, double low, double high, boolean inclusive) This method checks if the given value is within the specified range.static boolean
inRange
(int value, int low, int high) This method checks if the given value is within the specified range inclusive.static boolean
inRange
(int value, int low, int high, boolean inclusive) This method checks if the given value is within the specified range.static byte
intToByte
(int data, int index) This method returns the indexed byte of an integer.static int
leastSignificantSetBit
(int data) This method returns a bit mask of the least significant set bit.static int
leastSignificantSetBitPosition
(int data) This method returns the bit position of the least significant set bit of the given data.static double
magnitude
(double... nums) This method calculates the magnitudes of the given array of numbers.static double
maxMagnitude
(double... nums) This method returns the maximum magnitude of numbers in the specified array.static double
median
(double... num) This method calculates and returns the median of the numbers in the given array.static double
modulo
(double a, double b) This method calculates the modulo of two numbers.static int
mostSignificantSetBit
(int data) This method returns a bit mask of the least significant set bit.static int
mostSignificantSetBitPosition
(int data) This method returns the bit position of the most significant set bit of the given data.static double[]
normalize
(double... nums) This method normalizes the given array of numbers such that no number exceeds +/- 1.0.static void
normalizeInPlace
(double[] nums) This method normalizes the given array of numbers such that no number exceeds +/- 1.0.static org.apache.commons.math3.linear.RealVector
polarToCartesian
(double r, double theta) Convert a point from a polar coordinate system to a cartesian coordinate system.static org.apache.commons.math3.linear.RealVector
rotateCCW
(org.apache.commons.math3.linear.RealVector vector, double angle) Rotate a point counter-clockwise about the origin.static org.apache.commons.math3.linear.RealVector
rotateCW
(org.apache.commons.math3.linear.RealVector vector, double angle) Rotate a point clockwise about the origin.static int
round
(double num) This method rounds a double to the nearest integer.static double
round
(double num, double precision) This method rounds a double to the specified precision.static double
scaleRange
(double value, double lowSrcRange, double highSrcRange, double lowDstRange, double highDstRange) This method scales the given value from the source range to the target range.static int
scaleRange
(int value, int lowSrcRange, int highSrcRange, int lowDstRange, int highDstRange) This method scales the given value from the source range to the target range.static int
setBitMask
(int... bitPositions) This method sets a bitmask with the given bit positions.static double
sum
(double... nums) This method sums an array of numbers.
-
Field Details
-
MM_PER_INCH
public static final double MM_PER_INCH- See Also:
-
METERS_PER_INCH
public static final double METERS_PER_INCH- See Also:
-
INCHES_PER_MM
public static final double INCHES_PER_MM- See Also:
-
INCHES_PER_CM
public static final double INCHES_PER_CM- See Also:
-
INCHES_PER_METER
public static final double INCHES_PER_METER- See Also:
-
EARTH_GRAVITATIONAL_CONSTANT
public static final double EARTH_GRAVITATIONAL_CONSTANT- See Also:
-
BATTERY_NOMINAL_VOLTAGE
public static final double BATTERY_NOMINAL_VOLTAGE- See Also:
-
-
Constructor Details
-
TrcUtil
public TrcUtil()
-
-
Method Details
-
modulo
public static double modulo(double a, double b) This method calculates the modulo of two numbers. Unlike the%
operator, this returns a number in the range [0, b). For some reason, in Java, the%
operator actually does remainder, which means the result is in the range (-b, b).- Parameters:
a
- specifies the dividend.b
- specifies the divisor.- Returns:
- the modulo in the range [0, b)
-
sum
public static double sum(double... nums) This method sums an array of numbers.- Parameters:
nums
- specifies the array of numbers to be summed.- Returns:
- sum of the numbers.
-
median
public static double median(double... num) This method calculates and returns the median of the numbers in the given array.- Parameters:
num
- specifies the number array.- Returns:
- median of numbers in the array.
-
average
public static double average(double... nums) This method calculates and returns the average of the numbers in the given array.- Parameters:
nums
- specifies the number array.- Returns:
- average of all numbers in the array. If the array is empty, return 0.
-
magnitude
public static double magnitude(double... nums) This method calculates the magnitudes of the given array of numbers.- Parameters:
nums
- specifies the number array.- Returns:
- magnitude of all numbers in the array.
-
maxMagnitude
public static double maxMagnitude(double... nums) This method returns the maximum magnitude of numbers in the specified array.- Parameters:
nums
- specifies the number array.- Returns:
- maximum magnitude of the numbers in the array.
-
leastSignificantSetBit
public static int leastSignificantSetBit(int data) This method returns a bit mask of the least significant set bit.- Parameters:
data
- specifies the data to find the least significant set bit.- Returns:
- bit mask that has only the least significant set bit.
-
leastSignificantSetBitPosition
public static int leastSignificantSetBitPosition(int data) This method returns the bit position of the least significant set bit of the given data.- Parameters:
data
- specifies the data to determine its least significant set bit position.- Returns:
- 0-based least significant set bit position. -1 if no set bit.
-
mostSignificantSetBit
public static int mostSignificantSetBit(int data) This method returns a bit mask of the least significant set bit.- Parameters:
data
- specifies the data to find the least significant set bit.- Returns:
- bit mask that has only the least significant set bit.
-
mostSignificantSetBitPosition
public static int mostSignificantSetBitPosition(int data) This method returns the bit position of the most significant set bit of the given data.- Parameters:
data
- specifies the data to determine its most significant set bit position.- Returns:
- 0-based most significant set bit position. -1 if no set bit.
-
setBitMask
public static int setBitMask(int... bitPositions) This method sets a bitmask with the given bit positions.- Parameters:
bitPositions
- specifies the bit positions to be set to 1. Bit positions are 0-based.- Returns:
- bit mask.
-
normalizeInPlace
public static void normalizeInPlace(double[] nums) This method normalizes the given array of numbers such that no number exceeds +/- 1.0. If no number exceeds the magnitude of 1.0, nothing will change, otherwise the original nums array will be modified in place.- Parameters:
nums
- specifies the number array.
-
normalize
public static double[] normalize(double... nums) This method normalizes the given array of numbers such that no number exceeds +/- 1.0.- Parameters:
nums
- specifies the number array.- Returns:
- normalized number array.
-
round
public static int round(double num) This method rounds a double to the nearest integer.- Parameters:
num
- specifies the number to round.- Returns:
- number rounded to the nearest integer.
-
round
public static double round(double num, double precision) This method rounds a double to the specified precision.- Parameters:
num
- specifies the number to round.precision
- specifies the precision to round to.- Returns:
- number rounded to the specified precision.
-
inRange
public static boolean inRange(int value, int low, int high, boolean inclusive) This method checks if the given value is within the specified range.- Parameters:
value
- The value to be checked.low
- The low limit of the range.high
- The high limit of the range.inclusive
- specifies true if the range is inclusive [low, high], otherwise the range is exclusive (low, high).- Returns:
- true if the value is within range, false otherwise.
-
inRange
public static boolean inRange(int value, int low, int high) This method checks if the given value is within the specified range inclusive.- Parameters:
value
- The value to be checked.low
- The low limit of the range.high
- The high limit of the range.- Returns:
- true if the value is within range, false otherwise.
-
inRange
public static boolean inRange(double value, double low, double high, boolean inclusive) This method checks if the given value is within the specified range.- Parameters:
value
- The value to be checked.low
- The low limit of the range.high
- The high limit of the range.inclusive
- specifies true if the range is inclusive [low, high], otherwise the range is exclusive (low,high).- Returns:
- true if the value is within range, false otherwise.
-
inRange
public static boolean inRange(double value, double low, double high) This method checks if the given value is within the specified range inclusive.- Parameters:
value
- The value to be checked.low
- The low limit of the range.high
- The high limit of the range.- Returns:
- true if the value is within range, false otherwise.
-
clipRange
public static int clipRange(int value, int lowLimit, int highLimit) This method clips the given value to the range limited by the given low and high limits.- Parameters:
value
- specifies the value to be clippedlowLimit
- specifies the low limit of the range.highLimit
- specifies the high limit of the range.- Returns:
- the result of the clipped value.
-
clipRange
public static double clipRange(double value, double lowLimit, double highLimit) This method clips the given value to the range limited by the given low and high limits.- Parameters:
value
- specifies the value to be clippedlowLimit
- specifies the low limit of the range.highLimit
- specifies the high limit of the range.- Returns:
- the result of the clipped value.
-
clipRange
public static double clipRange(double value, double limit) This method clips the given value to the range limited by -limit as the low limit and limit as the high limit.- Parameters:
value
- specifies the value to be clippedlimit
- specifies the limit of the range between -limit and limit.- Returns:
- the result of the clipped value.
-
clipRange
public static double clipRange(double value) This method clips the given value to the range between -1.0 and 1.0.- Parameters:
value
- specifies the value to be clipped- Returns:
- the result of the clipped value.
-
scaleRange
public static int scaleRange(int value, int lowSrcRange, int highSrcRange, int lowDstRange, int highDstRange) This method scales the given value from the source range to the target range.- Parameters:
value
- specifies the value to be scaled.lowSrcRange
- specifies the low limit of the source range.highSrcRange
- specifies the high limit of the source range.lowDstRange
- specifies the low limit of the target range.highDstRange
- specifies the high limit of the target range- Returns:
- the result of the scaled value.
-
scaleRange
public static double scaleRange(double value, double lowSrcRange, double highSrcRange, double lowDstRange, double highDstRange) This method scales the given value from the source range to the target range.- Parameters:
value
- specifies the value to be scaled.lowSrcRange
- specifies the low limit of the source range.highSrcRange
- specifies the high limit of the source range.lowDstRange
- specifies the low limit of the target range.highDstRange
- specifies the high limit of the target range- Returns:
- the result of the scaled value.
-
applyDeadband
public static double applyDeadband(double value, double deadband) This method checks if the given value is within the deadband range. If so, it returns 0.0 else it returns the unchanged value.- Parameters:
value
- specifies the value to be checked.deadband
- specifies the deadband zone.- Returns:
- the value 0.0 if within deadband, unaltered otherwise.
-
intToByte
public static byte intToByte(int data, int index) This method returns the indexed byte of an integer.- Parameters:
data
- specifies the integer value.index
- specifies the byte index.- Returns:
- indexed byte of the integer.
-
bytesToInt
public static int bytesToInt(byte data1, byte data2, byte data3, byte data4) This method combines two bytes into an integer.- Parameters:
data1
- specifies the lowest byte.data2
- specifies the next lowest byte.data3
- specifies the next byte.data4
- specifies the highest byte.- Returns:
- the converted integer.
-
bytesToInt
public static int bytesToInt(byte low, byte high) This method combines two bytes into an integer.- Parameters:
low
- specifies the low byte.high
- specifies the high byte.- Returns:
- the converted integer.
-
bytesToInt
public static int bytesToInt(byte data) This method converts a byte into an integer.- Parameters:
data
- specifies the byte data.- Returns:
- the converted integer.
-
bytesToShort
public static short bytesToShort(byte low, byte high) This method combines two bytes into a short.- Parameters:
low
- specifies the low byte.high
- specifies the high byte.- Returns:
- the converted short.
-
polarToCartesian
public static org.apache.commons.math3.linear.RealVector polarToCartesian(double r, double theta) Convert a point from a polar coordinate system to a cartesian coordinate system.- Parameters:
r
- Magnitude of vectortheta
- Direction of vector, in degrees clockwise from 0 (+y)- Returns:
- Vector in a cartesian coordinate system representing the same point.
-
createVector
public static org.apache.commons.math3.linear.RealVector createVector(double... vector) Create anArrayRealVector
with the supplied numbers. This is just quality of life to reduce typing.- Parameters:
vector
- The data to put in the vector.- Returns:
- A new vector instance with the data.
-
rotateCCW
public static org.apache.commons.math3.linear.RealVector rotateCCW(org.apache.commons.math3.linear.RealVector vector, double angle) Rotate a point counter-clockwise about the origin.- Parameters:
vector
- The vector to rotate.angle
- The angle in degrees to rotate by.- Returns:
- The vector after the rotation transformation.
-
rotateCW
public static org.apache.commons.math3.linear.RealVector rotateCW(org.apache.commons.math3.linear.RealVector vector, double angle) Rotate a point clockwise about the origin.- Parameters:
vector
- The vector to rotate.angle
- The angle in degrees to rotate by.- Returns:
- The vector after the rotation transformation.
-
createCCWRotationMatrix
public static org.apache.commons.math3.linear.RealMatrix createCCWRotationMatrix(double angle) Create a rotation matrix that will rotate a point counter-clockwise about the origin by a specific number of degrees.- Parameters:
angle
- The angle in degrees to rotate by.- Returns:
- A rotation matrix describing a counter-clockwise rotation by
angle
degrees.
-
createCWRotationMatrix
public static org.apache.commons.math3.linear.RealMatrix createCWRotationMatrix(double angle) Create a rotation matrix that will rotate a point clockwise about the origin by a specific number of degrees.- Parameters:
angle
- The angle in degrees to rotate by.- Returns:
- A rotation matrix describing a clockwise rotation by
angle
degrees.
-