Skip to content

Commit

Permalink
Resolve "Add test play for stopping ball"
Browse files Browse the repository at this point in the history
Closes #1981

See merge request main/Sumatra!1867

sumatra-commit: 2cb92f480bec69c7f99579704906037b671e38f3
  • Loading branch information
mickmack1213 authored and TIGERs GitLab committed Jul 7, 2024
1 parent 33c849f commit 5da84e3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import edu.tigers.sumatra.math.vector.IVector2;
import edu.tigers.sumatra.math.vector.IVector3;
import edu.tigers.sumatra.math.vector.Vector2;
import edu.tigers.sumatra.math.vector.Vector3;
import edu.tigers.sumatra.math.vector.Vector3f;
import lombok.Data;
import lombok.NonNull;
Expand Down Expand Up @@ -66,6 +67,17 @@ public static Pose nan()
}


public static Pose valueOf(String value)
{
IVector3 pose = Vector3.valueOf(value);
if (pose == null)
{
return null;
}
return Pose.from(pose);
}


@Override
public Pose mirrored()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2009 - 2020, DHBW Mannheim - TIGERs Mannheim
*/

package edu.tigers.sumatra.math.pose;

import com.github.g3force.s2vconverter.IString2ValueConverter;
import edu.tigers.sumatra.math.vector.Vector3;
import lombok.extern.log4j.Log4j2;


@Log4j2
public class PoseConverter implements IString2ValueConverter
{
@Override
public boolean supportedClass(final Class<?> impl)
{
return impl.equals(Pose.class);
}


@Override
public Object parseString(final Class<?> impl, final String value)
{
if (impl.equals(Pose.class))
{
return Pose.valueOf(value);
}
return null;
}


@Override
public String toString(final Class<?> impl, final Object value)
{
Pose pose = (Pose) value;
return Vector3.from2d(pose.getPos(), pose.getOrientation()).getSaveableString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# Copyright (c) 2009 - 2017, DHBW Mannheim - TIGERs Mannheim
#

edu.tigers.sumatra.math.vector.VectorConverter
edu.tigers.sumatra.math.vector.VectorConverter
edu.tigers.sumatra.math.pose.PoseConverter
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

package edu.tigers.sumatra.time;

import java.util.function.BooleanSupplier;


/**
* A timestamp-based timer that can check if a predefined duration has running out
*
Expand All @@ -13,42 +16,42 @@ public class TimestampTimer
{
private long tStart = 0;
private long duration = 0;


/**
* Create a new timer
*
*
* @param duration the duration of this timer [s]
*/
public TimestampTimer(final double duration)
{
setDuration(duration);
}


/**
* Reset the timer. Time is not running afterwards, until {@link TimestampTimer#start(long)} is called.
*/
public void reset()
{
tStart = 0;
}


/**
* Start the timer with given start time
*
*
* @param tStart the starting timestamp [ns]
*/
public void start(long tStart)
{
this.tStart = tStart;
}


/**
* Start timer if not already started
*
*
* @param tStart the start timestamp
*/
public void update(long tStart)
Expand All @@ -58,29 +61,29 @@ public void update(long tStart)
this.tStart = tStart;
}
}


/**
* @param duration the duration in [s] after which time is up
*/
public void setDuration(double duration)
{
this.duration = (long) (duration * 1e9);
}


/**
* Check if time is up
*
*
* @param curTimestamp the current time as timestamp [ns]
* @return true, if curTimestamp is newer than tStart + duration
*/
public boolean isTimeUp(long curTimestamp)
{
return tStart != 0 && (curTimestamp - tStart) > duration;
}


/**
* @param curTimestamp current timestamp
* @return the time from tStart to now in [s]
Expand All @@ -93,8 +96,8 @@ public double getCurrentTime(long curTimestamp)
}
return (curTimestamp - tStart) / 1e9;
}


/**
* @param curTimestamp current timestamp
* @return the remaining time of this timer
Expand All @@ -103,19 +106,54 @@ public double getRemainingTime(long curTimestamp)
{
return getDuration() - getCurrentTime(curTimestamp);
}


public double getDuration()
{
return duration / 1e9;
}


/**
* @return true, if the timer is running (not reset)
*/
public boolean isRunning()
{
return tStart != 0;
}


public boolean isTimeUpWithCondition(
long timestamp,
BooleanSupplier condition
)
{
if (condition.getAsBoolean())
{
update(timestamp);
return isTimeUp(timestamp);
}
reset();
return false;
}


public void whenConditionIsMet(
long timestamp,
BooleanSupplier condition,
Runnable runnable
)
{
if (condition.getAsBoolean())
{
update(timestamp);
if (isTimeUp(timestamp))
{
runnable.run();
}
} else
{
reset();
}
}
}

0 comments on commit 5da84e3

Please sign in to comment.