Exemple #1
0
        public void Backward(double frequency, double dutyCycle)
        {
            if (pwmController == null || servoGpioPinEn == null || gpioPinIn1 == null || gpioPinIn2 == null)
            {
                return;
            }

            var max = pwmController.MaxFrequency;
            var min = pwmController.MinFrequency;

            frequency = Math.Min(frequency, max);
            frequency = Math.Max(frequency, min);

            var maxDuty = 1.0;
            var minDuty = 0.0;

            dutyCycle = Math.Min(dutyCycle, maxDuty);
            dutyCycle = Math.Max(dutyCycle, minDuty);

            pwmController.SetDesiredFrequency(frequency);
            servoGpioPinEn.SetActiveDutyCyclePercentage(dutyCycle);

            gpioPinIn1.Write(GpioPinValue.High);
            gpioPinIn2.Write(GpioPinValue.Low);

            servoGpioPinEn.Start();
        }
Exemple #2
0
        private async Task EnsureInitializedAsync()
        {
            if (_isInitialized)
            {
                return;
            }

            // Create PWM manager
            var pwmManager = new PwmProviderManager();

            // Add providers ~ pwmControllers
            //var provider = new PCA9685Provider(_i2Caddr);
            pwmManager.Providers.Add(new PCA9685());

            // Get the well-known controller collection back
            var pwmControllers = await pwmManager.GetControllersAsync();

            _pwmController = pwmControllers.Last();
            if (_frequency > _pwmController.MaxFrequency)
            {
                _frequency = _pwmController.MaxFrequency;
            }
            _pwmController.SetDesiredFrequency(_frequency);

            _isInitialized = true;
        }
Exemple #3
0
 /// <summary>
 ///
 /// </summary>
 /// <returns></returns>
 private async Task InitBuzzer()
 {
     pwmController = (await PwmController.GetControllersAsync(PwmSoftware.PwmProviderSoftware.GetPwmProvider()))[0];
     pwmController.SetDesiredFrequency(40);
     buzzer = pwmController.OpenPin(BUZZER_PIN);
     buzzer.SetActiveDutyCyclePercentage(RestingPulseLegnth);
 }
Exemple #4
0
        public async Task <bool> Rotate(LuisResult result, object context, object speak)
        {
            var speech = (App.SpeechFunc)speak;

            var c = (Context)context;

            if (c.IsIoTCore)
            {
                speech("Feel the burn baby!");

                pwmController = (await PwmController.GetControllersAsync(PwmSoftware.PwmProviderSoftware.GetPwmProvider()))[0];
                pwmController.SetDesiredFrequency(50);

                try
                {
                    motorPin = pwmController.OpenPin(26);
                }

                catch
                { }

                motorPin.SetActiveDutyCyclePercentage(RestingPulseLegnth);
                motorPin.Start();
                iteration = 0;
                timer     = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromSeconds(1));
            }
            else
            {
                speech("I am a fully functioning PC, not a robot.");
            }
            return(true);
        }
Exemple #5
0
        public ServoMotor(string controller, int PwmPinNumber)
        {
            PwmController PWM = PwmController.FromId(controller);

            servo = PWM.OpenPin(PwmPinNumber);
            PWM.SetDesiredFrequency(1 / 0.020);
        }
Exemple #6
0
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Check presence of GPIO Controller
            // Since this is UWP, this application runs on desktop, mobile, as well as embedded devices
            // best to confirm we are running on an embedded device like R Pi
            GpioController gpio = GpioController.GetDefault();

            if (gpio == null)
            {
                Debug.WriteLine("This device does not have GPIO Controller.");
                return;
            }

            var pwmManager = new PwmProviderManager();

            pwmManager.Providers.Add(new SoftPwm());

            var pwmContollers = await pwmManager.GetControllersAsync();

            pwmController = pwmContollers[0];
            pwmController.SetDesiredFrequency(50);

            pwmPin = pwmController.OpenPin(servoPin);
            pwmPin.Start();

            timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(15)
            };
            timer.Tick += Timer_Tick;
            timer.Start();

            IsClockwise = false;
        }
