Skip to content

Commit

Permalink
Add alternate encoder support to FalconMAX (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekma authored Dec 14, 2019
1 parent 8937888 commit 7dcec45
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.ghrobotics.lib.motors.rev

import com.revrobotics.AlternateEncoderType
import com.revrobotics.CANPIDController
import com.revrobotics.CANSparkMax
import com.revrobotics.CANSparkMaxLowLevel
Expand All @@ -25,17 +26,42 @@ import org.ghrobotics.lib.mathematics.units.nativeunit.NativeUnitModel
import org.ghrobotics.lib.motors.AbstractFalconMotor
import org.ghrobotics.lib.motors.FalconMotor

/**
* Creates a Spark MAX motor controller. The alternate encoder CPR is defaulted
* to the CPR of the REV Through Bore Encoder.
*/
class FalconMAX<K : SIKey>(
val canSparkMax: CANSparkMax,
val model: NativeUnitModel<K>
val model: NativeUnitModel<K>,
useAlternateEncoder: Boolean = false,
alternateEncoderCPR: Int = 8192
) : AbstractFalconMotor<K>() {

constructor(id: Int, type: CANSparkMaxLowLevel.MotorType, model: NativeUnitModel<K>) : this(
CANSparkMax(id, type), model
/**
* Creates a Spark MAX motor controller. The alternate encoder CPR is defaulted
* to the CPR of the REV Through Bore Encoder.
*/
constructor(
id: Int,
type: CANSparkMaxLowLevel.MotorType,
model: NativeUnitModel<K>,
useAlternateEncoder: Boolean = false,
alternateEncoderCPR: Int = 8196
) : this(
CANSparkMax(id, type), model, useAlternateEncoder, alternateEncoderCPR
)

val controller: CANPIDController = canSparkMax.pidController
override val encoder = FalconMAXEncoder(canSparkMax.encoder, model)
override val encoder = FalconMAXEncoder(
if (useAlternateEncoder) canSparkMax.getAlternateEncoder(
AlternateEncoderType.kQuadrature,
alternateEncoderCPR
) else canSparkMax.encoder, model
)

init {
controller.setFeedbackDevice(encoder.canEncoder)
}

override val voltageOutput: SIUnit<Volt>
get() = (canSparkMax.appliedOutput * canSparkMax.busVoltage).volts
Expand Down Expand Up @@ -103,12 +129,16 @@ class FalconMAX<K : SIKey>(
fun <K : SIKey> falconMAX(
canSparkMax: CANSparkMax,
model: NativeUnitModel<K>,
useAlternateEncoder: Boolean = false,
alternateEncoderCPR: Int = 8192,
block: FalconMAX<K>.() -> Unit
) = FalconMAX(canSparkMax, model).also(block)
) = FalconMAX(canSparkMax, model, useAlternateEncoder, alternateEncoderCPR).also(block)

fun <K : SIKey> falconMAX(
id: Int,
type: CANSparkMaxLowLevel.MotorType,
model: NativeUnitModel<K>,
useAlternateEncoder: Boolean = false,
alternateEncoderCPR: Int = 8192,
block: FalconMAX<K>.() -> Unit
) = FalconMAX(id, type, model).also(block)
) = FalconMAX(id, type, model, useAlternateEncoder, alternateEncoderCPR).also(block)
81 changes: 42 additions & 39 deletions vendorREV/vendordeps/REVRobotics.json
Original file line number Diff line number Diff line change
@@ -1,64 +1,67 @@
{
"fileName": "REVRobotics.json",
"name": "REVRobotics",
"version": "1.4.33",
"uuid": "3f48eb8c-50fe-43a6-9cb7-44c86353c4cb",
"mavenUrls": [
"http://www.revrobotics.com/content/sw/max/sdk/maven/"
],
"jsonUrl": "http://www.revrobotics.com/content/sw/max/sdk/REVRobotics.json",
"javaDependencies": [
{
"groupId": "com.revrobotics.frc",
"artifactId": "SparkMax-java",
"version": "1.4.33"
}
],
"jniDependencies": [
"cppDependencies": [
{
"groupId": "com.revrobotics.frc",
"artifactId": "SparkMax-driver",
"version": "1.4.33",
"skipInvalidPlatforms": true,
"isJar": false,
"validPlatforms": [
"artifactId": "SparkMax-cpp",
"binaryPlatforms": [
"windowsx86-64",
"windowsx86",
"linuxx86-64",
"linuxathena",
"linuxraspbian"
]
}
],
"cppDependencies": [
{
],
"groupId": "com.revrobotics.frc",
"artifactId": "SparkMax-cpp",
"version": "1.4.33",
"libName": "SparkMax",
"headerClassifier": "headers",
"libName": "SparkMax",
"sharedLibrary": false,
"skipInvalidPlatforms": true,
"version": "1.5.0"
},
{
"artifactId": "SparkMax-driver",
"binaryPlatforms": [
"windowsx86-64",
"windowsx86",
"linuxx86-64",
"linuxathena",
"linuxraspbian"
]
},
{
],
"groupId": "com.revrobotics.frc",
"artifactId": "SparkMax-driver",
"version": "1.4.33",
"libName": "SparkMaxDriver",
"headerClassifier": "headers",
"libName": "SparkMaxDriver",
"sharedLibrary": false,
"skipInvalidPlatforms": true,
"binaryPlatforms": [
"version": "1.5.0"
}
],
"fileName": "REVRobotics.json",
"javaDependencies": [
{
"artifactId": "SparkMax-java",
"groupId": "com.revrobotics.frc",
"version": "1.5.0"
}
],
"jniDependencies": [
{
"artifactId": "SparkMax-driver",
"groupId": "com.revrobotics.frc",
"isJar": false,
"skipInvalidPlatforms": true,
"validPlatforms": [
"windowsx86-64",
"windowsx86",
"linuxx86-64",
"linuxathena",
"linuxraspbian"
]
],
"version": "1.5.0"
}
]
],
"jsonUrl": "http://www.revrobotics.com/content/sw/max/sdk/REVRobotics.json",
"mavenUrls": [
"http://www.revrobotics.com/content/sw/max/sdk/maven/"
],
"name": "REVRobotics",
"uuid": "3f48eb8c-50fe-43a6-9cb7-44c86353c4cb",
"version": "1.5.0"
}

0 comments on commit 7dcec45

Please sign in to comment.