Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shom770 committed Jan 16, 2024
1 parent e6dd2ad commit eca1639
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/main/kotlin/com/team4099/robot2023/BuildConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package com.team4099.robot2023
const val MAVEN_GROUP = ""
const val MAVEN_NAME = "Crescendo-2024"
const val VERSION = "unspecified"
const val GIT_REVISION = 26
const val GIT_SHA = "73f4f3ef5bab17de6b7bbc919ece786ceb7b52ea"
const val GIT_DATE = "2024-01-15T19:49:07Z"
const val GIT_REVISION = 27
const val GIT_SHA = "e6dd2ad00da75da1686f2dc6e2688aab809838a9"
const val GIT_DATE = "2024-01-15T20:47:37Z"
const val GIT_BRANCH = "intake"
const val BUILD_DATE = "2024-01-15T20:41:51Z"
const val BUILD_UNIX_TIME = 1705369311220L
const val BUILD_DATE = "2024-01-16T14:47:03Z"
const val BUILD_UNIX_TIME = 1705434423104L
const val DIRTY = 1
3 changes: 2 additions & 1 deletion src/main/kotlin/com/team4099/robot2023/RobotContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.team4099.robot2023.subsystems.intake.IntakeIONEO
import com.team4099.robot2023.subsystems.intake.IntakeIOSim
import com.team4099.robot2023.subsystems.limelight.LimelightVision
import com.team4099.robot2023.subsystems.limelight.LimelightVisionIO
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import com.team4099.robot2023.subsystems.vision.Vision
import com.team4099.robot2023.subsystems.vision.camera.CameraIONorthstar
import com.team4099.robot2023.util.driver.Ryan
Expand Down Expand Up @@ -90,7 +91,7 @@ object RobotContainer {
}

fun zeroSensors() {
drivetrain.zeroSensors()
drivetrain.currentRequest = DrivetrainRequest.ZeroSensors()
}

fun zeroAngle(toAngle: Angle) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.team4099.robot2023.commands.drivetrain

import com.team4099.robot2023.subsystems.drivetrain.drive.Drivetrain
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import edu.wpi.first.wpilibj2.command.Command
import org.team4099.lib.units.base.meters
import org.team4099.lib.units.derived.radians
Expand All @@ -12,7 +13,7 @@ class DriveBrakeModeCommand(val drivetrain: Drivetrain) : Command() {
}

