引用 | 編輯
i1012347
2019-01-13 18:33 |
樓主
|
||
x0
https://stackoverflow.com/questions/44966702/control-a-servo-by-c-windows-10-iot-and-the-magellanic-servocontroller-cpu-ihttps://www.google.com/search?rlz=1C1AOHY_zh-TWTW708TW708&ei=cRI7XNXTC8jc8QXO9b2ADA&q=android+c%23+servo&oq=android+c%23+servo&gs_l=psy-ab.3...2816.8165..8560...0.0..0.107.500.4j2......0....1..gws-wiz.......0i71j0j0i22i30j0i13i30.D87a6QN1xMU Control a Servo by C#, Windows 10 IoT and the Magellanic.ServoController - CPU issues and how to resolve them? using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Magellanic.ServoController; using System; using System.Threading.Tasks; namespace App5 { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } private async Task MoveServoToCentre(double MovementTime, double CalculatedFrequency) { using (var servo = new ServoController(5)) { servo.Frequency = Convert.ToInt32(CalculatedFrequency); await servo.Connect(); servo.SetPosition(90).AllowTimeToMove(Convert.ToInt32(MovementTime)).Go(); servo.Dispose(); } } private async void Slider_Value_ChangedAsync(object sender, Windows.UI.Xaml.Controls.Primitives.RangeBaseValueChangedEventArgs e) { Slider slider = sender as Slider; if (slider.Value < 50) { await CalculationAsync(slider.Value); } else if (slider.Value > 50) { await CalculationAsync(slider.Value); } } public string PreviousValue; public double RequiredFrequenz; private async Task CalculationAsync(double value) { if (PreviousValue != null) { double Result = System.Math.Abs(Convert.ToDouble(PreviousValue)) - value; if (Result < 0) { RequiredFrequenz = 100; } else if (Result > 0) { RequiredFrequenz = 200; } double TimeToMove = System.Math.Abs(((int)Math.Round(Result * 3.8))); System.Diagnostics.Debug.WriteLine(TimeToMove); System.Diagnostics.Debug.WriteLine(Result); System.Diagnostics.Debug.WriteLine(value); System.Diagnostics.Debug.WriteLine(PreviousValue); System.Diagnostics.Debug.WriteLine(RequiredFrequenz); await MoveServoToCentre(TimeToMove, RequiredFrequenz); TimeToMove = 0; } else { if (value > 50) { double TimeToMove = ((int)Math.Round((value - 50) * 3.8)); RequiredFrequenz = 100; System.Diagnostics.Debug.WriteLine(TimeToMove); System.Diagnostics.Debug.WriteLine(value); System.Diagnostics.Debug.WriteLine(PreviousValue); System.Diagnostics.Debug.WriteLine(RequiredFrequenz); await MoveServoToCentre(TimeToMove, RequiredFrequenz); TimeToMove = 0; } else if (value < 50) { double TimeToMove = System.Math.Abs(((int)Math.Round((value - 50) * 3.8))); RequiredFrequenz = 200; System.Diagnostics.Debug.WriteLine(TimeToMove); System.Diagnostics.Debug.WriteLine(value); System.Diagnostics.Debug.WriteLine(PreviousValue); System.Diagnostics.Debug.WriteLine(RequiredFrequenz); await MoveServoToCentre(TimeToMove, RequiredFrequenz); TimeToMove = 0; } } PreviousValue = value.ToString(); value = 0; } } }[/pre] Here a link to the code of the NuGet PlugIn: https://github.com/jeremylindsayni/Magellanic.ServoController/blob/master/ServoController.cs ServoController servo = new ServoController(5); protected override async void OnNavigatedTo(NavigationEventArgs e) { await servo.Connect(); } private void MoveServoToCentre(double MovementTime, double CalculatedFrequency) { servo.Frequency = Convert.ToInt32(CalculatedFrequency); servo.SetPosition(90).AllowTimeToMove(Convert.ToInt32(MovementTime)).Go(); }[/pre] shareimprove this answer Introduction In this article, I am explaining about controlling the ServoMotor using Potentiometer in Arduino. In this article, I will control the Servo Motor in the adjustment of Potentiometer. Parts of the List
Connection Servo Motor
Potentiometer
x0
|