This repository has been archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: pid motor controllers * feat: ftc dashboard config motor controller
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/utils/MotorController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.firstinspires.ftc.teamcode.utils | ||
|
||
import com.qualcomm.robotcore.hardware.DcMotor | ||
import com.qualcomm.robotcore.util.ElapsedTime | ||
import kotlin.math.abs | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
class MotorControllerGroup( | ||
target: Int, | ||
vararg motors: DcMotor, | ||
) { | ||
private val controllers = motors.map { MotorController(target, it) } | ||
|
||
fun update() { | ||
for (controller in controllers) { | ||
controller.update() | ||
} | ||
} | ||
|
||
fun isDone() = controllers.all { it.isDone() } | ||
} | ||
|
||
class MotorController( | ||
private val target: Int, | ||
private val motor: DcMotor, | ||
) { | ||
private val runtime = ElapsedTime() | ||
|
||
private var previousTime = 0.0 | ||
private var previousError = Int.MAX_VALUE | ||
|
||
private var i = 0.0 | ||
|
||
fun update() { | ||
val currentTime = runtime.milliseconds() | ||
val currentError = target - motor.currentPosition | ||
|
||
val p = RobotConfig.MotorController.pid.p * currentError | ||
|
||
i += RobotConfig.MotorController.pid.i * currentError * (currentTime - previousTime) | ||
|
||
// Clamp between [-MAX_I, MAX_I] | ||
i = max(min(i, RobotConfig.MotorController.iLimit), -RobotConfig.MotorController.iLimit) | ||
|
||
val d = RobotConfig.MotorController.pid.d * (currentError - previousError) / (currentTime - previousTime) | ||
|
||
motor.power = max(min(p + i + d, RobotConfig.MotorController.powerLimit), -RobotConfig.MotorController.powerLimit) | ||
|
||
previousTime = currentTime | ||
previousError = currentError | ||
} | ||
|
||
fun isDone() = abs(previousError) < 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters