private Tuple <double, double> JoystickToDiff(double x, double y, double minJoystick, double maxJoystick, double minSpeed, double maxSpeed) { // If x and y are 0, then there is not much to calculate... if (x == 0 && y == 0) { return(new Tuple <double, double>(0, 0)); } // First Compute the angle in deg // First hypotenuse var z = Math.Sqrt(x * x + y * y); // angle in radians var rad = Math.Acos(Math.Abs(x) / z); // and in degrees var angle = rad * 180 / Math.PI; // Now angle indicates the measure of turn // Along a straight line, with an angle o, the turn co-efficient is same // this applies for angles between 0-90, with angle 0 the coeff is -1 // with angle 45, the co-efficient is 0 and with angle 90, it is 1 var tcoeff = -1 + (angle / 90) * 2; var turn = tcoeff * Math.Abs(Math.Abs(y) - Math.Abs(x)); turn = Math.Round(turn * 100, 0) / 100; // And max of y or x is the movement var mov = Math.Max(Math.Abs(y), Math.Abs(x)); double rawLeft, rawRight; // First and third quadrant if ((x >= 0 && y >= 0) || (x < 0 && y < 0)) { rawLeft = mov; rawRight = turn; } else { rawRight = mov; rawLeft = turn; } // Reverse polarity if (y < 0) { rawLeft = -rawLeft; rawRight = -rawRight; } // minJoystick, maxJoystick, minSpeed, maxSpeed // Map the values onto the defined rang var leftOut = ValueMapper.Map(rawLeft, minJoystick, maxJoystick, minSpeed, maxSpeed); var rightOut = ValueMapper.Map(rawRight, minJoystick, maxJoystick, minSpeed, maxSpeed); return(new Tuple <double, double>(leftOut, rightOut)); }
private void OnRemoteControlMessageReceived(IMessage message) { var rcMessage = (RemoteControlMessage)message; if (rcMessage.Key == (int)RemoteControlKey.Throttle) { _messageBroker.Publish(new ServoMessage { Id = 0, PulseWidth = (int)ValueMapper.Map(rcMessage.Value, -1, 1, 500, 2500) }); } }
private void Gamepad_AxisChanged(object sender, AxisEventArgs e) { var rcKey = MapToRemoteControlKey(e.Axis, true); if (rcKey != RemoteControlKey.Invalid) { var value = ValueMapper.Map(e.Value, -32767, 32767, -1, 1) * ((rcKey == RemoteControlKey.Throttle || rcKey == RemoteControlKey.Yaw) ? -1 : 1); KeyChanged?.Invoke(this, new RemoteControlEventArgs { Key = rcKey, // Gamepad's Throttle and Yaw values are inverted.. fix that before sending Value = value }); } }
public void GivenNotRequiredEmptyValue_WhenMapping_ItIsSetToDefaultValue() { var parsingStrategyProvider = new Mock <IProvideParsingStrategy>(); var mapper = new ValueMapper(parsingStrategyProvider.Object); var columnMapping = new NameColumnMapping(string.Empty, string.Empty, string.Empty, null, null, null, false, null); var result = mapper.Map( value: string.Empty, propertyType: typeof(int), rowIndex: 0, columnMapping: columnMapping); result.IsSuccess.Should().BeTrue(); result.Value.Should().Be(string.Empty); }
public void GivenInvalidValue_WhenParsingFails_FailureIsReturned() { var value = "foo"; var parsingStrategyProvider = new Mock <IProvideParsingStrategy>(); parsingStrategyProvider.Setup(v => v.Parse(typeof(int), value.ToString(), string.Empty)).Returns(Result.Fail <object, string>("Parsing failed")); var mapper = new ValueMapper(parsingStrategyProvider.Object); var columnMapping = new NameColumnMapping(string.Empty, string.Empty, string.Empty, null, null, null, false, null); var result = mapper.Map( value: value, propertyType: typeof(int), rowIndex: 0, columnMapping: columnMapping); result.IsFailure.Should().BeTrue(); }
/// <summary> /// Generate the actual drainage values by combining noise and standard derivation /// </summary> private void GenerateDrainage() { for (var ix = 0; ix < this.Dimensions.Width; ++ix) { for (var iy = 0; iy < this.Dimensions.Height; ++iy) { // Drainage under water is just 0.0 if (this.Elevation[ix, iy] <= this.Elevation.SeaThreshold || this.Elevation[ix, iy] >= this.Elevation.LandThreshold) { this.Values[ix, iy] = 0.0f; } else { var drainage = this.StandardDerivationWeight * this.StandardDerivation[ix, iy] + this.NoiseWeight * this.NoiseValues[ix, iy]; this.Values[ix, iy] = drainage; } } } this.Normalize(this.Values); this.DesertThreshold = this.CalculateThreshold(this.Parameters.DesertPercentage, true); this.RockyThreshold = this.CalculateThreshold(this.Parameters.RockyPercentage, true); this.HillsThreshold = this.CalculateThreshold(this.Parameters.HillsPercentage, true); var mapper = new ValueMapper { { this.DesertThreshold, 0.32f }, { this.RockyThreshold, 0.49f }, { this.HillsThreshold, 0.65f } }; for (var ix = 0; ix < this.Dimensions.Width; ++ix) { for (var iy = 0; iy < this.Dimensions.Height; ++iy) { this.Values[ix, iy] = mapper.Map(this.Values[ix, iy]); } } }
public void GivenValidValue_WhenParsingIsSuccesful_ItSucceeds() { var value = 50; var parsingStrategyProvider = new Mock <IProvideParsingStrategy>(); parsingStrategyProvider.Setup(v => v.Parse(typeof(int), value.ToString(), string.Empty)).Returns(Result.Ok <object, string>(value)); var mapper = new ValueMapper(parsingStrategyProvider.Object); var columnMapping = new NameColumnMapping(string.Empty, string.Empty, string.Empty, null, null, null, false, null); var result = mapper.Map( value: value.ToString(), propertyType: typeof(int), rowIndex: 0, columnMapping: columnMapping); result.IsSuccess.Should().BeTrue(); result.Value.Should().Be(value); }
/// <summary> /// Combine rain shadow and noise /// </summary> protected void GenerateRainfall() { for (var ix = 0; ix < this.Dimensions.Width; ++ix) { for (var iy = 0; iy < this.Dimensions.Height; ++iy) { if (this.Elevation[ix, iy] < this.Elevation.SeaThreshold) { this.Values[ix, iy] = 0.0f; } else { var rainfall = this.RainShadowWeight * this.RainShadow[ix, iy] + this.NoiseWeight * this.NoiseValues[ix, iy]; this.Values[ix, iy] = rainfall; } } } this.Normalize(this.Values); this.BarrenThreshold = this.CalculateThreshold(this.Parameters.BarrenPercentage, true); this.GrassThreshold = this.CalculateThreshold(this.Parameters.GrassPercentage, true); this.ConiferThreshold = this.CalculateThreshold(this.Parameters.ConiferPercentage, true); var mapper = new ValueMapper { { this.BarrenThreshold, 0.09f }, { this.GrassThreshold, 0.65f }, { this.ConiferThreshold, 0.88f } }; for (var ix = 0; ix < this.Dimensions.Width; ++ix) { for (var iy = 0; iy < this.Dimensions.Height; ++iy) { this.Values[ix, iy] = mapper.Map(this.Values[ix, iy]); } } }
public void GivenRequiredEmptyValue_WhenMapping_ValidationFails() { var parsingStrategyProvider = new Mock <IProvideParsingStrategy>(); var mapper = new ValueMapper(parsingStrategyProvider.Object); var columnMapping = new NameColumnMapping( string.Empty, string.Empty, string.Empty, new List <IParsingRule> { new RequiredRule() }, null, null, false, null); var result = mapper.Map( value: string.Empty, propertyType: typeof(int), rowIndex: 0, columnMapping: columnMapping); result.IsFailure.Should().BeTrue(); }