Skip to content

Commit feca1d7

Browse files
Lynx sensor module (#16)
digital and i2c compatability --------- Co-authored-by: AchintyaAkula <achintya.akula@gmail.com>
1 parent 6697523 commit feca1d7

13 files changed

Lines changed: 445 additions & 43 deletions

File tree

.idea/caches/deviceStreaming.xml

Lines changed: 169 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.

0 commit comments

Comments
 (0)