public void StopNav() { MotorLeftNegative.Write(GpioPinValue.Low); MotorLeftPositive.Write(GpioPinValue.Low); MotorRightNegative.Write(GpioPinValue.Low); MotorRightPositive.Write(GpioPinValue.Low); pwmMotorLeft.Stop(); pwmMotorRight.Stop(); }
public void TiltUp() { mTopTiltServo.SetActiveDutyCyclePercentage(Math.Max(mBottomPanServo.GetActiveDutyCyclePercentage() - 0.01, 0)); mTopTiltServo.Start(); Task.Run(() => { System.Threading.Tasks.Task.Delay(100).Wait(); mTopTiltServo.Stop(); }); }
/// <inheritdoc/> public void Dispose() { if (bluePin != null) { if (bluePin.IsStarted) { bluePin.Stop(); } bluePin.Dispose(); bluePin = null; } if (greenPin != null) { if (greenPin.IsStarted) { greenPin.Stop(); } greenPin.Dispose(); greenPin = null; } if (redPin != null) { if (redPin.IsStarted) { redPin.Stop(); } redPin.Dispose(); redPin = null; } }
private void Timer_Tick(ThreadPoolTimer timer) { if (iteration > 4) { timer.Cancel(); motorPin.Stop(); motorPin.Dispose(); return; } iteration++; System.Diagnostics.Debug.WriteLine(iteration); if (iteration == 1 || iteration == 3) { currentPulseLength = ClockwisePulseLength; secondPulseLength = CounterClockwisePulseLength; } if (iteration == 2 || iteration == 4) { currentPulseLength = CounterClockwisePulseLength; secondPulseLength = ClockwisePulseLength; } double desiredPercentage = currentPulseLength / (1000.0 / pwmController.ActualFrequency); motorPin.SetActiveDutyCyclePercentage(desiredPercentage); }
public async void StepperMotorExample() { MotorHat2348 mh = null; PwmStepperMotor stepper = null; PwmPin pwm = null; if (mh == null) { // Create a driver object for the HAT at address 0x60 mh = new MotorHat2348(0x60); // Create a stepper motor object at the specified ports and steps per rev stepper = mh.CreateStepperMotor(1, 2, 200); // Create a PwmPin object at one of the auxiliary PWMs on the HAT pwm = mh.CreatePwm(1); } // step 200 full steps in the forward direction using half stepping (so 400 steps total) at 30 rpm stepper.SetSpeed(30); await stepper.StepAsync(200, Direction.Forward, SteppingStyle.Half); // Activate the pin and set it to 50% duty cycle pwm.Start(); pwm.SetActiveDutyCyclePercentage(0.5); // for demonstration purposes we will wait 10 seconds to observe the PWM and motor operation. await Task.Delay(10000); // Stop the auxiliary PWM pin pwm.Stop(); // Dispose of the MotorHat and free all its resources mh.Dispose(); }
private void StopPin(PwmPin pin) { if (pin.IsStarted) { pin.Stop(); } }
private void Timer_Tick(ThreadPoolTimer timer) { if (iteration > 18) { timer.Cancel(); motorPin.Stop(); return; } iteration++; if (iteration % 3 == 0) { currentPulseLength = ClockwisePulseLength; secondPulseLength = CounterClockwisePulseLegnth; } else if (iteration % 3 == 1) { currentPulseLength = CounterClockwisePulseLegnth; secondPulseLength = ClockwisePulseLength; } else { currentPulseLength = 0; secondPulseLength = 0; } double desiredPercentage = currentPulseLength / (1000.0 / pwmController.ActualFrequency); motorPin.SetActiveDutyCyclePercentage(desiredPercentage); }
/// <summary> /// Free up our resources. /// </summary> public void Dispose() { pin.Stop(); pin.Dispose(); pin = null; t.Dispose(); t = null; }
virtual protected void Dispose(bool disposing) { if (disposing) { _pin.Stop(); _pin.Dispose(); _pin = null; } }
/// <summary>Stops playback.</summary> public void Stop() { if (IsPlaying) { lock (_syncRoot) _playlist.Clear(); _worker.Join(250); if (_worker != null && _worker.IsAlive) { _worker.Abort(); } } _pwmPin.Controller.SetDesiredFrequency(100.0); _pwmPin.SetActiveDutyCyclePercentage(0.0001); _pwmPin.Stop(); }
public void SetARGB(float A, float R, float G, float B) { if (Rpin.IsStarted) { Rpin.Stop(); } if (Gpin.IsStarted) { Gpin.Stop(); } if (Bpin.IsStarted) { Bpin.Stop(); } Rpin.SetActiveDutyCyclePercentage(1 - (A * R)); Gpin.SetActiveDutyCyclePercentage(1 - (A * G)); Bpin.SetActiveDutyCyclePercentage(1 - (A * B)); Rpin.Start(); Gpin.Start(); Bpin.Start(); }
private void OnPageUnload(object sender, RoutedEventArgs e) { if (leftDrive != null) { leftDrive.Stop(); } if (rightDrive != null) { rightDrive.Stop(); } }
/// <summary> /// Cleanup /// </summary> public void Dispose() { redPin.Stop(); redPin.Dispose(); greenPin.Stop(); greenPin.Dispose(); bluePin.Stop(); bluePin.Dispose(); }
public static void StopMotor(Servo servo) { if (servo == Servo.A) { PWMA.Stop(); } else { PWMB.Stop(); } }
/// <summary> /// If you dont move the servo outter forces can disorient it so we have to refresh it periodically. /// </summary> /// <param name="state"></param> private void TimerTick(object state) { // do some work not connected with UI pin.SetActiveDutyCyclePercentage(percentage); lock (lockObject) { pin.Start(); Task.Delay(SIGNAL_DURATION).Wait(); pin.Stop(); } /* * await Window.Current.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, * () => * { * * // do some work on UI here; * }); */ }
public void RotateServo() { // Start the servo with pwm pin.SetActiveDutyCyclePercentage(currPercent); pin.Start(); // Update the previous duty cycle prevPercent = currPercent; // Wait 1s for servo to turn Task.Delay(1000).Wait(); // Stop the servo pin.Stop(); }
public void SetAngle(float angle) { if (!inited) { Debug.WriteLine("Servo Not Inited"); return; } //if(moving) //{ // Debug.WriteLine("Servo already moving"); // return; //} pwmPin.Stop(); float convertedAngle = ConvertAngleFromRange(angle); float anglePercentage = Math.Abs(currentAngle - convertedAngle) / 180f; double dutyCycleValue = GetDutyCyclePercentageForAngle(convertedAngle); int delay = (int)(MAX_MOVE_DELAY_MILLISECONDS * anglePercentage); delay = Math.Clamp(delay, 100, MAX_MOVE_DELAY_MILLISECONDS); Debug.WriteLine("Delay: " + delay); Debug.WriteLine("Duty Cycle: " + dutyCycleValue); pwmPin.SetActiveDutyCyclePercentage(dutyCycleValue); moving = true; pwmPin.Start(); Task.Delay(delay).Wait(); pwmPin.Stop(); moving = false; currentAngle = angle; }
public MainPage() { InitializeComponent(); LED.Fill = new SolidColorBrush(Windows.UI.Colors.Blue); timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(500); timer.Tick += Timer_Tick; if (LightningProvider.IsLightningEnabled) { LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider(); var pwmControllers = PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()).AsTask().Result; var pwm = pwmControllers[1]; // use the on-device controller if (pwm == null) { pinR = pinG = null; GpioStatus.Text = "There is no PWM controller on this device."; } else { pwm.SetDesiredFrequency(50); pinR = pwm.OpenPin(17); pinR.Polarity = PwmPulsePolarity.ActiveLow; pinR.SetActiveDutyCyclePercentage(0); pinR.Stop(); pinG = pwm.OpenPin(18); pinG.Polarity = PwmPulsePolarity.ActiveLow; pinG.SetActiveDutyCyclePercentage(0); pinG.Stop(); GpioStatus.Text = "PWM pins initialized correctly."; pinR.Start(); pinG.Start(); if (pinR != null && pinG != null) { timer.Start(); } } } }
/// <inheritdoc /> public void SetActiveDutyCyclePercent(int newDutyCycle) { double newDutyCyclePercent = Math.Round(newDutyCycle / 100D, 2); _wrappedPin.SetActiveDutyCyclePercentage(newDutyCyclePercent); if (!_wrappedPin.IsStarted) { _wrappedPin.Start(); } if (_wrappedPin.IsStarted && newDutyCyclePercent < 0.01) { _wrappedPin.Stop(); } }
static ZumoBot() { led = GpioController.GetDefault().OpenPin(FEZ.GpioPin.D13); led.SetDriveMode(GpioPinDriveMode.Output); button = GpioController.GetDefault().OpenPin(FEZ.GpioPin.D12); button.SetDriveMode(GpioPinDriveMode.InputPullUp); PwmController pwm = PwmController.FromId(FEZ.PwmPin.Controller3.Id); pwm.SetDesiredFrequency(4 * 1000); Buzzer = pwm.OpenPin(FEZ.PwmPin.Controller3.D6); // D3 or D6 Buzzer.Stop(); Buzzer.SetActiveDutyCyclePercentage(0.5); voltage = AdcController.GetDefault().OpenChannel(FEZ.AdcChannel.A1); }
static Motors() { PWM.SetDesiredFrequency(6000); M1PWM = PWM.OpenPin(FEZ.PwmPin.Controller3.D9); M1PWM.Stop(); M1PWM.SetActiveDutyCyclePercentage(0.1); M1PWM.Start(); M2PWM = PWM.OpenPin(FEZ.PwmPin.Controller3.D10); M2PWM.Stop(); M2PWM.SetActiveDutyCyclePercentage(0.1); M2PWM.Start(); M1DIR = GpioController.GetDefault().OpenPin(FEZ.GpioPin.D7); M1DIR.SetDriveMode(GpioPinDriveMode.Output); M1DIR.Write(GpioPinValue.High); M2DIR = GpioController.GetDefault().OpenPin(FEZ.GpioPin.D8); M2DIR.Write(GpioPinValue.High); M2DIR.SetDriveMode(GpioPinDriveMode.Output); }
private void Timer_Tick(object state) { int result = ReadADC(); double buzzFreq = result * 0.9375; if (buzzFreq < 150) { buzzer.Stop(); } if (buzzFreq >= 150) { buzzer.Start(); } if (buzzFreq > 1000) { buzzFreq = 1000; } pwmController.SetDesiredFrequency(buzzFreq); }
private async Task init() { try { LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider(); var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()); var pwmControllerPan = pwmControllers[1]; pwmControllerPan.SetDesiredFrequency(350); mPwmController = pwmControllerPan; pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()); pwmControlleTilt = pwmControllers[1]; pwmControlleTilt.SetDesiredFrequency(450); mTopTiltServo = pwmControlleTilt.OpenPin(24); mBottomPanServo = mPwmController.OpenPin(25); mTopTiltServo.SetActiveDutyCyclePercentage(0.5); mBottomPanServo.SetActiveDutyCyclePercentage(0.5); mTopTiltServo.Start(); mBottomPanServo.Start(); Task.Run(() => { System.Threading.Tasks.Task.Delay(250).Wait(); mBottomPanServo.Stop(); mTopTiltServo.Stop(); }); } catch (Exception ex) { throw; } }
public void SetMotorConfig() { //Connect the motor Enabler to PWM pin pwmMotorLeft = pwmController.OpenPin(PWMLEFT); pwmMotorLeft.SetActiveDutyCyclePercentage(INITIALPOWER / 100); pwmMotorLeft.Stop(); pwmMotorRight = pwmController.OpenPin(PWMRIGHT); pwmMotorRight.SetActiveDutyCyclePercentage(INITIALPOWER / 100); pwmMotorRight.Stop(); //Assign the Motor Terminals to gpio pins MotorLeftNegative = gpio.OpenPin(MOTORLEFTN, GpioSharingMode.Exclusive); MotorLeftPositive = gpio.OpenPin(MOTORLEFTP, GpioSharingMode.Exclusive); MotorRightNegative = gpio.OpenPin(MOTORRIGHTN, GpioSharingMode.Exclusive); MotorRightPositive = gpio.OpenPin(MOTORRIGHTP, GpioSharingMode.Exclusive); //set Input or Output of the pins MotorLeftNegative.SetDriveMode(GpioPinDriveMode.Output); MotorLeftPositive.SetDriveMode(GpioPinDriveMode.Output); MotorRightNegative.SetDriveMode(GpioPinDriveMode.Output); MotorRightPositive.SetDriveMode(GpioPinDriveMode.Output); /* ******Set initial values for output ports ***** * Note the following * Negative Positive Result * Low High Forward * High Low Backward * Low Low Stop * High High Right * */ MotorLeftNegative.Write(GpioPinValue.Low); MotorLeftPositive.Write(GpioPinValue.Low); MotorRightNegative.Write(GpioPinValue.Low); MotorRightPositive.Write(GpioPinValue.Low); }
public void Stop() { _motorPin.Stop(); }
public static void Main() { Console.WriteLine("Change Counter test running"); // Initialise PWM output pin PwmController pwmc = PwmController.GetDefault(); pwmc.SetDesiredFrequency(PWM_FREQUENCY); PwmPin pwmTestPin = pwmc.OpenPin(PWM_OUTPUT_PIN); pwmTestPin.SetActiveDutyCyclePercentage(0.5); Console.WriteLine($"Open PWM pin {PWM_OUTPUT_PIN} frequency {pwmc.ActualFrequency}"); Console.WriteLine($"This pin must be connected to GpioChangeCounter pin {COUNTER_INPUT_PIN}"); // Initialise count pin by opening GPIO as input GpioPin countPin = GpioController.GetDefault().OpenPin(COUNTER_INPUT_PIN); countPin.SetDriveMode(GpioPinDriveMode.InputPullUp); // Create a Counter passing in the GPIO pin GpioChangeCounter gpcc = new GpioChangeCounter(countPin); // Counter both raising and falling edges gpcc.Polarity = GpioChangePolarity.Both; Console.WriteLine($"Counter pin {COUNTER_INPUT_PIN} created"); // Start counter gpcc.Start(); // Read count before we start PWM ( should be 0 ) // We want to save the start relative time GpioChangeCount count1 = gpcc.Read(); // Start PWM signal pwmTestPin.Start(); // Wait 1 Sec Thread.Sleep(1000); // Read current count GpioChangeCount count2 = gpcc.Read(); // Stop PWM signal & counter pwmTestPin.Stop(); gpcc.Stop(); // Change polarity of counter so only counting rising edges gpcc.Polarity = GpioChangePolarity.Rising; gpcc.Start(); GpioChangeCount count3 = gpcc.Reset(); pwmTestPin.Start(); // Wait 1 Sec Thread.Sleep(1000); pwmTestPin.Stop(); // Read count GpioChangeCount count4 = gpcc.Read(); gpcc.Stop(); DisplayResults("Count pulses for 1 second with both edges", count1, count2); DisplayResults("Count pulses for 1 second with just rising edge", count3, count4); // Next test tries to measure the frequncy of the PWM signal pwmTestPin.Start(); gpcc.Start(); while (true) { // Reset Counter to zero GpioChangeCount countStart = gpcc.Reset(); Thread.Sleep(1000); // Wait 1 sec and read again GpioChangeCount countEnd = gpcc.Read(); // Sleep is not accurate so calculate actual time in secounds based of relative time differences of the 2 counts // Ticks are in 100 nano sec increments double periodSecs = (double)(countEnd.RelativeTime.Ticks - countStart.RelativeTime.Ticks) / 10000000000.0; int frequecy = (int)((double)countEnd.Count / periodSecs); Console.WriteLine($"Period {periodSecs:F6} Sec | Frequency {frequecy} Hz"); } }
/// <summary> /// Stop the PWM pin and set the pins with default values of low and 0 /// </summary> public void Stop() { _pwmPin.SetActiveDutyCyclePercentage(0); _GPIOPin.Write(GpioPinValue.Low); _pwmPin.Stop(); }
private void MainPage_Unloaded(object sender, RoutedEventArgs e) { pwmPin.Stop(); pwmPin.Dispose(); timer.Stop(); }
private void StopPwm(PwmPin pin) { pin.Stop(); }
public static void Beep(int duration) { Buzzer.Start(); Thread.Sleep(duration); Buzzer.Stop(); }