RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
POVDirection.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.driverstation;
6
7import java.util.Optional;
8import org.wpilib.math.geometry.Rotation2d;
9import org.wpilib.system.Timer;
10
12public enum POVDirection {
14 CENTER(0x00),
16 UP(0x01),
18 UP_RIGHT(0x01 | 0x02),
20 RIGHT(0x02),
22 DOWN_RIGHT(0x02 | 0x04),
24 DOWN(0x04),
26 DOWN_LEFT(0x04 | 0x08),
28 LEFT(0x08),
30 UP_LEFT(0x01 | 0x08);
31
32 private static final double INVALID_POV_VALUE_INTERVAL = 1.0;
33 private static double s_nextMessageTime;
34
42 public static POVDirection of(byte value) {
43 for (var direction : values()) {
44 if (direction.value == value) {
45 return direction;
46 }
47 }
48 double currentTime = Timer.getTimestamp();
49 if (currentTime > s_nextMessageTime) {
50 DriverStationErrors.reportError("Invalid POV value " + value + "!", false);
51 s_nextMessageTime = currentTime + INVALID_POV_VALUE_INTERVAL;
52 }
53 return CENTER;
54 }
55
57 public final byte value;
58
59 POVDirection(int value) {
60 this.value = (byte) value;
61 }
62
69 public Optional<Rotation2d> getAngle() {
70 return switch (this) {
71 case CENTER -> Optional.empty();
72 case UP -> Optional.of(Rotation2d.fromDegrees(0));
73 case UP_RIGHT -> Optional.of(Rotation2d.fromDegrees(45));
74 case RIGHT -> Optional.of(Rotation2d.fromDegrees(90));
75 case DOWN_RIGHT -> Optional.of(Rotation2d.fromDegrees(135));
76 case DOWN -> Optional.of(Rotation2d.fromDegrees(180));
77 case DOWN_LEFT -> Optional.of(Rotation2d.fromDegrees(225));
78 case LEFT -> Optional.of(Rotation2d.fromDegrees(270));
79 case UP_LEFT -> Optional.of(Rotation2d.fromDegrees(315));
80 };
81 }
82}
static void reportError(String error, boolean printTrace)
static Rotation2d fromDegrees(double degrees)
static double getTimestamp()
Definition Timer.java:26
Optional< Rotation2d > getAngle()
static POVDirection of(byte value)