Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Switch to palantir format because it handles lambdas better
Browse files Browse the repository at this point in the history
  • Loading branch information
rcahoon committed Dec 14, 2024
1 parent 1cea4ae commit 0224549
Show file tree
Hide file tree
Showing 102 changed files with 2,256 additions and 3,082 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ spotless {
exclude '**/build/**', '**/build-*/**'
}
toggleOffOn()
googleJavaFormat().formatJavadoc(false).aosp()
palantirJavaFormat('2.39.0').style('AOSP')
removeUnusedImports()
importOrder()
trimTrailingWhitespace()
endWithNewline()
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/team766/config/ConfigFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,10 @@ private static Object getRawValue(final JSONObject obj, final String key) {
JSONObject parentObj = getParent(obj, keyParts);
var rawValue = parentObj.opt(keyParts[keyParts.length - 1]);
if (rawValue instanceof JSONObject) {
throw new IllegalArgumentException(
"The config file cannot store both a single config "
+ "setting and a group of config settings with the name "
+ key
+ " Please pick a different name");
throw new IllegalArgumentException("The config file cannot store both a single config "
+ "setting and a group of config settings with the name "
+ key
+ " Please pick a different name");
}
if (rawValue == null) {
parentObj.put(keyParts[keyParts.length - 1], JSONObject.NULL);
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/team766/config/ConfigValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ public E parseJsonValue(final Object configValue) {
return each;
}
}
throw new IllegalArgumentException(
"Unrecognized enum value: "
+ enumName
+ "; values are "
+ Arrays.stream(enumClass.getEnumConstants())
.map(e -> e.name())
.collect(Collectors.joining(", ")));
throw new IllegalArgumentException("Unrecognized enum value: "
+ enumName
+ "; values are "
+ Arrays.stream(enumClass.getEnumConstants())
.map(e -> e.name())
.collect(Collectors.joining(", ")));
}
}
26 changes: 12 additions & 14 deletions src/main/java/com/team766/controllers/PIDController.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,10 @@ public void calculate(final double cur_input) {
total_error = -1 / ki;
}

double out =
Kp.valueOr(0.0) * cur_error
+ Ki.valueOr(0.0) * total_error
+ Kd.valueOr(0.0) * error_rate
+ Kff.valueOr(0.0) * setpoint;
double out = Kp.valueOr(0.0) * cur_error
+ Ki.valueOr(0.0) * total_error
+ Kd.valueOr(0.0) * error_rate
+ Kff.valueOr(0.0) * setpoint;
prev_error = cur_error;

pr("Pre-clip output: " + out);
Expand All @@ -287,15 +286,14 @@ public void calculate(final double cur_input) {

lastTime = timeProvider.get();

pr(
" Total Error: "
+ total_error
+ " Current Error: "
+ cur_error
+ " Output: "
+ output_value
+ " Setpoint: "
+ setpoint);
pr(" Total Error: "
+ total_error
+ " Current Error: "
+ cur_error
+ " Output: "
+ output_value
+ " Setpoint: "
+ setpoint);
}

