Skip to content

Commit

Permalink
chore: Send speed 0 when no data is received
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Apr 20, 2024
1 parent 59b96bf commit 7ddd89b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/main/java/telraam/logic/positioner/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
public class Position {
private final int teamId;
private double progress; // Progress of the lap. Between 0-1
private double speed; // Current speed. progress / second
private long timestamp;
private double speed; // Current speed. progress / millisecond
private long timestamp; // Timestamp in milliseconds

public Position(int teamId) {
this.teamId = teamId;
this.progress = 0;
this.speed = 0;
this.timestamp = 0;
this.timestamp = System.currentTimeMillis();
}

public void update(double progress, double speed) {
public Position(int teamId, double progress) {
this.teamId = teamId;
this.progress = progress;
this.speed = speed;
this.speed = 0;
this.timestamp = System.currentTimeMillis();
}

public void update(double progress, double speed, long timestamp) {
this.progress = progress;
this.speed = speed;
this.timestamp = timestamp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import telraam.database.models.Detection;
import telraam.database.models.Station;
import telraam.database.models.Team;
import telraam.logic.positioner.Position;
import telraam.logic.positioner.PositionSender;
import telraam.logic.positioner.Positioner;

Expand All @@ -26,6 +27,7 @@ public class Nostradamus implements Positioner {
private final double AVERAGE_SPRINTING_SPEED_M_MS = 0.00684; // Average sprinting speed m / ms
private final int MIN_RSSI = -84;
private final int FINISH_OFFSET = 0;
private final int MAX_NO_DATA_MS = 30000;
private final Jdbi jdbi;
private final List<Detection> newDetections; // Contains not yet handled detections
private final Lock detectionLock;
Expand Down Expand Up @@ -102,6 +104,7 @@ private void calculatePosition() {
}
newDetections.clear();
detectionLock.unlock(); // Use lock as short as possible
dataLock.unlock();

if (!changedTeams.isEmpty()) {
// Update
Expand All @@ -115,7 +118,17 @@ private void calculatePosition() {
);
}

dataLock.unlock();
long now = System.currentTimeMillis();
for (Map.Entry<Integer, TeamData> entry: teamData.entrySet()) {
if (now - entry.getValue().getPreviousStationArrival() > MAX_NO_DATA_MS) {
positionSender.send(
Collections.singletonList(new Position(
entry.getKey(),
entry.getValue().getPosition().getProgress()
))
);
}
}

// zzzzzzzz
try {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/telraam/logic/positioner/nostradamus/TeamData.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class TeamData {
private final DetectionList detections; // List with all relevant detections
private final Map<Integer, StationData> stations; // Station list
@Getter
private long previousStationArrival; // Arrival time of previous station. Used to calculate the average times
private StationData currentStation; // Current station location
private StationData previousStation; // Previous station location
Expand Down Expand Up @@ -74,13 +75,17 @@ private boolean isNextStation() {
return Objects.equals(previousStation.nextStation().getId(), currentStation.station().getId());
}

private double normalize(double number) {
return (number + 1) % 1;
}

// Update the position data
public void updatePosition() {
long currentTime = System.currentTimeMillis();

// Animation is currently at progress x
long milliSecondsSince = currentTime - position.getTimestamp();
double theoreticalProgress = (position.getProgress() + (position.getSpeed() * milliSecondsSince)) % 1;
double theoreticalProgress = normalize(position.getProgress() + (position.getSpeed() * milliSecondsSince));

// Arrive at next station at timestamp y and progress z
double median = getMedian(currentStation.times());
Expand All @@ -89,18 +94,18 @@ public void updatePosition() {

double speed, progress;
// Determine whether to speed up / slow down the animation or teleport it
double difference = (currentStation.currentProgress() - theoreticalProgress + 1) % 1;
double difference = normalize(currentStation.currentProgress() - theoreticalProgress);
if ((difference >= maxDeviance && difference <= 1 - maxDeviance)) {
// Animation was too far behind or ahead so teleport
progress = currentStation.currentProgress();
speed = ((currentStation.nextProgress() - progress + 1) % 1) / median;
speed = normalize(currentStation.nextProgress() - progress) / median;
} else {
// Animation is close enough, adjust so that we're synced at the next station
progress = theoreticalProgress;
speed = ((goalProgress - theoreticalProgress + 1) % 1) / (nextStationArrival - currentTime);
speed = normalize(goalProgress - theoreticalProgress) / (nextStationArrival - currentTime);
}

position.update(progress, speed);
position.update(progress, speed, currentTime);
}

// Get the medium of the average times
Expand Down

0 comments on commit 7ddd89b

Please sign in to comment.