Skip to content

Commit

Permalink
rebuild and tune mechanisms
Browse files Browse the repository at this point in the history
  • Loading branch information
huangn2005 committed Feb 10, 2023
1 parent 3ac191f commit 0e0ff94
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class CycleAuto(val right: Boolean) : CommandLinearOpMode<October>(::Oc
val offset = 5.0
val startPose = pose2d(0.0, offset, PI)
var intakePose = pose2d(if (right) 52.5 else 52.5, if (right) -21.0 else 20.25, if (right) PI/2.0 else -PI/2.0)
var outputPose = pose2d(if (right) 48.5 else 50.5, if (right) 11.0 else -12.0, if (right) PI else PI)
var outputPose = pose2d(if (right) 48.5 else 48.5, if (right) 11.0 else -12.0, if (right) PI else PI)
val MIDDLE = if (right) PI/2.0 else -PI/2.0
val SIDE = if (right) -PI/2.0 else PI/2.0
val cycleOffset = if (right)
Expand Down Expand Up @@ -101,7 +101,7 @@ abstract class CycleAuto(val right: Boolean) : CommandLinearOpMode<October>(::Oc
// VerticalSlides.State.RunTo(VerticalSlides.Height.MID.pos)
// }
// await { !bot.output.lift.isBusy }
task { bot.output.claw.state = Output.ClawState.OPEN }
task { bot.output.claw.state = Output.ClawState.WIDE }
delay(100.milliseconds)
task { bot.output.arm.state = Output.ArmState.MAX_UP }
delay(200.milliseconds)
Expand Down Expand Up @@ -208,19 +208,6 @@ abstract class CycleAuto(val right: Boolean) : CommandLinearOpMode<October>(::Oc
}
}

fun Double.angleWrap(): Double {
var x = this;
while (x >= PI) {
x -= 2 * PI
}
while (x <= -PI) {
x += 2 * PI
}
return x
}

fun TrajectorySequenceBuilder.turnWrap(angle: Double) = turn(angle.angleWrap())

@Autonomous(preselectTeleOp = "OctoberTele", group = "_official")
class LeftCycleAuto : CycleAuto(false)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package org.thenuts.powerplay.opmode.auto

import com.acmerobotics.roadrunner.geometry.Pose2d
import com.acmerobotics.roadrunner.geometry.Vector2d
import com.qualcomm.robotcore.eventloop.opmode.Autonomous
import org.thenuts.powerplay.acme.trajectorysequence.TrajectorySequenceBuilder
import org.thenuts.powerplay.game.Alliance
import org.thenuts.powerplay.game.Mode
import org.thenuts.powerplay.game.Signal
import org.thenuts.powerplay.opmode.CommandLinearOpMode
import org.thenuts.powerplay.opmode.commands.go
import org.thenuts.powerplay.subsystems.output.VerticalSlides
import org.thenuts.powerplay.subsystems.October
import org.thenuts.powerplay.subsystems.output.Output
import org.thenuts.switchboard.command.Command
import org.thenuts.switchboard.command.CommandScheduler
import org.thenuts.switchboard.dsl.mkSequential
import kotlin.math.PI
import kotlin.time.Duration.Companion.milliseconds

abstract class PassthruAuto(val right: Boolean) : CommandLinearOpMode<October>(::October, Alliance.RED, Mode.AUTO) {
lateinit var cmd: Command

override fun postInitHook() {
bot.vision?.front?.startDebug()
bot.vision?.signal?.enable()
bot.vision?.gamepad = gamepad1

val reference = Pose2d(if (right) -36.0 else 36.0, 72.0, -PI/2.0)

fun vec2d(x: Double, y: Double): Vector2d {
if (REFERENCE)
return Vector2d(x, y).rotated(reference.heading) + reference.vec()
else return Vector2d(x, y)
}

fun pose2d(x: Double, y: Double, heading: Double): Pose2d {
if (REFERENCE)
return Pose2d(vec2d(x, y), heading + reference.heading)
else return Pose2d(x, y, heading)
}

fun heading(heading: Double): Double {
if (REFERENCE)
return heading + reference.heading
else return heading
}

val offset = 5.0
val startPose = pose2d(0.0, offset, PI)
var intakePose = pose2d(if (right) 52.5 else 52.5, if (right) -21.0 else 20.25, if (right) PI/2.0 else -PI/2.0)
var outputPose = pose2d(if (right) 48.5 else 50.5, if (right) 11.0 else -12.0, if (right) PI else PI)
val MIDDLE = if (right) PI/2.0 else -PI/2.0
val SIDE = if (right) -PI/2.0 else PI/2.0
val cycleOffset = if (right)
pose2d(0.0, 0.0, 0.0)
else
pose2d(0.0, 2.75, 0.0)

bot.drive.poseEstimate = startPose
cmd = mkSequential {
// add(ExtendCommand(bot.output, scoreHeight, Output.OutputSide.SAMESIDE))
task { bot.output.claw.state = Output.ClawState.CLOSED }
// delay(500.milliseconds)

go(bot.drive, startPose) {
setTangent(-PI/4.0)
splineToConstantHeading(vec2d(3.0, offset/2.0), heading(-PI/4.0))
// back(3.0)
addDisplacementMarker {
bot.output.arm.state = Output.ArmState.MAX_UP
}
// if (right) {
// strafeRight(24.0 - offset)
// back(48.0)
// } else {
// strafeLeft(24.0 + offset)
// back(48.0)
// }
// strafeLeft(offset)
splineToConstantHeading(vec2d(8.0, 0.0), heading(0.0))
splineToConstantHeading(vec2d(52.0, 0.0), heading(0.0))
addDisplacementMarker {
bot.output.lift.state =
VerticalSlides.State.RunTo(VerticalSlides.Height.HIGH.pos)
}
splineToConstantHeading(outputPose.vec(), heading(MIDDLE))
}

fun output(height: Int) {
task {
bot.output.lift.state =
VerticalSlides.State.RunTo(height)
}
task { bot.output.arm.state = Output.ArmState.CLEAR }
await { !bot.output.lift.isBusy }
// delay(1000.milliseconds)
task { bot.output.arm.state = Output.ArmState.PASSTHRU_OUTPUT }
delay(300.milliseconds)
// task {
// bot.output.lift.state =
// VerticalSlides.State.RunTo(VerticalSlides.Height.MID.pos)
// }
// await { !bot.output.lift.isBusy }
task { bot.output.claw.state = Output.ClawState.OPEN }
delay(150.milliseconds)
task { bot.output.arm.state = Output.ArmState.CLEAR }
delay(200.milliseconds)
}

output(VerticalSlides.Height.HIGH.pos)

repeat(2) { i ->
val stackHeight = 5 - i
//
// go(bot.drive, outputPose) {
// turnWrap(intakePose.heading - outputPose.heading)
// }

task { bot.output.lift.state = VerticalSlides.State.RunTo(0) }
await { !bot.output.isBusy }

task { bot.output.arm.state = Output.ArmState.values()[stackHeight - 1] }
await { !bot.output.isBusy }

go(bot.drive, outputPose/*.copy(heading = intakePose.heading)*/) {
setTangent((outputPose.heading + PI).angleWrap())
splineTo(intakePose.vec(), heading(SIDE))
}

task { bot.output.claw.state = Output.ClawState.CLOSED }
delay(500.milliseconds)

task { bot.output.lift.state = VerticalSlides.State.RunTo(VerticalSlides.Height.ABOVE_STACK.pos) }
await { !bot.output.lift.isBusy }
task { bot.output.arm.state = Output.ArmState.MAX_UP }


go(bot.drive, intakePose) {
// lineToLinearHeading(outputPose)
setTangent(heading(SIDE))
splineTo(outputPose.vec(), outputPose.heading)
turnWrap(outputPose.heading - intakePose.heading)
}

intakePose += cycleOffset
outputPose += cycleOffset


output(VerticalSlides.Height.HIGH.pos)
}

switch({ bot.vision!!.signal.finalTarget }) {
value(Signal.LEFT) {
go(bot.drive, outputPose) {
setTangent(heading(SIDE))
splineToConstantHeading(vec2d(51.0, 0.0), SIDE)
addDisplacementMarker { bot.output.lift.runTo(VerticalSlides.Height.MID.pos) }
splineToConstantHeading(vec2d(32.0, 0.0), PI)
splineToConstantHeading(vec2d(32.0, 23.0), PI/2.0)
splineToConstantHeading(vec2d(35.0, 23.0), 0.0)
}
}
value(Signal.MID) {
go(bot.drive, outputPose) {
setTangent(heading(SIDE))
splineToConstantHeading(vec2d(51.0, 0.0), SIDE)
addDisplacementMarker { bot.output.lift.runTo(VerticalSlides.Height.MID.pos) }
splineToConstantHeading(vec2d(35.0, 0.0), PI)
}
}
value(Signal.RIGHT) {
go(bot.drive, outputPose) {
setTangent(heading(SIDE))
splineToConstantHeading(vec2d(51.0, 0.0), SIDE)
addDisplacementMarker { bot.output.lift.runTo(VerticalSlides.Height.MID.pos) }
splineToConstantHeading(vec2d(32.0, 0.0), PI)
splineToConstantHeading(vec2d(32.0, -23.0), -PI/2.0)
splineToConstantHeading(vec2d(35.0, -23.0), 0.0)
}
}
}
task { bot.output.lift.runTo(0) }
delay(1000.milliseconds)
task { stop() }
}
}

override fun stopHook() {
bot.vision?.front?.stopDebug()
bot.vision?.signal?.disable()
}

override fun initLoopHook() {
log.out["signal"] = bot.vision?.signal?.finalTarget
}

override fun postStartHook() {
bot.vision?.front?.stopDebug()
bot.vision?.signal?.disable()
bot.vision?.gamepad = null
sched.addCommand(cmd)
}

override fun loopHook() {
log.out["COMMAND ORDER"] = sched.nodes.filterIsInstance<CommandScheduler.CommandNode>().map { it.runner.cmd::class.simpleName }
}

companion object {
@JvmField var REFERENCE = false
}
}

@Autonomous(preselectTeleOp = "OctoberTele", group = "_official")
class LeftPassthruAuto : PassthruAuto(false)

@Autonomous(preselectTeleOp = "OctoberTele", group = "_official")
class RightPassthruAuto : PassthruAuto(true)
18 changes: 18 additions & 0 deletions TeamCode/src/main/java/org/thenuts/powerplay/opmode/auto/ext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.thenuts.powerplay.opmode.auto

import org.thenuts.powerplay.acme.trajectorysequence.TrajectorySequenceBuilder
import kotlin.math.PI


fun Double.angleWrap(): Double {
var x = this;
while (x > PI) {
x -= 2 * PI
}
while (x <= -PI) {
x += 2 * PI
}
return x
}

fun TrajectorySequenceBuilder.turnWrap(angle: Double) = turn(angle.angleWrap())
31 changes: 20 additions & 11 deletions TeamCode/src/main/java/org/thenuts/powerplay/opmode/tele/Driver2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ class Driver2(val gamepad: Gamepad, val bot: October, val outputSlot: SlotComman
} else if (pad.left_bumper && !prev.left_bumper) {
// open claw
outputSlot.interrupt(mkSequential {
task { bot.output.claw.state = Output.ClawState.OPEN }

task { bot.output.claw.state = if (bot.output.claw.state == Output.ClawState.OPEN) Output.ClawState.WIDE
else Output.ClawState.OPEN }
await { !bot.output.isBusy }
})
/*
Expand Down Expand Up @@ -189,13 +191,17 @@ class Driver2(val gamepad: Gamepad, val bot: October, val outputSlot: SlotComman
task { bot.output.arm.state = Output.ArmState.SAMESIDE_OUTPUT }
await { !bot.output.isBusy }
})
} else if (pad.x && false) {
// interruptTo(Output.OutputState.S_LOWER)
// outputSlot.interrupt(mkSequential {
// task { bot.output.arm.state = Output.ArmState.SAMESIDE_OUTPUT }
// await { !bot.output.isBusy }
// })
if ((!prev.x || frame.n % 4 == 0L) && bot.output.claw.state != Output.ClawState.CLOSED) {
} else if (pad.x && !prev.x) {
interruptTo(Output.OutputState.S_LOWER)
outputSlot.interrupt(mkSequential {
task {
bot.output.arm.state =
if (bot.output.arm.state == Output.ArmState.PASSTHRU_HOVER) Output.ArmState.PASSTHRU_OUTPUT
else Output.ArmState.PASSTHRU_HOVER
}
await { !bot.output.isBusy }
})
/*if ((!prev.x || frame.n % 4 == 0L) && bot.output.claw.state != Output.ClawState.CLOSED) {
val dist = bot.intake_dist.getDistance(DistanceUnit.INCH)
bot.log.out["intake_dist"] = dist
if (dist < CLAW_DIST)
Expand All @@ -204,7 +210,7 @@ class Driver2(val gamepad: Gamepad, val bot: October, val outputSlot: SlotComman
// task { bot.output.arm.state = Output.ArmState.CLEAR }
// await { !bot.output.isBusy }
// })
}
}*/
} else if (pad.y && !prev.y) {
// interruptTo(Output.OutputState.CLEAR)
outputSlot.interrupt(mkSequential {
Expand All @@ -214,11 +220,14 @@ class Driver2(val gamepad: Gamepad, val bot: October, val outputSlot: SlotComman
} else if (pad.a && !prev.a) {
// interruptTo(Output.OutputState.GROUND)
outputSlot.interrupt(mkSequential {
task { bot.output.claw.state = Output.ClawState.OPEN }
// task { bot.output.arm.state = Output.ArmState.CLEAR }
if (bot.output.arm.state.pos < Output.ArmState.CLEAR.pos) {
task { bot.output.arm.state = Output.ArmState.CLEAR }
await { !bot.output.isBusy }
}
task { bot.output.lift.runTo(0) }
await { !bot.output.isBusy }
task { bot.output.arm.state = Output.ArmState.values()[intakeHeight - 1] }
task { bot.output.claw.state = Output.ClawState.WIDE }
})
}

Expand Down
2 changes: 1 addition & 1 deletion TeamCode/src/main/java/org/thenuts/powerplay/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## TeamCode Module
x## TeamCode Module

Welcome!

Expand Down
Loading

0 comments on commit 0e0ff94

Please sign in to comment.