Skip to content

Commit

Permalink
chore: smoothen out some more
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Apr 19, 2024
1 parent 69b843e commit 98d50ab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/telraam/logic/positioner/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public void update(double progress, double speed) {
this.progress = progress;
this.speed = speed;
this.timestamp = System.currentTimeMillis();
System.out.println("Progress: " + progress + " | Speed: " + speed + " | Timestamp: " + timestamp);
}
}
21 changes: 15 additions & 6 deletions src/main/java/telraam/logic/positioner/nostradamus/TeamData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TeamData {
private final int totalDistance; // Total distance of the track
@Getter
private final Position position; // Data to send to the websocket
private final int maxDeviance; // Maximum deviance the animation can have from the reality
private final double maxDeviance; // Maximum deviance the animation can have from the reality


public TeamData(int teamId, int interval, List<Station> stations, int averageAmount, double sprintingSpeed, int finishOffset) {
Expand All @@ -40,7 +40,7 @@ public TeamData(int teamId, int interval, List<Station> stations, int averageAmo
this.previousStationArrival = System.currentTimeMillis();
this.currentStation = new StationData(new Station(-10), new Station(-9), new CircularQueue<>(0), -10, -10, -10); // Will never trigger `isNextStation` for the first station
this.position = new Position(teamId);
this.maxDeviance = 1 / stations.size();
this.maxDeviance = (double) 1 / stations.size();
}

// Add a new detection
Expand All @@ -52,11 +52,13 @@ public boolean addDetection(Detection e) {
// Is at a new station that is in front of the previous one.
previousStation = currentStation;
currentStation = stations.get(e.getStationId());
System.out.println("Station: " + currentStation.index());
long now = System.currentTimeMillis();
if (isNextStation()) {
// Only add the time if it goes forward by exactly one station
previousStation.averageTimes().add(System.currentTimeMillis() - previousStationArrival);
previousStation.averageTimes().add(now - previousStationArrival);
}
previousStationArrival = System.currentTimeMillis();
previousStationArrival = now;

return true;
}
Expand Down Expand Up @@ -87,13 +89,20 @@ public void updatePosition() {

double speed, progress;
// Determine whether to speed up / slow down the animation or teleport it
double difference = (goalProgress - theoreticalProgress + 1) % 1;
if ((difference >= maxDeviance && difference <= 1 - maxDeviance) || previousStation.averageTimes().size() < 5) {
double difference = (currentStation.currentProgress() - theoreticalProgress + 1) % 1;
if ((difference >= maxDeviance && difference <= 1 - maxDeviance) || currentStation.averageTimes().size() < 3) {
System.out.println("Too fast");
System.out.println("Size: " + currentStation.averageTimes().size());
System.out.print("Average times: ");
currentStation.averageTimes().forEach(time -> System.out.print(" | " + time));
System.out.println("");
System.out.println("Goal: " + currentStation.currentProgress() + " Theoretical: " + theoreticalProgress + " Difference: " + (currentStation.currentProgress() - theoreticalProgress + 1) % 1);
// Animation was too far behind or ahead so teleport
progress = currentStation.currentProgress();
speed = ((currentStation.nextProgress() - progress + 1) % 1) / getMedian();
} else {
// Animation is close enough, adjust so that we're synced at the next station
System.out.println("Good");
progress = theoreticalProgress;
speed = ((goalProgress - theoreticalProgress + 1) % 1) / (nextStationArrival - currentTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void fetch() {
return;
}


WebsocketClient websocketClient = new WebsocketClient(url);
websocketClient.addOnOpenHandler(() -> {
websocketClient.sendMessage(wsMessageEncoded);
Expand Down Expand Up @@ -122,7 +121,7 @@ public void fetch() {
new Timestamp((long) (detection.detectionTimestamp * 1000)),
new Timestamp(System.currentTimeMillis())
));
detection_mac_addresses.add(detection.mac);
detection_mac_addresses.add(detection.mac.toUpperCase());
}
if (!new_detections.isEmpty()) {
List<Detection> db_detections = detectionDAO.insertAllWithoutBaton(new_detections, detection_mac_addresses);
Expand Down

0 comments on commit 98d50ab

Please sign in to comment.