Skip to content

Commit d3ec213

Browse files
committed
more ll fusion work
1 parent f599d20 commit d3ec213

4 files changed

Lines changed: 51 additions & 10 deletions

File tree

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/common/subsystem/Drive.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ open class Drive(val opMode: OpMode, limelight: Limelight? = null) : Subsystem("
5252
println("end pose ${follower.currentPath?.endPose()}")
5353
}
5454

55-
fun drawRobot() {
55+
fun drawRobot(pose: Pose2d = this.pose, name: String = "pose", color: String = "white") {
5656
canvas.moveCursor(pose.x, pose.y)
57-
canvas.setStyle(fill = "none", outline = "white", width = 1.5)
57+
canvas.setStyle(fill = "none", outline = color, width = 1.5)
5858
canvas.circle(8.0)
5959
val lookPos = pose + Vec2d(1.0, 0.0).rotate(pose.radians) * 8.0
6060
canvas.line(lookPos.x, lookPos.y)
6161

62-
opMode.tel.addData("pose.x", pose.x)
63-
opMode.tel.addData("pose.y", pose.y)
64-
opMode.tel.addData("pose.h", pose.heading)
62+
opMode.tel.addData("$name.x", pose.x)
63+
opMode.tel.addData("$name.y", pose.y)
64+
opMode.tel.addData("$name.h", pose.heading)
6565
}
6666

6767
companion object {

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/common/subsystem/Limelight.kt

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package org.firstinspires.ftc.teamcode.common.subsystem
33
import com.bylazar.configurables.annotations.Configurable
44
import com.millburnx.cmdx.Command
55
import com.millburnx.util.Pose2d
6+
import com.millburnx.util.vector.Vec2d
67
import com.qualcomm.hardware.limelightvision.Limelight3A
78
import org.firstinspires.ftc.teamcode.common.GlobalStore
89
import org.firstinspires.ftc.teamcode.common.hardware.fromFTC
10+
import org.firstinspires.ftc.teamcode.common.hardware.normalizeDegrees
911
import org.firstinspires.ftc.teamcode.common.util.OpModeLoop
1012
import org.firstinspires.ftc.teamcode.opmode.OpMode
1113

@@ -20,6 +22,12 @@ class Limelight(
2022

2123
var getPose: () -> Pose2d = { Pose2d() }
2224
var setPose: (Pose2d) -> Unit = {}
25+
var drawPose: (Pose2d) -> Unit = {}
26+
27+
/**
28+
* Heading of turret in global space
29+
*/
30+
var turretHeading: () -> Double = { 0.0 }
2331

2432
var localizationState: LocalizationState =
2533
if (GlobalStore.autonPose == null) LocalizationState.NONE else LocalizationState.READY
@@ -35,22 +43,45 @@ class Limelight(
3543
if (localizationState == LocalizationState.NONE) {
3644
val result = limelight.getLatestResult()
3745
if (result != null && result.isValid) {
38-
val pose = Pose2d.fromFTC(result.botpose)
46+
val pose = convertLL(Pose2d.fromFTC(result.botpose), useDriveHeading = false)
3947
this@Limelight.pose = pose to System.nanoTime()
4048
setPose(pose)
49+
drawPose(pose)
4150
localizationState = LocalizationState.READY
4251
}
4352
}
4453
if (localizationState == LocalizationState.READY) {
45-
limelight.updateRobotOrientation(getPose().heading)
54+
limelight.updateRobotOrientation(normalizeDegrees(getPose().heading + turretHeading()))
4655
val result = limelight.getLatestResult()
4756
if (result != null && result.isValid) {
48-
pose = Pose2d.fromFTC(result.botpose_MT2) to System.nanoTime()
57+
val pose = convertLL(Pose2d.fromFTC(result.botpose_MT2))
58+
this@Limelight.pose = pose to System.nanoTime()
59+
drawPose(pose)
4960
}
5061
}
5162
}
5263
}
5364

65+
fun convertLL(pose: Pose2d, useDriveHeading: Boolean = true): Pose2d {
66+
// the primary thing we need to handle here
67+
// is conversion from the turret frame to the robot frame
68+
// so account for the offset of the centers and the heading of the turret
69+
70+
val newHeading = normalizeDegrees(pose.heading - turretHeading())
71+
72+
val offset = Vec2d(16.0, -42.0) / 25.4
73+
74+
val driveHeading = if (useDriveHeading) {
75+
getPose().heading
76+
} else {
77+
newHeading
78+
}
79+
80+
val newPos = pose.position - offset.rotate(driveHeading)
81+
82+
return Pose2d(newPos, newHeading)
83+
}
84+
5485
enum class LocalizationState {
5586
NONE,
5687
READY;

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/opmode/test/localization/FusionTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.millburnx.util.vector.Vec2d
77
import com.qualcomm.robotcore.eventloop.opmode.TeleOp
88
import org.firstinspires.ftc.teamcode.common.subsystem.Limelight
99
import org.firstinspires.ftc.teamcode.common.subsystem.TeleOpDrive
10+
import org.firstinspires.ftc.teamcode.common.subsystem.Turret
1011
import org.firstinspires.ftc.teamcode.common.util.OpModeLoop
1112
import org.firstinspires.ftc.teamcode.opmode.OpMode
1213

@@ -16,6 +17,11 @@ class FusionTest : OpMode() {
1617
override fun run() {
1718
val limelight = Limelight(this)
1819
val drive = TeleOpDrive(this, true, limelight)
20+
val turret = Turret(this, { drive.pose.heading }, { drive.velocity.heading }, { voltageSensor.voltage })
21+
limelight.turretHeading = { turret.angle }
22+
limelight.drawPose = { pose ->
23+
drive.drawRobot(pose, "llPose", "yellow")
24+
}
1925

2026
scheduler.schedule(Command() {
2127
val canvas = PanelsField.field

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/pedro/PinpointLocalizer.kt renamed to TeamCode/src/main/java/org/firstinspires/ftc/teamcode/pedro/FusionLocalizer.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class FusionLocalizer(hardwareMap: HardwareMap, val deltaTime: () -> Double, val
3737
}
3838

3939
init {
40+
limelight?.getPose = { _pose }
41+
limelight?.setPose = { setPose(it) }
4042
while (pinpoint.deviceStatus != GoBildaPinpointDriver.DeviceStatus.READY) {
4143
Thread.yield()
4244
}
@@ -57,12 +59,14 @@ class FusionLocalizer(hardwareMap: HardwareMap, val deltaTime: () -> Double, val
5759

5860
override fun setStartPose(p0: Pose) = setPose(p0) // low-key cannot be bothered to compensate, just don't update?
5961

60-
override fun setPose(p0: Pose) {
61-
pinpoint.position = Pose2d.fromPedro(p0).toFTC()
62+
fun setPose(p0: Pose2d) {
63+
pinpoint.position = p0.toFTC()
6264
kfX.reset()
6365
kfY.reset()
6466
}
6567

68+
override fun setPose(p0: Pose) = setPose(Pose2d.fromPedro(p0))
69+
6670
override fun update() {
6771
pinpoint.update()
6872
val pose = Pose2d.fromFTC(pinpoint.position)

0 commit comments

Comments
 (0)