Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
implement sim pose estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
max-niederman committed Jan 18, 2024
1 parent f2b79c5 commit 3d22ff4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ object Drivetrain : Subsystem {
io.updateInputs(inputs)
Logger.processInputs("Drivetrain", inputs)

poseEstimator.update(
inputs.gyroRotation.toRotation2d(),
inputs.measuredPositions.toTypedArray()
)

Logger.recordOutput("Drivetrain/EstimatedPose", estimatedPose)
}

Expand Down Expand Up @@ -158,8 +163,8 @@ class DrivetrainIOReal : DrivetrainIO() {
}

class DrivetrainIOSim : DrivetrainIO() {
override val gyro = GyroSim()
override val modules = PerCorner.generate { SimSwerveModule() }
override val gyro = GyroSim(modules.map { it })
}

// Constants
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/frcteam3636/frc2024/subsystems/drivetrain/Gyro.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.frcteam3636.frc2024.subsystems.drivetrain

import com.frcteam3636.frc2024.Robot
import com.frcteam3636.frc2024.utils.swerve.PerCorner
import com.kauailabs.navx.frc.AHRS
import edu.wpi.first.math.geometry.Quaternion
import edu.wpi.first.math.geometry.Rotation3d
import edu.wpi.first.math.geometry.Translation2d
import kotlin.math.sign


interface Gyro {
Expand All @@ -16,7 +20,7 @@ class GyroNavX(private var offset: Rotation3d = Rotation3d()) : Gyro {
private val ahrs = AHRS()

override var rotation: Rotation3d
get() = Rotation3d(
get() = offset + Rotation3d(
Quaternion(
ahrs.quaternionW.toDouble(),
ahrs.quaternionX.toDouble(),
Expand All @@ -25,18 +29,20 @@ class GyroNavX(private var offset: Rotation3d = Rotation3d()) : Gyro {
)
)
set(value) {
val currentRotation = Rotation3d(
Quaternion(
ahrs.quaternionW.toDouble(),
ahrs.quaternionX.toDouble(),
ahrs.quaternionY.toDouble(),
ahrs.quaternionZ.toDouble()
)
)
offset = currentRotation - rotation
offset = value - rotation
}
}

class GyroSim : Gyro {
class GyroSim(private val modules: PerCorner<SwerveModule>) : Gyro {
override var rotation = Rotation3d()

override fun periodic() {
val moduleVelocities = modules.map { Translation2d(it.state.speedMetersPerSecond, it.state.angle) }
val translationVelocity = moduleVelocities.reduce(Translation2d::plus) / 4.0;
val rotationalVelocities = moduleVelocities.map { it - translationVelocity }
val yawVelocity =
sign(rotationalVelocities.frontLeft.y) * rotationalVelocities.frontLeft.norm / MODULE_POSITIONS.frontLeft.translation.norm

rotation += Rotation3d(0.0, 0.0, yawVelocity) * Robot.period
}
}

0 comments on commit 3d22ff4

Please sign in to comment.