Skip to content

Commit

Permalink
docs: finish documenting hydrozoa
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Nov 28, 2024
1 parent ff1357a commit ddb2e87
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 53 deletions.
21 changes: 5 additions & 16 deletions docs.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
--block-font-family: var(--body-font-family);
--code-font-family: monospace;
/* Base font sizes for body and code elements */
--body-font-size: 14px;
--code-font-size: 14px;
--body-font-size: 16px;
--code-font-size: 16px;
/* Text colors for body and block elements */
--body-text-color: #353833;
--block-text-color: #474747;
Expand Down Expand Up @@ -921,12 +921,12 @@ li.ui-static-link a, li.ui-static-link a:visited {
.search-tag-desc-result {
font-style: italic;
font-size: 11px;
font-size: 16px;
}
.search-tag-holder-result {
font-style: italic;
font-size: 12px;
font-size: 16px;
}
.search-tag-result:target {
Expand Down Expand Up @@ -1525,7 +1525,6 @@ body {
background-color: #ffffff;
color: #141414;
font-family: 'Roboto', sans-serif;
font-size: 76%;
margin: 0;
}

Expand Down Expand Up @@ -1663,16 +1662,6 @@ section#field-summary, section#constructor-summary, section#method-summary, sect
margin-left: -10px;
}

section#field-detail, section#constructor-detail, section#method-detail,
#field-summary > div.caption,
#constructor-summary > div.caption,
#nested-class-summary > div.caption,
#all-packages-table > div.caption > span,
#class > div.caption > span,
#method > div.caption > span {
display: none;
}

#field-summary > h2, #constructor-summary > h2, #method-summary > h2, #nested-class-summary > h2 {
font-style: normal;
}
Expand All @@ -1684,7 +1673,7 @@ body.class-declaration-page .summary .inherited-list h2 {
background: none;
border: none;
font-weight: normal;
font-size: 15px;
font-size: 16px;

}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/dev/vexide/hydrozoa/CompetitionRobot.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,77 @@
package dev.vexide.hydrozoa;

import java.util.function.Function;

/**
* A robot that can be used in a competition. Classes implementing this interface can be used with the competition
* runtime to control what the robot does in different states.
*
* @see CompetitionRuntime#start(Function)
*/
public interface CompetitionRobot {
/**
* Called when the robot is connected to the field.
*/
default void connected() {
}

/**
* Called when the robot is disconnected from the field.
*/
default void disconnected() {
}

/**
* Called when the robot switches to the disabled mode.
*/
default void disabledInit() {
}

/**
* Called periodically while the robot is disabled.
*/
default void disabledPeriodic() {
}

/**
* Called when the robot exits the disabled mode.
*/
default void disabledExit() {
}

/**
* Called when the robot switches to the autonomous mode.
*/
default void autonomousInit() {
}

/**
* Called periodically while the robot is in the autonomous mode.
*/
default void autonomousPeriodic() {
}

/**
* Called when the robot exits the autonomous mode.
*/
default void autonomousExit() {
}

/**
* Called when the robot switches to the driver mode.
*/
default void driverInit() {
}

/**
* Called periodically while the robot is in the driver mode.
*/
default void driverPeriodic() {
}

/**
* Called when the robot exits the driver mode.
*/
default void driverExit() {
}
}
102 changes: 93 additions & 9 deletions src/main/java/dev/vexide/hydrozoa/CompetitionRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,47 @@
import java.util.Optional;
import java.util.function.Function;

