Ejemplo n.º 1
0
        private async void btnInitialize_Click(object sender, RoutedEventArgs e)
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
            }

            gpioController = await GpioController.GetDefaultAsync();

            if (gpioController == null)
            {
                return;
            }

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

            if (pwmControllers == null)
            {
                return;
            }
            LeftMotor = new MotorControl(gpioController, pwmControllers[1]);
            LeftMotor.Initialize(LEFTINA, LEFTINB, LEFTPWM);
            RightMotor = new MotorControl(gpioController, pwmControllers[1]);
            RightMotor.Initialize(RIGHTINA, RIGHTINB, RIGHTPWM);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        // Initialize the PwmController class instance
        public async void Initialize()
        {
            try
            {
                var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

                var pwmController = pwmControllers[1];    // the device controller
                pwmController.SetDesiredFrequency(50);

                Pwm = pwmController.OpenPin(LED_pin);
                Pwm.SetActiveDutyCyclePercentage(0);  // start at 0%
                Pwm.Start();
                if (Pwm == null)
                {
                    Debug.WriteLine("ERROR! Pwm device {0} may be in use.");
                    return;
                }
                Debug.WriteLine("GPIO pin setup for Pwm.");
            }
            catch (Exception e)
            {
                Debug.WriteLine("EXEPTION CAUGHT: " + e.Message + "\n" + e.StackTrace);
                throw;
            }
        }
Ejemplo n.º 6
0
        private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

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

                var pwmController = pwmControllers[1]; // the on-device controller
                pwmController.SetDesiredFrequency(50); // try to match 50Hz

                _pin27 = pwmController.OpenPin(27);
                _pin27.SetActiveDutyCyclePercentage(0);
                _pin27.Start();
            }

            var gpioController = await GpioController.GetDefaultAsync();

            if (gpioController == null)
            {
                StatusMessage.Text = "There is no GPIO controller on this device.";
                return;
            }
            _pin22 = gpioController.OpenPin(22);
            _pin22.SetDriveMode(GpioPinDriveMode.Output);
            _pin22.Write(GpioPinValue.Low);
        }
        private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

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

                var pwmController = pwmControllers[1]; // the on-device controller
                pwmController.SetDesiredFrequency(50); // try to match 50Hz


                internetLed = new InternetLed();

                _pin18 = pwmController.OpenPin(18);

                while (true)
                {
                    await internetLed.GetThrottleFromWeb();

                    double test = internetLed.getThrottle() / 100.0;

                    _pin18.SetActiveDutyCyclePercentage(internetLed.getThrottle() / 100.0);
                    _pin18.Start();


                    await Task.Delay(200);
                }
            }
        }
Ejemplo n.º 8
0
        protected override async Task <PwmController> GetController()
        {
            //return await Pwm​Controller.GetDefaultAsync();
            // TODO: how to get soft PWM without guessing or studying source code?
            // https://github.com/ms-iot/lightning/blob/develop/Providers/PwmDeviceProvider.cpp
            var controllers = await Pwm​Controller.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

            return(controllers[1]);    // software PWM
        }
Ejemplo n.º 9
0
        async void OpenPwnPin(int iPin)
        {
            var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

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

            pwmController.SetDesiredFrequency(50); // try to match 50Hz, or 20ms
            _servoPin = pwmController.OpenPin(iPin);
            _servoPin.Start();
            _servoPin.SetActiveDutyCyclePercentage(0);
        }
Ejemplo n.º 10
0
        private async Task SetupPWM()
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
                var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

                var pwmController = pwmControllers[1];                 // use the on-device controller
                pwmController.SetDesiredFrequency(50);                 // try to match 50Hz
                pin = pwmController.OpenPin(21);
            }
        }
Ejemplo n.º 11
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;
            }
        }
Ejemplo n.º 12
0
        private async void InitGPIO()
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
                var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

                _pwmController = pwmControllers[1];

                _outputPin = _pwmController.OpenPin(18);
                _outputPin.SetActiveDutyCyclePercentage(0.012);
                _outputPin.Start();
            }
        }
Ejemplo n.º 13
0
        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();
                    }
                }
            }
        }
