From d94f787a0675486f59c523984c58646b87b95fbb Mon Sep 17 00:00:00 2001 From: sfiorani <109297780+sfiorani@users.noreply.github.com> Date: Wed, 13 Nov 2024 08:30:53 +0100 Subject: [PATCH] fix: fixed nmea position unit of measurement (#5543) * fix: fixed nmea position unit of measurement Signed-off-by: SimoneFiorani * refactor: improved field names Signed-off-by: SimoneFiorani * refactor: changed fields names Signed-off-by: SimoneFiorani * fix: remained unnecessary conversion Signed-off-by: SimoneFiorani --------- Signed-off-by: SimoneFiorani (cherry picked from commit d60b6b8f023569234d6a2a4d067d9f75d1410a66) --- .../eclipse/kura/position/NmeaPosition.java | 67 ++++++++++--------- .../kura/nm/position/MMLocationParser.java | 45 +++++++------ 2 files changed, 61 insertions(+), 51 deletions(-) diff --git a/kura/org.eclipse.kura.api/src/main/java/org/eclipse/kura/position/NmeaPosition.java b/kura/org.eclipse.kura.api/src/main/java/org/eclipse/kura/position/NmeaPosition.java index e511642b95f..85c3c1fe1bd 100644 --- a/kura/org.eclipse.kura.api/src/main/java/org/eclipse/kura/position/NmeaPosition.java +++ b/kura/org.eclipse.kura.api/src/main/java/org/eclipse/kura/position/NmeaPosition.java @@ -24,8 +24,7 @@ *
  • Latitude in degrees *
  • Track in degrees *
  • Altitude in meters - *
  • Speed in km/h - *
  • Speed in mph + *
  • Speed in m/s (this field has different getters to retrieved value in m/s, km/h or mph) * * It adds to the OSGI Position class the following fields:
    *
      @@ -47,11 +46,14 @@ @ProviderType public class NmeaPosition { - private double latitude; - private double longitude; - private double altitude; - private double speed; - private double track; + private static final double MS_TO_KMH = 3.6; + private static final double MS_TO_MPH = 2.24; + + private double latitudeDegrees; + private double longitudeDegrees; + private double altitudeMeters; + private double speedMetersPerSecond; + private double trackDegrees; private int fixQuality; private int nrSatellites; private double mDOP; @@ -63,28 +65,29 @@ public class NmeaPosition { private char latitudeHemisphere; private char longitudeHemisphere; - public NmeaPosition(double lat, double lon, double alt, double speed, double track) { - this(lat, lon, alt, speed, track, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, '0', '0', '0'); + public NmeaPosition(double latDegrees, double lonDegrees, double altDegrees, double speedMps, double trackDegrees) { + this(latDegrees, lonDegrees, altDegrees, speedMps, trackDegrees, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, '0', '0', '0'); } @SuppressWarnings("checkstyle:parameterNumber") - public NmeaPosition(double lat, double lon, double alt, double speed, double track, int fixQuality, - int nrSatellites, double dop, double pdop, double hdop, double vdop, int fix3D) { - this(lat, lon, alt, speed, track, fixQuality, nrSatellites, dop, pdop, hdop, vdop, fix3D, '0', '0', '0'); + public NmeaPosition(double latDegrees, double lonDegrees, double altDegrees, double speedMps, double trackDegrees, + int fixQuality, int nrSatellites, double dop, double pdop, double hdop, double vdop, int fix3D) { + this(latDegrees, lonDegrees, altDegrees, speedMps, trackDegrees, fixQuality, nrSatellites, dop, pdop, hdop, + vdop, fix3D, '0', '0', '0'); } /** * @since 2.0 */ @SuppressWarnings("checkstyle:parameterNumber") - public NmeaPosition(double lat, double lon, double alt, double speed, double track, int fixQuality, - int nrSatellites, double dop, double pdop, double hdop, double vdop, int fix3D, char validF, char hemiLat, - char hemiLon) { - this.latitude = lat; - this.longitude = lon; - this.altitude = alt; - this.speed = speed; - this.track = track; + public NmeaPosition(double latDegrees, double lonDegrees, double altDegrees, double speedMps, double trackDegrees, + int fixQuality, int nrSatellites, double dop, double pdop, double hdop, double vdop, int fix3D, char validF, + char hemiLat, char hemiLon) { + this.latitudeDegrees = latDegrees; + this.longitudeDegrees = lonDegrees; + this.altitudeMeters = altDegrees; + this.speedMetersPerSecond = speedMps; + this.trackDegrees = trackDegrees; this.fixQuality = fixQuality; this.nrSatellites = nrSatellites; this.mDOP = dop; @@ -101,69 +104,69 @@ public NmeaPosition(double lat, double lon, double alt, double speed, double tra * Return the latitude in degrees */ public double getLatitude() { - return this.latitude; + return this.latitudeDegrees; } public void setLatitude(double latitude) { - this.latitude = latitude; + this.latitudeDegrees = latitude; } /** * Return the longitude in degrees */ public double getLongitude() { - return this.longitude; + return this.longitudeDegrees; } public void setLongitude(double longitude) { - this.longitude = longitude; + this.longitudeDegrees = longitude; } /** * Return the altitude in meters */ public double getAltitude() { - return this.altitude; + return this.altitudeMeters; } public void setAltitude(double altitude) { - this.altitude = altitude; + this.altitudeMeters = altitude; } /** * Return the speed in km/h */ public double getSpeedKmh() { - return this.speed * 3.6; + return this.speedMetersPerSecond * MS_TO_KMH; } /** * Return the speed in mph */ public double getSpeedMph() { - return this.speed * 2.24; + return this.speedMetersPerSecond * MS_TO_MPH; } /** * Return the speed in m/s */ public double getSpeed() { - return this.speed; + return this.speedMetersPerSecond; } public void setSpeed(double speed) { - this.speed = speed; + this.speedMetersPerSecond = speed; } /** * Return the track in degrees */ public double getTrack() { - return this.track; + return this.trackDegrees; } public void setTrack(double track) { - this.track = track; + this.trackDegrees = track; } public int getFixQuality() { diff --git a/kura/org.eclipse.kura.nm/src/main/java/org/eclipse/kura/nm/position/MMLocationParser.java b/kura/org.eclipse.kura.nm/src/main/java/org/eclipse/kura/nm/position/MMLocationParser.java index 867bca37f3d..04ad048c4b2 100644 --- a/kura/org.eclipse.kura.nm/src/main/java/org/eclipse/kura/nm/position/MMLocationParser.java +++ b/kura/org.eclipse.kura.nm/src/main/java/org/eclipse/kura/nm/position/MMLocationParser.java @@ -38,15 +38,16 @@ public class MMLocationParser { private static final Logger logger = LoggerFactory.getLogger(MMLocationParser.class); - private static final double KNOTS_TO_M_S = 1 / 1.94384449; + private static final double KNOTS_TO_MS = 1 / 1.94384449; + private int gnssTypeUpdateCounter = 0; private static final int GNSSTYPE_RESET_COUNTER = 50; - private Double lat = 0.0; - private Double lon = 0.0; - private Double alt = 0.0; - private Double speed = 0.0; - private Double track = 0.0; + private Double latitudeDegrees = 0.0; + private Double longitudeDegrees = 0.0; + private Double altitudeMeters = 0.0; + private Double speedMetersPerSecond = 0.0; + private Double trackDegrees = 0.0; private int fixQuality; private int nrSatellites; @@ -68,10 +69,15 @@ public LocalDateTime getLocalDateTime() { return Objects.requireNonNull(LocalDateTime.of(date, time)); } + /* + * Conversions are required since org.osgi.util.position.Position requires values in radians for latitude, longitude + * and track. + */ public Position getPosition() { - return Objects.requireNonNull(new Position(new Measurement(this.lat, Unit.rad), - new Measurement(this.lon, Unit.rad), new Measurement(this.alt, Unit.m), - new Measurement(this.speed, Unit.m_s), new Measurement(this.track, Unit.rad))); + return Objects.requireNonNull(new Position(new Measurement(Math.toRadians(this.latitudeDegrees), Unit.rad), + new Measurement(Math.toRadians(this.longitudeDegrees), Unit.rad), + new Measurement(this.altitudeMeters, Unit.m), new Measurement(this.speedMetersPerSecond, Unit.m_s), + new Measurement(Math.toRadians(this.trackDegrees), Unit.rad))); } public Set getGnssTypes() { @@ -83,9 +89,9 @@ public boolean isFixed() { } public NmeaPosition getNmeaPosition() { - return new NmeaPosition(this.lat, this.lon, this.alt, this.speed, this.track, this.fixQuality, - this.nrSatellites, this.mDOP, this.mPDOP, this.mHDOP, this.mVDOP, this.m3Dfix, this.validFix, - this.latitudeHemisphere, this.longitudeHemisphere); + return new NmeaPosition(this.latitudeDegrees, this.longitudeDegrees, this.altitudeMeters, + this.speedMetersPerSecond, this.trackDegrees, this.fixQuality, this.nrSatellites, this.mDOP, this.mPDOP, + this.mHDOP, this.mVDOP, this.m3Dfix, this.validFix, this.latitudeHemisphere, this.longitudeHemisphere); } public String getNmeaTime() { @@ -102,15 +108,15 @@ public void parseRawLocation(Variant rawLocationVariant) { switch (rawEntry.getKey()) { case "latitude": - this.lat = Math.toRadians((Double) rawEntry.getValue().getValue()); + this.latitudeDegrees = (Double) rawEntry.getValue().getValue(); break; case "longitude": - this.lon = Math.toRadians((Double) rawEntry.getValue().getValue()); + this.longitudeDegrees = (Double) rawEntry.getValue().getValue(); break; case "altitude": - this.alt = (Double) rawEntry.getValue().getValue(); + this.altitudeMeters = (Double) rawEntry.getValue().getValue(); break; // time comes in format HHmmss.SS, so we cut the string after the dot to extract only the util information @@ -210,10 +216,10 @@ private void parseRmcSentence(List rmcTokens) { this.date = LocalDate.parse(rmcTokens.get(9), DateTimeFormatter.ofPattern("ddMyy")); } if (!rmcTokens.get(7).isEmpty()) { - this.speed = Double.parseDouble(rmcTokens.get(7)) * KNOTS_TO_M_S; + this.speedMetersPerSecond = Double.parseDouble(rmcTokens.get(7)) * KNOTS_TO_MS; } if (!rmcTokens.get(8).isEmpty()) { - this.track = Math.toRadians(Double.parseDouble(rmcTokens.get(8))); + this.trackDegrees = Double.parseDouble(rmcTokens.get(8)); } if (!rmcTokens.get(4).isEmpty()) { this.latitudeHemisphere = rmcTokens.get(4).charAt(0); @@ -287,7 +293,8 @@ private GNSSType sentenceIdToGnssType(String type) { @Override public String toString() { - return "ModemManagerProvider [latitude=" + lat + ", longitude=" + lon + ", altitude=" + alt + ", speed=" + speed - + ", timestamp=" + time + ", date=" + date + ", gnssType=" + gnssTypes + "]"; + return "ModemManagerProvider [latitude=" + latitudeDegrees + ", longitude=" + longitudeDegrees + ", altitude=" + + altitudeMeters + ", speed=" + speedMetersPerSecond + ", timestamp=" + time + ", date=" + date + + ", gnssType=" + gnssTypes + "]"; } }