/**
* A runtime environment which manages the competition state and robot lifecycle.
*/
public final class CompetitionRuntime {
private CompetitionRuntime() {
}

/**
* The default interval at which the robot will run its periodic methods.
*/
public static final Duration DEFAULT_PERIOD = Duration.ofMillis(2);

private static @Nullable Boolean wasConnected = null;
private static @Nullable Mode previousMode = null;

/**
* The rate at which this robot will run its periodic methods.
*/
public static @NotNull Duration period = DEFAULT_PERIOD;

/**
* Asserts that this robot is enabled.
*
* @throws DeviceException if this robot is disabled
*/
public static void assertRobotEnabled() throws DeviceException {
if (!mode().equals(Mode.Driver)) {
if (mode().equals(Mode.Disabled)) {
throw new RobotDisabledException();
}
}

/**
* Starts the competition runtime, using the given function to create the robot instance.
* @param factory a function which creates a robot instance from the peripherals
* @throws IllegalStateException if the peripherals have already been taken or this method has already been called
*/
public static void start(@NotNull Function<@NotNull Peripherals, @NotNull CompetitionRobot> factory) throws IllegalStateException {
try {
var robot = factory.apply(Peripherals.take().orElseThrow(() -> new IllegalStateException("Peripherals already taken")));

//noinspection InfiniteLoopStatement
while (true) {
var begin = Instant.now();

Expand Down Expand Up @@ -79,30 +101,81 @@ public static void start(@NotNull Function<@NotNull Peripherals, @NotNull Compet
}
}

/**
* Gets the current competition status.
* @return the competition status
*/
@Contract(value = "-> new", pure = true)
public static @NotNull Status status() {
return Status.fromBitflags(VexSdk.vexCompetitionStatus());
}

/**
* Determines whether this robot is connected to a competition control system (either a competition switch or
* field control).
* @return {@code true} if the robot is connected, {@code false} otherwise
*/
@Contract(pure = true)
public static boolean connected() {
return status().connected();
}

/**
* Determines which competition system this robot is connected to.
* @return the competition system, or an empty optional if the robot is not connected to one
*/
@Contract(pure = true)
public static @NotNull Optional<CompetitionSystem> system() {
return status().competitionSystem();
}

/**
* Determines the current mode of the robot's competition - disabled, autonomous, or driver.
* @return the current mode
*/
public static @NotNull Mode mode() {
return status().mode();
}

/**
* A competition system which the robot can be connected to.
*/
public enum CompetitionSystem {
/**
* The robot is connected to a Smart Field Control system.
*/
FieldControl,
/**
* The robot is connected to a competition switch.
*/
CompetitionSwitch,
}

/**
* An enumeration of the possible competition modes.
*/
public enum Mode {
/**
* The robot is disabled by field control.
*/
Disabled,
/**
* The robot is in autonomous mode.
*/
Autonomous,
/**
* The robot is in driver control mode.
*/
Driver,
}

/**
* The status of the competition.
* @param disabled whether the robot is disabled by field control
* @param autonomous whether the robot is in autonomous mode
* @param connected whether the robot is connected to competition control
* @param system whether the robot is connected to field control
*/
public record Status(boolean disabled, boolean autonomous, boolean connected, boolean system) {
/**
* Robot is disabled by field control.
Expand All @@ -121,16 +194,29 @@ public record Status(boolean disabled, boolean autonomous, boolean connected, bo
*/
static final int SYSTEM = 1 << 3;

/**
* Creates a new competition status from bitflags.
* @param flags the bitflags representing the status
* @return the new competition status
*/
@Contract(value = "_ -> new", pure = true)
public static @NotNull Status fromBitflags(int flags) {
return new Status((flags & DISABLED) != 0, (flags & AUTONOMOUS) != 0, (flags & CONNECTED) != 0, (flags & SYSTEM) != 0);
}

/**
* Converts this competition status to bitflags.
* @return the bitflags representing the status
*/
@Contract(pure = true)
public static int toBitflags(@NotNull Status status) {
return (status.disabled() ? DISABLED : 0) | (status.autonomous() ? AUTONOMOUS : 0) | (status.connected() ? CONNECTED : 0) | (status.system() ? SYSTEM : 0);
public int toBitflags() {
return (disabled() ? DISABLED : 0) | (autonomous() ? AUTONOMOUS : 0) | (connected() ? CONNECTED : 0) | (system() ? SYSTEM : 0);
}

/**
* Determines the competition system that the robot is connected to.
* @return the competition system, or an empty optional if the robot is not connected
*/
@Contract(pure = true)
public @NotNull Optional<CompetitionSystem> competitionSystem() {
if (connected()) {
Expand All @@ -144,6 +230,10 @@ public static int toBitflags(@NotNull Status status) {
}
}

/**
* Determines the current mode of the robot's competition.
* @return the current mode
*/
@Contract(pure = true)
public @NotNull Mode mode() {
if (disabled()) {
Expand All @@ -155,10 +245,4 @@ public static int toBitflags(@NotNull Status status) {
}
}
}

public enum Mode {
Disabled,
Autonomous,
Driver,
}
}
Loading

0 comments on commit ddb2e87

Please sign in to comment.