@@ -6,15 +6,23 @@ import com.qualcomm.robotcore.hardware.DcMotorSimple
66import com.qualcomm.robotcore.hardware.HardwareMap
77import pioneer.Constants
88import pioneer.helpers.Pose
9+ import pioneer.pathing.follower.RobotFeedforward
910import kotlin.math.abs
10- import kotlin.math.sign
1111
1212class MecanumBase (
1313 private val hardwareMap : HardwareMap ,
1414 private val motorConfig : Map <String , DcMotorSimple .Direction > = Constants .Drive .MOTOR_CONFIG ,
1515) : HardwareComponent {
1616 private lateinit var motors: Map <String , DcMotorEx >
1717
18+ private val feedforward = RobotFeedforward (
19+ Constants .Drive .kV.x, Constants .Drive .kA.x,
20+ Constants .Drive .kV.y, Constants .Drive .kA.y,
21+ Constants .Drive .kV.theta, Constants .Drive .kA.theta,
22+ Constants .Drive .kS.x, Constants .Drive .kS.y,
23+ Constants .Drive .kS.theta,
24+ )
25+
1826 override fun init () {
1927 motors =
2028 motorConfig.mapValues { (name, direction) ->
@@ -61,14 +69,9 @@ class MecanumBase(
6169 * Feedforward control for motion profiling
6270 */
6371 fun setDriveVA (pose : Pose ) {
64- val ffX = calculateFeedforward(pose.vx, pose.ax, Constants .Drive .kV.x, Constants .Drive .kA.x, Constants .Drive .kS.x)
65- val ffY = calculateFeedforward(pose.vy, pose.ay, Constants .Drive .kV.y, Constants .Drive .kA.y, Constants .Drive .kS.y)
66- val ffTheta =
67- calculateFeedforward(pose.omega, pose.alpha, Constants .Drive .kV.theta, Constants .Drive .kA.theta, Constants .Drive .kS.theta)
68-
69- // val ffX = calculateFeedforward(pose.vx, pose.ax, Constants.Drive.kV.x, Constants.Drive.kAX, Constants.Drive.kS.x)
70- // val ffY = calculateFeedforward(pose.vy, pose.ay, Constants.Drive.kV.y, Constants.Drive.kAY, Constants.Drive.kS.y)
71- // val ffTheta = calculateFeedforward(pose.omega, pose.alpha, Constants.Drive.kV.theta, Constants.Drive.kAT, Constants.Drive.kS.theta)
72+ val ffX = feedforward.calculateX(pose.vx, pose.ax)
73+ val ffY = feedforward.calculateY(pose.vy, pose.ay)
74+ val ffTheta = feedforward.calculateTheta(pose.omega, pose.alpha)
7275
7376 val motorPowers = calculateMotorPowers(Pose (vx = ffX, vy = ffY, omega = ffTheta))
7477 motors.values.forEachIndexed { index, motor ->
@@ -84,14 +87,6 @@ class MecanumBase(
8487 return listOf (leftFrontPower, leftBackPower, rightFrontPower, rightBackPower)
8588 }
8689
87- private fun calculateFeedforward (
88- v : Double ,
89- a : Double ,
90- kV : Double ,
91- kA : Double ,
92- kS : Double ,
93- ): Double = v * kV + a * kA + if (abs(v) > 1e- 3 ) kS * sign(v) else 0.0
94-
9590 fun stop () {
9691 motors.values.forEach { it.power = 0.0 }
9792 }
0 commit comments