use of me.wobblyyyy.pathfinder2.time.ElapsedTimer in project Pathfinder2 by Wobblyyyy.
the class TestTaskTrajectory method testMaxTimeTaskTrajectory.
@Test
public void testMaxTimeTaskTrajectory() {
ElapsedTimer timer = new ElapsedTimer(true);
Trajectory trajectory = new TaskTrajectoryBuilder().setMaxTimeMs(10).setIsFinished(() -> false).build();
testTrajectory(trajectory, pathfinder.getPosition(), 100);
Assertions.assertTrue(timer.elapsedMs() < 10);
}
use of me.wobblyyyy.pathfinder2.time.ElapsedTimer in project Pathfinder2 by Wobblyyyy.
the class Pathfinder method tickUntil.
/**
* Tick Pathfinder according to a {@code TickConfig}.
*
* @param config the ticking configuration to use.
* @return {@code this}, used for method chaining.
*/
public Pathfinder tickUntil(TickConfig config) {
double delayMs = config.getDelayMs();
if (delayMs > 0) {
ElapsedTimer delayTimer = new ElapsedTimer(true);
while (delayTimer.elapsedMs() <= delayMs) ;
}
double timeoutMs = config.getTimeoutMs();
ElapsedTimer timer = new ElapsedTimer(true);
List<Supplier<Boolean>> shouldContinueRunning = config.getShouldContinueRunning();
List<Supplier<Boolean>> shouldStopRunning = config.getShouldStopRunning();
List<Runnable> onTick = config.getOnTick();
List<Runnable> onFinish = config.getOnFinish();
while (timer.elapsedMs() <= timeoutMs) {
for (Supplier<Boolean> supplier : shouldContinueRunning) {
if (!supplier.get()) {
break;
}
}
for (Supplier<Boolean> supplier : shouldStopRunning) {
if (supplier.get()) {
break;
}
}
tick();
for (Runnable runnable : onTick) {
runnable.run();
}
}
for (Runnable runnable : onFinish) {
runnable.run();
}
return this;
}
use of me.wobblyyyy.pathfinder2.time.ElapsedTimer in project Pathfinder2 by Wobblyyyy.
the class TestTaskTrajectory method testMinTimeTaskTrajectory.
@Test
public void testMinTimeTaskTrajectory() {
ElapsedTimer timer = new ElapsedTimer(true);
Trajectory trajectory = new TaskTrajectoryBuilder().setMinTimeMs(10).setIsFinished(() -> timer.elapsedMs() > 10).build();
testTrajectory(trajectory, pathfinder.getPosition(), 100);
Assertions.assertTrue(timer.elapsedMs() > 10);
}
use of me.wobblyyyy.pathfinder2.time.ElapsedTimer in project Pathfinder2 by Wobblyyyy.
the class GenericTrajectoryTester method follow.
public void follow(Trajectory trajectory, PointXYZ point) {
// this is pretty disgusting code, but it gets the job done
// basically just spawn a new monitor thread to ensure that it doesn't
// take over a certain amount of time to execute the trajectory. if it
// does, something's broken, so the test should fail
pathfinder.followTrajectory(trajectory);
ElapsedTimer timer = new ElapsedTimer();
AtomicBoolean hasNotExpired = new AtomicBoolean(true);
AtomicBoolean hasExecuted = new AtomicBoolean(false);
Thread monitor = new Thread(() -> {
timer.start();
try {
while (!hasExecuted.get()) Thread.sleep(1);
while (timer.elapsedMs() < maximumExecutionTimeMs) {
Thread.sleep(1);
if (!pathfinder.isActive())
break;
}
if (pathfinder.isActive())
hasNotExpired.set(false);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
monitor.start();
pathfinder.tickUntil(hasNotExpired::get, pf -> hasExecuted.set(true));
if (!hasNotExpired.get())
throw new RuntimeException(StringUtils.format("Trajectory <%s> (target <%s>) took more than %s " + "milliseconds to execute, meaning something " + "went wrong with following the trajectory!", trajectory, point, maximumExecutionTimeMs));
assertPositionIs(point);
}
use of me.wobblyyyy.pathfinder2.time.ElapsedTimer in project Pathfinder2 by Wobblyyyy.
the class Trajectory method withTimeLimits.
/**
* Create a wrapper trajectory around this trajectory that behaves exactly
* the same as {@code this} trajectory, but with time limits. The
* trajectory will only execute if it has not exceeded
* {@code maximumTimeMs}. At the very least, this will execute until
* {@code minimumTimeMs} has elapsed.
*
* @param minimumTimeMs the minimum amount of time the trajectory may
* execute for, in milliseconds.
* @param maximumTimeMs the maximum amount of time the trajectory may
* execute for, in milliseconds.
* @return a wrapper for {@code this} trajectory.
*/
default Trajectory withTimeLimits(double minimumTimeMs, double maximumTimeMs) {
Function<PointXYZ, PointXYZ> nextMarkerFunction = InternalTrajectoryUtils.nextMarkerFunction(this);
Function<PointXYZ, Boolean> isDoneFunction = InternalTrajectoryUtils.isDoneFunction(this);
Function<PointXYZ, Double> speedFunction = InternalTrajectoryUtils.speedFunction(this);
Supplier<String> _toString = this::toString;
return new Trajectory() {
final ElapsedTimer timer = new ElapsedTimer();
@Override
public PointXYZ nextMarker(PointXYZ current) {
if (!timer.hasStarted())
timer.start();
return nextMarkerFunction.apply(current);
}
@Override
public boolean isDone(PointXYZ current) {
double elapsedMs = timer.elapsedMs();
if (elapsedMs < minimumTimeMs)
return true;
else if (elapsedMs < maximumTimeMs)
return isDoneFunction.apply(current);
else
return (elapsedMs > maximumTimeMs);
}
@Override
public double speed(PointXYZ current) {
return speedFunction.apply(current);
}
@Override
public String toString() {
return _toString.get();
}
};
}
Aggregations