FRC.WPILib.CTRE by RobotDotNet

<PackageReference Include="FRC.WPILib.CTRE" Version="2017.0.2" />

.NET API 131,584 bytes

 CANTalon

This Class represents a CAN Talon SRX Motor Controller.
using HAL.Base; using NetworkTables; using NetworkTables.Tables; using System; using System.Runtime.CompilerServices; using WPILib; using WPILib.Interfaces; using WPILib.LiveWindow; namespace CTRE { public class CANTalon : IDisposable { public enum FeedbackDevice { QuadEncoder = 0, AnalogPotentiometer = 2, AnalogEncoder = 3, EncoderRising = 4, EncoderFalling = 5, CtreMagEncoderRelative = 6, CtreMagEncoderAbsolute = 7, PulseWidth = 8 } public enum StatusFrameRate { General, Feedback, QuadEncoder, AnalogTempVbat, PulseWidth } public enum FeedbackDeviceStatus { FeedbackStatusUnknown, FeedbackStatusPresent, FeedbackStatusNotPresent } public enum SetValueMotionProfile { Disable, Enable, Hold } public struct TrajectoryPoint { public double Position { get; set; } public double Velocity { get; set; } public int TimeDurMs { get; set; } public int ProfileSlotSelect { get; set; } public bool VelocityOnly { get; set; } public bool IsLastPoint { get; set; } public bool ZeroPos { get; set; } } public struct MotionProfileStatus { public int TopBufferRem { get; } public int TopBufferCnt { get; } public int BtmBufferCnt { get; } public bool HasUnderrun { get; } public bool IsUnderrun { get; } public bool ActivePointValid { get; } public TrajectoryPoint ActivePoint { get; } public SetValueMotionProfile OutputEnable { get; } public MotionProfileStatus(int topRem, int topCnt, int btmCnt, bool hasUnder, bool isUnder, bool activeValid, TrajectoryPoint activePoint, SetValueMotionProfile outEnable) { TopBufferRem = topRem; TopBufferCnt = topCnt; BtmBufferCnt = btmCnt; HasUnderrun = hasUnder; IsUnderrun = isUnder; ActivePointValid = activeValid; ActivePoint = activePoint; OutputEnable = outEnable; } } private readonly MotorSafetyHelper m_safetyHelper; private const int NativeAdcUnitsPerRotation = 1024; private const double NativePwdUnitsPerRotation = 4096; private const double MinutesPer100MsUnits = 0.0016666666666666668; private ControlMode m_controlMode; private readonly IntPtr m_talonPointer; private const double DelayForSolicitedSignals = 0.004; private bool m_controlEnabled; private int m_profile; private double m_setPoint; private bool m_stopped; private int m_codesPerRev; private int m_numPotTurns; private FeedbackDevice m_feedbackDevice; private const int DefaultControlPeriodMs = 10; private const int DefaultEnablePeriodMs = 50; public const int MaxTalonId = 62; protected static readonly Resource TalonIds = new Resource(62); public bool Inverted { get; set; } public int EncoderCodesPerRev { set { m_codesPerRev = value; SetParameter(HALCanTalonSRX.ParamID.eNumberEncoderCPR, (double)m_codesPerRev); } } public int PotentiometerTurns { set { m_numPotTurns = value; SetParameter(HALCanTalonSRX.ParamID.eNumberPotTurns, (double)m_numPotTurns); } } public ControlMode MotorControlMode { get { return m_controlMode; } set { if (m_controlMode != value) ApplyControlMode(value); } } public FeedbackDevice FeedBackDevice { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFeedbackDeviceSelect(m_talonPointer, ref param); CheckCTRStatus(status, "FeedBackDevice", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 812); return (FeedbackDevice)param; } set { m_feedbackDevice = value; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetFeedbackDeviceSelect(m_talonPointer, (int)value); CheckCTRStatus(status, "FeedBackDevice", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 819); } } public bool ControlEnabled { get { return m_controlEnabled; } set { if (m_controlEnabled != value) { if (m_controlEnabled && !value) { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetModeSelect(m_talonPointer, 15); CheckCTRStatus(status, "ControlEnabled", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 841); m_controlEnabled = false; } else m_controlEnabled = true; } } } public double P { get { CTR_Code status; if (m_profile == 0) { status = HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 1); CheckCTRStatus(status, "P", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 860); } else { status = HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 11); CheckCTRStatus(status, "P", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 865); } CheckCTRStatus(status, "P", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 867); Timer.Delay(0.004); double gain = 0; status = HALCanTalonSRX.ctre_TalonSRX_GetPgain(m_talonPointer, m_profile, ref gain); CheckCTRStatus(status, "P", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 873); return gain; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetPgain(m_talonPointer, m_profile, value); CheckCTRStatus(status, "P", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 879); } } public double I { get { CTR_Code status = (m_profile != 0) ? HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 12) : HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 2); CheckCTRStatus(status, "I", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 899); Timer.Delay(0.004); double gain = 0; status = HALCanTalonSRX.ctre_TalonSRX_GetIgain(m_talonPointer, m_profile, ref gain); CheckCTRStatus(status, "I", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 905); return gain; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetIgain(m_talonPointer, m_profile, value); CheckCTRStatus(status, "I", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 911); } } public double D { get { CTR_Code status = (m_profile != 0) ? HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 13) : HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 3); CheckCTRStatus(status, "D", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 929); Timer.Delay(0.004); double gain = 0; status = HALCanTalonSRX.ctre_TalonSRX_GetDgain(m_talonPointer, m_profile, ref gain); CheckCTRStatus(status, "D", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 935); return gain; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetDgain(m_talonPointer, m_profile, value); CheckCTRStatus(status, "D", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 941); } } public double F { get { CTR_Code status = (m_profile != 0) ? HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 14) : HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 4); CheckCTRStatus(status, "F", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 959); Timer.Delay(0.004); double gain = 0; status = HALCanTalonSRX.ctre_TalonSRX_GetFgain(m_talonPointer, m_profile, ref gain); CheckCTRStatus(status, "F", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 965); return gain; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetFgain(m_talonPointer, m_profile, value); CheckCTRStatus(status, "F", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 971); } } public int IZone { get { CTR_Code status = (m_profile != 0) ? HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 15) : HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 5); CheckCTRStatus(status, "IZone", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 992); Timer.Delay(0.004); int zone = 0; status = HALCanTalonSRX.ctre_TalonSRX_GetIzone(m_talonPointer, m_profile, ref zone); CheckCTRStatus(status, "IZone", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 998); return zone; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetIzone(m_talonPointer, m_profile, value); CheckCTRStatus(status, "IZone", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1004); } } public double CloseLoopRampRate { get { CTR_Code status = (m_profile != 0) ? HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 16) : HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 6); CheckCTRStatus(status, "CloseLoopRampRate", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1053); Timer.Delay(0.004); int closeLoopRampRate = 0; status = HALCanTalonSRX.ctre_TalonSRX_GetCloseLoopRampRate(m_talonPointer, m_profile, ref closeLoopRampRate); CheckCTRStatus(status, "CloseLoopRampRate", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1059); return (double)closeLoopRampRate / 1023 * 12 * 1000; } set { int closeLoopRampRate = (int)(value * 1023 / 12 / 1000); CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetCloseLoopRampRate(m_talonPointer, m_profile, closeLoopRampRate); CheckCTRStatus(status, "CloseLoopRampRate", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1066); } } public double Setpoint { get { return m_setPoint; } set { Set(value); } } public int Profile { get { return m_profile; } set { if (value != 0 && value != 1) throw new ArgumentOutOfRangeException("value", "Talon PID profile must be 0 or 1."); m_profile = value; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetProfileSlotSelect(m_talonPointer, m_profile); CheckCTRStatus(status, "Profile", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1124); } } public double VoltageCompensationRampRate { get { double value = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetParamResponse(m_talonPointer, 116, ref value); CheckCTRStatus(status, "VoltageCompensationRampRate", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1137); return value * 1000; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetVoltageCompensationRate(m_talonPointer, value / 1000); CheckCTRStatus(status, "VoltageCompensationRampRate", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1143); } } public NeutralMode NeutralMode { set { CTR_Code val; switch ((int)value) { default: val = HALCanTalonSRX.ctre_TalonSRX_SetOverrideBrakeType(m_talonPointer, 0); break; case 1: val = HALCanTalonSRX.ctre_TalonSRX_SetOverrideBrakeType(m_talonPointer, 2); break; case 2: val = HALCanTalonSRX.ctre_TalonSRX_SetOverrideBrakeType(m_talonPointer, 1); break; } if ((int)val > 0) CheckCTRStatus(val, "NeutralMode", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1171); } } public LimitMode LimitMode { set { switch ((int)value) { case 0: ForwardSoftLimitEnabled = false; ReverseSoftLimitEnabled = false; EnableLimitSwitches(true, true); break; case 1: ForwardSoftLimitEnabled = true; ReverseSoftLimitEnabled = true; EnableLimitSwitches(true, true); break; case 2: ForwardSoftLimitEnabled = false; ReverseSoftLimitEnabled = false; EnableLimitSwitches(false, false); break; } } } public double ForwardLimit { set { ForwardSoftLimit = (double)(int)value; } } public double ReverseLimit { set { ReverseSoftLimit = (double)(int)value; } } public double MaxOutputVoltage { set { ConfigPeakOutputVoltage(value, 0 - value); } } public float FaultTime { set { Utility.CheckStatus(-9, "FaultTime", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1239); } } public double VoltageRampRate { set { int param = (int)(value * 1023 / 12 / 100); CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetRampThrottle(m_talonPointer, param); CheckCTRStatus(status, "VoltageRampRate", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1256); } } public uint FirmwareVersion { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFirmVers(m_talonPointer, ref param); CheckCTRStatus(status, "FirmwareVersion", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1267); return (uint)param; } } public int DeviceId { get; } public double ForwardSoftLimit { get { int forwardLimit = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetForwardSoftLimit(m_talonPointer, ref forwardLimit); CheckCTRStatus(status, "ForwardSoftLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1286); return (double)forwardLimit; } set { int forwardLimit = ScaleRotationsToNativeUnits(m_feedbackDevice, value); CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetForwardSoftLimit(m_talonPointer, forwardLimit); CheckCTRStatus(status, "ForwardSoftLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1294); } } public bool ForwardSoftLimitEnabled { get { int enable = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetForwardSoftEnable(m_talonPointer, ref enable); CheckCTRStatus(status, "ForwardSoftLimitEnabled", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1307); return enable != 0; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetForwardSoftEnable(m_talonPointer, value ? 1 : 0); CheckCTRStatus(status, "ForwardSoftLimitEnabled", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1313); } } public double ReverseSoftLimit { get { int reverseLimit = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetReverseSoftLimit(m_talonPointer, ref reverseLimit); CheckCTRStatus(status, "ReverseSoftLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1327); return (double)reverseLimit; } set { int reverseLimit = ScaleRotationsToNativeUnits(m_feedbackDevice, value); CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetReverseSoftLimit(m_talonPointer, reverseLimit); CheckCTRStatus(status, "ReverseSoftLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1334); } } public bool ReverseSoftLimitEnabled { get { int enable = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetReverseSoftEnable(m_talonPointer, ref enable); CheckCTRStatus(status, "ReverseSoftLimitEnabled", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1347); return enable != 0; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetReverseSoftEnable(m_talonPointer, value ? 1 : 0); CheckCTRStatus(status, "ReverseSoftLimitEnabled", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1353); } } public bool ForwardLimitSwitchNormallyOpen { get { int value = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 32); CheckCTRStatus(status, "ForwardLimitSwitchNormallyOpen", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1388); Timer.Delay(0.004); status = HALCanTalonSRX.ctre_TalonSRX_GetParamResponseInt32(m_talonPointer, 32, ref value); CheckCTRStatus(status, "ForwardLimitSwitchNormallyOpen", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1392); return value == 0; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetParam(m_talonPointer, 32, (double)((!value) ? 1 : 0)); CheckCTRStatus(status, "ForwardLimitSwitchNormallyOpen", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1398); } } public bool ReverseLimitSwitchNormallyOpen { get { int value = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 33); CheckCTRStatus(status, "ReverseLimitSwitchNormallyOpen", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1411); Timer.Delay(0.004); status = HALCanTalonSRX.ctre_TalonSRX_GetParamResponseInt32(m_talonPointer, 33, ref value); CheckCTRStatus(status, "ReverseLimitSwitchNormallyOpen", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1415); return value == 0; } set { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetParam(m_talonPointer, 33, (double)((!value) ? 1 : 0)); CheckCTRStatus(status, "ReverseLimitSwitchNormallyOpen", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1421); } } public int FaultOverTemp { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFault_OverTemp(m_talonPointer, ref param); CheckCTRStatus(status, "FaultOverTemp", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1472); return param; } } public int FaultUnderVoltage { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFault_UnderVoltage(m_talonPointer, ref param); CheckCTRStatus(status, "FaultUnderVoltage", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1486); return param; } } public int FaultForwardLimit { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFault_ForLim(m_talonPointer, ref param); CheckCTRStatus(status, "FaultForwardLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1500); return param; } } public int FaultReverseLimit { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFault_RevLim(m_talonPointer, ref param); CheckCTRStatus(status, "FaultReverseLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1513); return param; } } public int FaultHardwareFailure { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFault_HardwareFailure(m_talonPointer, ref param); CheckCTRStatus(status, "FaultHardwareFailure", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1527); return param; } } public int FaultForwardSoftLimit { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFault_ForSoftLim(m_talonPointer, ref param); CheckCTRStatus(status, "FaultForwardSoftLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1541); return param; } } public int FaultReverseSoftLimit { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetFault_RevSoftLim(m_talonPointer, ref param); CheckCTRStatus(status, "FaultReverseSoftLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1554); return param; } } public int StickyFaultOverTemp { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetStckyFault_OverTemp(m_talonPointer, ref param); CheckCTRStatus(status, "StickyFaultOverTemp", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1568); return param; } } public int StickyFaultUnderVoltage { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetStckyFault_UnderVoltage(m_talonPointer, ref param); CheckCTRStatus(status, "StickyFaultUnderVoltage", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1582); return param; } } public int StickyFaultForwardLimit { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetStckyFault_ForLim(m_talonPointer, ref param); CheckCTRStatus(status, "StickyFaultForwardLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1596); return param; } } public int StickyFaultReverseLimit { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetStckyFault_RevLim(m_talonPointer, ref param); CheckCTRStatus(status, "StickyFaultReverseLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1610); return param; } } public int StickyFaultForwardSoftLimit { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetStckyFault_ForSoftLim(m_talonPointer, ref param); CheckCTRStatus(status, "StickyFaultForwardSoftLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1624); return param; } } public int StickyFaultReverseSoftLimit { get { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetStckyFault_RevSoftLim(m_talonPointer, ref param); CheckCTRStatus(status, "StickyFaultReverseSoftLimit", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1638); return param; } } public double Expiration { get { return m_safetyHelper.get_Expiration(); } set { m_safetyHelper.set_Expiration(value); } } public bool Alive => m_safetyHelper.get_Alive(); public bool SafetyEnabled { get { return m_safetyHelper.get_SafetyEnabled(); } set { m_safetyHelper.set_SafetyEnabled(value); } } public string Description => $"""{DeviceId}"; public PIDSourceType PIDSourceType { get; set; } = 0; public bool Enabled => ControlEnabled; public string SmartDashboardType => "CANSpeedController"; public ITable Table { get; set; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool CheckCTRStatus(CTR_Code status, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0) { if ((int)status > 0) DriverStation.ReportError(HAL.HAL_GetErrorMessage((int)status), true, (int)status, memberName, filePath, lineNumber); return (int)status == 0; } public CANTalon(int deviceNumber) : this(deviceNumber, 10, 50) { } public CANTalon(int deviceNumber, int controlPeriodMs, int enablePeriodMs) { if (deviceNumber < 0 || deviceNumber >= 62) throw new ArgumentOutOfRangeException("deviceNumber", "Talon IDs must be between 0 and 62 inclusive."); TalonIds.Allocate(deviceNumber, $"""{deviceNumber}"""); DeviceId = deviceNumber; m_talonPointer = HALCanTalonSRX.ctre_TalonSRX_Create3(deviceNumber, controlPeriodMs, enablePeriodMs); m_safetyHelper = new MotorSafetyHelper(this); m_controlEnabled = true; m_setPoint = 0; Profile = 0; m_codesPerRev = 0; m_numPotTurns = 0; m_feedbackDevice = FeedbackDevice.QuadEncoder; ApplyControlMode(0); LiveWindow.AddActuator("CANTalonSRX", deviceNumber, this); } public void Dispose() { TalonIds.Deallocate(DeviceId); HALCanTalonSRX.ctre_TalonSRX_Destroy(m_talonPointer); } public void SetParameter(HALCanTalonSRX.ParamID paramEnum, double value) { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetParam(m_talonPointer, (int)paramEnum, value); CheckCTRStatus(status, "SetParameter", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 301); } public void ReverseSensor(bool flip) { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetRevFeedbackSensor(m_talonPointer, flip ? 1 : 0); CheckCTRStatus(status, "ReverseSensor", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 311); } public void ReverseOutput(bool flip) { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetRevMotDuringCloseLoopEn(m_talonPointer, flip ? 1 : 0); CheckCTRStatus(status, "ReverseOutput", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 321); } public int GetEncoderPosition() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetEncPosition(m_talonPointer, ref param); CheckCTRStatus(status, "GetEncoderPosition", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 332); return param; } public void SetEncoderPostition(int newPosition) { SetParameter(HALCanTalonSRX.ParamID.eEncPosition, (double)newPosition); } public int GetEncoderVelocity() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetEncVel(m_talonPointer, ref param); CheckCTRStatus(status, "GetEncoderVelocity", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 353); return param; } public int GetPulseWidthPosition() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetPulseWidthPosition(m_talonPointer, ref param); CheckCTRStatus(status, "GetPulseWidthPosition", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 365); return param; } public void SetPulseWidthPosition(int newPosition) { SetParameter(HALCanTalonSRX.ParamID.ePwdPosition, (double)newPosition); } public int GetPulseWidthVelocity() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetPulseWidthVelocity(m_talonPointer, ref param); CheckCTRStatus(status, "GetPulseWidthVelocity", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 386); return param; } public int GetPulseWidthRiseToFallUs() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetPulseWidthRiseToFallUs(m_talonPointer, ref param); CheckCTRStatus(status, "GetPulseWidthRiseToFallUs", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 398); return param; } public int GetPulseWidthRiseToRiseUs() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetPulseWidthRiseToRiseUs(m_talonPointer, ref param); CheckCTRStatus(status, "GetPulseWidthRiseToRiseUs", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 410); return param; } public FeedbackDeviceStatus IsSensorPresent(FeedbackDevice feedbackDevice) { FeedbackDeviceStatus result = FeedbackDeviceStatus.FeedbackStatusUnknown; switch (feedbackDevice) { case FeedbackDevice.CtreMagEncoderRelative: case FeedbackDevice.CtreMagEncoderAbsolute: case FeedbackDevice.PulseWidth: { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_IsPulseWidthSensorPresent(m_talonPointer, ref param); CheckCTRStatus(status, "IsSensorPresent", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 435); result = ((param != 0) ? FeedbackDeviceStatus.FeedbackStatusPresent : FeedbackDeviceStatus.FeedbackStatusNotPresent); break; } } return result; } public int GetNumberOfQuadIdxRises() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetEncIndexRiseEvents(m_talonPointer, ref param); CheckCTRStatus(status, "GetNumberOfQuadIdxRises", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 457); return param; } public int GetPinStateQuadA() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetQuadApin(m_talonPointer, ref param); CheckCTRStatus(status, "GetPinStateQuadA", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 469); return param; } public int GetPinStateQuadB() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetQuadBpin(m_talonPointer, ref param); CheckCTRStatus(status, "GetPinStateQuadB", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 481); return param; } public int GetPinStateQuadIdx() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetQuadIdxpin(m_talonPointer, ref param); CheckCTRStatus(status, "GetPinStateQuadIdx", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 493); return param; } public void SetAnalogPosition(int newPosition) { SetParameter(HALCanTalonSRX.ParamID.eAinPosition, (double)newPosition); } public int GetAnalogInPosition() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetAnalogInWithOv(m_talonPointer, ref param); CheckCTRStatus(status, "GetAnalogInPosition", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 515); return param; } public int GetAnalogInRaw() { return GetAnalogInPosition() & 1023; } public int GetAnalogInVelocity() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetAnalogInVel(m_talonPointer, ref param); CheckCTRStatus(status, "GetAnalogInVelocity", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 536); return param; } public int GetClosedLoopError() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetCloseLoopErr(m_talonPointer, ref param); CheckCTRStatus(status, "GetClosedLoopError", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 548); return param; } public void SetAllowableClosedLoopErr(int allowableCloseLoopError) { if (m_profile == 0) SetParameter(HALCanTalonSRX.ParamID.eProfileParamSlot0_AllowableClosedLoopErr, (double)allowableCloseLoopError); else SetParameter(HALCanTalonSRX.ParamID.eProfileParamSlot1_AllowableClosedLoopErr, (double)allowableCloseLoopError); } public bool IsForwardLimitSwitchClosed() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetLimitSwitchClosedFor(m_talonPointer, ref param); CheckCTRStatus(status, "IsForwardLimitSwitchClosed", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 576); return param != 0; } public bool IsReverseLimitSwitchClosed() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetLimitSwitchClosedRev(m_talonPointer, ref param); CheckCTRStatus(status, "IsReverseLimitSwitchClosed", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 588); return param != 0; } public bool IsBrakeEnabledDuringNeutral() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetBrakeIsEnabled(m_talonPointer, ref param); CheckCTRStatus(status, "IsBrakeEnabledDuringNeutral", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 600); return param != 0; } public double GetTemperature() { double param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetTemp(m_talonPointer, ref param); CheckCTRStatus(status, "GetTemperature", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 629); return param; } public double GetOutputCurrent() { double param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetCurrent(m_talonPointer, ref param); CheckCTRStatus(status, "GetOutputCurrent", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 638); return param; } public double GetOutputVoltage() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetAppliedThrottle(m_talonPointer, ref param); CheckCTRStatus(status, "GetOutputVoltage", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 647); return GetBusVoltage() * ((double)param / 1023); } public double GetBusVoltage() { double param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetBatteryV(m_talonPointer, ref param); CheckCTRStatus(status, "GetBusVoltage", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 656); return param; } public double GetPosition() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetSensorPosition(m_talonPointer, ref param); CheckCTRStatus(status, "GetPosition", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 665); return ScaleNativeUnitsToRotations(m_feedbackDevice, param); } public void SetPosition(double pos) { int pos2 = ScaleRotationsToNativeUnits(m_feedbackDevice, pos); CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetSensorPosition(m_talonPointer, pos2); CheckCTRStatus(status, "SetPosition", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 677); } public double GetSpeed() { int param = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetSensorVelocity(m_talonPointer, ref param); CheckCTRStatus(status, "GetSpeed", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 685); return ScaleNativeUnitsToRpm(m_feedbackDevice, param); } public bool GetForwardLimitOk() { int faultForwardLimit = FaultForwardLimit; return FaultForwardSoftLimit == 0 && faultForwardLimit == 0; } public bool GetReverseLimitOk() { int faultReverseLimit = FaultReverseLimit; return FaultReverseSoftLimit == 0 && faultReverseLimit == 0; } public Faults GetFaults() { Faults val = 0; int param = 0; CTR_Code val2 = HALCanTalonSRX.ctre_TalonSRX_GetFault_OverTemp(m_talonPointer, ref param); if ((int)val2 > 0) CheckCTRStatus(val2, "GetFaults", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 716); val |= ((param != 0) ? 2 : 0); param = 0; val2 = HALCanTalonSRX.ctre_TalonSRX_GetFault_UnderVoltage(m_talonPointer, ref param); if ((int)val2 > 0) CheckCTRStatus(val2, "GetFaults", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 727); val |= ((param != 0) ? 4 : 0); param = 0; val2 = HALCanTalonSRX.ctre_TalonSRX_GetFault_ForLim(m_talonPointer, ref param); if ((int)val2 > 0) CheckCTRStatus(val2, "GetFaults", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 738); val |= ((param != 0) ? 16 : 0); param = 0; val2 = HALCanTalonSRX.ctre_TalonSRX_GetFault_RevLim(m_talonPointer, ref param); if ((int)val2 > 0) CheckCTRStatus(val2, "GetFaults", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 749); val |= ((param != 0) ? 32 : 0); param = 0; val2 = HALCanTalonSRX.ctre_TalonSRX_GetFault_ForSoftLim(m_talonPointer, ref param); if ((int)val2 > 0) CheckCTRStatus(val2, "GetFaults", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 760); val |= ((param != 0) ? 64 : 0); param = 0; val2 = HALCanTalonSRX.ctre_TalonSRX_GetFault_RevSoftLim(m_talonPointer, ref param); if ((int)val2 > 0) CheckCTRStatus(val2, "GetFaults", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 771); return val | ((param != 0) ? 128 : 0); } private void ApplyControlMode(ControlMode value) { m_controlMode = value; if ((int)value == 15) m_controlEnabled = false; HALCanTalonSRX.ctre_TalonSRX_SetModeSelect(m_talonPointer, 15); HAL.Report(52, (int)(byte)(DeviceId + 1), (int)(byte)m_controlMode, (string)null); } public void Enable() { ControlEnabled = true; } public double GetIaccum() { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_RequestParam(m_talonPointer, 93); CheckCTRStatus(status, "GetIaccum", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1015); Timer.Delay(0.004); int value = 0; status = HALCanTalonSRX.ctre_TalonSRX_GetParamResponseInt32(m_talonPointer, 93, ref value); CheckCTRStatus(status, "GetIaccum", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1020); return (double)value; } public void ClearIAccum() { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_SetParam(m_talonPointer, 93, 0); CheckCTRStatus(status, "ClearIAccum", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1030); } public void SetPID(double p, double i, double d, double f, int izone, double closeLoopRampRate, int profile) { if (profile != 0 && profile != 1) throw new ArgumentOutOfRangeException("profile", "Talon PID profile must be 0 or 1."); Profile = profile; P = p; I = i; D = d; F = f; IZone = izone; CloseLoopRampRate = closeLoopRampRate; } public void SetPID(double p, double i, double d) { SetPID(p, i, d, 0, 0, 0, m_profile); } public void ConfigSoftPositionLimits(double forwardLimitPosition, double reverseLimitPosition) { LimitMode = 1; ForwardLimit = forwardLimitPosition; ReverseLimit = reverseLimitPosition; } public void DisableSoftPositionLimits() { LimitMode = 0; } public void ClearStickyFaults() { CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_ClearStickyFaults(m_talonPointer); CheckCTRStatus(status, "ClearStickyFaults", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1363); } public void EnableLimitSwitches(bool forward, bool reverse) { int param = 4 | ((forward ? 1 : 0) << 1) | (reverse ? 1 : 0); CTR_Code val = HALCanTalonSRX.ctre_TalonSRX_SetOverrideLimitSwitchEn(m_talonPointer, param); if ((int)val > 0) CheckCTRStatus(val, "EnableLimitSwitches", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1376); } public void ConfigPeakOutputVoltage(double forwardVoltage, double reverseVoltage) { if (forwardVoltage > 12) forwardVoltage = 12; else if (forwardVoltage < 0) { forwardVoltage = 0; } if (reverseVoltage > 0) reverseVoltage = 0; else if (reverseVoltage < -12) { reverseVoltage = -12; } SetParameter(HALCanTalonSRX.ParamID.ePeakPosOutput, 1023 * forwardVoltage / 12); SetParameter(HALCanTalonSRX.ParamID.ePeakNegOutput, 1023 * reverseVoltage / 12); } public void ConfigNominalOutputVoltage(double forwardVoltage, double reverseVoltage) { if (forwardVoltage > 12) forwardVoltage = 12; else if (forwardVoltage < 0) { forwardVoltage = 0; } if (reverseVoltage > 0) reverseVoltage = 0; else if (reverseVoltage < -12) { reverseVoltage = -12; } SetParameter(HALCanTalonSRX.ParamID.eNominalPosOutput, 1023 * forwardVoltage / 12); SetParameter(HALCanTalonSRX.ParamID.eNominalNegOutput, 1023 * reverseVoltage / 12); } [Obsolete("Set the ControlEnabled to false.")] public void StopMotor() { ControlEnabled = false; m_stopped = true; } public void PidWrite(double value) { if ((int)m_controlMode != 0) throw new InvalidOperationException("PID on RoboRIO only supported in Voltage Bus (PWM-like) mode"); Set(value); } public virtual double PidGet() { return GetPosition(); } public virtual void Set(double value) { m_safetyHelper.Feed(); if (m_stopped) { ControlEnabled = true; m_stopped = false; } if (m_controlEnabled) { m_setPoint = value; CTR_Code val = 0; ControlMode controlMode = m_controlMode; switch ((int)controlMode) { case 0: HALCanTalonSRX.ctre_TalonSRX_Set(m_talonPointer, Inverted ? (0 - value) : value); val = 0; break; case 4: { int num2 = (int)(value * 256); val = HALCanTalonSRX.ctre_TalonSRX_SetDemand(m_talonPointer, Inverted ? (-num2) : num2); break; } case 1: val = HALCanTalonSRX.ctre_TalonSRX_SetDemand(m_talonPointer, ScaleRotationsToNativeUnits(m_feedbackDevice, value)); break; case 2: val = HALCanTalonSRX.ctre_TalonSRX_SetDemand(m_talonPointer, ScaleVelocityToNativeUnits(m_feedbackDevice, Inverted ? (0 - value) : value)); break; case 5: val = HALCanTalonSRX.ctre_TalonSRX_SetDemand(m_talonPointer, (int)value); break; case 3: { double num = (Inverted ? (0 - value) : value) * 1000; val = HALCanTalonSRX.ctre_TalonSRX_SetDemand(m_talonPointer, (int)num); break; } default: val = 0; break; } CheckCTRStatus(val, "Set", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1734); val = HALCanTalonSRX.ctre_TalonSRX_SetModeSelect(m_talonPointer, (int)MotorControlMode); CheckCTRStatus(val, "Set", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1736); } } public virtual double Get() { double num = 0; int param = 0; ControlMode controlMode = m_controlMode; switch ((int)controlMode) { case 4: return GetOutputVoltage(); case 3: return GetOutputCurrent(); case 2: HALCanTalonSRX.ctre_TalonSRX_GetSensorVelocity(m_talonPointer, ref param); return ScaleNativeUnitsToRpm(m_feedbackDevice, param); case 1: HALCanTalonSRX.ctre_TalonSRX_GetSensorPosition(m_talonPointer, ref param); return ScaleNativeUnitsToRotations(m_feedbackDevice, param); default: HALCanTalonSRX.ctre_TalonSRX_GetAppliedThrottle(m_talonPointer, ref param); return (double)param / 1023; } } [Obsolete("This is only here to make CAN Jaguars happy")] public void Set(double value, byte syncGroup) { Set(value); } internal double GetNativeUnitsPerRotationScalar(FeedbackDevice devToLookup) { double num = 0; bool flag = false; switch (devToLookup) { case FeedbackDevice.QuadEncoder: { int num2 = 4; switch (m_feedbackDevice) { case FeedbackDevice.CtreMagEncoderRelative: case FeedbackDevice.CtreMagEncoderAbsolute: num = 4096; flag = true; break; case FeedbackDevice.EncoderRising: case FeedbackDevice.EncoderFalling: num2 = 1; break; } if (!flag && m_codesPerRev != 0) { num = (double)(4 * m_codesPerRev); flag = true; } break; } case FeedbackDevice.AnalogPotentiometer: case FeedbackDevice.AnalogEncoder: if (m_numPotTurns != 0) { num = 1024 / (double)m_numPotTurns; flag = true; } break; case FeedbackDevice.EncoderRising: case FeedbackDevice.EncoderFalling: if (m_codesPerRev != 0) { num = (double)m_codesPerRev; flag = true; } break; case FeedbackDevice.CtreMagEncoderRelative: case FeedbackDevice.CtreMagEncoderAbsolute: case FeedbackDevice.PulseWidth: num = 4096; flag = true; break; } return (!flag) ? 0 : num; } internal int ScaleRotationsToNativeUnits(FeedbackDevice devToLookup, double fullRotations) { int result = (int)fullRotations; double nativeUnitsPerRotationScalar = GetNativeUnitsPerRotationScalar(devToLookup); if (nativeUnitsPerRotationScalar > 0) result = (int)(fullRotations * nativeUnitsPerRotationScalar); return result; } internal int ScaleVelocityToNativeUnits(FeedbackDevice devToLookup, double rpm) { int result = (int)rpm; double nativeUnitsPerRotationScalar = GetNativeUnitsPerRotationScalar(devToLookup); if (nativeUnitsPerRotationScalar > 0) result = (int)(rpm * 0.0016666666666666668 * nativeUnitsPerRotationScalar); return result; } internal double ScaleNativeUnitsToRotations(FeedbackDevice devToLookup, int nativePos) { double result = (double)nativePos; double nativeUnitsPerRotationScalar = GetNativeUnitsPerRotationScalar(devToLookup); if (nativeUnitsPerRotationScalar > 0) result = (double)nativePos / nativeUnitsPerRotationScalar; return result; } internal double ScaleNativeUnitsToRpm(FeedbackDevice devToLookup, long nativeVel) { double result = (double)nativeVel; double nativeUnitsPerRotationScalar = GetNativeUnitsPerRotationScalar(devToLookup); if (nativeUnitsPerRotationScalar > 0) result = (double)nativeVel / (nativeUnitsPerRotationScalar * 0.0016666666666666668); return result; } public void EnableZeroSensorPositionOnIndex(bool enable, bool risingEdge) { if (enable) { SetParameter(HALCanTalonSRX.ParamID.eQuadIdxPolarity, (double)(risingEdge ? 1 : 0)); SetParameter(HALCanTalonSRX.ParamID.eClearPositionOnIdx, 1); } else { SetParameter(HALCanTalonSRX.ParamID.eClearPositionOnIdx, 0); SetParameter(HALCanTalonSRX.ParamID.eQuadIdxPolarity, (double)(risingEdge ? 1 : 0)); } } public void ChangeMotionControlFramePeriod(int periodMs) { HALCanTalonSRX.ctre_TalonSRX_ChangeMotionControlFramePeriod(m_talonPointer, periodMs); } public void ClearMotionProfileTrajectories() { HALCanTalonSRX.ctre_TalonSRX_ClearMotionProfileTrajectories(m_talonPointer); } public int GetMotionProfileTopLevelBufferCount() { return HALCanTalonSRX.ctre_TalonSRX_GetMotionProfileTopLevelBufferCount(m_talonPointer); } public bool PushMotionProfileTrajectory(TrajectoryPoint trajPt) { if (!IsMotionProfileTopLevelBufferFull()) { int targPos = ScaleRotationsToNativeUnits(m_feedbackDevice, trajPt.Position); int targVel = ScaleRotationsToNativeUnits(m_feedbackDevice, trajPt.Velocity); int profileSlotSelect = (trajPt.ProfileSlotSelect > 0) ? 1 : 0; int num = trajPt.TimeDurMs; if (num > 255) num = 255; if (num < 0) num = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_PushMotionProfileTrajectory(m_talonPointer, targPos, targVel, profileSlotSelect, num, trajPt.VelocityOnly ? 1 : 0, trajPt.IsLastPoint ? 1 : 0, trajPt.ZeroPos ? 1 : 0); CheckCTRStatus(status, "PushMotionProfileTrajectory", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1962); return true; } return false; } public bool IsMotionProfileTopLevelBufferFull() { return HALCanTalonSRX.ctre_TalonSRX_IsMotionProfileTopLevelBufferFull(m_talonPointer) != 0; } public void ProcessMotionProfileBuffer() { HALCanTalonSRX.ctre_TalonSRX_ProcessMotionProfileBuffer(m_talonPointer); } public MotionProfileStatus GetMotionProfileStatus() { int flags = 0; int profileSlotSelect = 0; int targPos = 0; int targVel = 0; int topBufferRemaining = 0; int topBufferCnt = 0; int btmBufferCnt = 0; int outputEnable = 0; CTR_Code status = HALCanTalonSRX.ctre_TalonSRX_GetMotionProfileStatus(m_talonPointer, ref flags, ref profileSlotSelect, ref targPos, ref targVel, ref topBufferRemaining, ref topBufferCnt, ref btmBufferCnt, ref outputEnable); CheckCTRStatus(status, "GetMotionProfileStatus", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 1999); bool hasUnder = ((flags & 2) > 0) ? true : false; bool isUnder = ((flags & 4) > 0) ? true : false; bool activeValid = ((flags & 1) > 0) ? true : false; bool isLastPoint = ((flags & 8) > 0) ? true : false; bool velocityOnly = ((flags & 16) > 0) ? true : false; double position = ScaleNativeUnitsToRotations(m_feedbackDevice, targPos); double velocity = ScaleNativeUnitsToRpm(m_feedbackDevice, targVel); SetValueMotionProfile outEnable = (SetValueMotionProfile)outputEnable; bool zeroPos = false; int timeDurMs = 0; TrajectoryPoint trajectoryPoint = default(TrajectoryPoint); trajectoryPoint.IsLastPoint = isLastPoint; trajectoryPoint.VelocityOnly = velocityOnly; trajectoryPoint.Position = position; trajectoryPoint.Velocity = velocity; trajectoryPoint.ProfileSlotSelect = profileSlotSelect; trajectoryPoint.ZeroPos = zeroPos; trajectoryPoint.TimeDurMs = timeDurMs; TrajectoryPoint activePoint = trajectoryPoint; return new MotionProfileStatus(topBufferRemaining, topBufferCnt, btmBufferCnt, hasUnder, isUnder, activeValid, activePoint, outEnable); } public void ClearMotionProfileHasUnderrun() { SetParameter(HALCanTalonSRX.ParamID.eMotionProfileHasUnderrunErr, 0); } public void Reset() { Disable(); ClearIAccum(); } public double GetError() { return (double)GetClosedLoopError(); } public void SelectProfileSlot(int slotIdx) { m_profile = ((slotIdx != 0) ? 1 : 0); CTR_Code val = HALCanTalonSRX.ctre_TalonSRX_SetProfileSlotSelect(m_talonPointer, m_profile); if ((int)val > 0) CheckCTRStatus(val, "SelectProfileSlot", "C:\\projects\\wpilib-ctre\\src\\FRC.WPILib.CTRE\\CANTalon.cs", 2059); } public void Disable() { ControlEnabled = false; } public void UpdateTable() { if ((object)Table != null) { Table.PutString("~TYPE~", SmartDashboardType); Table.PutString("Type", "CANTalon"); Table.PutNumber("Mode", (double)MotorControlMode); if (ControlModeExtensions.IsPID(MotorControlMode)) { Table.PutNumber("p", P); Table.PutNumber("i", I); Table.PutNumber("d", D); Table.PutNumber("f", F); } Table.PutBoolean("Enabled", Enabled); Table.PutNumber("Value", Get()); } } public void StartLiveWindowMode() { Set(0); Table.AddTableListener(this, true); } public void StopLiveWindowMode() { Set(0); Table.RemoveTableListener(this); } public void ValueChanged(ITable source, string key, Value value, NotifyFlags flags) { switch (key) { case "Enabled": if (value.GetBoolean()) Enable(); else Disable(); break; case "Value": Set(value.GetDouble()); break; case "Mode": MotorControlMode = (int)value.GetDouble(); break; } if (ControlModeExtensions.IsPID(MotorControlMode)) { switch (key) { default: if (key == "f") F = value.GetDouble(); break; case "p": P = value.GetDouble(); break; case "i": I = value.GetDouble(); break; case "d": D = value.GetDouble(); break; } } } public void InitTable(ITable subtable) { Table = subtable; UpdateTable(); } } }