001package com.ctre.phoenix;
002
003/**
004 * Class with basic utility methods
005 */
006public class Util
007{
008        /**
009         * Caps the value
010         * @param value Value to cap
011         * @param peak Maximum/-Minimum value can be
012         * @return Capped value
013         */
014        public static double cap(double value, double peak)
015        {
016                if(value < -peak) value = -peak;
017                if(value > peak) value = peak;
018                return value;
019        }
020        
021        /**
022         * Scales rotations to native units
023         * @param scalar Value to scale by
024         * @param fullRotations Number of rotations to scale by
025         * @return Scaled units
026         */
027        public static int scaleRotationsToNativeUnits(double scalar, double fullRotations) {
028                /* first assume we don't have config info, prep the default return */
029                int retval = (int) fullRotations;
030                /* apply scalar if its available */
031                if (scalar > 0) {
032                  retval = (int) (fullRotations * scalar);
033                }
034                return retval;
035        }
036        /**
037         * Scales velocity to native units
038         * @param scalar Value to scale by
039         * @param rpm Velocity in rotations per minute
040         * @return Scaled velocity
041         */
042        public static int scaleVelocityToNativeUnits(double scalar, double rpm) {
043                /* first assume we don't have config info, prep the default return */
044                int retval = (int) rpm;
045                /* apply scalar if its available */
046                if (scalar > 0) {
047                  retval = (int) (rpm * scalar);
048                }
049                return retval;
050        }
051        /**
052         * Scales native units to rotations
053         * @param scalar Value to scale by
054         * @param nativePos Native position units
055         * @return Scaled units
056         */
057        public static double scaleNativeUnitsToRotations(double scalar, long nativePos) {
058                /* first assume we don't have config info, prep the default return */
059                double retval = (double) nativePos;
060                /* retrieve scaling info */
061                if (scalar > 0) {
062                  retval = ((double) nativePos) / scalar;
063                }
064                return retval;
065        }
066        /**
067         * Scales Native units to velocity
068         * @param scalar Value to scale by
069         * @param nativeVel Native velocity units
070         * @return Scaled units
071         */
072        public static double scaleNativeUnitsToRpm(double scalar, long nativeVel) {
073                /* first assume we don't have config info, prep the default return */
074                double retval = (double) nativeVel;
075                /* apply scalar if its available */
076                if (scalar > 0) {
077                  retval = (double) (nativeVel) / (scalar);
078                }
079                return retval;
080        }
081}