Skip to content

Commit eba3914

Browse files
committed
WIP - before re-configure to re-use commands for FX/SRX
1 parent 1061d86 commit eba3914

File tree

8 files changed

+395
-123
lines changed

8 files changed

+395
-123
lines changed

src/main/kotlin/org/strykeforce/thirdcoast/device/TalonFXService.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.ctre.phoenix.motorcontrol.can.SlotConfiguration
66
import com.ctre.phoenix.motorcontrol.can.TalonFX
77
import com.ctre.phoenix.motorcontrol.can.TalonFXConfiguration
88
import mu.KotlinLogging
9-
import org.strykeforce.thirdcoast.talon.TalonFXItem
109
import org.strykeforce.thirdcoast.telemetry.TelemetryService
1110
import java.lang.IllegalStateException
1211
import kotlin.math.log
@@ -64,7 +63,7 @@ class TalonFXService(private val telemetryService: TelemetryService, factory: (i
6463
it.enableVoltageCompensation(voltageCompensation)
6564
it.setSensorPhase(sensorPhase)
6665
it.setInverted(OUTPUT_INVERTED_DEFAULT)
67-
telemetryService.register(TalonFXItem(it))
66+
telemetryService.register(it)
6867
}
6968
telemetryService.start()
7069
return new
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.strykeforce.thirdcoast.talon
2+
3+
import com.ctre.phoenix.motorcontrol.ControlMode
4+
import edu.wpi.first.wpilibj.Timer
5+
import mu.KotlinLogging
6+
import net.consensys.cava.toml.TomlTable
7+
import org.jline.reader.EndOfFileException
8+
import org.koin.standalone.inject
9+
import org.strykeforce.thirdcoast.command.AbstractCommand
10+
import org.strykeforce.thirdcoast.command.Command
11+
import org.strykeforce.thirdcoast.command.prompt
12+
import org.strykeforce.thirdcoast.device.TalonFXService
13+
import org.strykeforce.thirdcoast.warn
14+
import java.lang.Exception
15+
16+
private val logger = KotlinLogging.logger {}
17+
18+
class RunTalonFxCommand(
19+
parent: Command?,
20+
key: String,
21+
toml: TomlTable
22+
) : AbstractCommand(parent, key, toml) {
23+
24+
private val talonFxService: TalonFXService by inject()
25+
26+
override fun execute(): Command {
27+
var done = false
28+
29+
while (!done){
30+
try {
31+
val line = reader.readLine(prompt())
32+
if(line.isEmpty()) throw EndOfFileException()
33+
val setpoints = line.split(",")
34+
val setpoint = setpoints[0].toDouble()
35+
val duration = if(setpoints.size > 1) setpoints[1].toDouble() else 0.0
36+
val mode = talonFxService.controlMode
37+
38+
//Check inputs
39+
if(mode == ControlMode.PercentOutput && !(-1.0..1.0).contains(setpoint)){
40+
terminal.warn("setpoint must be in range -1.0 to 1.0 for percent output mode")
41+
continue
42+
}
43+
if((mode == ControlMode.MotionMagic || mode == ControlMode.Position) && duration > 0.0){
44+
terminal.warn("specifying a duration in position modes not allowed")
45+
continue
46+
}
47+
48+
//Run Talon FX's
49+
talonFxService.active.forEach{ it.set(mode, setpoint)}
50+
51+
if(duration > 0.0){
52+
logger.debug{ "run duration = $duration seconds" }
53+
Timer.delay(duration)
54+
logger.debug { "run duration expired, setting output = 0.0" }
55+
talonFxService.active.forEach{ it.set(mode, 0.0) }
56+
}
57+
} catch (e: Exception){
58+
done = true
59+
}
60+
}
61+
return super.execute()
62+
}
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.strykeforce.thirdcoast.talon
2+
3+
import com.ctre.phoenix.motorcontrol.can.TalonFX
4+
import net.consensys.cava.toml.TomlTable
5+
import org.koin.standalone.inject
6+
import org.strykeforce.thirdcoast.command.AbstractCommand
7+
import org.strykeforce.thirdcoast.command.Command
8+
import org.strykeforce.thirdcoast.command.prompt
9+
import org.strykeforce.thirdcoast.device.TalonFXService
10+
import org.strykeforce.thirdcoast.info
11+
import org.strykeforce.thirdcoast.readIntList
12+
import org.strykeforce.thirdcoast.warn
13+
import java.lang.Exception
14+
import java.lang.IllegalArgumentException
15+
16+
class SelectTalonFxCommand(parent: Command?,
17+
key: String,
18+
toml: TomlTable
19+
) : AbstractCommand(parent, key, toml) {
20+
21+
private val talonFXService: TalonFXService by inject()
22+
23+
override val menu: String
24+
get() = formatMenu(talonFXService.active.map(TalonFX::getDeviceID).joinToString())
25+
26+
override fun execute(): Command {
27+
while (true){
28+
try {
29+
val ids = reader.readIntList(this.prompt("ids"), talonFXService.active.map(TalonFX::getDeviceID))
30+
val new = talonFXService.activate(ids)
31+
if(new.isNotEmpty()){
32+
terminal.info("reset control mode, current limit enabled, brake, voltage compensation\\nand sensor phase \" +\n" +
33+
" \"for talonfxs: ${new.joinToString()}")
34+
}
35+
return super.execute()
36+
} catch (e: IllegalArgumentException){
37+
terminal.warn("Please enter a list of TalonFx ids separated by ','")
38+
}
39+
}
40+
}
41+
}

src/main/kotlin/org/strykeforce/thirdcoast/talon/TalonFXItem.kt

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/main/kotlin/org/strykeforce/thirdcoast/talon/TalonFXParameterCommand.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/kotlin/org/strykeforce/thirdcoast/talon/TalonFXParameter.kt renamed to src/main/kotlin/org/strykeforce/thirdcoast/talon/TalonFxParameter.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import org.strykeforce.thirdcoast.command.AbstractParameter
55
import org.strykeforce.thirdcoast.command.Command
66
import org.strykeforce.thirdcoast.parseResource
77

8-
class TalonFXParameter(command: Command, toml: TomlTable, val enum: Enum) : AbstractParameter(command, toml) {
8+
class TalonFxParameter(command: Command, toml: TomlTable, val enum: fxEnum) : AbstractParameter(command, toml) {
99

10-
enum class Enum{
10+
enum class fxEnum{
1111
SLOT_P,
1212
SLOT_I,
1313
SLOT_D,
@@ -36,7 +36,10 @@ class TalonFXParameter(command: Command, toml: TomlTable, val enum: Enum) : Abst
3636
STATOR_CURRENT_LIMIT,
3737
STATOR_CURRENT_LIMIT_THRES_CURRENT,
3838
STATOR_CURRENT_LIMIT_THRES_TIME,
39-
CURRENT_LIMIT_PEAK_DURATION,
39+
SUPPLY_CURRENT_LIMIT_ENABLE,
40+
SUPPLY_CURRENT_LIMIT,
41+
SUPPLY_CURRENT_LIMIT_THRES_CURRENT,
42+
SUPPLY_CURRENT_LIMIT_THRES_TIME,
4043
STATUS_GENERAL,
4144
STATUS_FEEDBACK0,
4245
STATUS_QUAD_ENCODER,
@@ -49,15 +52,16 @@ class TalonFXParameter(command: Command, toml: TomlTable, val enum: Enum) : Abst
4952
SOFT_LIMIT_THRESHOLD_FORWARD,
5053
SOFT_LIMIT_THRESHOLD_REVERSE,
5154
VELOCITY_MEASUREMENT_WINDOW,
55+
INTEGRATED_SENSOR_OFFSET_DEGREES,
5256
FACTORY_DEFAULTS,
5357
}
5458

5559
companion object {
5660
private val tomlTable by lazy { parseResource("/talonFx.toml") }
5761

58-
fun create(command: Command, param: String): TalonFXParameter {
62+
fun create(command: Command, param: String): TalonFxParameter {
5963
val toml = tomlTable.getTable(param) ?: throw java.lang.IllegalArgumentException("missing param: $param")
60-
return TalonFXParameter(command, toml, Enum.valueOf(param))
64+
return TalonFxParameter(command, toml, fxEnum.valueOf(param))
6165
}
6266
}
6367
}

0 commit comments

Comments
 (0)