Unverified Commit 9347e39d authored by MarkG's avatar MarkG Committed by NicolaiO
Browse files

Resolve "Integrate grSim"

Closes #1573

See merge request main/Sumatra!1321

sumatra-commit: ed2ce72ad564b5a3a147b77389ec2df983b331c5
parent aa700062
Pipeline #8590 failed with stage
in 1 minute and 19 seconds
/*
* Copyright (c) 2009 - 2018, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.bot;
import com.sleepycat.persist.model.Persistent;
import edu.tigers.sumatra.ids.BotID;
import edu.tigers.sumatra.math.pose.Pose;
import edu.tigers.sumatra.math.vector.Vector3f;
@Persistent
public class BotState extends State
{
private final BotID botID;
@SuppressWarnings("unused") // berkeley
private BotState()
{
super();
botID = BotID.noBot();
}
private BotState(final BotID botID, final State state)
{
super(state.getPose(), state.getVel3());
this.botID = botID;
}
public static BotState of(final BotID botID, final State state)
{
return new BotState(botID, state);
}
public static BotState nan()
{
return BotState.of(BotID.noBot(), State.of(Pose.nan(), Vector3f.nan()));
}
@Override
public BotState mirrored()
{
return BotState.of(botID, super.mirrored());
}
public BotState interpolate(final BotState state, double percentage)
{
return BotState.of(botID, super.interpolate(state, percentage));
}
public BotID getBotID()
{
return botID;
......
/*
* Copyright (c) 2009 - 2017, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.bot;
......@@ -11,4 +11,5 @@ public enum EBotType
UNKNOWN,
TIGERS,
SUMATRA,
SIMULATOR,
}
/*
* Copyright (c) 2009 - 2020, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.bot;
......@@ -62,6 +62,12 @@ public class State implements IMirrorable<State>, IExportable, IInterpolatable<S
}
public static State nan()
{
return State.of(Pose.nan(), Vector3f.nan());
}
@Override
public State mirrored()
{
......
/*
* *********************************************************
* Copyright (c) 2009 - 2013, DHBW Mannheim - Tigers Mannheim
* Project: TIGERS - Sumatra
* Date: Jan 11, 2013
* Author(s): Nicolai Ommer <nicolai.ommer@gmail.com>
* *********************************************************
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.view;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.EnumMap;
import java.util.Map;
import net.miginfocom.swing.MigLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.EnumMap;
import java.util.Map;
/**
* Panel for displaying FPS of Worldframe and AIInfoFrame
*
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/
public class FpsPanel extends JPanel
{
private static final long serialVersionUID = -4915659461230793676L;
private Map<EFpsType, JLabel> labelMap = new EnumMap<>(EFpsType.class);
/**
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/
private static final long serialVersionUID = -4915659461230793676L;
private static final int LABEL_WIDTH = 60;
private final Map<EFpsType, JLabel> labelMap = new EnumMap<>(EFpsType.class);
public enum EFpsType
{
/** */
CAM("Cam: ", "Camera FPS"),
/** */
WP("WP: ", "World Predictor FPS"),
/** */
AI_Y("AIY: ", "Yellow Team AI FPS"),
/** */
AI_B("AIB: ", "Blue Team AI FPS"), ;
final String prefix;
final String desc;
AI_B("AIB: ", "Blue Team AI FPS"),
;
final String prefix;
final String desc;
EFpsType(final String prefix, final String desc)
{
this.prefix = prefix;
this.desc = desc;
}
}
/**
* New FpsPanel
*/
public FpsPanel()
{
// --- border ---
final TitledBorder border = BorderFactory.createTitledBorder("fps");
setBorder(border);
setLayout(new MigLayout("fill, inset 0", "[]5[]5[]5[]5[]"));
int width = 60;
for (EFpsType ft : EFpsType.values())
{
JLabel lbl = new JLabel(ft.prefix + "-");
lbl.setToolTipText(ft.desc);
labelMap.put(ft, lbl);
add(lbl);
lbl.setMinimumSize(new Dimension(width, 0));
lbl.setMinimumSize(new Dimension(LABEL_WIDTH, 0));
}
}
/**
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
* @param type
* @param fps
*/
......@@ -94,11 +77,8 @@ public class FpsPanel extends JPanel
EventQueue.invokeLater(() -> lbl.setText(txt));
}
}
/**
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/
public void clearFps()
{
for (EFpsType ft : EFpsType.values())
......
/*
* Copyright (c) 2009 - 2020, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.math.circle;
......@@ -60,7 +60,7 @@ public class Circle extends ACircle
*/
protected Circle(final IVector2 center, final double radius)
{
if (radius <= 0)
if (radius < 0)
{
throw new IllegalArgumentException("Radius of a circle must be larger than zero!");
}
......
/*
* Copyright (c) 2009 - 2020, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.math.pose;
......@@ -60,6 +60,12 @@ public class Pose implements IMirrorable<Pose>
}
public static Pose nan()
{
return Pose.from(Vector3f.nan());
}
@Override
public Pose mirrored()
{
......
/*
* Copyright (c) 2009 - 2020, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.math.vector;
......@@ -81,6 +81,17 @@ public class Vector3 extends AVector3
}
/**
* Create new NaN vector
*
* @return new instance
*/
public static Vector3 nan()
{
return new Vector3(Double.NaN, Double.NaN, Double.NaN);
}
/**
* @param x value
* @param y value
......
/*
* Copyright (c) 2009 - 2017, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.math.vector;
......@@ -30,28 +30,28 @@ public final class Vector3f extends AVector3
public static final Vector3f ZERO_VECTOR = fromXYZ(0, 0, 0);
/** Vector3f(NaN,NaN,NaN) */
public static final Vector3f UNINITIALIZED = fromXYZ(Double.NaN, Double.NaN, Double.NaN);
private final double x;
private final double y;
private final double z;
private Vector3f()
{
x = 0;
y = 0;
z = 0;
}
private Vector3f(final double x, final double y, final double z)
{
this.x = x;
this.y = y;
this.z = z;
}
/**
* @param x value
* @param y value
......@@ -62,8 +62,8 @@ public final class Vector3f extends AVector3
{
return new Vector3f(x, y, z);
}
/**
* @param xy vector
* @param z value
......@@ -73,8 +73,8 @@ public final class Vector3f extends AVector3
{
return new Vector3f(xy.x(), xy.y(), z);
}
/**
* @param original vector to copy
* @return new instance
......@@ -87,47 +87,58 @@ public final class Vector3f extends AVector3
}
return new Vector3f(original.x(), original.y(), original.z());
}
/**
* Create new zero vector
*
*
* @return new instance
*/
public static Vector3f zero()
{
return new Vector3f();
}
/**
* Create new NaN vector
*
* @return new instance
*/
public static Vector3f nan()
{
return UNINITIALIZED;
}
@Override
public IVector3 copy()
{
return Vector3.copy(this);
}
@Override
public IVector3 getXYZVector()
{
return Vector3.copy(this);
}
@Override
public double x()
{
return x;
}
@Override
public double y()
{
return y;
}
@Override
public double z()
{
......
/*
* Copyright (c) 2009 - 2018, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.clock;
......@@ -9,7 +9,7 @@ package edu.tigers.sumatra.clock;
*/
public class FpsCounter
{
private static final int UPDATE_FREQ = 10;
private static final double TIME_WINDOW = 0.5;
private long lastTime = 0;
private double fps = 0;
private int counter = 0;
......@@ -24,17 +24,14 @@ public class FpsCounter
public boolean newFrame(final long timestamp)
{
boolean fpsChanged = false;
if (counter >= UPDATE_FREQ)
double timeDiff = (timestamp - lastTime) / 1e9;
if (timeDiff > TIME_WINDOW)
{
long timeDiff = timestamp - lastTime;
if (timeDiff != 0)
{
double newFps = UPDATE_FREQ / (timeDiff / 1e9);
fpsChanged = Math.abs(fps - newFps) > 0.01;
fps = newFps;
lastTime = timestamp;
counter = 0;
}
double newFps = counter / timeDiff;
fpsChanged = Math.abs(fps - newFps) > 0.1;
fps = newFps;
lastTime = timestamp;
counter = 0;
}
counter++;
return fpsChanged;
......
/*
* Copyright (c) 2009 - 2021, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.network;
import edu.tigers.sumatra.util.Safe;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.function.Consumer;
/**
* A threaded UDP sender and receiver.
*/
@Log4j2
@RequiredArgsConstructor
public class UdpTransceiver
{
private static final int SOCKET_TIMEOUT_MS = 1000;
private final String host;
private final int port;
private DatagramSocket socket;
private InetAddress address;
private int bufferSize;
private Thread thread;
@Setter
private Consumer<byte[]> responseConsumer = b -> {
};
public void start()
{
if (thread == null)
{
thread = new Thread(() -> Safe.run(this::receive));
thread.setName(toString());
thread.start();
}
}
public void stop()
{
if (thread != null)
{
thread.interrupt();
thread = null;
}
}
@Override
public String toString()
{
return "UDP Transceiver " + host + ":" + port;
}
private void connect()
{
if (socket == null)
{
try
{
socket = new DatagramSocket();
address = InetAddress.getByName(host);
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
bufferSize = socket.getReceiveBufferSize();
} catch (IOException e)
{
log.warn("Failed to setup socket for {}", this, e);
}
}
}
private void disconnect()
{
if (socket != null)
{
socket.close();
socket = null;
}
}
private void receive()
{
try
{
connect();
log.debug("Start listening for messages from {}", this);
byte[] buf = new byte[bufferSize];
while (!Thread.interrupted())
{
try
{
var packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
responseConsumer.accept(Arrays.copyOf(packet.getData(), packet.getLength()));
} catch (@SuppressWarnings("squid:S1166") SocketTimeoutException e)
{
// go on in the loop (checking if we got interrupted and need to stop)
} catch (IOException e)
{
log.warn("Failed to receive data from {}", this, e);
}
}
} finally
{
disconnect();
log.debug("Stopped listening for messages from {}", this);
}
}