001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.motorcontrol;
003
004import com.ctre.phoenix.ErrorCode;
005import com.ctre.phoenix.motorcontrol.can.BaseTalon;
006import com.ctre.phoenix.motorcontrol.can.MotControllerJNI;
007
008/**
009 * Collection of sensors available to the Talon FX.
010 * <p>
011 * For best performance and update-rate, we recommend using the
012 * configSelectedFeedbackSensor() and getSelectedSensor*() routines. However
013 * there are occasions where accessing raw sensor values may be useful or
014 * convenient. Particularly if you are seeding one sensor based on another, or
015 * need to circumvent sensor-phase.
016 * <p>
017 * Use the getTalonFXSensorCollection() routine inside your motor controller to create
018 * a sensor collection.
019 *
020 * @deprecated This device's Phoenix 5 API is deprecated for removal in the
021 * 2025 season. Users should update to Phoenix 6 firmware and migrate to the
022 * Phoenix 6 API. A migration guide is available at
023 * https://v6.docs.ctr-electronics.com/en/stable/docs/migration/migration-guide/index.html.
024 * <p>
025 * If the Phoenix 5 API must be used for this device, the device must have 22.X
026 * firmware. This firmware is available in Tuner X after selecting Phoenix 5 in
027 * the firmware year dropdown.
028 */
029@Deprecated(since = "2024", forRemoval = true)
030public class TalonFXSensorCollection {
031    private long _handle;
032
033    /**
034     * Constructor for SensorCollection
035     *
036     * @param motorController Motor Controller to connect Collection to
037     */
038    public TalonFXSensorCollection(BaseTalon motorController) {
039        _handle = motorController.getHandle();
040    }
041
042    /**
043     * Get the IntegratedSensor position of the Talon FX, regardless of whether
044     * it is actually being used for feedback.  The units are 2048 per rotation.
045     * Note : Future versions of software may support scaling features (rotations, radians, degrees, etc) depending on the configuration.
046     * <p>
047     * This method relies on the Status 21 message, which has a default period of 240ms. For more
048     * information, see: https://phoenix-documentation.readthedocs.io/en/latest/ch18_CommonAPI.html
049     *
050     * @return the IntegratedSensor position.
051     */
052    public double getIntegratedSensorPosition() {
053        return MotControllerJNI.GetIntegratedSensorPosition(_handle);
054    }
055
056    /**
057     * Get the IntegratedSensor absolute position of the Talon FX, regardless of whether
058     * it is actually being used for feedback.  This will be within one rotation (2048 units).
059     * The signage and range will depend on the configuration.
060     * Note : Future versions of software may support scaling features (rotations, radians, degrees, etc) depending on the configuration.
061     * <p>
062     * This method relies on the Status 21 message, which has a default period of 240ms. For more
063     * information, see: https://phoenix-documentation.readthedocs.io/en/latest/ch18_CommonAPI.html
064     *
065     * @return the IntegratedSensor absolute position.
066     */
067    public double getIntegratedSensorAbsolutePosition() {
068        return MotControllerJNI.GetIntegratedSensorAbsolutePosition(_handle);
069    }
070
071    /**
072     * Get the IntegratedSensor velocity of the Talon FX, regardless of whether
073     * it is actually being used for feedback.
074     * One unit represents one position unit per 100ms (2048 position units per 100ms).
075     * The signage and range will depend on the configuration.
076     * Note : Future versions of software may support scaling features (rotations, radians, degrees, etc) depending on the configuration.
077     * <p>
078     * This method relies on the Status 21 message, which has a default period of 240ms. For more
079     * information, see: https://phoenix-documentation.readthedocs.io/en/latest/ch18_CommonAPI.html
080     *
081     * @return the IntegratedSensor velocity.
082     */
083    public double getIntegratedSensorVelocity() {
084        return MotControllerJNI.GetIntegratedSensorVelocity(_handle);
085    }
086
087    /**
088     * Set the IntegratedSensor reported position.  Typically this is used to "zero" the
089     * sensor. This only works with IntegratedSensor.  To set the selected sensor position
090     * regardless of what type it is, see SetSelectedSensorPosition in the motor controller class.
091     *
092     * @param newPosition The position value to apply to the sensor.
093     * @param timeoutMs   Timeout value in ms. If nonzero, function will wait for
094     *                    config success and report an error if it times out.
095     *                    If zero, no blocking or checking is performed.
096     * @return error code.
097     */
098    public ErrorCode setIntegratedSensorPosition(double newPosition,
099                                                 int timeoutMs) {
100        return ErrorCode.valueOf(MotControllerJNI.SetIntegratedSensorPosition(_handle, newPosition, timeoutMs));
101    }
102
103    /**
104     * Set the IntegratedSensor reported position based on the absolute position.
105     * This can also be done automatically on power boot depending on configuration.
106     *
107     * @param timeoutMs Timeout value in ms. If nonzero, function will wait for
108     *                  config success and report an error if it times out.
109     *                  If zero, no blocking or checking is performed.
110     * @return error code.
111     */
112    public ErrorCode setIntegratedSensorPositionToAbsolute(int timeoutMs) {
113        return ErrorCode.valueOf(MotControllerJNI.SetIntegratedSensorPositionToAbsolute(_handle, timeoutMs));
114    }
115
116    /**
117     * Is forward limit switch closed.
118     * <p>
119     * This method relies on the Status 1 message, which has a default period of 10ms. For more
120     * information, see: https://phoenix-documentation.readthedocs.io/en/latest/ch18_CommonAPI.html
121     *
122     * @return '1' iff forward limit switch is closed, 0 iff switch is open. This function works
123     * regardless if limit switch feature is enabled.  Remote limit features do not impact this routine.
124     */
125    public int isFwdLimitSwitchClosed() {
126        return MotControllerJNI.IsFwdLimitSwitchClosed(_handle);
127    }
128
129    /**
130     * Is reverse limit switch closed.
131     * <p>
132     * This method relies on the Status 1 message, which has a default period of 10ms. For more
133     * information, see: https://phoenix-documentation.readthedocs.io/en/latest/ch18_CommonAPI.html
134     *
135     * @return '1' iff reverse limit switch is closed, 0 iff switch is open. This function works
136     * regardless if limit switch feature is enabled.  Remote limit features do not impact this routine.
137     */
138    public int isRevLimitSwitchClosed() {
139        return MotControllerJNI.IsRevLimitSwitchClosed(_handle);
140    }
141}