Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Encoder strafe #59

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ffe4450
fix: decrease max drive speed
Vif15 Mar 8, 2024
022e37d
fix: driveTo now works backwards
Vif15 Mar 8, 2024
c61572b
feat: make driveTo linear
Vif15 Mar 8, 2024
95cda90
feat: autonomous strafing based off encoders
Vif15 Mar 8, 2024
975b830
feat: autonomous strafing based off encoders
Vif15 Mar 8, 2024
a6cfdef
feat: enum for parking positions
Vif15 Mar 9, 2024
5180ffd
fix: rename import
Vif15 Mar 11, 2024
7fa8bc7
feat: park position enum
Vif15 Mar 11, 2024
998be52
fix rename import
Vif15 Mar 11, 2024
c70193b
fix rename import
Vif15 Mar 11, 2024
18b22a9
feat: enum class for which truss to drive through in auto
Vif15 Mar 11, 2024
7721585
fix: spinTo is now linear
Vif15 Mar 11, 2024
3995e0d
fix: lower spin speed for auto
Vif15 Mar 11, 2024
348f49c
fix: move center scan area
Vif15 Mar 11, 2024
ae1db40
draft: more complex auto
Vif15 Mar 11, 2024
51db5db
draft: more complex auto (frontstage red left completed
Vif15 Mar 12, 2024
32ab06e
draft: more complex auto
Vif15 Mar 13, 2024
6954958
draft: more complex auto (front stage red left added truss support)
Vif15 Mar 13, 2024
5fe5b4c
draft: more complex auto (front stage blue left)
Vif15 Mar 13, 2024
6052661
draft: more complex auto (add center truss support)
Vif15 Mar 13, 2024
d6450ba
fix: driveTo now stops after 9 seconds
Vif15 Mar 13, 2024
a5dd631
fix: adjust PLACE_POSITION
Vif15 Mar 13, 2024
f73d3c2
feat: all back stage blue auto working
Vif15 Mar 13, 2024
a834f15
auto draft + april movement
Vif15 Mar 13, 2024
9d0c8f0
feat: added a wait param for auto front/back
Vif15 Mar 14, 2024
8400e11
feat: if apriltag not found spin slightly in hopes that it will be found
Vif15 Mar 14, 2024
fb3df51
draft: back stage right red (mirror of back stage left blue)
Vif15 Mar 14, 2024
6c088fb
feat: remove april spin
Vif15 Mar 15, 2024
f325ba7
draft: auto
Vif15 Mar 15, 2024
5d62535
fix: drone pos
Vif15 Mar 15, 2024
387fb12
fix: stuff
Vif15 Mar 17, 2024
e82177e
feat: let's get down to business
Vif15 Mar 17, 2024
5188aef
merge: update from main
BD103 Mar 17, 2024
06fcb1b
refactor: fmt
BD103 Mar 17, 2024
aa73187
fix: build error
BD103 Mar 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ object AprilMovement : API() {
var headingError = Double.MAX_VALUE
var strafeError = Double.MAX_VALUE

var time = System.currentTimeMillis()

// Drive while opmode is active and target has not been reached
while (
opModeIsActive() &&
Expand All @@ -52,6 +54,11 @@ object AprilMovement : API() {
// Try scanning for the april tag
val tag = AprilVision.detect(tagId)

// Stop if runtime is more than 7 seconds
if (System.currentTimeMillis() - time > 11000) {
break
}

// If no tag of the expected ID is found
if (tag == null) {
// Stop moving, hoping that the tag will be scanned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.firstinspires.ftc.teamcode.api.linear
import com.qualcomm.robotcore.hardware.DcMotor
import com.qualcomm.robotcore.hardware.DcMotorSimple
import com.qualcomm.robotcore.hardware.PIDCoefficients
import com.qualcomm.robotcore.util.ElapsedTime
import org.firstinspires.ftc.teamcode.api.API
import org.firstinspires.ftc.teamcode.api.TriWheels
import org.firstinspires.ftc.teamcode.api.linear.Encoders.driveTo
Expand All @@ -12,7 +11,6 @@ import org.firstinspires.ftc.teamcode.utils.MotorController
import org.firstinspires.ftc.teamcode.utils.MotorControllerGroup
import org.firstinspires.ftc.teamcode.utils.RobotConfig
import kotlin.math.abs
import kotlin.math.min

/**
* An API for manipulating wheels using encoders.
Expand Down Expand Up @@ -58,36 +56,24 @@ object Encoders : API() {
val leftTarget = left.currentPosition + ticks
val rightTarget = right.currentPosition + ticks

val d: Int =
if (inches > 0) {
1
} else {
-1
}

// Set left direction to reverse, undoing it at the end even if an exception is thrown
try {
right.direction = DcMotorSimple.Direction.REVERSE

val runtime = ElapsedTime()

while (
abs(leftTarget - left.currentPosition) > RobotConfig.Encoders.ENCODER_ERROR &&
abs(rightTarget - right.currentPosition) > RobotConfig.Encoders.ENCODER_ERROR &&
linearOpMode.opModeIsActive()
) {
// Accelerate the longer the robot has been running
val timeSpeed = runtime.seconds() * RobotConfig.Encoders.TIME_GAIN

left.power =
min(
min(
(leftTarget - left.currentPosition) * RobotConfig.Encoders.ENCODER_GAIN,
timeSpeed,
),
RobotConfig.Encoders.MAX_DRIVE_SPEED,
)
right.power =
min(
min(
(rightTarget - right.currentPosition) * RobotConfig.Encoders.ENCODER_GAIN,
timeSpeed,
),
RobotConfig.Encoders.MAX_DRIVE_SPEED,
)
left.power = RobotConfig.Encoders.MAX_DRIVE_SPEED * d
right.power = RobotConfig.Encoders.MAX_DRIVE_SPEED * d

with(linearOpMode.telemetry) {
addData("Status", "Encoder Driving")
Expand Down Expand Up @@ -139,25 +125,14 @@ object Encoders : API() {
TriWheels.blue.targetPosition = ticks

try {
val runtime = ElapsedTime()

while (
TriWheels.red.isBusy &&
TriWheels.green.isBusy &&
TriWheels.blue.isBusy &&
linearOpMode.opModeIsActive()
) {
// Accelerate the longer the robot has been running
val timeSpeed = runtime.seconds() * RobotConfig.Encoders.TIME_GAIN

TriWheels.rotate(
min(
min(
abs(TriWheels.red.currentPosition - TriWheels.red.targetPosition) * RobotConfig.Encoders.ENCODER_GAIN,
timeSpeed,
),
RobotConfig.Encoders.MAX_SPIN_SPEED,
),
RobotConfig.Encoders.MAX_SPIN_SPEED,
)

with(linearOpMode.telemetry) {
Expand Down Expand Up @@ -185,6 +160,39 @@ object Encoders : API() {
}
}

/**
* strafeTo is a robust and experimental function that allows strafing towards pi/2 and
* 3pi/2 using encoders. This function will be replaced by moveTo which will have no
* restrictions on direction.
*/
fun strafeTo(
direction: Direction,
inches: Double,
) {
TriWheels.stopAndResetMotors()

val (left, right, back) = this.defineWheels(direction)
val ticks = this.inchesToTick(inches)
val d: Int

if (direction == Direction.Left) {
d = 1
while (back.currentPosition > -ticks && linearOpMode.opModeIsActive()) {
left.power = 0.09 * d
right.power = 0.09 * d
back.power = -0.18 * d
}
} else {
d = -1
while (back.currentPosition < ticks && linearOpMode.opModeIsActive()) {
left.power = 0.09 * d
right.power = 0.09 * d
back.power = -0.18 * d
}
}
TriWheels.stopAndResetMotors()
}

fun driveTo2(
direction: Direction,
inches: Double,
Expand Down Expand Up @@ -286,6 +294,8 @@ object Encoders : API() {
Direction.Red -> Triple(TriWheels.blue, TriWheels.green, TriWheels.red)
Direction.Green -> Triple(TriWheels.red, TriWheels.blue, TriWheels.green)
Direction.Blue -> Triple(TriWheels.green, TriWheels.red, TriWheels.blue)
Direction.Left -> Triple(TriWheels.blue, TriWheels.green, TriWheels.red)
Direction.Right -> Triple(TriWheels.blue, TriWheels.green, TriWheels.red)
}

/**
Expand All @@ -297,5 +307,7 @@ object Encoders : API() {
Red,
Green,
Blue,
Left,
Right,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.firstinspires.ftc.robotcore.external.Telemetry
import org.firstinspires.ftc.robotcore.internal.camera.calibration.CameraCalibration
import org.firstinspires.ftc.teamcode.api.API
import org.firstinspires.ftc.teamcode.utils.RobotConfig
import org.firstinspires.ftc.teamcode.utils.Team
import org.firstinspires.ftc.teamcode.utils.auto.Team
import org.firstinspires.ftc.vision.VisionProcessor
import org.opencv.core.Core
import org.opencv.core.Mat
Expand Down
Loading