Skip to content

Commit b080d46

Browse files
committed
Estimate flywheel velocity.
Added shooting while moving, untested. Changed field reset position and some shooting constants.
1 parent 80bbcfd commit b080d46

File tree

7 files changed

+48
-28
lines changed

7 files changed

+48
-28
lines changed

TeamCode/src/main/kotlin/pioneer/Constants.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ object Constants {
190190

191191
object Turret {
192192
const val TICKS_PER_REV = 384.5 * 3
193-
const val HEIGHT = 0.0 // TODO MEASURE
193+
const val HEIGHT = 30.0
194194
const val THETA = 0.93
195195
const val ANGLE_TOLERANCE_RADIANS = 0.075
196+
const val LAUNCH_TIME = 0.125
196197
}
197198

198199
object ServoPositions {

TeamCode/src/main/kotlin/pioneer/decode/GoalTag.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ enum class GoalTag(
4545
val height: Double
4646
get() = position[2].toDouble()
4747

48+
val shootingHeight = height + 20.0
49+
4850
// Tag pose plus half the goal depth (46.45 cm)
4951
val shootingPose: Pose
5052
get() =

TeamCode/src/main/kotlin/pioneer/hardware/Flywheel.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import com.qualcomm.robotcore.hardware.HardwareMap
77
import com.qualcomm.robotcore.hardware.PIDFCoefficients
88
import org.firstinspires.ftc.robotcore.external.navigation.CurrentUnit
99
import pioneer.Constants
10+
import pioneer.decode.GoalTag
1011
import pioneer.helpers.FileLogger
1112
import pioneer.helpers.Pose
1213
import kotlin.math.cos
14+
import kotlin.math.pow
15+
import kotlin.math.sin
1316
import kotlin.math.sqrt
1417
import kotlin.math.tan
1518

@@ -56,9 +59,9 @@ class Flywheel(
5659
target: Pose,
5760
pose: Pose,
5861
): Double {
59-
val tempTargetHeight = 0 // TODO Replace with GoalTag info when merged with CV branch
60-
val heightDiff = tempTargetHeight - Constants.Turret.HEIGHT
61-
val groundDistance = pose distanceTo pose
62+
val shootPose = pose + Pose(pose.vx * Constants.Turret.LAUNCH_TIME, pose.vy * Constants.Turret.LAUNCH_TIME)
63+
val heightDiff = GoalTag.BLUE.shootingHeight - Constants.Turret.HEIGHT
64+
val groundDistance = shootPose distanceTo target
6265
val v0 =
6366
(groundDistance) / (
6467
cos(
@@ -67,6 +70,10 @@ class Flywheel(
6770
)
6871

6972
val flywheelVelocity = 1.58901 * v0 + 17.2812
70-
return flywheelVelocity
73+
val thetaToTarget = -(shootPose angleTo target)
74+
val newTargetVelocityX = sin(thetaToTarget) * flywheelVelocity - pose.vx
75+
val newTargetVelocityY = cos(thetaToTarget) * flywheelVelocity - pose.vy
76+
val newTargetVelocity = sqrt(newTargetVelocityX.pow(2) + newTargetVelocityY.pow(2))
77+
return newTargetVelocity
7178
}
7279
}

TeamCode/src/main/kotlin/pioneer/hardware/Turret.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.qualcomm.robotcore.hardware.DcMotor
44
import com.qualcomm.robotcore.hardware.DcMotorEx
55
import com.qualcomm.robotcore.hardware.DcMotorSimple
66
import com.qualcomm.robotcore.hardware.HardwareMap
7+
import org.firstinspires.ftc.robotcore.external.Const
78
import pioneer.Constants
89
import pioneer.helpers.MathUtils
910
import pioneer.helpers.Pose
@@ -92,9 +93,10 @@ class Turret(
9293
pose: Pose,
9394
target: Pose,
9495
) {
96+
val shootPose = pose + Pose(pose.vx * Constants.Turret.LAUNCH_TIME, pose.vy * Constants.Turret.LAUNCH_TIME)
9597
// General Angle(From robot 0 to target):
96-
val targetTheta = (pose angleTo target)
97-
val turretTheta = (PI / 2 + targetTheta) - pose.theta
98+
val targetTheta = (shootPose angleTo target)
99+
val turretTheta = (PI / 2 + targetTheta) - shootPose.theta
98100
gotoAngle(MathUtils.normalizeRadians(turretTheta), 0.85)
99101
}
100102
}

TeamCode/src/main/kotlin/pioneer/opmodes/teleop/Teleop.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Teleop : BaseOpMode() {
5959
telemetry.addData("Target Goal", driver2.targetGoal)
6060
telemetry.addData("Turret Mode", bot.turret?.mode)
6161
telemetry.addData("Shoot State", driver2.shootState)
62+
telemetry.addData("Estimating Flywheel Speed", driver2.isEstimateSpeed)
6263
telemetry.addData("Flywheel Speed", driver2.flywheelVelocityEnum)
6364
telemetry.addData("Flywheel TPS", bot.flywheel?.velocity)
6465
telemetry.addData("Turret Angle", driver2.turretAngle)

TeamCode/src/main/kotlin/pioneer/opmodes/teleop/drivers/TeleopDriver1.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ class TeleopDriver1(
117117
private fun handleResetPose() {
118118
if (gamepad.options) {
119119
if (bot.allianceColor == AllianceColor.RED) {
120-
bot.pinpoint?.reset(Pose(-80.0, -95.0, theta = 0.1))
120+
bot.pinpoint?.reset(Pose(-86.7, -99.0, theta = 0.1))
121121
} else {
122-
bot.pinpoint?.reset(Pose(80.0, -95.0, theta = 0.1))
122+
bot.pinpoint?.reset(Pose(86.7, -99.0, theta = 0.1))
123123
}
124124
}
125125
}

TeamCode/src/main/kotlin/pioneer/opmodes/teleop/drivers/TeleopDriver2.kt

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,33 @@ class TeleopDriver2(
1919
private val gamepad: Gamepad,
2020
private val bot: Bot,
2121
) {
22-
enum class FlywheelSpeed(
22+
enum class FlywheelSpeedRange(
2323
val velocity: Double,
2424
) {
2525
SHORT_RANGE(800.0),
2626
LONG_RANGE(1000.0),
2727
}
2828

29+
val isEstimateSpeed = Toggle(true)
2930
private val chrono = Chrono(autoUpdate = false)
3031
private val isAutoTracking = Toggle(false)
3132
private val flywheelToggle = Toggle(false)
32-
private val changeFlywheelSpeedToggle = Toggle(false)
33+
private val changeFlywheelRangeToggle = Toggle(false)
3334

3435
var P = Points(bot.allianceColor)
3536

3637
enum class ShootState { READY, MOVING_TO_POSITION, LAUNCHING }
3738

38-
var flywheelVelocityEnum = FlywheelSpeed.SHORT_RANGE
39+
var flywheelVelocityEnum = FlywheelSpeedRange.SHORT_RANGE
3940
var shootState = ShootState.READY
40-
var targetGoal = Pose()
41+
var targetGoal = GoalTag.RED
4142
private var shootingAll = false
4243
private var remainingShots = 0
4344
var turretAngle = 0.0
45+
var flywheelSpeed = 0.0
4446

4547
fun update() {
48+
checkTargetGoal()
4649
updateFlywheelSpeed()
4750
handleFlywheel()
4851
handleTurret()
@@ -52,24 +55,39 @@ class TeleopDriver2(
5255
chrono.update() // Manual update to allow dt to match across the loop.
5356
}
5457

58+
private fun checkTargetGoal() {
59+
if (bot.allianceColor == AllianceColor.BLUE) {
60+
targetGoal = GoalTag.BLUE
61+
} else { return }
62+
}
63+
5564
private fun updateFlywheelSpeed() {
65+
isEstimateSpeed.toggle(gamepad.dpad_right)
5666
// if (flywheelSpeed < 1.0 && gamepad.dpad_up) {
5767
// flywheelSpeed += chrono.dt * 0.5
5868
// }
5969
// if (flywheelSpeed > 0.0 && gamepad.dpad_down) {
6070
// flywheelSpeed -= chrono.dt * 0.5
6171
// }
6272
// flywheelSpeed = flywheelSpeed.coerceIn(0.0, 1.0)
63-
changeFlywheelSpeedToggle.toggle(gamepad.dpad_up)
64-
if (changeFlywheelSpeedToggle.justChanged) {
73+
74+
if (isEstimateSpeed.state) {
75+
flywheelSpeed = bot.flywheel!!.estimateVelocity(targetGoal.shootingPose, bot.pinpoint?.pose ?: Pose())
76+
} else {
77+
flywheelSpeed = flywheelVelocityEnum.velocity
78+
}
79+
80+
changeFlywheelRangeToggle.toggle(gamepad.dpad_up)
81+
82+
if (changeFlywheelRangeToggle.justChanged) {
6583
flywheelVelocityEnum = flywheelVelocityEnum.next()
6684
}
6785
}
6886

6987
private fun handleFlywheel() {
7088
flywheelToggle.toggle(gamepad.dpad_left)
7189
if (flywheelToggle.state) {
72-
bot.flywheel?.velocity = flywheelVelocityEnum.velocity
90+
bot.flywheel?.velocity = flywheelSpeed
7391
} else {
7492
bot.flywheel?.velocity = 0.0
7593
}
@@ -174,20 +192,9 @@ class TeleopDriver2(
174192

175193
private fun handleAutoTrack() {
176194
if (bot.turret?.mode == Turret.Mode.AUTO_TRACK) {
177-
if (bot.allianceColor == AllianceColor.RED) {
178-
targetGoal = GoalTag.RED.pose
179-
} else {
180-
targetGoal = GoalTag.BLUE.pose
181-
}
182195
bot.turret?.autoTrack(
183196
bot.pinpoint?.pose ?: Pose(),
184-
if (bot.allianceColor ==
185-
AllianceColor.BLUE
186-
) {
187-
GoalTag.BLUE.shootingPose
188-
} else {
189-
GoalTag.RED.shootingPose
190-
},
197+
targetGoal.shootingPose,
191198
)
192199
}
193200
}

0 commit comments

Comments
 (0)