Ejemplo n.º 14
0
        public async Task Connect()
        {
            var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

            if (pwmControllers != null)
            {
                // use the on-device controller
                var pwmController = pwmControllers[1];

                // Set the frequency, defaulted to 50Hz
                pwmController.SetDesiredFrequency(Frequency);

                ServoGpioPin = pwmController.OpenPin(ServoPin);
            }
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
        public static async Task <PinManager> CreateAsync()
        {
            if (!LightningProvider.IsLightningEnabled)
            {
                throw new NotSupportedException("Controller needs Ligtning provider");
            }

            var gpioController = await GpioController.GetDefaultAsync();

            var pwmController = await PwmController.GetDefaultAsync();

            var pwmProvider = LightningPwmProvider.GetPwmProvider();

            var list       = (await PwmController.GetControllersAsync(pwmProvider));
            var length     = list.Count;
            var controller = list[1];

            controller.SetDesiredFrequency(50);
            return(new PinManager(gpioController, controller));
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
0
        private async void button2_Click(object sender, RoutedEventArgs e)
        {

            //pinValue = GpioPinValue.Low;
            //pin.Write(GpioPinValue.Low);
            //LED.Fill = redBrush;



            LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

                var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());
                var pwmController = pwmControllers[0]; // use the on-device controller
                pwmController.SetDesiredFrequency(50); // try to match 50Hz

               var _pin = pwmController.OpenPin(LED_PIN);
                _pin.SetActiveDutyCyclePercentage(.75);
                _pin.Start();
            
        }
Ejemplo n.º 20
0
        private async void btnPwmInitialize_Click(object sender, RoutedEventArgs e)
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
            }

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

            if (pwmControllers != null)
            {
                // use the on-device controller
                var pwmController = pwmControllers[1];

                // Set the frequency, defaulted to 50Hz
                pwmController.SetDesiredFrequency(pwmController.MinFrequency);

                // Open pin 5 for pulse width modulation
                var servoGpioPin = pwmController.OpenPin(18);

                // Set the Duty Cycle - 0.05 will set the servo to its 0 degree position
                servoGpioPin.SetActiveDutyCyclePercentage(.9);

                // Start PWN from pin 5, and give the servo a second to move to position
                servoGpioPin.Start();
                //Task.Delay(1000).Wait();
                //servoGpioPin.Stop();

                //// Set the Duty Cycle - 0.1 will set the servo to its 180 degree position
                //servoGpioPin.SetActiveDutyCyclePercentage(0.1);

                //// Start PWN from pin 5, and give the servo a second to move to position
                //servoGpioPin.Start();
                //Task.Delay(1000).Wait();
                //servoGpioPin.Stop();
            }
            //var infraredSensor = new InfraredSensor(4);
            //infraredSensor.Initialize();

            //infraredSensor.InterruptHandler += InfraredSensor_InterruptHandler;
        }
Ejemplo n.º 21
0
        async void OnLoaded(Object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            if (Microsoft.IoT.Lightning.Providers.LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
            }
            else
            {
                return;
            }

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

            if (controllers == null)
            {
                return;
            }
            var controller = controllers[1];

            controller.SetDesiredFrequency(50.0);

            _pwmPin = controller.OpenPin(LED_PIN);
            _pwmPin.SetActiveDutyCyclePercentage(1.0);
            _pwmPin.Start();

            while (true)
            {
                for (var i = INCREMENT; i > 0; i--)
                {
                    var brightness = i / INCREMENT;
                    _pwmPin.SetActiveDutyCyclePercentage(brightness);
                    Task.Delay(DELAY).Wait();
                }
                for (var i = 0; i < INCREMENT; i++)
                {
                    var brightness = i / INCREMENT;
                    _pwmPin.SetActiveDutyCyclePercentage(brightness);
                    Task.Delay(DELAY).Wait();
                }
            }
        }
Ejemplo n.º 22
0
        private async static Task <bool> Initialise()
        {
            IsInitialising = true;
            bool status = false;

            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
                var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());

                pwmController = pwmControllers[1];
                pwmController.SetDesiredFrequency(700);
                status = true;
            }
            else
            {
                Debug.WriteLine("Lightning not enabled");
            }
            IsInitialising = false;
            return(status);
        }
Ejemplo n.º 23
0
        private async Task InitGpio()
        {
            if (LightningProvider.IsLightningEnabled)
            {
                // Do something with the Lightning providers
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

                _pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[1];
                _servoPin      = _pwmController.OpenPin(_servoPinNum);
                _servoPin.SetActiveDutyCyclePercentage(0);
                _servoPin.Start();

                _motorPin = _pwmController.OpenPin(_motorPinNumber);
                _pwmController.SetDesiredFrequency(50);
                _motorPin.SetActiveDutyCyclePercentage(0);
                _motorPin.Start();
            }
            else
            {
                Debug.WriteLine("Please enable lightning providers");
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Initialize
        /// </summary>
        public async Task InitializeAsync()
        {
            if (!LightningProvider.IsLightningEnabled)
            {
                throw new NullReferenceException("Lightning isn't enabled !");
            }

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

            redPin = controller.OpenPin(redPinNum);
            redPin.SetActiveDutyCyclePercentage(0);
            redPin.Start();

            greenPin = controller.OpenPin(greenPinNum);
            greenPin.SetActiveDutyCyclePercentage(0);
            greenPin.Start();

            bluePin = controller.OpenPin(bluePinNum);
            bluePin.SetActiveDutyCyclePercentage(0);
            bluePin.Start();
        }
Ejemplo n.º 25
0
        private async Task InitGPIO()
        {
            if (LightningProvider.IsLightningEnabled)
            {
                // Do something with the Lightning providers
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

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

                var pwmController = pwmControllers[1]; // the on-device controller
                pwmController.SetDesiredFrequency(50); // try to match 50Hz

                motorPin = pwmController.OpenPin(_motorPinNumber);
                motorPin.SetActiveDutyCyclePercentage(RestingPulseLegnth);
                motorPin.Start();

                _servoPin = pwmController.OpenPin(_servoPinNum);
                _servoPin.SetActiveDutyCyclePercentage(0);
                _servoPin.Start();
            }
            else
            {
                StatusMessage.Text = "Please enable lightning providers";
            }

            var gpioController = await GpioController.GetDefaultAsync();

            if (gpioController == null)
            {
                StatusMessage.Text = "There is no GPIO controller on this device.";
                return;
            }

            _ledPin = gpioController.OpenPin(_ledPinNumber);
            _ledPin.SetDriveMode(GpioPinDriveMode.Output);
            _ledPin.Write(GpioPinValue.Low);
        }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
0
        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;
            }
        }
