1+ package org.strykeforce.thirdcoast.telemetry.item
2+
3+ import com.ctre.phoenix.CANifier
4+ import com.ctre.phoenix.CANifier.PWMChannel.*
5+ import com.ctre.phoenix.motorcontrol.can.TalonSRX
6+ import org.strykeforce.thirdcoast.telemetry.grapher.Measure
7+ import org.strykeforce.thirdcoast.telemetry.grapher.Measure.*
8+ import java.util.function.DoubleSupplier
9+
10+ /* * Represents a [TalonSRX] telemetry-enable Item. */
11+ class CanifierItem @JvmOverloads constructor(
12+ private val canifier : CANifier ,
13+ override val description : String = " CANifier ${canifier.deviceID} "
14+ ) : Item {
15+
16+ override val deviceId = canifier.deviceID
17+ override val type = " canifier"
18+ override val measures
19+ get() = MEASURES
20+
21+ override fun measurementFor (measure : Measure ): DoubleSupplier {
22+
23+ return when (measure) {
24+ UNKNOWN -> DoubleSupplier { 2767.0 }
25+ PWM0_PULSE_WIDTH -> DoubleSupplier { pulseWidthFor(PWMChannel0 ) }
26+ PWM0_PERIOD -> DoubleSupplier { periodFor(PWMChannel0 ) }
27+ PWM0_PULSE_WIDTH_POSITION -> DoubleSupplier { pulseWidthPositionFor(PWMChannel0 ) }
28+ PWM1_PULSE_WIDTH -> DoubleSupplier { pulseWidthFor(PWMChannel1 ) }
29+ PWM1_PERIOD -> DoubleSupplier { periodFor(PWMChannel1 ) }
30+ PWM1_PULSE_WIDTH_POSITION -> DoubleSupplier { pulseWidthPositionFor(PWMChannel1 ) }
31+ PWM2_PULSE_WIDTH -> DoubleSupplier { pulseWidthFor(PWMChannel2 ) }
32+ PWM2_PERIOD -> DoubleSupplier { periodFor(PWMChannel2 ) }
33+ PWM2_PULSE_WIDTH_POSITION -> DoubleSupplier { pulseWidthPositionFor(PWMChannel2 ) }
34+ PWM3_PULSE_WIDTH -> DoubleSupplier { pulseWidthFor(PWMChannel3 ) }
35+ PWM3_PERIOD -> DoubleSupplier { periodFor(PWMChannel3 ) }
36+ PWM3_PULSE_WIDTH_POSITION -> DoubleSupplier { pulseWidthPositionFor(PWMChannel3 ) }
37+ QUAD_POSITION -> DoubleSupplier { canifier.quadraturePosition.toDouble() }
38+ QUAD_VELOCITY -> DoubleSupplier { canifier.quadratureVelocity.toDouble() }
39+
40+ else -> TODO (" $measure not implemented" )
41+ }
42+ }
43+
44+ private fun pulseWidthFor (channel : CANifier .PWMChannel ): Double {
45+ val pulseWidthAndPeriod = DoubleArray (2 )
46+ canifier.getPWMInput(channel, pulseWidthAndPeriod)
47+ return pulseWidthAndPeriod[0 ]
48+ }
49+
50+ private fun periodFor (channel : CANifier .PWMChannel ): Double {
51+ val pulseWidthAndPeriod = DoubleArray (2 )
52+ canifier.getPWMInput(channel, pulseWidthAndPeriod)
53+ return pulseWidthAndPeriod[1 ]
54+ }
55+
56+ private fun pulseWidthPositionFor (channel : CANifier .PWMChannel ): Double {
57+ val pulseWidthAndPeriod = DoubleArray (2 )
58+ canifier.getPWMInput(channel, pulseWidthAndPeriod)
59+ return 4096.0 * pulseWidthAndPeriod[0 ] / pulseWidthAndPeriod[1 ]
60+ }
61+
62+ override fun equals (other : Any? ): Boolean {
63+ if (this == = other) return true
64+ if (javaClass != other?.javaClass) return false
65+
66+ other as CanifierItem
67+
68+ if (deviceId != other.deviceId) return false
69+
70+ return true
71+ }
72+
73+ override fun hashCode () = deviceId
74+
75+ companion object {
76+ val MEASURES = setOf (
77+ PWM0_PULSE_WIDTH_POSITION ,
78+ QUAD_POSITION ,
79+ QUAD_VELOCITY ,
80+ PWM0_PULSE_WIDTH ,
81+ PWM0_PERIOD ,
82+ PWM1_PULSE_WIDTH ,
83+ PWM1_PERIOD ,
84+ PWM2_PULSE_WIDTH ,
85+ PWM2_PERIOD ,
86+ PWM3_PULSE_WIDTH ,
87+ PWM3_PERIOD
88+ )
89+ }
90+ }
0 commit comments