11package org.strykeforce.thirdcoast.talon
22
3+ import com.ctre.phoenix.motorcontrol.FeedbackDevice
34import com.ctre.phoenix.motorcontrol.LimitSwitchNormal
45import com.ctre.phoenix.motorcontrol.LimitSwitchNormal.*
56import com.ctre.phoenix.motorcontrol.LimitSwitchSource
67import com.ctre.phoenix.motorcontrol.LimitSwitchSource.*
8+ import com.ctre.phoenix.motorcontrol.can.BaseTalon
9+ import com.ctre.phoenix.motorcontrol.can.BaseTalonConfiguration
710import mu.KotlinLogging
811import net.consensys.cava.toml.TomlTable
912import org.koin.standalone.inject
1013import org.strykeforce.thirdcoast.command.AbstractSelectCommand
1114import org.strykeforce.thirdcoast.command.Command
15+ import org.strykeforce.thirdcoast.device.TalonFxService
1216import org.strykeforce.thirdcoast.device.TalonService
1317
1418private val logger = KotlinLogging .logger {}
@@ -26,31 +30,50 @@ class SelectHardLimitSourceCommand(
2630) {
2731
2832 private val talonService: TalonService by inject()
33+ private val talonFxService: TalonFxService by inject()
34+
35+ val type = toml.getString(Command .DEVICE_KEY ) ? : throw Exception (" $key : ${Command .DEVICE_KEY } missing" )
2936 val isForward = toml.getBoolean(" forward" ) ? : true
3037
3138 val activeSource
3239 get() = values[activeIndex]
3340
3441
35- override val activeIndex
36- get() = values.indexOf(
37- if (isForward)
38- talonService.activeConfiguration.forwardLimitSwitchSource
39- else
40- talonService.activeConfiguration.reverseLimitSwitchSource
41- )
42+ override val activeIndex: Int
43+ get() {
44+ var config: BaseTalonConfiguration
45+ if (type == " srx" ) config = talonService.activeConfiguration
46+ else if (type == " fx" ) config = talonFxService.activeConfiguration
47+ else throw IllegalArgumentException ()
48+ return values.indexOf(
49+ if (isForward)
50+ config.forwardLimitSwitchSource
51+ else
52+ config.reverseLimitSwitchSource
53+ )
54+ }
4255
4356 override fun setActive (index : Int ) {
44- val active = talonService.active
57+ var active: Set <BaseTalon >
58+ var config: BaseTalonConfiguration
59+ if (type == " srx" ) {
60+ active = talonService.active
61+ config = talonService.activeConfiguration
62+ }
63+ else if (type == " fx" ) {
64+ active = talonFxService.active
65+ config = talonFxService.activeConfiguration
66+ }
67+ else throw IllegalArgumentException ()
4568 val source = values[index]
4669 val normal = getNormal()
4770 if (isForward) {
4871 active.forEach { it.configForwardLimitSwitchSource(source, normal) }
49- talonService.activeConfiguration .forwardLimitSwitchSource = source
72+ config .forwardLimitSwitchSource = source
5073 logger.info { " set forward hard limit to $source , $normal " }
5174 } else {
5275 active.forEach { it.configReverseLimitSwitchSource(source, normal) }
53- talonService.activeConfiguration .reverseLimitSwitchSource = source
76+ config .reverseLimitSwitchSource = source
5477 logger.info { " set reverse hard limit to $source , $normal " }
5578 }
5679 }
@@ -78,30 +101,46 @@ class SelectHardLimitNormalCommand(
78101) {
79102
80103 private val talonService: TalonService by inject()
104+ private val talonFxService: TalonFxService by inject()
105+ val type = toml.getString(Command .DEVICE_KEY ) ? : throw Exception (" $key : ${Command .DEVICE_KEY } missing" )
81106 val isForward = toml.getBoolean(" forward" ) ? : true
82107
108+ var activeConfig = BaseTalonConfiguration (FeedbackDevice .CTRE_MagEncoder_Relative )
109+
83110 val activeNormal
84111 get() = values[activeIndex]
85112
86- override val activeIndex
87- get() = values.indexOf(
88- if (isForward)
89- talonService.activeConfiguration.forwardLimitSwitchNormal
90- else
91- talonService.activeConfiguration.reverseLimitSwitchNormal
92- )
113+ override val activeIndex: Int
114+ get() {
115+ if (type == " srx" ) activeConfig = talonService.activeConfiguration
116+ else if (type == " fx" ) activeConfig = talonFxService.activeConfiguration
117+ else throw IllegalArgumentException ()
118+ return values.indexOf(
119+ if (isForward)
120+ activeConfig.forwardLimitSwitchNormal
121+ else
122+ activeConfig.reverseLimitSwitchNormal
123+ )
124+ }
93125
94126 override fun setActive (index : Int ) {
95- val active = talonService.active
127+ val active: Set <BaseTalon >
128+ if (type == " srx" ){
129+ activeConfig = talonService.activeConfiguration
130+ active = talonService.active
131+ } else if (type == " fx" ){
132+ activeConfig = talonFxService.activeConfiguration
133+ active = talonFxService.active
134+ } else throw IllegalArgumentException ()
96135 val source = getSource()
97136 val normal = values[index]
98137 if (isForward) {
99138 active.forEach { it.configForwardLimitSwitchSource(source, normal) }
100- talonService.activeConfiguration .forwardLimitSwitchNormal = normal
139+ activeConfig .forwardLimitSwitchNormal = normal
101140 logger.info { " set forward hard limit to $source , $normal " }
102141 } else {
103142 active.forEach { it.configReverseLimitSwitchSource(source, normal) }
104- talonService.activeConfiguration .reverseLimitSwitchNormal = normal
143+ activeConfig .reverseLimitSwitchNormal = normal
105144 logger.info { " set reverse hard limit to $source , $normal " }
106145 }
107146 }
0 commit comments