public double getOutput() {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/team766/framework/AdvancedUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ private static void checkProcedureReservationsDisjoint(
final var thisReservations = callingContext.getRequirements();
for (var req : procedure.reservations()) {
if (thisReservations.contains(req)) {
throw new IllegalArgumentException(
callingContext.getName()
+ " tried to launch "
+ procedure.getName()
+ " asynchronously, but both have a reservation on "
+ req.getName());
throw new IllegalArgumentException(callingContext.getName()
+ " tried to launch "
+ procedure.getName()
+ " asynchronously, but both have a reservation on "
+ req.getName());
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/team766/framework/Conditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ public class Conditions {
*/
public static <T> T waitForValue(Context context, Supplier<Optional<T>> supplier) {
final AtomicReference<T> result = new AtomicReference<>();
context.waitFor(
() -> {
final var t = supplier.get();
t.ifPresent(result::set);
return t.isPresent();
});
context.waitFor(() -> {
final var t = supplier.get();
t.ifPresent(result::set);
return t.isPresent();
});
return result.get();
}

Expand Down
36 changes: 15 additions & 21 deletions src/main/java/com/team766/framework/ContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,12 @@ public String getStackTrace() {
*/
private String getExecutionPoint() {
StackWalker walker = StackWalker.getInstance();
return walker.walk(
s ->
s.dropWhile(f -> f.getClassName() != ContextImpl.this.getClass().getName())
.filter(
f ->
f.getClassName()
!= ContextImpl.this.getClass().getName())
.findFirst()
.map(StackFrame::toString)
.orElse(null));
return walker.walk(s -> s.dropWhile(
f -> f.getClassName() != ContextImpl.this.getClass().getName())
.filter(f -> f.getClassName() != ContextImpl.this.getClass().getName())
.findFirst()
.map(StackFrame::toString)
.orElse(null));
}

/**
Expand Down Expand Up @@ -253,11 +249,10 @@ private void transferControl(final ControlOwner thisOwner, final ControlOwner de
// Make sure we currently have the baton before trying to give it to
// someone else.
if (m_controlOwner != thisOwner) {
throw new IllegalStateException(
"Subroutine had control owner "
+ m_controlOwner
+ " but assumed control owner "
+ thisOwner);
throw new IllegalStateException("Subroutine had control owner "
+ m_controlOwner
+ " but assumed control owner "
+ thisOwner);
}
// Pass the baton.
m_controlOwner = desiredOwner;
Expand Down Expand Up @@ -358,12 +353,11 @@ private void checkProcedureReservationsSubset(Procedure procedure) {
final var thisReservations = getRequirements();
for (var req : procedure.reservations()) {
if (!thisReservations.contains(req)) {
throw new IllegalArgumentException(
getName()
+ " tried to run "
+ procedure.getName()
+ " but is missing the reservation on "
+ req.getName());
throw new IllegalArgumentException(getName()
+ " tried to run "
+ procedure.getName()
+ " but is missing the reservation on "
+ req.getName());
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/team766/framework/Mechanism.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ protected void checkContextReservation() {
}
if (superstructure != null) {
if (!superstructure.isRunningPeriodic()) {
var exception =
new IllegalStateException(
this.getName()
+ " is part of a superstructure but was used by something outside the superstructure");
var exception = new IllegalStateException(
this.getName()
+ " is part of a superstructure but was used by something outside the superstructure");
Logger.get(Category.FRAMEWORK)
.logRaw(
Severity.ERROR,
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/team766/framework/ReservingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public final class ReservingCommand extends WrapperCommand {
*/
public static void checkCurrentCommandHasReservation(Subsystem subsystem) {
if (!currentCommandHasReservation(subsystem)) {
var exception =
new IllegalStateException(
subsystem.getName() + " tried to be used without reserving it");
var exception = new IllegalStateException(
subsystem.getName() + " tried to be used without reserving it");
Logger.get(Category.FRAMEWORK)
.logRaw(
Severity.ERROR,
Expand Down Expand Up @@ -86,12 +85,11 @@ private static void checkProcedureReservationsSubset(Command parent, Command chi
final var thisReservations = parent.getRequirements();
for (var req : child.getRequirements()) {
if (!thisReservations.contains(req)) {
throw new IllegalArgumentException(
parent.getName()
+ " tried to run "
+ child.getName()
+ " but is missing the reservation on "
+ req.getName());
throw new IllegalArgumentException(parent.getName()
+ " tried to run "
+ child.getName()
+ " but is missing the reservation on "
+ req.getName());
}
}
}
Expand Down
104 changes: 45 additions & 59 deletions src/main/java/com/team766/framework/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,29 @@ private void applyRulePersistence(
this.cancellationOnFinish = Cancellation.DO_NOT_CANCEL;
}
case ONCE_AND_HOLD -> {
this.onTriggeringProcedure =
() -> {
final Procedure procedure = action.get();
return new FunctionalProcedure(
procedure.getName(),
procedure.reservations(),
context -> {
procedure.run(context);
context.waitFor(() -> false);
});
};
this.onTriggeringProcedure = () -> {
final Procedure procedure = action.get();
return new FunctionalProcedure(
procedure.getName(), procedure.reservations(), context -> {
procedure.run(context);
context.waitFor(() -> false);
});
};
this.cancellationOnFinish = Cancellation.CANCEL_NEWLY_ACTION;
}
case REPEATEDLY -> {
this.onTriggeringProcedure =
() -> {
final Procedure procedure = action.get();
return new FunctionalProcedure(
procedure.getName(),
procedure.reservations(),
context -> {
Procedure currentProcedure = procedure;
while (true) {
context.runSync(currentProcedure);
context.yield();
currentProcedure = action.get();
}
});
};
this.onTriggeringProcedure = () -> {
final Procedure procedure = action.get();
return new FunctionalProcedure(
procedure.getName(), procedure.reservations(), context -> {
Procedure currentProcedure = procedure;
while (true) {
context.runSync(currentProcedure);
context.yield();
currentProcedure = action.get();
}
});
};
this.cancellationOnFinish = Cancellation.CANCEL_NEWLY_ACTION;
}
}
Expand Down Expand Up @@ -184,31 +178,27 @@ public Builder whenNotTriggering(Rule.Builder... rules) {
private List<Rule> build(BooleanSupplier parentPredicate) {
final var rules = new ArrayList<Rule>();

final BooleanSupplier fullPredicate =
parentPredicate == null
? predicate
: () -> parentPredicate.getAsBoolean() && predicate.getAsBoolean();
final var thisRule =
new Rule(
name,
fullPredicate,
onTriggeringProcedure,
cancellationOnFinish,
finishedTriggeringProcedure);
final BooleanSupplier fullPredicate = parentPredicate == null
? predicate
: () -> parentPredicate.getAsBoolean() && predicate.getAsBoolean();
final var thisRule = new Rule(
name,
fullPredicate,
onTriggeringProcedure,
cancellationOnFinish,
finishedTriggeringProcedure);
rules.add(thisRule);

// Important! These composed predicates shouldn't invoke `predicate`. `predicate` should
// only be invoked once per call to RuleEngine.run(), so having all rules in the
// hierarchy call it would not work as expected. Instead, we have the child rules query
// the triggering state of the parent rule.
final BooleanSupplier composedPredicate =
parentPredicate == null
? () -> thisRule.isTriggering()
: () -> parentPredicate.getAsBoolean() && thisRule.isTriggering();
final BooleanSupplier negativeComposedPredicate =
parentPredicate == null
? () -> !thisRule.isTriggering()
: () -> parentPredicate.getAsBoolean() && !thisRule.isTriggering();
final BooleanSupplier composedPredicate = parentPredicate == null
? () -> thisRule.isTriggering()
: () -> parentPredicate.getAsBoolean() && thisRule.isTriggering();
final BooleanSupplier negativeComposedPredicate = parentPredicate == null
? () -> !thisRule.isTriggering()
: () -> parentPredicate.getAsBoolean() && !thisRule.isTriggering();
for (var r : composedRules) {
rules.addAll(r.build(composedPredicate));
}
Expand Down Expand Up @@ -297,21 +287,17 @@ public String getName() {

/* package */ void evaluate() {
if (predicate.getAsBoolean()) {
currentTriggerType =
switch (currentTriggerType) {
case NONE -> TriggerType.NEWLY;
case NEWLY -> TriggerType.CONTINUING;
case CONTINUING -> TriggerType.CONTINUING;
case FINISHED -> TriggerType.NEWLY;
};
currentTriggerType = switch (currentTriggerType) {
case NONE -> TriggerType.NEWLY;
case NEWLY -> TriggerType.CONTINUING;
case CONTINUING -> TriggerType.CONTINUING;
case FINISHED -> TriggerType.NEWLY;};
} else {
currentTriggerType =
switch (currentTriggerType) {
case NONE -> TriggerType.NONE;
case NEWLY -> TriggerType.FINISHED;
case CONTINUING -> TriggerType.FINISHED;
case FINISHED -> TriggerType.NONE;
};
currentTriggerType = switch (currentTriggerType) {
case NONE -> TriggerType.NONE;
case NEWLY -> TriggerType.FINISHED;
case CONTINUING -> TriggerType.FINISHED;
case FINISHED -> TriggerType.NONE;};
}
}

Expand Down
20 changes: 7 additions & 13 deletions src/main/java/com/team766/framework/SchedulerMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ public class SchedulerMonitor {

public static void start() {
if (c_thread != null) {
CommandScheduler.getInstance()
.onCommandExecute(
__ -> {
++c_iterationCount;
});
CommandScheduler.getInstance().onCommandExecute(__ -> {
++c_iterationCount;
});
c_thread = new Thread(SchedulerMonitor::monitor);
c_thread.setDaemon(true);
c_thread.start();
Expand All @@ -38,14 +36,10 @@ private static void monitor() {
"The code has gotten stuck. You probably have an unintended infinite "
+ "loop or need to add a call to context.yield() in a Procedure.\n"
+ Thread.getAllStackTraces().entrySet().stream()
.map(
e ->
e.getKey().getName()
+ ":\n"
+ StackTraceUtils
.getStackTrace(
e
.getValue()))
.map(e -> e.getKey().getName()
+ ":\n"
+ StackTraceUtils.getStackTrace(
e.getValue()))
.collect(Collectors.joining("\n")));
}

Expand Down
Loading

0 comments on commit 0224549

Please sign in to comment.