2

I am working on raspberry pi 2 mod B with windows IOT. I am trying to create a background process where in I can start rotation of my DC motor using L298N motor driver. At back ground instance the code runs but the motor doesn't move and the code for the motion is running perfectly when put in a foreground task. where am i going wrong? My code is:

public sealed class TileUpdateTask : IBackgroundTask
{
    private const int DC_Black = 5;
    private const int DC_Red = 6;
    private GpioPin Black_1;
    private GpioPin Red_2;
    private GpioPinValue pinValue1;
    private GpioPinValue pinValue2;

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
        InitGPIO();
        MessageDialog mybox = new MessageDialog("this shit actually works!!!!!");
        mybox.Title = "It worked";
        await mybox.ShowAsync();
        deferral.Complete();
    }

    private void  InitGPIO()
    {
        var gpio = GpioController.GetDefault();
        // Show an error if there is no GPIO controller
        if (gpio == null)
        {
            Black_1 = null;
            return;
        }
        Black_1 = gpio.OpenPin(DC_Black);
        Red_2 = gpio.OpenPin(DC_Red);
        analogSigGen();
    }

    public async void analogSigGen()
    {
        for (int i = 0; i < 100000; i++)
        {
            pinValue1 = GpioPinValue.Low;
            Black_1.Write(pinValue1);
            Black_1.SetDriveMode(GpioPinDriveMode.Output);
            pinValue2 = GpioPinValue.Low;
            Red_2.Write(pinValue2);
            Red_2.SetDriveMode(GpioPinDriveMode.Output);

            await Task.Delay(10000);

            pinValue1 = GpioPinValue.High;
            Black_1.Write(pinValue1);
            Black_1.SetDriveMode(GpioPinDriveMode.Output);
            pinValue2 = GpioPinValue.Low;
            Red_2.Write(pinValue2);
            Red_2.SetDriveMode(GpioPinDriveMode.Output);

            await Task.Delay(10000);

            pinValue1 = GpioPinValue.Low;
            Black_1.Write(pinValue1);
            Black_1.SetDriveMode(GpioPinDriveMode.Output);
            pinValue2 = GpioPinValue.Low;
            Red_2.Write(pinValue2);
            Red_2.SetDriveMode(GpioPinDriveMode.Output);

            await Task.Delay(10000);

            pinValue1 = GpioPinValue.Low;
            Black_1.Write(pinValue1);
            Black_1.SetDriveMode(GpioPinDriveMode.Output);
            pinValue2 = GpioPinValue.High;
            Red_2.Write(pinValue2);
            Red_2.SetDriveMode(GpioPinDriveMode.Output);
            await Task.Delay(10000);
        }
    }
}

Also the message box is not shown. This is the code of my background Task Am I missing something here? The code where I am defining the task in the MainPage.xaml.cs is as follows:

private async void RegisterBackgroundTask()
    {
        var taskName = "TileUpdateTask";

        var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
        if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
        {
            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                if (task.Value.Name == taskName)
                {
                    return;
                }
            }
            BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
            taskBuilder.Name = taskName;
            taskBuilder.TaskEntryPoint = typeof(BackgroundTasks.TileUpdateTask).FullName;
            taskBuilder.SetTrigger(new Windows.ApplicationModel.Background.TimeTrigger(15,false)); //////////////////////////////here!!!!!!!!
            var registeration = taskBuilder.Register();
        }

    }

This is called in the OnNavigatedTo method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.