Ejemplo n.º 1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //
            // TODO: Insert code to perform background work
            //
            // If you start any asynchronous methods here, prevent the task
            // from closing prematurely by using BackgroundTaskDeferral as
            // described in http://aka.ms/backgroundtaskdeferral

            BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

            motorHat = new MotorHat();
            await motorHat.InitAsync(1600).ConfigureAwait(false);

            stepper = motorHat.GetStepper(200, 1); //200 steps/revolution, motor port 1
            stepper.SetSpeed(10);                  //10 rpm

            while (true)
            {
                stepper.step(100, MotorHat.Stepper.Command.FORWARD, MotorHat.Stepper.Style.SINGLE);
                stepper.step(100, MotorHat.Stepper.Command.BACKWARD, MotorHat.Stepper.Style.SINGLE);

                stepper.step(100, MotorHat.Stepper.Command.FORWARD, MotorHat.Stepper.Style.DOUBLE);
                stepper.step(100, MotorHat.Stepper.Command.BACKWARD, MotorHat.Stepper.Style.DOUBLE);

                stepper.step(100, MotorHat.Stepper.Command.FORWARD, MotorHat.Stepper.Style.INTERLEAVE);
                stepper.step(100, MotorHat.Stepper.Command.BACKWARD, MotorHat.Stepper.Style.INTERLEAVE);

                stepper.step(100, MotorHat.Stepper.Command.FORWARD, MotorHat.Stepper.Style.MICROSTEP);
                stepper.step(100, MotorHat.Stepper.Command.BACKWARD, MotorHat.Stepper.Style.MICROSTEP);
            }
        }
Ejemplo n.º 2
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //
            // TODO: Insert code to perform background work
            //
            // If you start any asynchronous methods here, prevent the task
            // from closing prematurely by using BackgroundTaskDeferral as
            // described in http://aka.ms/backgroundtaskdeferral
            //
            BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

            motorHat = new MotorHat();
            await motorHat.InitAsync(1600).ConfigureAwait(false);

            stepper1 = motorHat.GetStepper(200, 1); //200 steps/revolution, stepper port 1
            //           stepper2 = motorHat.GetStepper(200, 2);  //200 steps/revolution, stepper port 2
            stepper1.SetSpeed(10);                  //10 rpm
//            stepper2.SetSpeed(10); //10 rpm

            //dcMotor1 = motorHat.GetMotor(3);  //motor port 3
            dcMotor2 = motorHat.GetMotor(4);  //motor port 4

            Stepper1CancellationTokenSource = new CancellationTokenSource();
            //Stepper2CancellationTokenSource = new CancellationTokenSource();

            //Motor1CancellationTokenSource = new CancellationTokenSource();
            Motor2CancellationTokenSource = new CancellationTokenSource();

            Task stepper1Task = Task.Run(() => RunStepper(stepper1, Stepper1CancellationTokenSource.Token));
            //Task stepper2Task = Task.Run(() => RunStepper(stepper2, Stepper2CancellationTokenSource.Token));

            //Task motor1Task = Task.Run(() => RunMotor(dcMotor1, Motor1CancellationTokenSource.Token));
            Task motor2Task = Task.Run(() => RunMotor(dcMotor2, Motor2CancellationTokenSource.Token));

            List <Task> tasks = new List <Task>();

            tasks.Add(stepper1Task);
            // tasks.Add(stepper2Task);

            //tasks.Add(motor1Task);
            tasks.Add(motor2Task);

            Task.WaitAll(tasks.ToArray());

            deferral.Complete();
        }