override fun execute() {
drivetrain.setOpenLoop(
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(
0.0.radians.perSecond, Pair(0.0.meters.perSecond, 0.0.meters.perSecond)
)
drivetrain.swerveModules.forEach() { it.setDriveBrakeMode(true) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.team4099.lib.trajectory.CustomTrajectoryGenerator
import com.team4099.lib.trajectory.Waypoint
import com.team4099.robot2023.config.constants.DrivetrainConstants
import com.team4099.robot2023.subsystems.drivetrain.drive.Drivetrain
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import com.team4099.robot2023.util.AllianceFlipUtil
import com.team4099.robot2023.util.Velocity2d
import edu.wpi.first.math.kinematics.ChassisSpeeds
Expand Down Expand Up @@ -215,7 +216,7 @@ class DrivePathCommand(
Pose2dWPILIB(desiredState.poseMeters.translation, desiredRotation.position)
)

drivetrain.setClosedLoop(
drivetrain.currentRequest = DrivetrainRequest.ClosedLoop(
nextDriveState,
ChassisAccels(xAccel, yAccel, 0.0.radians.perSecond.perSecond).chassisAccelsWPILIB
)
Expand Down Expand Up @@ -280,16 +281,16 @@ class DrivePathCommand(
Logger.recordOutput("ActiveCommands/DrivePathCommand", false)
if (interrupted) {
// Stop where we are if interrupted
drivetrain.setOpenLoop(
0.degrees.perSecond, Pair(0.meters.perSecond, 0.meters.perSecond)
)
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(
0.0.radians.perSecond, Pair(0.0.meters.perSecond, 0.0.meters.perSecond)
)
} else {
// Execute one last time to end up in the final state of the trajectory
// Since we weren't interrupted, we know curTime > endTime
execute()
drivetrain.setOpenLoop(
0.degrees.perSecond, Pair(0.meters.perSecond, 0.meters.perSecond)
)
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(
0.0.radians.perSecond, Pair(0.0.meters.perSecond, 0.0.meters.perSecond)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.team4099.robot2023.commands.drivetrain
import com.team4099.lib.logging.LoggedTunableValue
import com.team4099.robot2023.config.constants.DrivetrainConstants
import com.team4099.robot2023.subsystems.drivetrain.drive.Drivetrain
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import edu.wpi.first.wpilibj.RobotBase
import edu.wpi.first.wpilibj2.command.Command
import org.littletonrobotics.junction.Logger
Expand Down Expand Up @@ -81,7 +82,7 @@ class GoToAngle(val drivetrain: Drivetrain) : Command() {

val thetaFeedback = thetaPID.calculate(drivetrain.odometryPose.rotation, desiredAngle)

drivetrain.setOpenLoop(
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(
thetaFeedback, Pair(0.0.meters.perSecond, 0.0.meters.perSecond), fieldOriented = true
)

Expand All @@ -97,8 +98,8 @@ class GoToAngle(val drivetrain: Drivetrain) : Command() {
}

override fun end(interrupted: Boolean) {
drivetrain.setOpenLoop(
0.0.radians.perSecond, Pair(0.0.meters.perSecond, 0.0.meters.perSecond)
)
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(
0.0.radians.perSecond, Pair(0.0.meters.perSecond, 0.0.meters.perSecond)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.team4099.lib.hal.Clock
import com.team4099.lib.logging.LoggedTunableValue
import com.team4099.robot2023.config.constants.DrivetrainConstants
import com.team4099.robot2023.subsystems.drivetrain.drive.Drivetrain
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import edu.wpi.first.wpilibj2.command.Command
import org.littletonrobotics.junction.Logger
import org.team4099.lib.controller.ProfiledPIDController
Expand Down Expand Up @@ -91,7 +92,7 @@ class GyroAutoLevel(val drivetrain: Drivetrain) : Command() {
override fun execute() {
Logger.recordOutput("ActiveCommands/AutoLevelCommand", true)

drivetrain.setOpenLoop(
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(
0.0.radians.perSecond, gyroFeedback, fieldOriented = true
)

Expand Down Expand Up @@ -119,8 +120,8 @@ class GyroAutoLevel(val drivetrain: Drivetrain) : Command() {
}

override fun end(interrupted: Boolean) {
drivetrain.setOpenLoop(
0.0.radians.perSecond, Pair(0.0.meters.perSecond, 0.0.meters.perSecond)
)
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(
0.0.radians.perSecond, Pair(0.0.meters.perSecond, 0.0.meters.perSecond)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.team4099.robot2023.commands.drivetrain

import com.team4099.robot2023.subsystems.drivetrain.drive.Drivetrain
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import edu.wpi.first.wpilibj2.command.Command
import org.team4099.lib.units.base.feet
import org.team4099.lib.units.derived.degrees
Expand All @@ -12,7 +13,7 @@ class OpenLoopReverseCommand(val drivetrain: Drivetrain) : Command() {
}

override fun execute() {
drivetrain.setOpenLoop(
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(
0.degrees.perSecond,
Pair(-5.0.feet.perSecond, 0.0.feet.perSecond),
fieldOriented = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.team4099.robot2023.commands.drivetrain

import com.team4099.lib.logging.LoggedTunableValue
import com.team4099.robot2023.config.constants.DrivetrainConstants
import com.team4099.robot2023.subsystems.drivetrain.drive.Drivetrain
import com.team4099.robot2023.subsystems.superstructure.Request
import edu.wpi.first.wpilibj.RobotBase
import edu.wpi.first.wpilibj2.command.Command
import org.littletonrobotics.junction.Logger
import org.team4099.lib.controller.ProfiledPIDController
import org.team4099.lib.controller.TrapezoidProfile
import org.team4099.lib.geometry.Pose2d
import org.team4099.lib.units.Velocity
import org.team4099.lib.units.base.inMeters
import org.team4099.lib.units.derived.Angle
import org.team4099.lib.units.derived.Radian
import org.team4099.lib.units.derived.degrees
import org.team4099.lib.units.derived.inDegrees
import org.team4099.lib.units.derived.inDegreesPerSecondPerDegree
import org.team4099.lib.units.derived.inDegreesPerSecondPerDegreePerSecond
import org.team4099.lib.units.derived.inDegreesPerSecondPerDegreeSeconds
import org.team4099.lib.units.derived.perDegree
import org.team4099.lib.units.derived.perDegreePerSecond
import org.team4099.lib.units.derived.perDegreeSeconds
import org.team4099.lib.units.derived.radians
import org.team4099.lib.units.inDegreesPerSecond
import org.team4099.lib.units.perSecond
import kotlin.math.PI
import kotlin.math.tan

class TargetPoseCommand(val drivetrain: Drivetrain, val targetPose: Pose2d) : Command() {
private val thetaPID: ProfiledPIDController<Radian, Velocity<Radian>>

val thetakP =
LoggedTunableValue(
"Pathfollow/thetakP",
Pair({ it.inDegreesPerSecondPerDegree }, { it.degrees.perSecond.perDegree })
)
val thetakI =
LoggedTunableValue(
"Pathfollow/thetakI",
Pair(
{ it.inDegreesPerSecondPerDegreeSeconds }, { it.degrees.perSecond.perDegreeSeconds }
)
)
val thetakD =
LoggedTunableValue(
"Pathfollow/thetakD",
Pair(
{ it.inDegreesPerSecondPerDegreePerSecond },
{ it.degrees.perSecond.perDegreePerSecond }
)
)

val thetaMaxVel =
LoggedTunableValue("Pathfollow/thetaMaxVel", DrivetrainConstants.PID.MAX_AUTO_ANGULAR_VEL)
val thetaMaxAccel =
LoggedTunableValue("Pathfollow/thetaMaxAccel", DrivetrainConstants.PID.MAX_AUTO_ANGULAR_ACCEL)
var desiredAngle: Angle = 0.0.degrees

init {
addRequirements(drivetrain)

if (RobotBase.isReal()) {
thetakP.initDefault(DrivetrainConstants.PID.AUTO_THETA_PID_KP)
thetakI.initDefault(DrivetrainConstants.PID.AUTO_THETA_PID_KI)
thetakD.initDefault(DrivetrainConstants.PID.AUTO_THETA_PID_KD)
} else {
thetakP.initDefault(DrivetrainConstants.PID.SIM_AUTO_THETA_PID_KP)
thetakI.initDefault(DrivetrainConstants.PID.SIM_AUTO_THETA_PID_KI)
thetakD.initDefault(DrivetrainConstants.PID.SIM_AUTO_THETA_PID_KD)
}

thetaPID =
ProfiledPIDController(
thetakP.get(),
thetakI.get(),
thetakD.get(),
TrapezoidProfile.Constraints(thetaMaxVel.get(), thetaMaxAccel.get())
)
thetaPID.enableContinuousInput(-PI.radians, PI.radians)
}

override fun initialize() {
thetaPID.reset(drivetrain.odometryPose.rotation)
}

override fun execute() {
Logger.recordOutput("ActiveCommands/TargetPoseCommand", true)

val currentPose = drivetrain.odometryPose
desiredAngle =
tan((currentPose.y - targetPose.y).inMeters / (currentPose.x - targetPose.x).inMeters)
.radians
val thetaFeedback = thetaPID.calculate(currentPose.rotation, desiredAngle)

drivetrain.currentRequest =
Request.DrivetrainRequest.OpenLoop(
thetaFeedback,
Pair(drivetrain.fieldVelocity.x, drivetrain.fieldVelocity.y),
fieldOriented = true
)

Logger.recordOutput("AutoLevel/CurrentYawDegrees", drivetrain.odometryPose.rotation.inDegrees)
Logger.recordOutput("AutoLevel/DesiredYawDegrees", desiredAngle.inDegrees)
Logger.recordOutput("AutoLevel/thetaFeedbackDPS", thetaFeedback.inDegreesPerSecond)
}

override fun isFinished(): Boolean {
return (drivetrain.odometryPose.rotation - desiredAngle).absoluteValue <
DrivetrainConstants.PID.AUTO_THETA_ALLOWED_ERROR
}

override fun end(interrupted: Boolean) {
drivetrain.currentRequest =
Request.DrivetrainRequest.OpenLoop(
0.0.radians.perSecond, Pair(drivetrain.fieldVelocity.x, drivetrain.fieldVelocity.y)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.team4099.robot2023.commands.drivetrain

import com.team4099.robot2023.subsystems.drivetrain.drive.Drivetrain
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import com.team4099.robot2023.util.driver.DriverProfile
import edu.wpi.first.wpilibj2.command.Command
import org.littletonrobotics.junction.Logger
Expand All @@ -23,7 +24,7 @@ class TeleopDriveCommand(
override fun execute() {
val speed = driver.driveSpeedClampedSupplier(driveX, driveY, slowMode)
val rotation = driver.rotationSpeedClampedSupplier(turn, slowMode)
drivetrain.setOpenLoop(rotation, speed)
drivetrain.currentRequest = DrivetrainRequest.OpenLoop(rotation, speed)
Logger.recordOutput("ActiveCommands/TeleopDriveCommand", true)
}
override fun isFinished(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.team4099.robot2023.commands.drivetrain

import com.team4099.robot2023.subsystems.drivetrain.drive.Drivetrain
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import edu.wpi.first.wpilibj2.command.Command
import org.littletonrobotics.junction.Logger

Expand All @@ -11,7 +12,7 @@ class ZeroSensorsCommand(val drivetrain: Drivetrain) : Command() {
}

override fun initialize() {
drivetrain.zeroSensors()
drivetrain.currentRequest = DrivetrainRequest.ZeroSensors()
}

override fun isFinished(): Boolean {
Expand Down
Loading

0 comments on commit eca1639

Please sign in to comment.