Created
March 24, 2016 14:07
-
-
Save jsturtevant/6aa9832d0072438c1069 to your computer and use it in GitHub Desktop.
Windows IoT 10 Servo Motor Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Microsoft. All rights reserved. | |
// Modified and Licensed under MIT from https://github.com/ms-iot/samples | |
using Windows.ApplicationModel.Background; | |
using Windows.Devices.Gpio; | |
using System.Diagnostics; | |
using Windows.System.Threading; | |
// The Background Application template is documented at http://go.microsoft.com/fwlink/?LinkID=533884&clcid=0x409 | |
namespace ServoMotorBasics | |
{ | |
public sealed class StartupTask : IBackgroundTask | |
{ | |
BackgroundTaskDeferral deferral; | |
GpioPin servoPin; | |
ThreadPoolTimer timer; | |
//A pulse of 2ms moves the servo clockwise | |
double ForwardPulseWidth = 2; | |
//A pulse of 1ms moves the servo counterclockwise | |
double BackwardPulseWidth = 1; | |
double PulseFrequency = 20; | |
double currentPulseWidth; | |
Stopwatch stopwatch; | |
public void Run(IBackgroundTaskInstance taskInstance) | |
{ | |
deferral = taskInstance.GetDeferral(); | |
//Motor starts off | |
currentPulseWidth = 0; | |
//The stopwatch will be used to precisely time calls to pulse the motor. | |
stopwatch = Stopwatch.StartNew(); | |
GpioController controller = GpioController.GetDefault(); | |
servoPin = controller.OpenPin(13); | |
servoPin.SetDriveMode(GpioPinDriveMode.Output); | |
// Here is how you would move the motor. Call a funciton like this when ever you are ready to move motor | |
MoveMotor(ForwardPulseWidth); | |
Wait(3000); //wait three seconds | |
//move it backwards | |
MoveMotor(BackwardPulseWidth); | |
} | |
private void MoveMotor(double direction) | |
{ | |
//If a button is pressed the pulsewidth is changed to cause the motor to spin in the appropriate direction | |
//Write the pin high for the appropriate length of time | |
if (currentPulseWidth != 0) | |
{ | |
servoPin.Write(GpioPinValue.High); | |
} | |
//Use the wait helper method to wait for the length of the pulse | |
Wait(currentPulseWidth); | |
//The pulse if over and so set the pin to low and then wait until it's time for the next pulse | |
servoPin.Write(GpioPinValue.Low); | |
Wait(PulseFrequency - currentPulseWidth); | |
} | |
//A synchronous wait is used to avoid yielding the thread | |
//This method calculates the number of CPU ticks will elapse in the specified time and spins | |
//in a loop until that threshold is hit. This allows for very precise timing. | |
private void Wait(double milliseconds) | |
{ | |
long initialTick = stopwatch.ElapsedTicks; | |
long initialElapsed = stopwatch.ElapsedMilliseconds; | |
double desiredTicks = milliseconds / 1000.0 * Stopwatch.Frequency; | |
double finalTick = initialTick + desiredTicks; | |
while (stopwatch.ElapsedTicks < finalTick) | |
{ | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, how are you? I'm using the translator to be able to write in English, hahaha. And I want to ask a question :c as it is implemented this code in a project? It is that days ago I try to operate a servomotor with a Raspberry pi 3 and Windows 10 IoT. You would greatly appreciate if I can guide, please.