Beispiel #1
0
        public async Task InitAsync()
        {
            var listing = await AppServiceCatalog.FindAppServiceProvidersAsync("com.softwarelogistics.gcodemachine");

            if (listing != null && listing.Any())
            {
                var packageName = listing[0].PackageFamilyName;
                Debug.WriteLine("Found Package - Opening: " + packageName);

                _appServiceConnection = new AppServiceConnection();
                _appServiceConnection.PackageFamilyName = packageName;
                _appServiceConnection.AppServiceName    = "com.softwarelogistics.gcodemachine";

                var status = await _appServiceConnection.OpenAsync();

                if (status == AppServiceConnectionStatus.Success)
                {
                    Debug.WriteLine("Success Opening");
                    _appServiceConnection.RequestReceived += _appServiceConnection_RequestReceived;
                }
                else
                {
                    Debug.WriteLine("Failed Opening " + status.ToString());
                }

                _appService = new AppService(_appServiceConnection);
            }
            else
            {
                Debug.Write("Could not connect to background service.");
            }

            if (LightningProvider.IsLightningEnabled)
            {
                var gpioController = (await GpioController.GetControllersAsync(LightningGpioProvider.GetGpioProvider()))[0];
                foreach (var axis in _axes)
                {
                    axis.Init(gpioController, _appService);
                }

                _topLight.Init(gpioController);
                _bottomLight.Init(gpioController);
                _vacuum.Init(gpioController);

                _motorPower.Init(gpioController);
                _motorPower.Disable();
            }
            else
            {
                Debug.WriteLine("Ligtning IS NOT Enabled, please enable through Device Portal");
            }

            StartWorkLoop();
        }
        /// <summary>
        /// Configures the <see cref="LightningProvider"/> when enabled.
        /// </summary>
        /// <remarks>
        /// Not thread safe, must be called in a thread safe context.
        /// </remarks>
        public static void Initialize()
        {
            // Do nothing when already configured
            if (_initialized)
            {
                return;
            }
            lock (_lock)
            {
                // Thread-safe double-check lock
                if (_initialized)
                {
                    return;
                }

                // Set the Lightning Provider as the default if Lightning driver is enabled on the target device
                _lightningEnabled = LightningProvider.IsLightningEnabled;
                if (_lightningEnabled)
                {
                    LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

                    // Add multiple controllers from new lightning provider
                    Gpio = GpioController.GetControllersAsync(LightningGpioProvider.GetGpioProvider()).AsTask().GetAwaiter().GetResult();
                    I2c  = I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()).AsTask().GetAwaiter().GetResult();
                    Spi  = SpiController.GetControllersAsync(LightningSpiProvider.GetSpiProvider()).AsTask().GetAwaiter().GetResult();
                }
                else
                {
                    // Add single instance providers from old inbox driver
                    Gpio = new ReadOnlyCollection <GpioController>(new[] { GpioController.GetDefault() });
                    I2c  = new ReadOnlyCollection <I2cController>(new[] { I2cController.GetDefaultAsync().AsTask().GetAwaiter().GetResult() });
                    Spi  = new ReadOnlyCollection <SpiController>(new[] { SpiController.GetDefaultAsync().AsTask().GetAwaiter().GetResult() });
                }

                // Flag initialized
                _initialized = true;
            }
        }
Beispiel #3
0
        public MainPage()
        {
            this.mOffsetQueue = new Queue <double>();
            this.InitializeComponent();

            this.Sequences = new Dictionary <string, Sequence <double> >
            {
                { "Constant", new Sequence <double>(new double[] { 1 }) },
                { "Step", new Sequence <double>(new double[] { 0, 1 }) },
                { "Step (multi)", new Sequence <double>(new double[] { 0, 0.25, 0, 0.25, 0, 0.5, 0.25, 1 }) },
                { "Sawtooth", new Sequence <double>(new double[] { 0, 0.25, 0.5, 0.75, 1 }) },
                { "Ramp", new Sequence <double>(new double[] { 0, 0.1, 0.25, 0.4, 0.6, 1 }) },
                { "Sinusoidal", new Sequence <double>(SinusoidalSequence()) },
                { "Sinusoidal (multi)", new Sequence <double>(SinusoidalSequence(0.5), SinusoidalSequence(0.7), SinusoidalSequence(0.5), SinusoidalSequence(1)) },
                { "Random", new Sequence <double>(RandomSequence(30, 0, 1)) },
            };
            this.SelectedSequence = Sequences["Constant"];

            // populate the sequence list
            foreach (var entry in this.Sequences.Keys.OrderBy(x => x))
            {
                this.SequenceList.Items.Add(entry);
            }

            Task.Run(async() =>
            {
                try
                {
                    if (!LightningProvider.IsLightningEnabled)
                    {
                        throw new Exception("Requires Lighting to be enabled");
                    }

                    var pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[1];
                    if (pwmController == null)
                    {
                        throw new Exception("No PWM controller");
                    }

                    var gpioController = (await GpioController.GetControllersAsync(LightningGpioProvider.GetGpioProvider()))[0];
                    if (gpioController == null)
                    {
                        throw new Exception("No PWM controller");
                    }

                    pwmController.SetDesiredFrequency(PwmFrequency);
                    var motorAEnable = pwmController.OpenPin(MotorAEnablePin);
                    var motorAInput  = gpioController.OpenPin(MotorAInputPin);
                    motorAInput.SetDriveMode(GpioPinDriveMode.Output);
                    var motorBEnable = pwmController.OpenPin(MotorBEnablePin);
                    var motorBInput  = gpioController.OpenPin(MotorBInputPin);
                    motorBInput.SetDriveMode(GpioPinDriveMode.Output);

                    var motorDriver = new L239D()
                    {
                        Enable12Pin = motorAEnable,
                        Input1Pin   = motorAInput,
                        Enable34Pin = motorBEnable,
                        Input3Pin   = motorBInput,
                    };

                    motorDriver.StartMotor(L239D.InputPins.Input1);
                    motorDriver.StartMotor(L239D.InputPins.Input3);

                    while (true)
                    {
                        var sequence = this.SelectedSequence;
                        foreach (var value in sequence)
                        {
                            var power = this.MinPower + value * (this.MaxPower - this.MinPower);
                            this.mOffsetQueue.Enqueue(power);
                            motorDriver.DriveMotor(L239D.EnablePins.Enable12, power);

                            // handle offset if any
                            while (this.mOffsetQueue.Count > this.MotorOffset)
                            {
                                power = this.mOffsetQueue.Dequeue();
                            }

                            motorDriver.DriveMotor(L239D.EnablePins.Enable34, power);

                            await Task.Delay((int)(MinTime + (MaxTime - MinTime) * (1.0 - this.SpeedFactor)));

                            if (sequence != this.SelectedSequence)
                            {
                                break;
                            }
                        }
                    }

                    motorAEnable.Dispose();
                    motorAInput.Dispose();
                }
                catch (Exception ex)
                {
                    var error = ex.ToString();
                }
            });
        }