Skip to content

Commit

Permalink
making it work(ish) again
Browse files Browse the repository at this point in the history
  • Loading branch information
epshteinmatthew committed Dec 29, 2024
1 parent ae539db commit 011ed13
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 58 deletions.
15 changes: 0 additions & 15 deletions src/main/deploy/paths/goToAmpFile.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
{"commands" : [
[
"gotoAmp"
],
[
"gotoSpeakerLeft"
],
[
"gotoAmp"
],
[
"gotoSpeakerLeft"
],
[
"gotoAmp"
],
[
"gotoSpeakerLeft"
]
]
}
5 changes: 3 additions & 2 deletions src/main/kotlin/frc/lib/FieldPositions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ object FieldPositions {

}

val amp = FieldLocation(Pose2d(14.929358, 8.2042, 270.0.rotation2dFromDeg()),
val amp = FieldLocation(
Pose2d(14.929358, 8.2042, 270.0.rotation2dFromDeg()),
Pose2d(1.84404, 8.2042, 270.0.rotation2dFromDeg()))

val sourceBaseline = FieldLocation(
Expand All @@ -38,7 +39,7 @@ object FieldPositions {

//obstacles do need to be iterated through, but they do not need to have names
val obstacles:Array<Obstacle> = arrayOf(
Obstacle(Pose2d(1.0, 1.0, 0.0.rotation2dFromDeg()), 1.0),
Obstacle(Pose2d(4.6, 4.2, 0.0.rotation2dFromDeg()), 1.5)
//list obstacles here...
)

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/frc/lib/swerve/SwerveDriveBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlin.math.IEEErem
import kotlin.math.PI

class SwerveDriveBase(startingPose: Pose2d) : SubsystemBase() {
private val field = Field2d()
val field = Field2d()
private val shuffleboardTab = Shuffleboard.getTab("Drivetrain")
private val gyroInputs = GyroIOInputsAutoLogged()
private var poseEstimator: SwerveDrivePoseEstimator
Expand Down Expand Up @@ -63,7 +63,7 @@ class SwerveDriveBase(startingPose: Pose2d) : SubsystemBase() {

private val camera: Camera
init {
field.getObject("Obstacle").pose = Pose2d(1.0,2.0,0.0.rotation2dFromDeg())
field.getObject("Obstacle").pose = Pose2d(4.6,4.2,0.0.rotation2dFromDeg())
SwerveDriveConstants.DrivetrainConsts.thetaPIDController.enableContinuousInput(
180.0, -180.0
)
Expand Down
75 changes: 36 additions & 39 deletions src/main/kotlin/frc/robot/RobotContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ class RobotContainer {
goal.red
else
goal.blue
var midpoint = Pose2d()

return gotoPose(to)
}

Expand All @@ -85,46 +83,11 @@ class RobotContainer {
*/
fun gotoPose(to: Pose2d):Command {
val midpoint = AtomicReference(Pose2d(0.0, 0.0, 0.0.rotation2dFromDeg()))
val points = AtomicReference(mutableListOf<Pose2d>())
val toRun = AtomicReference(SequentialCommandGroup())
return runOnce({
val pose = driveSubsystem.getPose()
thingy.setDouble(pose.x)
val m: Double = (to.y - pose.y) / (to.x - pose.x)
val b: Double = -m*to.x + to.y

for (obstacle in obstacles) {
val ntwo = b - obstacle.location.y
val obx = obstacle.location.x
val btwo = -obx * 2 + ntwo * 2 * m
val a = m.pow(2) + 1
val c = ntwo.pow(2) + obx.pow(2) - obstacle.radiusMeters.pow(2)
val det = btwo.pow(2) - 4 * a * c
if (det >= 0) {
val intersection: Pose2d = if (pose.x > obstacle.location.x) Pose2d(
((-btwo + sqrt(det)) / (2 * a)), (m * ((-btwo + sqrt(det)) / (2 * a)) + b), pose.rotation
) else Pose2d(
((-btwo - sqrt(det)) / (2 * a)), (m * ((-btwo + sqrt(det)) / (2 * a)) + b), pose.rotation
)
midpoint.set(Pose2d(
//FIX THIS SO IT DOESNt COLLIDE WITH WALLS
intersection.x + SwerveDriveConstants.DrivetrainConsts.TRACK_WIDTH_METERS+obstacle.radiusMeters,
-1 / m * (intersection.x + SwerveDriveConstants.DrivetrainConsts.TRACK_WIDTH_METERS + obstacle.radiusMeters - midpoint.get().x) + midpoint.get().y,
pose.rotation
))
//REWRITE!!!
val pts = points.get()
pts.add(midpoint.get())
points.set(pts)
}
}
val pts = points.get()
pts.add(to)
points.set(pts)

val points = pathfind(driveSubsystem.getPose(), to)
val sqi = toRun.get()
for (point in points.get()){
for (point in points){
sqi.addCommands(SwerveAutoDrive(
{point},
Pose2d(0.1,0.1,10.0.rotation2dFromDeg()),
Expand All @@ -140,6 +103,40 @@ class RobotContainer {
)
}

fun pathfind(from:Pose2d, to:Pose2d):List<Pose2d> {
val pose = from
val m: Double = (to.y - pose.y) / (to.x - pose.x)
val b: Double = -m*to.x + to.y
for (obstacle in obstacles) {
val ntwo = b - obstacle.location.y
val obx = obstacle.location.x
val btwo = -obx * 2 + ntwo * 2 * m
val a = m.pow(2) + 1
val c = ntwo.pow(2) + obx.pow(2) - obstacle.radiusMeters.pow(2)
val det = btwo.pow(2) - 4 * a * c
if (det >= 0) {
val intersection: Pose2d = if (pose.x > obstacle.location.x) Pose2d(
((-btwo + sqrt(det)) / (2 * a)), (m * ((-btwo + sqrt(det)) / (2 * a)) + b), pose.rotation
) else Pose2d(
((-btwo - sqrt(det)) / (2 * a)), (m * ((-btwo + sqrt(det)) / (2 * a)) + b), pose.rotation
)
if(intersection.x in pose.x..to.x || intersection.x in to.x..pose.x) {
val midpoint =
Pose2d(
//FIX THIS SO IT DOESNt COLLIDE WITH WALLS
intersection.x + SwerveDriveConstants.DrivetrainConsts.TRACK_WIDTH_METERS + obstacle.radiusMeters,
-1 / m * (intersection.x + SwerveDriveConstants.DrivetrainConsts.TRACK_WIDTH_METERS + obstacle.radiusMeters),
pose.rotation
)
driveSubsystem.field.getObject(midpoint.x.toString()).pose = intersection
return listOf(pathfind(from, midpoint), pathfind(midpoint, to)).flatten()

}
}
}
return listOf(to)
}


fun goToSpeakerCloser(): Command {
var startingPose = Pose2d()
Expand Down

0 comments on commit 011ed13

Please sign in to comment.