Exemple #7
0
        /// <summary>
        /// Initializes the PWM controller
        /// </summary>
        /// <returns>true if successfull init, false otherwise</returns>
        public static async Task <bool> InitPWM()
        {
            // PWM Pins http://raspberrypi.stackexchange.com/questions/40812/raspberry-pi-2-b-gpio-pwm-and-interrupt-pins
            var controllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

            if (controllers.Count <= 1)
            {
                return(false);
            }
            driveControl = controllers[1];
            if (driveControl == null)
            {
                return(false);
            }
            //can't step through the below line because reasons
            try
            {
                driveControl.SetDesiredFrequency(1000);
            }
            catch
            {
                return(false);
            }
            LeftDrive  = new SMPWM();
            RightDrive = new SMPWM();
            LeftDrive.Init(5, 12, GpioController, driveControl);  //PWM grey wire, m2
            RightDrive.Init(6, 13, GpioController, driveControl); //PWM purple wire, m1
            LeftDrive.Start();
            RightDrive.Start();
            return(true);
        }
Exemple #8
0
 public static void SetMotorDuty(Servo servo, double speedPercentage, Direction direction)
 {
     if (speedPercentage < 0 || speedPercentage > 100)
     {
         throw new ArgumentOutOfRangeException("speedPercentage", "Must be between 0 and 100 %");
     }
     if (servo == Servo.A)
     {
         if (direction == Direction.Back)
         {
             DIRA.Write(GpioPinValue.Low);
         }
         else
         {
             DIRA.Write(GpioPinValue.High);
         }
         PWM1.SetDesiredFrequency(5000);
         PWMA.Start();
         PWMA.SetActiveDutyCyclePercentage(speedPercentage / 100);
     }
     else
     {
         if (direction == Direction.Forvard)
         {
             DIRB.Write(GpioPinValue.High);
         }
         else
         {
             DIRB.Write(GpioPinValue.Low);
         }
         PWM3.SetDesiredFrequency(5000);
         PWMB.Start();
         PWMB.SetActiveDutyCyclePercentage(speedPercentage / 100);
     }
 }
Exemple #9
0
        /// <summary>
        /// Constructor of Grove ServoMotor module
        /// </summary>
        /// <param name="controller">Id of pwm controller</param>
        /// <param name="pwmPinNumber">Pwm pin number of board</param>
        public ServoMotor(string controller, int pwmPinNumber)
        {
            PwmController pwm = PwmController.FromName(controller);

            _servo = pwm.OpenChannel(pwmPinNumber);
            pwm.SetDesiredFrequency(1 / 0.020);
        }
Exemple #10
0
        public void InitMotors()
        {
            Debug.WriteLine("Initialising motors");
            motorsBySide = new Dictionary <string, Motor>();

            Debug.WriteLine("Initialising driver");
            hat = new PwmController();
            hat.SetDesiredFrequency(90);

            var motor = new Motor(hat, 0);

            motorsBySide.Add("frontLeft", motor);

            motor = new Motor(hat, 4);
            motorsBySide.Add("frontRight", motor);

            motor = new Motor(hat, 8);
            motorsBySide.Add("rearRight", motor);

            motor = new Motor(hat, 12);
            motorsBySide.Add("rearLeft", motor);
            Debug.WriteLine("Initialized motors");

            ArmMotors();
        }
Exemple #11
0
        private async void LoadUserCode(object sender, RoutedEventArgs e)
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
            }
            else
            {
                return;
            }
            var controllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

            if (controllers.Count <= 1)
            {
                return;
            }
            driveControl = controllers[1];
            if (driveControl == null)
            {
                return;
            }
            if (driveControl == null)
            {
                return;
            }
            driveControl.SetDesiredFrequency(1000);
        }
Exemple #12
0
        private async void OnPageLoad(object sender, RoutedEventArgs e)
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
            }

            var gpioController = await GpioController.GetDefaultAsync();

            if (gpioController == null)
            {
                return;
            }

            var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());


            pwmController = pwmControllers[1];       //hard code from examples to use index 1
            pwmController.SetDesiredFrequency(1000); //do *not* debug over this line, it will crash
            rightDrive = pwmController.OpenPin(13);
            rightDrive.SetActiveDutyCyclePercentage(0.5);
            rightDrive.Start();
            leftDrive = pwmController.OpenPin(12);
            leftDrive.SetActiveDutyCyclePercentage(0.5);
            leftDrive.Start();
        }
