Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto aim #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions simgui-ds.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
{
"System Joysticks": {
"window": {
"visible": false
}
},
"keyboardJoysticks": [
{
"axisConfig": [
Expand Down Expand Up @@ -97,6 +92,9 @@
"robotJoysticks": [
{
"guid": "Keyboard0"
},
{
"guid": "Keyboard1"
}
]
}
4 changes: 2 additions & 2 deletions simgui.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"visible": false
}
},
"Timing": {
"RoboRIO": {
"window": {
"visible": false
"visible": true
}
}
},
Expand Down
86 changes: 86 additions & 0 deletions src/main/kotlin/com/team4099/lib/logging/DoubleArrayConversions.kt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not belong in pr

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.team4099.lib.logging

import edu.wpi.first.math.kinematics.SwerveModulePosition
import edu.wpi.first.math.kinematics.SwerveModuleState
import org.team4099.lib.geometry.Pose2d
import org.team4099.lib.geometry.Pose3d
import org.team4099.lib.geometry.Quaternion
import org.team4099.lib.geometry.Rotation3d
import org.team4099.lib.geometry.Transform2d
import org.team4099.lib.geometry.Transform3d
import org.team4099.lib.geometry.Translation2d
import org.team4099.lib.geometry.Translation3d
import org.team4099.lib.geometry.Twist2d
import org.team4099.lib.geometry.Twist3d
import org.team4099.lib.units.base.inMeters
import org.team4099.lib.units.derived.inRadians

fun Pose2d.toDoubleArray(): DoubleArray {
return doubleArrayOf(this.x.inMeters, this.y.inMeters, this.rotation.inRadians)
}

fun Pose3d.toDoubleArray(): DoubleArray {
return doubleArrayOf(
this.x.inMeters,
this.y.inMeters,
this.z.inMeters,
this.rotation.rotation3d.quaternion.w,
this.rotation.rotation3d.quaternion.x,
this.rotation.rotation3d.quaternion.y,
this.rotation.rotation3d.quaternion.z
)
}

fun Quaternion.toDoubleArray(): Array<Double> {
return arrayOf(this.w.inRadians, this.x.inMeters, this.y.inMeters, this.z.inMeters)
}

fun Rotation3d.toDoubleArray(): Array<Double> {
return arrayOf(this.x.inRadians, this.y.inRadians, this.z.inRadians)
}

fun Transform2d.toDoubleArray(): Array<Double> {
return arrayOf(this.translation.x.inMeters, this.translation.y.inMeters, this.rotation.inRadians)
}

fun Transform3d.toDoubleArray(): Array<Double> {
return arrayOf(
this.translation.x.inMeters,
this.translation.y.inMeters,
this.translation.z.inMeters,
this.rotation.x.inRadians,
this.rotation.y.inRadians,
this.rotation.z.inRadians
)
}

fun Translation2d.toDoubleArray(): Array<Double> {
return arrayOf(this.x.inMeters, this.y.inMeters)
}

fun Translation3d.toDoubleArray(): Array<Double> {
return arrayOf(this.x.inMeters, this.y.inMeters, this.z.inMeters)
}

fun Twist2d.toDoubleArray(): Array<Double> {
return arrayOf(this.dx.inMeters, this.dy.inMeters, this.dtheta.inRadians)
}

fun Twist3d.toDoubleArray(): Array<Double> {
return arrayOf(
this.dx.inMeters,
this.dy.inMeters,
this.dz.inMeters,
this.rx.inRadians,
this.ry.inRadians,
this.rz.inRadians
)
}

fun SwerveModulePosition.toDoubleArray(): Array<Double> {
return arrayOf(this.distanceMeters, this.angle.radians)
}

fun SwerveModuleState.toDoubleArray(): Array<Double> {
return arrayOf(this.speedMetersPerSecond, this.angle.radians)
}
12 changes: 6 additions & 6 deletions src/main/kotlin/com/team4099/robot2023/BuildConstants.kt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not belong in this pr (how is this pushing isn't it in git ignore?)

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 = 258
const val GIT_SHA = "0a3451d3fae466b43823feb361ab59009e458cc1"
const val GIT_DATE = "2024-02-14T20:51:17Z"
const val GIT_BRANCH = "main"
const val BUILD_DATE = "2024-02-15T21:03:56Z"
const val BUILD_UNIX_TIME = 1708049036263L
const val GIT_REVISION = 260
const val GIT_SHA = "851ecf84ee0e858110dc9ab517f2015eb3654d88"
const val GIT_DATE = "2024-02-19T15:19:59Z"
const val GIT_BRANCH = "AutoAim"
const val BUILD_DATE = "2024-02-20T01:25:57Z"
const val BUILD_UNIX_TIME = 1708410357413L
const val DIRTY = 1
1 change: 1 addition & 0 deletions src/main/kotlin/com/team4099/robot2023/Robot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ object Robot : LoggedRobot() {
// motor checker stuff
val motorCheckerStartTime = Clock.realTimestamp
MotorChecker.periodic()

Logger.recordOutput(
"LoggedRobot/Subsystems/MotorCheckerLoopTimeMS",
(Clock.realTimestamp - motorCheckerStartTime).inMilliseconds
Expand Down
28 changes: 27 additions & 1 deletion src/main/kotlin/com/team4099/robot2023/RobotContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ import org.team4099.lib.smoothDeadband
import org.team4099.lib.units.derived.Angle
import org.team4099.lib.units.derived.degrees
import com.team4099.robot2023.subsystems.superstructure.Request.DrivetrainRequest as DrivetrainRequest
import FieldConstants
import com.team4099.robot2023.commands.drivetrain.TargetPoseCommand
import com.team4099.robot2023.subsystems.wrist.WristIO
import edu.wpi.first.wpilibj2.command.ParallelCommandGroup
import org.team4099.lib.geometry.Pose2d
import org.team4099.lib.geometry.Pose3d
import org.team4099.lib.geometry.Rotation3d
import javax.swing.GroupLayout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥸 remove


object RobotContainer {
private val drivetrain: Drivetrain
Expand Down Expand Up @@ -88,7 +95,7 @@ object RobotContainer {
wrist = Wrist(WristIOSim)
}

superstructure = Superstructure(intake, feeder, elevator, wrist, flywheel)
superstructure = Superstructure(drivetrain, intake, feeder, elevator, wrist, flywheel)
vision.setDataInterfaces({ drivetrain.odometryPose }, { drivetrain.addVisionData(it) })
limelight.poseSupplier = { drivetrain.odometryPose }
}
Expand Down Expand Up @@ -148,6 +155,24 @@ object RobotContainer {

ControlBoard.resetGyro.whileTrue(ResetGyroYawCommand(drivetrain, toAngle = 180.degrees))

ControlBoard.intake.whileTrue(superstructure.groundIntakeCommand())
ControlBoard.ampPrep.whileTrue(superstructure.prepAmpCommand())
ControlBoard.scoreAmp.whileTrue(superstructure.scoreAmpCommand())
ControlBoard.autoAim.whileTrue(
ParallelCommandGroup(
TargetPoseCommand(
drivetrain,
Pose2d(
FieldConstants.Speaker.centerSpeakerOpening.x,
FieldConstants.Speaker.centerSpeakerOpening.y,
0.0.degrees
)
)
, superstructure.autoAimCommand())
)
ControlBoard.scoreSpeaker.whileTrue(superstructure.scoreSpeakerLowCommand())

/*
ControlBoard.runGroundIntake.whileTrue(superstructure.groundIntakeCommand())
ControlBoard.ejectGamePiece.whileTrue(superstructure.ejectGamePieceCommand())
ControlBoard.prepAmpScore.whileTrue(superstructure.prepAmpCommand())
Expand All @@ -158,6 +183,7 @@ object RobotContainer {
ControlBoard.climbExtend.whileTrue(superstructure.climbExtendCommand())
ControlBoard.climbRetract.whileTrue(superstructure.climbRetractCommand())
ControlBoard.requestIdle.whileTrue(superstructure.requestIdleCommand())
*/

/*
TUNING COMMANDS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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.inRadians
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.atan2

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)

Logger.recordOutput("Odometry/targetedPose", doubleArrayOf(targetPose.x.inMeters, targetPose.y.inMeters, targetPose.rotation.inRadians))

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
val relativeToRobotPose = targetPose.relativeTo(currentPose)
desiredAngle = currentPose.rotation + atan2(relativeToRobotPose.y.inMeters, relativeToRobotPose.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)
)
}
}
12 changes: 11 additions & 1 deletion src/main/kotlin/com/team4099/robot2023/config/ControlBoard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ object ControlBoard {
get() = -driver.leftYAxis

val turn: Double
get() = driver.rightXAxis
get() = operator.leftXAxis

val slowMode: Boolean
get() = driver.leftShoulderButton

val resetGyro = Trigger { driver.startButton && driver.selectButton }

/*
val shooterUp = Trigger { driver.bButton }
val shooterDown = Trigger { driver.xButton }
val wristTestUp = Trigger { driver.yButton }
Expand Down Expand Up @@ -64,4 +65,13 @@ object ControlBoard {
val testWrist = Trigger { operator.aButton }
val testElevator = Trigger { operator.bButton }
val setTuningMode = Trigger { driver.rightShoulderButton }

*/

val intake = Trigger {driver.aButton}
val scoreAmp = Trigger {driver.bButton}
val scoreSpeaker = Trigger {driver.xButton}

val autoAim = Trigger {operator.aButton}
val ampPrep = Trigger {operator.bButton}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.team4099.robot2023.config.constants

import org.team4099.lib.units.base.meters
import org.team4099.lib.units.base.seconds
import org.team4099.lib.units.milli
import org.team4099.lib.units.perSecond

typealias GamePiece = Constants.Universal.GamePiece

Expand All @@ -11,6 +13,7 @@ typealias NodeTier = Constants.Universal.NodeTier

object Constants {
object Universal {
val gravity = -9.807.meters.perSecond.perSecond
val SIM_MODE = Tuning.SimType.SIM
const val REAL_FIELD = false

Expand Down
Loading
Loading