Added Knots <-> MMperSec conversions to AC35UnitConverter.

Removed redundant/unused conversions.
Documented all of the conversions, and renamed them to pack/unpackX, to match the API spec.
Updated/added some tests in AC35UnitConverterTest.
RaceStatus now contains a Bearing instead of a packed int bearing.
RaceStatus now contains wind speed in knots, instead of MMperSec packed.
This means that only RaceStatus decoder/encoder need to care about the bits-over-wire packed values.
issue #35 #36
#story[1095]
This commit is contained in:
fjc40
2017-08-06 14:57:51 +12:00
parent a0f98eadaa
commit ff262a6227
9 changed files with 177 additions and 83 deletions
@@ -1,43 +1,88 @@
package network.Utils;
import shared.model.Constants;
/**
* Created by fwy13 on 28/04/17.
* Contains various unit conversion for encoding/decoding messages.
* Our program uses the "unpacked" units, and the over-the-wire format uses "packed" units (e.g., degrees stored as ints).
*/
public class AC35UnitConverter {
public static double convertGPS(int value){
//converts latitude or longitue to angle
/**
* Converts a packed GPSCoordinate (latitude or longitude) into the unpacked unit.
* @param value Packed lat/long value.
* @return Unpacked lat/long angle, in degrees.
*/
public static double unpackGPS(int value) {
return (double) value * 180.0 / 2147483648.0;//2^31 = 2147483648
}
public static int convertGPSToInt(double value){
//converts latitude or longitue to angle
/**
* Converts a latitude or longitude angle into a packed unit.
* @param value The lat/long angle, in degrees, to convert.
* @return The packed value.
*/
public static int packGPS(double value) {
return (int) (value * 2147483648.0/180.0);//2^31 = 2147483648
}
public static double convertHeading(long value){
return (double) value * 360.0/65536.0;//2^15
/**
* Unpacks a heading from an int to an angle in degrees (this is a bearing).
* @param value The packed value to unpack.
* @return The unpacked value in degrees.
*/
public static double unpackHeading(int value) {
return (value * 360.0 / 65536.0);//2^15
}
public static double convertHeading(int value){
return (double) value * 360.0/65536.0;//2^15
}
public static double convertHeading(double value){
return value * 360.0/65536.0;//2^15
}
public static int encodeHeading(int value){
/**
* Packs a heading (this is a bearing), in degrees, to a packed int value.
* @param value The heading in degrees.
* @return The packed value.
*/
public static int packHeading(double value) {
return (int) (value / 360.0 * 65536.0);//2^15
}
public static int encodeHeading(double value){
return (int) (value / 360.0 * 65536.0);//2^15
/**
* Unpacks a true wind angle from a short to an angle in degrees (this is an azimuth).
* @param value The packed value to unpack.
* @return The unpacked value in degrees.
*/
public static double unpackTrueWindAngle(short value) {
return (value * 180.0 / 32768.0);//-2^15 to 2^15
}
public static double convertTrueWindAngle(long value){
return (double) value * 180.0/32768.0;//-2^15 to 2^15
/**
* Packs a true wind angle (this is an azimuth) from an angle in degrees to a packed short value.
* @param value The unpacked value in degrees.
* @return The packed value.
*/
public static short packTrueWindAngle(double value) {
return (short) (value / 180.0 * 32768.0);//-2^15 to 2^15
}
/**
* Unpacks a speed, in millimeters per second, to a double, in knots.
* @param millimetersPerSec Speed in millimeters per second.
* @return Speed in knots.
*/
public static double unpackMMperSecToKnots(int millimetersPerSec) {
return (millimetersPerSec / Constants.KnotsToMMPerSecond);
}
/**
* Packs a speed, in knots, into an int, in millimeters per second.
* @param speedKnots Speed in knots.
* @return Speed in millimeters per second.
*/
public static int packKnotsToMMperSec(double speedKnots) {
return (int) (speedKnots * Constants.KnotsToMMPerSecond);
}
}