Exemple #13
0
 private async void InitPwm()
 {
     //_controller = (await PwmController.GetControllersAsync(PwmPCA9685.PwmProviderSoftware.GetPwmProvider())).First();
     _controller = (await PwmController.GetControllersAsync(PwmSoftware.PwmProviderSoftware.GetPwmProvider())).First();
     _controller.SetDesiredFrequency(40);
     _pin = _controller.OpenPin(5);
     _pin.SetActiveDutyCyclePercentage(0.5);
 }
        public RgbLed(int PwmPinNumber)
        {
            //PWM redLed = new PWM(PWMChannels.PWM_PIN_D11, 100, 0, false);
            PwmController PWM = PwmController.GetDefault();

            Lamp = PWM.OpenPin(PwmPinNumber);

            PWM.SetDesiredFrequency(100);
        }
Exemple #15
0
        /// <summary>
        /// Main class constructor for Buzzer
        /// <para><b>Pins used :</b> Cs, Pwm</para>
        /// </summary>
        /// <param name="socket">The socket on which the Buzzer Click board is plugged on MikroBus.Net</param>
        /// <exception cref="System.InvalidOperationException">Thrown if some pins are already in use by another board on the same socket</exception>
        public BuzzerClick(Hardware.Socket socket)
        {
            // Initialize PWM and set initial brightness
            PWM = PwmController.FromName(socket.PwmController);
            PWM.SetDesiredFrequency(10000);
            _buzzPwm = PWM.OpenChannel(socket.PwmChannel);
            _buzzPwm.SetActiveDutyCyclePercentage(0.0);
            _buzzPwm.Start();

            _playList = new Melody();
        }
Exemple #16
0
        public ServoMotor(string pwmId, ServoType type, int pwmPin)
        {
            PwmController pwmController = PwmController.FromId(pwmId);

            pwmController.SetDesiredFrequency(50);

            this.ConfigurePulseParameters(1.0, 2.0);
            this.servo = pwmController.OpenPin(pwmPin);

            this.type     = type;
            this.Position = 0;
        }
Exemple #17
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="PWMOutputPin">Output pin for Ref frequency</param>
        /// <param name="PWMFrequency">The Reference frequency</param>
        public Reference(int PWMOutputPin, int PWMFrequency)
        {
            PwmController pwmc = PwmController.GetDefault();

            pwmc.SetDesiredFrequency(PWMFrequency);

            pwmRefFreq = pwmc.OpenPin(PWMOutputPin);
            pwmRefFreq.SetActiveDutyCyclePercentage(0.5);

            Console.WriteLine("PWM max freq = " + pwmc.MaxFrequency.ToString());
            Console.WriteLine("PWM min freq = " + pwmc.MinFrequency.ToString());
        }
Exemple #18
0
        public async Task load()
        {
            try
            {
                switch (pwmselect.SelectedIndex)
                {
                case 1:
                    if (LightningProvider.IsLightningEnabled)
                    {
                        LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
                        var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

                        pwmController = pwmControllers[int.Parse(pwmidhd.Text)];     // use the on-device controller
                        pwmController.SetDesiredFrequency(double.Parse(frahz.Text));

                        //var _pin = pwmController.OpenPin(22);
                        //_pin.SetActiveDutyCyclePercentage(.25);
                        //_pin.Start();
                    }
                    else
                    {
                        await new MessageDialog("驱动程序不正常").ShowAsync();
                    }
                    break;

                case 0:
                {
                    var pwmManager = new PwmProviderManager();
                    pwmManager.Providers.Add(new SoftPwm());
                    var pwmControllers = await pwmManager.GetControllersAsync();

                    //use the first available PWM controller an set refresh rate (Hz)
                    pwmController = pwmControllers[0];
                    pwmController.SetDesiredFrequency(double.Parse(frahz.Text));
                }

                break;

                default:
                    gpioController = await GpioController.GetDefaultAsync();

                    break;
                }
                pwmselect.IsEnabled = false;
            }
            catch (Exception err)
            {
                await new MessageDialog("初始化设备失败:" + err.ToString()).ShowAsync();
                throw;
            }
        }