Ejemplo n.º 28
0
        private async void Initialize()
        {
            //Set everithing ready for PWM mode
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
            }

            var gpio = GpioController.GetDefault();

            GPIOPIN_LASER = new GpioPin[5];

            //Init lasers states
            for (int i = 0; i < LASER_PIN.Count; i++)
            {
                GPIOPIN_LASER[i] = gpio.OpenPin(LASER_PIN[i]);
                GPIOPIN_LASER[i].Write(GpioPinValue.Low);
                GPIOPIN_LASER[i].SetDriveMode(GpioPinDriveMode.Output);
            }

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

            if (pwmControllers != null)
            {
                var pwmController = pwmControllers[1];
                pwmController.SetDesiredFrequency(50);

                //Open the two pin to start working
                servoGpioPinX = pwmController.OpenPin(X_PIN);
                servoGpioPinY = pwmController.OpenPin(Y_PIN);
                MakeRandom();
            }
            else
            {
                GlobalDeferal.Complete();
            }
        }
Ejemplo n.º 29
0
        public async Task InitializeHardware()
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

                var pmwProvider = LightningPwmProvider.GetPwmProvider();

//                var pwmControllers = await Windows.Devices.Pwm.PwmController.GetControllersAsync(pmwProvider);
                var pwmControllers = Windows.Devices.Pwm.PwmController.GetControllersAsync(pmwProvider).GetAwaiter().GetResult();
                var pwmController  = pwmControllers[1]; // the on-device controller

                // This seems to go bad
                //pwmController.SetDesiredFrequency(100);

                _gpioController = await GpioController.GetDefaultAsync();

                IPin GetInputPin(int pinNr)
                {
                    return(new GpioPinWrapper(_gpioController.OpenPin(pinNr, GpioSharingMode.Exclusive), GpioPinDriveMode.InputPullUp));
                }

                _gateStates = new GateStates();
                _gateStates.Add(
                    // Center switches (on bread board)
                    new GateState(
                        // Yellow button, Closed
                        GetInputPin(27),
                        // Red button, Open
                        GetInputPin(22)),
                    1);
                _gateStates.Add(
                    // Top switches (on bread board)
                    new GateState(
                        // Yellow button, Closed
                        GetInputPin(4),
                        // Red button, Open
                        GetInputPin(17)),
                    2);

                IPwmPin GetPwmPin(int pinNr)
                {
                    //var pin = _gpioController.OpenPin(pinNr);
                    //pin.SetDriveMode(GpioPinDriveMode.Output);
                    return(new PwmPinWrapper(pwmController.OpenPin(pinNr)));
                }

                GateController =
                    new GateController(
                        // TODO : pins in settings, selector: 6, 13, 19
                        new Selector(GetOutputPins(6, 13, 19)),
                        new PwmController(
                            // TODO : pins in settings: 20, 21
                            GetPwmPin(20),
                            GetPwmPin(21),
                            // TODO : steps per change in settings
                            3),
                        _gateStates);

                // TODO : pins in settings: 24
                _heartBeatPin   = GetOutputPins(24).First();
                _heartBeatTimer = new Timer(HandleHeartBeat, _heartBeatState, 100, Timeout.Infinite);
            }
        }
Ejemplo n.º 30
0
        /// <summary>
        /// 链接到指定的L298N电机控制器
        /// </summary>
        private Motor(int [] gpios, int freq)
        {
            IN        = new PwmPin[PinNumbers];
            Frequency = freq;

            if (LightningProvider.IsLightningEnabled)
            {
                //获取控制器
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
                //获取系统Pwm控制器
                Task <IReadOnlyList <PwmController> > t = PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()).AsTask <IReadOnlyList <PwmController> >();
                t.Wait();
                PwmController controller = t.Result[1];
                //设置频率
                controller.SetDesiredFrequency(Frequency);
                //关联引脚
                for (int i = 0; i < PinNumbers; ++i)
                {
                    IN[i] = controller.OpenPin(gpios[i]);
                    IN[i].SetActiveDutyCyclePercentage(0);
                    IN[i].Start();
                }
            }
        }