|
1 | 1 | package pioneer.opmodes.other |
2 | 2 |
|
3 | 3 | import com.qualcomm.robotcore.eventloop.opmode.TeleOp |
4 | | -import pioneer.BotType |
| 4 | +import org.firstinspires.ftc.vision.apriltag.AprilTagProcessor |
| 5 | +import pioneer.Bot |
| 6 | +import pioneer.hardware.Camera |
| 7 | +import pioneer.localization.localizers.Pinpoint |
5 | 8 | import pioneer.opmodes.BaseOpMode |
| 9 | +import kotlin.math.* |
6 | 10 |
|
7 | 11 | @TeleOp(name = "April Tags Test") |
8 | | -class AprilTagsTest : BaseOpMode(BotType.GOBILDA_STARTER_BOT) { |
| 12 | +class AprilTagsTest : BaseOpMode() { |
| 13 | + private val processor : AprilTagProcessor = Camera.createAprilTagProcessor() |
| 14 | + |
9 | 15 | override fun onInit() { |
| 16 | + bot = Bot.builder() |
| 17 | + .add(Pinpoint(hardwareMap)) |
| 18 | + .add(Camera(hardwareMap, processors = arrayOf(processor))) |
| 19 | + .build() |
10 | 20 | } |
11 | 21 |
|
12 | 22 | override fun onLoop() { |
13 | 23 | calculateAprilTag() |
14 | 24 | addAprilTagTelemetryData() |
| 25 | + fieldPosition() |
15 | 26 | } |
16 | 27 |
|
17 | 28 | private fun fieldPosition() { |
18 | | - val detections = bot.aprilTagProcessor.detections |
| 29 | + val detections = processor.detections |
19 | 30 | //TODO: Avg position if given multiple tags? |
20 | 31 | for (detection in detections) { |
21 | | - var tagPosition = listOf(detection.metadata.fieldPosition[0], detection.metadata.fieldPosition[1], detection.metadata.fieldPosition[2]) |
22 | | - var fieldPositionWithTag = listOf((tagPosition[0]+detection.ftcPose.x).toFloat(), (tagPosition[1]+detection.ftcPose.y).toFloat(), (tagPosition[1]+detection.ftcPose.z).toFloat()) |
| 32 | + val tagPosition = listOf(detection.metadata.fieldPosition[0], detection.metadata.fieldPosition[1], detection.metadata.fieldPosition[2]) |
| 33 | + val fieldPositionWithTag = listOf((tagPosition[0]+detection.ftcPose.x).toFloat(), (tagPosition[1]+detection.ftcPose.y).toFloat(), (tagPosition[1]+detection.ftcPose.z).toFloat()) |
23 | 34 |
|
24 | 35 | telemetry.addLine("--Field Position From Tag (x, y, z): (%.2f, %.2f, %.2f)".format(fieldPositionWithTag[0], fieldPositionWithTag[1], fieldPositionWithTag[2])) |
25 | | - telemetry.addLine("--Bot Position (x, y): (%.2f, %.2f)".format(bot.localizer.pose.x, bot.localizer.pose.y)) |
| 36 | + telemetry.addLine("--Bot Position (x, y): (%.2f, %.2f)".format(bot.pinpoint?.pose?.x, bot.pinpoint?.pose?.y)) |
26 | 37 |
|
27 | 38 | } |
28 | 39 | } |
29 | 40 | @Deprecated("ts sucks just use the library") |
30 | 41 | private fun calculateAprilTag() { |
31 | | - val detections = bot.aprilTagProcessor.detections |
| 42 | + val detections = processor.detections |
32 | 43 | for (detection in detections) { |
33 | | - if (detection != null && detection.ftcPose != null) { |
34 | | - var rho = detection.ftcPose.range |
35 | | - var phi = detection.ftcPose.elevation |
36 | | - var theta = detection.ftcPose.bearing |
| 44 | + if (detection?.ftcPose != null) { |
| 45 | + val rho = detection.ftcPose.range |
| 46 | + val phi = detection.ftcPose.elevation |
| 47 | + val theta = detection.ftcPose.bearing |
37 | 48 |
|
38 | | - var height = Math.sin(phi) * rho |
39 | | - var hypotenuseXY = height / Math.tan(phi) |
40 | | - var dX = Math.sin(theta) * hypotenuseXY |
41 | | - var dY = Math.cos(theta) * hypotenuseXY |
| 49 | + val height = sin(phi) * rho |
| 50 | + val hypotenuseXY = height / tan(phi) |
| 51 | + val dX = sin(theta) * hypotenuseXY |
| 52 | + val dY = cos(theta) * hypotenuseXY |
42 | 53 |
|
43 | 54 | telemetry.addLine("--Calculated Rel (x, y, z): (%.2f, %.2f,%.2f)".format(dX, dY, height)) |
44 | 55 | } |
45 | 56 | } |
46 | 57 | } |
47 | 58 |
|
48 | 59 | private fun addAprilTagTelemetryData() { |
49 | | - val detections = bot.aprilTagProcessor.getDetections() |
| 60 | + val detections = processor.detections |
50 | 61 | for (detection in detections) { |
51 | 62 | // Check if tag or its properties are null to avoid null pointer exceptions |
52 | | - if (detection != null && detection.ftcPose != null) { |
| 63 | + if (detection?.ftcPose != null) { |
53 | 64 | telemetry.addData("Detection", detection.id) |
54 | 65 | telemetry.addLine( |
55 | 66 | "--Rel (x, y, z): (%.2f, %.2f, %.2f)".format( |
|
0 commit comments