Exemple #19
0
        /* We will initialize the PWM controller. For this we will be using Microsoft's Lightnening Provider
         * In Order this to work we neeed to change the Default Controller Driver to Direct Memory Access
         * Mapped driver. The Project Manifest file needs to be modified and low level devices capability
         * must be added. See documentation for details
         * */

        public async Task <bool> Initialize()
        {
            bInit = false;
            try
            {
                if (LightningProvider.IsLightningEnabled)
                {
                    LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
                }

                var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

                if (pwmControllers == null)
                {
                    return(false);
                }

                pwmController = pwmControllers[1];
                pwmController.SetDesiredFrequency(FREQUENCY); //Min: 24hz to Max: 1000 hz

                gpio = await GpioController.GetDefaultAsync();

                if (gpio == null)
                {
                    return(false);
                }

                pwmController.SetDesiredFrequency(FREQUENCY); //Min: 24hz to Max: 1000 hz
                bInit = true;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception Error {0} occured", e.ToString());
                bInit = false;
            }
            return(true);
        }
        public LightBulb()
        {
            PwmController pwmController = PwmController.FromId("TIM3");

            pwmController.SetDesiredFrequency(10000);

            this.red   = pwmController.OpenPin(BrainPad2.Light.Red);
            this.green = pwmController.OpenPin(BrainPad2.Light.Greeen);
            this.blue  = pwmController.OpenPin(BrainPad2.Light.Blue);

            this.red.Start();
            this.green.Start();
            this.blue.Start();
            this.TurnColor(0, 0, 0);
        }
Exemple #21
0
        /// <summary>
        /// This constructor is void, You must configure this class by this.SetupBasic()
        /// </summary>

        /// <summary>
        /// Initialize of pwm (it must be used after constructor PwmBasic() )
        /// </summary>
        /// <param name="__pinNumber">Pin of RPi where this pwm must be work</param>
        /// <param name="__frequency">Frequency of pwm (can change this param later)</param>
        /// <returns></returns>
        protected async Task SetupPwmCore(int __pinNumber, double __frequency)
        {
            var gpioController = GpioController.GetDefault();
            var pwmManager     = new PwmProviderManager();

            pwmManager.Providers.Add(new SoftPwm());

            var pwmControllers = await pwmManager.GetControllersAsync();

            _pwmController = pwmControllers[0];
            Frequency      = __frequency;
            _pwmController.SetDesiredFrequency(frequency);

            _pwmPin = _pwmController.OpenPin(__pinNumber);
            State   = true;
        }
Exemple #22
0
        public async void Setup(int _pinNumber, double _frequency)
        {
            var gpioController = GpioController.GetDefault();
            var pwmManager     = new PwmProviderManager();

            pwmManager.Providers.Add(new SoftPwm());

            var pwmControllers = await pwmManager.GetControllersAsync();

            _pwmController = pwmControllers[0];
            frequency      = _frequency; //TODO: get; set;
            _pwmController.SetDesiredFrequency(frequency);

            _pwmPin = _pwmController.OpenPin(_pinNumber);
            _pwmPin.Start();
        }
Exemple #23
0
        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);
        }
Exemple #24
0
        private async void Init(int pin, int frequency, int activeDutyCyclePercentage)
        {
            this.pin         = pin;
            currentFrequency = frequency;
            this.activeDutyCyclePercentage = activeDutyCyclePercentage;

            var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

            pwmController = pwmControllers[1]; // use the on-device controller

            motorPin = pwmController.OpenPin(pin);
            pwmController.SetDesiredFrequency(currentFrequency); // try to match 50Hz
            motorPin.SetActiveDutyCyclePercentage(activeDutyCyclePercentage);
            motorPin.Start();
            ready = true;
            BuzzerReady?.Invoke(this);
        }
        public void Initialize()
        {
            _redController.SetDesiredFrequency(10000);
            _greenController.SetDesiredFrequency(10000);
            _blueController.SetDesiredFrequency(10000);

            _redPin   = _redController.OpenPin(0);
            _greenPin = _greenController.OpenPin(2);
            _bluePin  = _blueController.OpenPin(4);

            _redPin.SetActiveDutyCyclePercentage(0.000f);
            _greenPin.SetActiveDutyCyclePercentage(0.000f);
            _bluePin.SetActiveDutyCyclePercentage(0.000f);

            _redPin.Start();
            _greenPin.Start();
            _bluePin.Start();
        }
        public async Task InitAsync()
        {
            pwmController = (await PwmController.GetControllersAsync(PwmSoftware.PwmProviderSoftware.GetPwmProvider()))[0];
            pwmController.SetDesiredFrequency(100);

            redPin    = InitPin(REDLED_PIN);
            orangePin = InitPin(ORANGELED_PIN);
            bluePin   = InitPin(BLUELED_PIN);

            SetCyclePercentage(redPin, 1);
            SetCyclePercentage(orangePin, 1);
            SetCyclePercentage(bluePin, 1);

            Task.Delay(50).Wait();

            SetCyclePercentage(redPin, 0);
            SetCyclePercentage(orangePin, 0);
            SetCyclePercentage(bluePin, 0);
        }
        async void InitPwm()
        {
            var gpio_controller = GpioController.GetDefault();
            var manager         = new PwmProviderManager();

            manager.Providers.Add(new SoftPwm());
            var controllers = await manager.GetControllersAsync();

            _controller = controllers[0];
            _controller.SetDesiredFrequency(120);

            _led = new RgbLed
            {
                RedPin   = _controller.OpenPin(PIN_R),
                GreenPin = _controller.OpenPin(PIN_G),
                BluePin  = _controller.OpenPin(PIN_B),
                Color    = Colors.Black
            };
        }
