1+ package org.strykeforce.thirdcoast.talon
2+
3+ import com.ctre.phoenix.motorcontrol.can.TalonFX
4+ import org.strykeforce.thirdcoast.telemetry.item.Measurable
5+ import org.strykeforce.thirdcoast.telemetry.item.Measure
6+ import java.util.function.DoubleSupplier
7+
8+ internal const val CLOSED_LOOP_TARGET = " CLOSED_LOOP_TARGET"
9+ internal const val STATOR_CURRENT = " STATOR_CURRENT"
10+ internal const val SUPPLY_CURRENT = " SUPPLY_CURRENT"
11+ internal const val OUTPUT_VOLTAGE = " OUTPUT_VOLTAGE"
12+ internal const val OUTPUT_PERCENT = " OUTPUT_PERCENT"
13+ internal const val INTEGRATED_SENSOR_POSITION = " INTEGRATED_SENSOR_POSITION"
14+ internal const val INTEGRATED_SENSOR_VELOCITY = " INTEGRATED_SENSOR_VELOCITY"
15+ internal const val INTEGRATED_SENSOR_ABSOLUTE_POSITION = " INTEGRATED_SENSOR_ABSOLUTE_POSITION"
16+ internal const val SELECTED_SENSOR_POSITION = " SELECTED_SENSOR_POSITION"
17+ internal const val SELECTED_SENSOR_VELOCITY = " SELECTED_SENSOR_VELOCITY"
18+ internal const val ACTIVE_TRAJECTORY_POSITION = " ACTIVE_TRAJECTORY_POSITION"
19+ internal const val ACTIVE_TRAJECTORY_VELOCITY = " ACTIVE_TRAJECTORY_VELOCITY"
20+ internal const val ACTIVE_TRAJECTORY_ARB_FEED_FWD = " ACTIVE_TRAJECTORY_ARB_FEED_FWD"
21+ internal const val CLOSED_LOOP_ERROR = " CLOSED_LOOP_ERROR"
22+ internal const val BUS_VOLTAGE = " BUS_VOLTAGE"
23+ internal const val ERROR_DERIVATIVE = " ERROR_DERIVATIVE"
24+ internal const val INTEGRAL_ACCUMULATOR = " INTEGRAL_ACCUMULATOR"
25+ internal const val FORWARD_LIMIT_SWITCH_CLOSED = " FORWARD_LIMIT_SWITCH_CLOSED"
26+ internal const val REVERSE_LIMIT_SWITCH_CLOSED = " REVERSE_LIMIT_SWITCH_CLOSED"
27+ internal const val TEMPERATURE = " TEMPERATURE"
28+
29+ class TalonFXItem @JvmOverloads constructor(
30+ private val talonFX : TalonFX ,
31+ override val description : String = " TalonFX ${talonFX.deviceID} "
32+ ) : Measurable {
33+
34+ override val deviceId = talonFX.deviceID
35+ override val type = " talonFX"
36+ override val measures = setOf (
37+ Measure (CLOSED_LOOP_TARGET , " Closed-loop Setpoint (PID 0)" ),
38+ Measure (STATOR_CURRENT , " Stator Current" ),
39+ Measure (SUPPLY_CURRENT , " Supply Current" ),
40+ Measure (OUTPUT_VOLTAGE , " Output Voltage" ),
41+ Measure (OUTPUT_PERCENT , " Output Percent" ),
42+ Measure (INTEGRATED_SENSOR_POSITION , " Integrated Sensor Position" ),
43+ Measure (INTEGRATED_SENSOR_ABSOLUTE_POSITION , " Integrated Sensor ABS Position" ),
44+ Measure (INTEGRATED_SENSOR_VELOCITY , " Integrated Sensor Velocity" ),
45+ Measure (SELECTED_SENSOR_POSITION , " Selected Sensor Position" ),
46+ Measure (SELECTED_SENSOR_VELOCITY , " Selected Sensor Velocity" ),
47+ Measure (ACTIVE_TRAJECTORY_POSITION , " Active Trajectory Position" ),
48+ Measure (ACTIVE_TRAJECTORY_VELOCITY , " Active Trajectory Velocity" ),
49+ Measure (ACTIVE_TRAJECTORY_ARB_FEED_FWD , " Active Trajectory Arb. Feed FWD" ),
50+ Measure (CLOSED_LOOP_ERROR , " Closed Loop Error (PID 0)" ),
51+ Measure (BUS_VOLTAGE , " Bus Voltage" ),
52+ Measure (ERROR_DERIVATIVE , " Error Derivative (PID 0)" ),
53+ Measure (INTEGRAL_ACCUMULATOR , " Integral Accumulator (PID 0)" ),
54+ Measure (FORWARD_LIMIT_SWITCH_CLOSED , " Forward Limit Switch Closed" ),
55+ Measure (REVERSE_LIMIT_SWITCH_CLOSED , " Reverse Limit Switch Closed" ),
56+ Measure (TEMPERATURE , " Controller Temperature" )
57+
58+ )
59+
60+ private val sensorCollection = requireNotNull(talonFX.sensorCollection)
61+
62+
63+ override fun measurementFor (measure : Measure ): DoubleSupplier {
64+ return when (measure.name) {
65+ CLOSED_LOOP_TARGET -> DoubleSupplier { talonFX.closedLoopTarget }
66+ STATOR_CURRENT -> DoubleSupplier { talonFX.statorCurrent }
67+ SUPPLY_CURRENT -> DoubleSupplier { talonFX.supplyCurrent }
68+ OUTPUT_VOLTAGE -> DoubleSupplier { talonFX.motorOutputVoltage }
69+ OUTPUT_PERCENT -> DoubleSupplier { talonFX.motorOutputPercent }
70+ INTEGRATED_SENSOR_VELOCITY -> DoubleSupplier { sensorCollection.integratedSensorVelocity }
71+ SELECTED_SENSOR_POSITION -> DoubleSupplier { talonFX.selectedSensorPosition.toDouble() }
72+ SELECTED_SENSOR_VELOCITY -> DoubleSupplier { talonFX.selectedSensorVelocity.toDouble() }
73+ ACTIVE_TRAJECTORY_POSITION -> DoubleSupplier { talonFX.activeTrajectoryPosition.toDouble() }
74+ ACTIVE_TRAJECTORY_VELOCITY -> DoubleSupplier { talonFX.activeTrajectoryVelocity.toDouble() }
75+ ACTIVE_TRAJECTORY_ARB_FEED_FWD -> DoubleSupplier { talonFX.activeTrajectoryArbFeedFwd }
76+ CLOSED_LOOP_ERROR -> DoubleSupplier { talonFX.closedLoopError.toDouble() }
77+ BUS_VOLTAGE -> DoubleSupplier { talonFX.busVoltage }
78+ ERROR_DERIVATIVE -> DoubleSupplier { talonFX.errorDerivative }
79+ INTEGRAL_ACCUMULATOR -> DoubleSupplier { talonFX.integralAccumulator }
80+ FORWARD_LIMIT_SWITCH_CLOSED -> DoubleSupplier { sensorCollection.isFwdLimitSwitchClosed.toDouble() }
81+ REVERSE_LIMIT_SWITCH_CLOSED -> DoubleSupplier { sensorCollection.isRevLimitSwitchClosed.toDouble() }
82+ INTEGRATED_SENSOR_POSITION -> DoubleSupplier { sensorCollection.integratedSensorPosition }
83+ INTEGRATED_SENSOR_ABSOLUTE_POSITION -> DoubleSupplier { sensorCollection.integratedSensorAbsolutePosition }
84+ TEMPERATURE -> DoubleSupplier { talonFX.temperature }
85+ else -> DoubleSupplier { 2767.0 }
86+ }
87+ }
88+
89+ override fun equals (other : Any? ): Boolean {
90+ if (this == other) return true
91+ if (javaClass != other?.javaClass) return false
92+ other as TalonFXItem
93+ if (deviceId != other.deviceId) return false
94+ return true
95+ }
96+
97+ override fun hashCode () = deviceId
98+ }
0 commit comments