RangeUtil { public static int map(float value, float fromLow, float fromHigh, int toLow, int toHigh) { return map(value, fromLow, fromHigh, toLow, toHigh, DEFAULT_MAP_CONSTRAIN); }  static int constrain(int value, int min, int max); static float constrain(float value, float min, float max); static double constrain(double value, double min, double max); static int map(float value, float fromLow, float fromHigh, int toLow, int toHigh); static int map(float value, float fromLow, float fromHigh, int toLow, int toHigh, boolean constrain); static float map(float value, float fromLow, float fromHigh, float toLow, float toHigh); static float map(float value, float fromLow, float fromHigh, float toLow, float toHigh, boolean constrain); static double map(double value, double fromLow, double fromHigh, double toLow, double toHigh); static double map(double value, double fromLow, double fromHigh, double toLow, double toHigh, boolean constrain);  }
@Test public void testMap() { float from_low = 0f; float from_high = 1f; int to_low_int = 0; int to_high_int = 180; Assert.assertEquals(to_low_int, RangeUtil.map(from_low, from_low, from_high, to_low_int, to_high_int)); Assert.assertEquals((to_low_int + to_high_int) / 2, RangeUtil.map((from_low + from_high) / 2, from_low, from_high, to_low_int, to_high_int)); Assert.assertEquals(to_high_int, RangeUtil.map(from_high, from_low, from_high, to_low_int, to_high_int)); Assert.assertEquals(to_low_int, RangeUtil.map(from_low - 10, from_low, from_high, to_low_int, to_high_int), DELTA_F); Assert.assertEquals(to_high_int, RangeUtil.map(from_high + 10, from_low, from_high, to_low_int, to_high_int), DELTA_F); Assert.assertEquals(-to_high_int, RangeUtil.map(from_low - from_high, from_low, from_high, to_low_int, to_high_int, false), DELTA_F); Assert.assertEquals(2 * to_high_int, RangeUtil.map(2 * from_high, from_low, from_high, to_low_int, to_high_int, false), DELTA_F); from_low = -90; from_high = 90; float to_low_float = 1f; float to_high_float = 2f; Assert.assertEquals(to_low_float, RangeUtil.map(from_low, from_low, from_high, to_low_float, to_high_float), DELTA_F); Assert.assertEquals((to_low_float + to_high_float) / 2, RangeUtil.map((from_low + from_high) / 2, from_low, from_high, to_low_float, to_high_float), DELTA_F); Assert.assertEquals(to_high_float, RangeUtil.map(from_high, from_low, from_high, to_low_float, to_high_float), DELTA_F); Assert.assertEquals(to_low_float, RangeUtil.map(from_low - 10, from_low, from_high, to_low_float, to_high_float), DELTA_F); Assert.assertEquals(to_high_float, RangeUtil.map(from_high + 10, from_low, from_high, to_low_float, to_high_float), DELTA_F); Assert.assertEquals(0.5, RangeUtil.map(from_low - from_high, from_low, from_high, to_low_float, to_high_float, false), DELTA_F); Assert.assertEquals(2.5, RangeUtil.map(2 * from_high, from_low, from_high, to_low_float, to_high_float, false), DELTA_F); }
@Test public void testServoRange() { float min_pulse_width_ms = 0.75f; float max_pulse_width_ms = 2.25f; float minus90_pulse_width_ms = 1f; float plus90_pulse_width_ms = 2f; Assert.assertEquals( RangeUtil.map(min_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), -45); Assert.assertEquals( RangeUtil.map(max_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 225); Assert.assertEquals( RangeUtil.map(minus90_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 0); Assert.assertEquals( RangeUtil.map(plus90_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 180); Assert.assertEquals(RangeUtil.map((minus90_pulse_width_ms + plus90_pulse_width_ms) / 2, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 90); Assert.assertEquals(0, RangeUtil.map(-1, -1, 1, 0, 180)); Assert.assertEquals(90, RangeUtil.map(0, -1, 1, 0, 180)); Assert.assertEquals(180, RangeUtil.map(1, -1, 1, 0, 180)); int servo_centre = 90; float value = -1; float pz_value = Math.round(servo_centre * value) + servo_centre; Assert.assertEquals(pz_value, RangeUtil.map(value, -1, 1, 0, 180), DELTA_F); value = 0; pz_value = Math.round(servo_centre * value) + servo_centre; Assert.assertEquals(pz_value, RangeUtil.map(value, -1, 1, 0, 180), DELTA_F); value = 1; pz_value = Math.round(servo_centre * value) + servo_centre; Assert.assertEquals(pz_value, RangeUtil.map(value, -1, 1, 0, 180), DELTA_F); }