Exemple #28
0
        /// <summary>
        /// Initialize the servo.
        /// </summary>
        /// <returns></returns>
        public async Task InitializeAsync()
        {
            if (!LightningProvider.IsLightningEnabled)
            {
                throw new Exception("Servo can only be used with Lihtning provider");
            }


            controller = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[1];

            pin = controller.OpenPin(PIN_NUMBER);
            controller.SetDesiredFrequency(FREQUENCY);


            DesiredPulseWidth = MIDDLE_PULSE_WIDTH;
            MoveServo();

            t = new Timer(TimerTick, null, 0, TimeSpan.FromMilliseconds(100).Milliseconds);
        }
Exemple #29
0
        public async Task <bool> InitAsync(double frequency)
        {
            try
            {
                var controllers = await PwmController.GetControllersAsync(PwmSoftware.PwmProviderSoftware.GetPwmProvider());

                _pwmController = controllers?.FirstOrDefault();

                if (_pwmController != null)
                {
                    _pwmController.SetDesiredFrequency(frequency);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            return(false);
        }
Exemple #30
0
        private async Task InitServoControllerAsync()
        {
            inited = false;

            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
            }

            PwmController pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[1];

            if (pwmController != null)
            {
                pwmController.SetDesiredFrequency(DESIRED_FREQUENCY);

                pwmPin = pwmController.OpenPin(GPIOPIN);
            }

            inited = (pwmPin != null);
        }
        private void RotateExample()
        {
            try
            {

                using (var hat = new PwmController())
                {
                    hat.SetDesiredFrequency(364); // spec'd to 658?

                    while (true)
                    {

                        int s = 0;
                        int m = 8;

                        int max = (int) ((float) 4096*(0.75));
                        int min = (int) ((float) 4096*(0.28));

                        while (true)
                        {
                            s += m;
                            if (s >= max)
                            {
                                s = max;
                                m = -4;
                            }
                            if (s <= min)
                            {
                                s = min;
                                m = 4;
                            }
                            hat.SetPulseParameters(15, s);

                            Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
                        }

                        hat.SetPulseParameters(15, 0);

                        hat.SetPulseParameters(4, 40); //aux
                        hat.SetPulseParameters(3, 40); //rud
                        hat.SetPulseParameters(2, 40); //ele
                        hat.SetPulseParameters(1, 40); //ail
                        hat.SetPulseParameters(0, 40); //thr

                        Task.Delay(TimeSpan.FromSeconds(5)).Wait();

                        hat.SetPulseParameters(15, 4096);

                        hat.SetPulseParameters(4, 4055); //aux
                        hat.SetPulseParameters(3, 4055); //rud
                        hat.SetPulseParameters(2, 4055); //ele
                        hat.SetPulseParameters(1, 4055); //ail
                        hat.SetPulseParameters(0, 4055); //thr

                        Task.Delay(TimeSpan.FromSeconds(5)).Wait();
                    }
                }
            }

            /* If the write fails display the error and stop running */
            catch (Exception ex)
            {
                Text_Status.Text = "Failed to communicate with device: " + ex.Message;
                return;
            }

        }