Skip to content

Commit

Permalink
fix: fixed nmea position unit of measurement (#5543)
Browse files Browse the repository at this point in the history
* fix: fixed nmea position unit of measurement

Signed-off-by: SimoneFiorani <[email protected]>

* refactor: improved field names

Signed-off-by: SimoneFiorani <[email protected]>

* refactor: changed fields names

Signed-off-by: SimoneFiorani <[email protected]>

* fix: remained unnecessary conversion

Signed-off-by: SimoneFiorani <[email protected]>

---------

Signed-off-by: SimoneFiorani <[email protected]>
  • Loading branch information
sfiorani authored and MMaiero committed Dec 19, 2024
1 parent 04699d9 commit 46bceab
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
* <li>Latitude in degrees
* <li>Track in degrees
* <li>Altitude in meters
* <li>Speed in km/h
* <li>Speed in mph
* <li>Speed in m/s (this field has different getters to retrieved value in m/s, km/h or mph)
* </ul>
* It adds to the OSGI Position class the following fields:<br>
* <ul>
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<GNSSType> getGnssTypes() {
Expand All @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -210,10 +216,10 @@ private void parseRmcSentence(List<String> 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);
Expand Down Expand Up @@ -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 + "]";
}
}

0 comments on commit 46bceab

Please sign in to comment.