Skip to content

Commit a345ffb

Browse files
committed
Merge branch 'feature/canifier-item'
2 parents 7657bda + 48578fc commit a345ffb

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ buildscript {
1212

1313
subprojects {
1414
group = 'org.strykeforce.thirdcoast'
15-
version = '18.7.2'
15+
version = '18.7.3'
1616

1717
apply plugin: 'java-library'
1818
apply plugin: 'idea'
@@ -23,6 +23,10 @@ subprojects {
2323
jcenter()
2424
}
2525

26+
wpi {
27+
ctreVersion = "5.7.1.0"
28+
}
29+
2630
dependencies {
2731
implementation wpilib()
2832
implementation ctre()

telemetry/src/main/kotlin/org/strykeforce/thirdcoast/telemetry/grapher/Measure.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ enum class Measure constructor(val description: String) {
3232
QUAD_IDX_PIN("Quadrature Index State"),
3333
PULSE_WIDTH_POSITION("Pulse Width Position"),
3434
PULSE_WIDTH_VELOCITY("Pulse Width Velocity"),
35-
PULSE_WIDTH_RISE_TO_FALL("Pulse Width Rise-Fall"),
36-
PULSE_WIDTH_RISE_TO_RISE("Pulse Width Rise-Rise"),
35+
PULSE_WIDTH_RISE_TO_FALL("PWM Pulse Width"),
36+
PULSE_WIDTH_RISE_TO_RISE("PWM Period"),
3737
FORWARD_LIMIT_SWITCH_CLOSED("Forward Limit Switch Closed"),
3838
REVERSE_LIMIT_SWITCH_CLOSED("Reverse Limit Switch Closed"),
3939
// FORWARD_SOFT_LIMIT("Forward Soft Limit"),
@@ -61,5 +61,19 @@ enum class Measure constructor(val description: String) {
6161
PATH_SEG_JERK("Trajectory Jerk"),
6262
PATH_SEG_HEADING("Trajectory Heading"),
6363
PATH_DISTANCE("Distance Travelled"),
64-
PATH_POSITION_ERROR("Path Position Error")
64+
PATH_POSITION_ERROR("Path Position Error"),
65+
66+
// Canifier
67+
PWM0_PULSE_WIDTH("PWM 0 Pulse Width"),
68+
PWM0_PERIOD("PWM 0 Period"),
69+
PWM0_PULSE_WIDTH_POSITION("PWM 0 Pulse Width Position"),
70+
PWM1_PULSE_WIDTH("PWM 1 Pulse Width"),
71+
PWM1_PERIOD("PWM 1 Period"),
72+
PWM1_PULSE_WIDTH_POSITION("PWM 1 Pulse Width Position"),
73+
PWM2_PULSE_WIDTH("PWM 2 Pulse Width"),
74+
PWM2_PERIOD("PWM 2 Period"),
75+
PWM2_PULSE_WIDTH_POSITION("PWM 2 Pulse Width Position"),
76+
PWM3_PULSE_WIDTH("PWM 3 Pulse Width"),
77+
PWM3_PERIOD("PWM 3 Period"),
78+
PWM3_PULSE_WIDTH_POSITION("PWM 3 Pulse Width Position"),
6579
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)