Skip to content

Commit 1c8da36

Browse files
authored
Merge branch 'NextFTC:develop' into DriveUtil
2 parents e9dcd28 + 2f7095b commit 1c8da36

15 files changed

Lines changed: 307 additions & 51 deletions

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ kotest = "5.9.1"
77
sloth = "0.2.4"
88
spotless = "8.1.0"
99
ejml = "0.41"
10-
ivy = "1.0.0"
10+
ivy = "1.0.1"
1111

1212
[libraries]
1313
dokka-mathjax-plugin = { module = "org.jetbrains.dokka:mathjax-plugin" }
@@ -23,7 +23,7 @@ ftc-hardware = { group = "org.firstinspires.ftc", name = "Hardware", version.ref
2323
ftc-common = { group = "org.firstinspires.ftc", name = "FtcCommon", version.ref = "ftc" }
2424
sloth = { module = "dev.frozenmilk.sinister:Sloth", version.ref = "sloth" }
2525
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
26-
ivy = { module = "com.pedropathing:ivy", version.ref = "ivy" }
26+
ivy = { module = "com.pedropathing.ivy:core", version.ref = "ivy" }
2727

2828
[bundles]
2929
kotest = ["kotest-runner", "kotest-assertations", "kotest-property", "kotest-data-driven"]

hardware/src/main/kotlin/dev/nextftc/hardware/actuators/NextCRServo.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ open class NextCRServo @JvmOverloads constructor(
4444
* @param port The servo port (in the range [0, 5]).
4545
* @param cacheTolerance Tolerance used by the [Caching] delegate for power updates; defaults to 0.01.
4646
*/
47-
@JvmOverloads constructor(module: NextLynxModule, port: Int, cacheTolerance: Double = 0.01) : this(
47+
@JvmOverloads constructor(
48+
module: NextLynxModule,
49+
port: Int,
50+
cacheTolerance: Double = 0.01,
51+
) : this(
4852
{ CRServoImplEx(module.servoController, port, ServoConfigurationType.getStandardServoType()) },
4953
cacheTolerance,
50-
)
54+
) {
55+
require(port in 0..3) { "Expected port in range [0, 3], got $port" }
56+
}
5157

52-
@JvmOverloads constructor(name: String, cacheTolerance: Double = 0.01) : this(
58+
@JvmOverloads constructor(
59+
name: String,
60+
cacheTolerance: Double = 0.01,
61+
) : this(
5362
{ RobotController.hardwareMap[name] as CRServoImplEx },
5463
cacheTolerance,
5564
)

hardware/src/main/kotlin/dev/nextftc/hardware/actuators/NextFeedbackCRServo.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
package dev.nextftc.hardware.actuators
1010

11-
import com.qualcomm.robotcore.hardware.AnalogInput
1211
import com.qualcomm.robotcore.hardware.CRServoImplEx
1312
import com.qualcomm.robotcore.hardware.configuration.typecontainers.ServoConfigurationType
1413
import dev.nextftc.hardware.RobotController
@@ -29,45 +28,48 @@ import dev.nextftc.units.radians
2928
*
3029
* @param initializer A function returning the backing [CRServoImplEx]. It will
3130
* be invoked lazily the first time the servo is accessed.
32-
* @param feedbackName Hardware map name of the analog input.
31+
* @param feedbackInitializer A function returning the backing [AnalogFeedback]
32+
* used to read the feedback angle. Accepts any [AnalogFeedback] implementation.
3333
* @param cacheTolerance Tolerance for the [NextCRServo] power caching delegate.
3434
*/
3535
class NextFeedbackCRServo @JvmOverloads constructor(
3636
initializer: () -> CRServoImplEx,
37-
feedbackName: String,
37+
feedbackInitializer: () -> AnalogFeedback,
3838
cacheTolerance: Double = 0.01,
3939
) : NextCRServo(initializer, cacheTolerance) {
4040

4141
/**
42-
* Constructor to create a NextFeedbackCRServo using a servo name.
42+
* Constructor to create a NextFeedbackCRServo using a servo name and an
43+
* already-constructed [AnalogFeedback] instance for the feedback input.
4344
*
4445
* @param servoName Hardware map name of the servo.
45-
* @param feedbackName Hardware map name of the analog input.
46+
* @param feedback An already-constructed [AnalogFeedback] instance to read the angle from.
4647
* @param cacheTolerance Tolerance for the [NextCRServo] power caching delegate.
4748
*/
4849
@JvmOverloads constructor(
4950
servoName: String,
50-
feedbackName: String,
51+
feedback: AnalogFeedback,
5152
cacheTolerance: Double = 0.01,
5253
) : this(
5354
{ RobotController.hardwareMap[servoName] as CRServoImplEx },
54-
feedbackName,
55+
{ feedback },
5556
cacheTolerance,
5657
)
5758

5859
/**
59-
* Constructor to create a NextFeedbackCRServo using a LynxModule and port number.
60+
* Constructor to create a NextFeedbackCRServo using a LynxModule and port number for
61+
* the servo, and an already-constructed [AnalogFeedback] instance for the feedback input.
6062
*
6163
* @param module The Lynx module, see [RobotController.controlHub], [RobotController.expansionHub],
6264
* and [RobotController.servoHubs].
6365
* @param port The servo port (in the range [0, 5]).
64-
* @param feedbackName Hardware map name of the analog input.
66+
* @param feedback An already-constructed [AnalogFeedback] instance to read the angle from.
6567
* @param cacheTolerance Tolerance for the [NextCRServo] power caching delegate.
6668
*/
6769
@JvmOverloads constructor(
6870
module: NextLynxModule,
6971
port: Int,
70-
feedbackName: String,
72+
feedback: AnalogFeedback,
7173
cacheTolerance: Double = 0.01,
7274
) : this(
7375
{
@@ -77,14 +79,12 @@ class NextFeedbackCRServo @JvmOverloads constructor(
7779
ServoConfigurationType.getStandardServoType(),
7880
)
7981
},
80-
feedbackName,
82+
{ feedback },
8183
cacheTolerance,
8284
)
8385

84-
private val analogInput by LazyHardware {
85-
RobotController.hardwareMap[feedbackName] as AnalogInput
86-
}
87-
private val rawAngleRadians: Double by AnalogFeedback { analogInput.voltage }
86+
private val analogInput by LazyHardware(feedbackInitializer)
87+
private val rawAngleRadians: Double by analogInput
8888

8989
/**
9090
* Actual angle of the servo, reported from the analog feedback input.

hardware/src/main/kotlin/dev/nextftc/hardware/actuators/NextFeedbackServo.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
package dev.nextftc.hardware.actuators
1010

11-
import com.qualcomm.robotcore.hardware.AnalogInput
1211
import com.qualcomm.robotcore.hardware.ServoImplEx
1312
import com.qualcomm.robotcore.hardware.configuration.typecontainers.ServoConfigurationType
1413
import dev.nextftc.hardware.RobotController
@@ -29,64 +28,65 @@ import dev.nextftc.units.radians
2928
* Example:
3029
*
3130
* ```
32-
* val arm = NextFeedbackServo("armServo", "armEncoder")
31+
* val arm = NextFeedbackServo("armServo", myAnalogFeedback)
3332
* arm.position = 0.5
3433
* val angle = arm.angle
3534
* ```
3635
*
3736
* @param initializer A function returning the backing [ServoImplEx]. It will be
3837
* invoked lazily the first time the servo is accessed.
39-
* @param feedbackName Hardware map name of the analog input.
38+
* @param feedbackInitializer A function returning the backing [AnalogFeedback]
39+
* used to read the feedback angle. Accepts any [AnalogFeedback] implementation.
4040
* @param cacheTolerance Tolerance for the [NextServo] position caching delegate.
4141
*/
4242
class NextFeedbackServo @JvmOverloads constructor(
4343
initializer: () -> ServoImplEx,
44-
feedbackName: String,
44+
feedbackInitializer: () -> AnalogFeedback,
4545
cacheTolerance: Double = 0.01,
4646
) : NextServo(initializer, cacheTolerance) {
4747

4848
/**
49-
* Constructor to create a NextFeedbackServo using a servo name.
49+
* Constructor to create a NextFeedbackServo using a servo name and an
50+
* already-constructed [AnalogFeedback] instance for the feedback input.
5051
*
5152
* @param servoName Hardware map name of the servo.
52-
* @param feedbackName Hardware map name of the analog input.
53+
* @param feedback An already-constructed [AnalogFeedback] instance to read the angle from.
5354
* @param cacheTolerance Tolerance for the [NextServo] position caching delegate.
5455
*/
5556
@JvmOverloads constructor(
5657
servoName: String,
57-
feedbackName: String,
58+
feedback: AnalogFeedback,
5859
cacheTolerance: Double = 0.01,
5960
) : this(
6061
{ RobotController.hardwareMap[servoName] as ServoImplEx },
61-
feedbackName,
62+
{ feedback },
6263
cacheTolerance,
6364
)
6465

6566
/**
66-
* Constructor to create a NextFeedbackServo using a LynxModule and port number.
67+
* Constructor to create a NextFeedbackServo using a LynxModule and port number for
68+
* the servo, and an already-constructed [AnalogFeedback] instance for the feedback input.
6769
*
6870
* @param module The Lynx module, see [RobotController.controlHub], [RobotController.expansionHub],
6971
* and [RobotController.servoHubs].
7072
* @param port The servo port (in the range [0, 5]).
71-
* @param feedbackName Hardware map name of the analog input.
73+
* @param feedback An already-constructed [AnalogFeedback] instance to read the angle from.
7274
* @param cacheTolerance Tolerance for the [NextServo] position caching delegate.
7375
*/
7476
@JvmOverloads constructor(
7577
module: NextLynxModule,
7678
port: Int,
77-
feedbackName: String,
79+
feedback: AnalogFeedback,
7880
cacheTolerance: Double = 0.01,
7981
) : this(
8082
{ ServoImplEx(module.servoController, port, ServoConfigurationType.getStandardServoType()) },
81-
feedbackName,
83+
{ feedback },
8284
cacheTolerance,
8385
)
8486

85-
private val analogInput by LazyHardware {
86-
RobotController.hardwareMap[feedbackName] as AnalogInput
87-
}
87+
private val analogInput by LazyHardware(feedbackInitializer)
8888

89-
private val rawAngleRadians: Double by AnalogFeedback { analogInput.voltage }
89+
private val rawAngleRadians: Double by analogInput
9090

9191
/**
9292
* Actual angle of the servo, reported from the analog feedback input.

hardware/src/main/kotlin/dev/nextftc/hardware/actuators/NextMotor.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class NextMotor @JvmOverloads constructor(
7777
{ DcMotorImplEx(module.motorController, port) },
7878
anglePerCount,
7979
cacheTolerance,
80-
)
80+
) {
81+
require(port in 0..3) { "Expected bus in range [0, 3], got $port" }
82+
}
8183

8284
/**
8385
* Constructs a motor by name from the current hardware map.
@@ -92,7 +94,11 @@ class NextMotor @JvmOverloads constructor(
9294
name: String,
9395
anglePerCount: Angle = 1.0.radians,
9496
cacheTolerance: Double = 0.01,
95-
) : this({ RobotController.hardwareMap[name] as DcMotorImplEx }, anglePerCount, cacheTolerance)
97+
) : this(
98+
{ RobotController.hardwareMap[name] as DcMotorImplEx },
99+
anglePerCount,
100+
cacheTolerance,
101+
)
96102

97103
init {
98104
motorEventLoop.bind(this::update)

hardware/src/main/kotlin/dev/nextftc/hardware/actuators/NextRGBIndicator.kt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package dev.nextftc.hardware.actuators
1010

11+
import dev.nextftc.hardware.RobotController
12+
import dev.nextftc.hardware.lynx.NextLynxModule
1113
import dev.nextftc.hardware.util.Caching
1214

1315
/**
@@ -25,12 +27,33 @@ import dev.nextftc.hardware.util.Caching
2527
* indicator.setBrightness(0.8)
2628
* ```
2729
*
28-
* @param name The name of the servo in the hardware map.
29-
* @param cacheTolerance Tolerance used by the [Caching] delegate for
30-
* position updates; defaults to 0.01.
3130
*/
32-
class NextRGBIndicator @JvmOverloads constructor(name: String, cacheTolerance: Double = 0.01) :
33-
NextServo(name, cacheTolerance) {
31+
class NextRGBIndicator : NextServo {
32+
33+
/**
34+
* @param module The Lynx Module, see [RobotController.controlHub], [RobotController.expansionHub],
35+
* and [RobotController.servoHubs]
36+
* @param port The servo port (in the range [0, 5])
37+
* @param cacheTolerance Tolerance used by the [Caching] delegate for position updates; defaults to 0.01.
38+
*/
39+
40+
@JvmOverloads
41+
constructor(
42+
module: NextLynxModule,
43+
port: Int,
44+
cacheTolerance: Double = 0.01,
45+
) : super(module, port, cacheTolerance)
46+
47+
/**
48+
* @param name The name of the servo in the hardware map.
49+
* @param cacheTolerance Tolerance used by the [Caching] delegate for
50+
* position updates; defaults to 0.01.
51+
*/
52+
@JvmOverloads
53+
constructor(
54+
name: String,
55+
cacheTolerance: Double = 0.01,
56+
) : super(name, cacheTolerance)
3457

3558
/**
3659
* Available colors/patterns for the headlights

hardware/src/main/kotlin/dev/nextftc/hardware/actuators/NextServo.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ open class NextServo @JvmOverloads constructor(
6363
{ ServoImplEx(module.servoController, port, ServoConfigurationType.getStandardServoType()) },
6464
cacheTolerance,
6565
direction,
66-
)
66+
) {
67+
require(port in 0..3) { "Expected bus in range [0, 3], got $port" }
68+
}
6769

6870
/**
6971
* Constructor to create a NextServo using configuration name

hardware/src/main/kotlin/dev/nextftc/hardware/lynx/NextLynxModule.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package dev.nextftc.hardware.lynx
22

3+
import com.qualcomm.hardware.lynx.LynxAnalogInputController
34
import com.qualcomm.hardware.lynx.LynxDcMotorController
5+
import com.qualcomm.hardware.lynx.LynxDigitalChannelController
6+
import com.qualcomm.hardware.lynx.LynxI2cDeviceSynch
7+
import com.qualcomm.hardware.lynx.LynxI2cDeviceSynchV2
48
import com.qualcomm.hardware.lynx.LynxModule
59
import com.qualcomm.hardware.lynx.LynxServoController
610
import dev.nextftc.hardware.RobotController
@@ -26,6 +30,10 @@ class NextLynxModule internal constructor(initializer: () -> LynxModule, @JvmFie
2630

2731
private val module by LazyHardware(initializer)
2832

33+
private val i2cControllers = Array(4) { bus ->
34+
LazyHardware { LynxI2cDeviceSynchV2(RobotController.appContext, module, bus) }
35+
}
36+
2937
/** Current module temperature. */
3038
val temperature: Temperature
3139
get() = module.getTemperature(TempUnit.CELSIUS).celsius
@@ -47,4 +55,20 @@ class NextLynxModule internal constructor(initializer: () -> LynxModule, @JvmFie
4755
val servoController: LynxServoController by LazyHardware {
4856
LynxServoController(RobotController.appContext, module)
4957
}
58+
59+
/** Creates or gets a [LynxI2cDeviceSynch] bound to this module. */
60+
fun i2cController(bus: Int): LynxI2cDeviceSynch {
61+
require(bus in 0..3) { "I2C bus must be in range of 0 - 3, got $bus" }
62+
return i2cControllers[bus].getValue(this, ::i2cControllers)
63+
}
64+
65+
/** Creates or gets a [LynxDigitalChannelController] bound to this module. */
66+
val digitalController: LynxDigitalChannelController by LazyHardware {
67+
LynxDigitalChannelController(RobotController.appContext, module)
68+
}
69+
70+
/** Creates or gets a [LynxAnalogInputController] bound to this module. */
71+
val analogController: LynxAnalogInputController by LazyHardware {
72+
LynxAnalogInputController(RobotController.appContext, module)
73+
}
5074
}

0 commit comments

Comments
 (0)