private async void Page_Loaded(object sender, RoutedEventArgs e) { UpdateUi(); BtnTargetSpeedIncrease.Click += BtnTargetSpeedIncrease_Click; BtnTargetSpeedDecrease.Click += BtnTargetSpeedDecrease_Click; BtnToleranceDecreaseKts.Click += BtnToleranceDecreaseKts_Click; BtnToleranceIncreaseKts.Click += BtnToleranceIncreaseKts_Click; _servo = new ServoController(5); await _servo.Connect(); _servo.SetPosition(ServoPosition).AllowTimeToMove(2000).Go(); }
private void ServoControlTask() { while (Activated) { // Doing this here for now CalculateSpeedAverages(); var speed = OpertationMode == AutopilotOperationMode.Average && SpeedList.Any() ? SpeedList.Average() : OpertationMode == AutopilotOperationMode.Track ? TrackSpeed : Speed; var diff = TargetSpeed - speed; if (Math.Abs(diff) > ToleranceSpeed && speed > 0) { if (diff < 0.0d) { ServoPosition -= 1; LogAsync(string.Format("{0} km/h off; decreasing by 1 degree to {1}", diff, ServoPosition)); } if (diff > 0.0d && ServoPosition >= 0) { ServoPosition += 1; LogAsync(string.Format("{0} km/h off; increasing by 1 degree to {1}", diff, ServoPosition)); } ServoController.SetPosition(ServoPosition).AllowTimeToMove(100).Go(); } else { LogAsync(string.Format("Speed of {0} withing tolerance or 0; doing nothing", speed)); } Task.Delay((int)(ToleranceSeconds * 1000)).Wait(); } LogAsync("Speed control loop ended. Setting servo back to 0 degree"); ServoPosition = 0; ServoController.SetPosition(0).AllowTimeToMove(2000).Go(); }
private async void Page_Loaded(object sender, RoutedEventArgs e) { ServoPosition = 0; TargetSpeed = 2.5d; ToleranceSpeed = 0.1d; ToleranceSeconds = 1.0d; BtnTargetSpeedActivate.Click += BtnTargetSpeedActivate_Click; BtnTargetSpeedIncrease.Click += BtnTargetSpeedIncrease_Click; BtnTargetSpeedDecrease.Click += BtnTargetSpeedDecrease_Click; BtnToleranceDecreaseKmh.Click += BtnToleranceDecreaseKmh_Click; BtnToleranceIncreaseKmh.Click += BtnToleranceIncreaseKmh_Click; BtnTimeFactorDecrease.Click += BtnTimeFactorDecrease_Click; BtnTimeFactorIncrease.Click += BtnTimeFactorIncrease_Click; BtnTest.Click += BtnTest_Click; TxtDebug.TextChanged += TxtDebug_TextChanged; var modes = Enum.GetValues(typeof(AutopilotOperationMode)).Cast <AutopilotOperationMode>(); CmbMode.ItemsSource = modes.ToList(); OpertationMode = 0; CmbMode.SelectedIndex = 0; CmbMode.SelectionChanged += CmbModeOnSelectionChanged; // Initialize Gps try { StartGps(); #region Compass Initialize // TODO: Check I2C connection of RPi //StartCompass(); //if (Compass.IsConnected()) //{ // _chacheCompassTask = Task.Run(() => // { // while (true) // { // CompassData.Add(Compass.GetRawData()); // if (_compassTaskToken.IsCancellationRequested) // { // _compassTaskToken.ThrowIfCancellationRequested(); // } // } // }, _compassTaskToken); //} #endregion #region ServoController ServoController = new ServoController(5); await ServoController.Connect(); ServoController.SetPosition(ServoPosition).AllowTimeToMove(2000).Go(); await Task.Run(async() => { while (UpdatingUi) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { UpdateUi(); }); Task.Delay(2000).Wait(); } }); #endregion } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format("Error starting app: {0}", ex.Message)); } }
private void ServoPositionSlider_Changed(object sender, RangeBaseValueChangedEventArgs e) { _servo.SetPosition((int)e.NewValue).AllowTimeToMove(100).Go(); ServoPosition = (int)e.NewValue; TxtServoPosition.Text = ServoPosition.ToString(CultureInfo.InvariantCulture); }