Exemple #1
0
        /// <summary>
        /// Initializes a new <see cref="PwmDCMotor"/> instance.
        /// </summary>
        /// <param name="controller">The <see cref="Windows.Devices.Pwm.PwmController"/> to use.</param>
        /// <param name="driver">The motor driver channel (1 through 4) for coil A.</param>
        internal PwmDCMotor(Windows.Devices.Pwm.PwmController controller, byte driver)
        {
            this._controller = controller;
            this._motorNum   = driver;

            // The PCA9685 PWM controller is used to control the inputs of two TB6612FNG dual motor drivers, "IC1" and "IC3".
            // Each TB6612FNG has two motor drivers. So we have motor driver circuits of IC1a, IC1b, IC3a and IC3b.
            // These correspond to motor hat screw terminals M1, M2, M3 and M4.
            // Each driver circuit ("IC1a", etc.) has one PWM pin and two IN pins.
            // The PWM pin expects a PWM input signal. The two IN pins expect a logic 0 or 1 input signal.
            // The variables pwm, in1 and in2 variables identify which PCA9685 PWM output pins will be used to drive this PwmDCMotor.
            // The pwm variable identifies which PCA9685 output pin is used to drive the xPWM input on the TB6612FNG.
            // And the in1 and in2 variables are used to specify which PCA9685 output pins are used to drive the xIN1 and xIN2 input pins of the TB6612FNG.
            byte pwm, in1, in2 = 0;

            if (driver == 1)
            {
                pwm = 8;
                in2 = 9;
                in1 = 10;
            }
            else if (driver == 2)
            {
                pwm = 13;
                in2 = 12;
                in1 = 11;
            }
            else if (driver == 3)
            {
                pwm = 2;
                in2 = 3;
                in1 = 4;
            }
            else if (driver == 4)
            {
                pwm = 7;
                in2 = 6;
                in1 = 5;
            }
            else
            {
                throw new MotorHatException("MotorHat Motor must be between 1 and 4 inclusive");
            }

            this._PWMpin = this._controller.OpenPin(pwm);
            this._PWMpin.Start();

            this._IN1pin = this._controller.OpenPin(in1);
            this._IN1pin.SetActiveDutyCyclePercentage(1);

            this._IN2pin = this._controller.OpenPin(in2);
            this._IN2pin.SetActiveDutyCyclePercentage(1);
        }
Exemple #2
0
        private async Task EnsureInitializedAsync()
        {
            if (isInitialized)
            {
                return;
            }
            var provider = new PCA9685Provider(this._i2caddr);

            this._pwm = (await Windows.Devices.Pwm.PwmController.GetControllersAsync(provider)).FirstOrDefault();
            if (this._frequency > this._pwm.MaxFrequency)
            {
                this._frequency = this._pwm.MaxFrequency;
            }
            this._pwm.SetDesiredFrequency(this._frequency);
            isInitialized = true;
        }
Exemple #3
0
 private void EnsureInitialized()
 {
     Task.Run(async() =>
     {
         if (isInitialized)
         {
             return;
         }
         var provider    = new PCA9685Provider(this._i2caddr);
         var controllers = await Windows.Devices.Pwm.PwmController.GetControllersAsync(provider);
         this._pwm       = controllers[0];
         if (this._frequency > this._pwm.MaxFrequency)
         {
             this._frequency = this._pwm.MaxFrequency;
         }
         this._pwm.SetDesiredFrequency(this._frequency);
         isInitialized = true;
     }).Wait();
 }
Exemple #4
0
        /// <summary>
        /// Initializes a new <see cref="PwmDCMotor"/> instance.
        /// </summary>
        /// <param name="controller">The <see cref="Windows.Devices.Pwm.PwmController"/> to use.</param>
        /// <param name="driver">The motor driver channel (1 through 4) for coil A.</param>
        internal PwmDCMotor(Windows.Devices.Pwm.PwmController controller, byte driver)
        {
            this._controller = controller;
            this._motorNum   = driver;
            byte pwm, in1, in2 = 0;

            if (driver == 0)
            {
                pwm = 8;
                in2 = 9;
                in1 = 10;
            }
            else if (driver == 1)
            {
                pwm = 13;
                in2 = 12;
                in1 = 11;
            }
            else if (driver == 2)
            {
                pwm = 2;
                in2 = 3;
                in1 = 4;
            }
            else if (driver == 3)
            {
                pwm = 7;
                in2 = 6;
                in1 = 5;
            }
            else
            {
                throw new MotorHatException("MotorHat Motor must be between 1 and 4 inclusive");
            }

            this._PWMpin = this._controller.OpenPin(pwm);
            this._IN1pin = this._controller.OpenPin(in1);
            this._IN2pin = this._controller.OpenPin(in2);
        }
Exemple #5
0
        /// <summary>
        /// Initializes a new <see cref="PwmStepperMotor"/> instance.
        /// </summary>
        /// <param name="controller">The <see cref="Windows.Devices.Pwm.PwmController"/> to use.</param>
        /// <param name="driverA">The motor driver channel (1 through 4) for coil A.</param>
        /// <param name="driverB">The motor driver channel (1 through 4) for coil B.</param>
        /// <param name="steps">The number of full steps per revolution that this motor has.</param>
        internal PwmStepperMotor(Windows.Devices.Pwm.PwmController controller, byte driverA, byte driverB, byte steps)
        {
            if ((driverA < 1) || (driverA > 4))
            {
                throw new MotorHatException("Stepper motor driverA must be between 1 and 4.");
            }
            if ((driverB < 1) || (driverB > 4))
            {
                throw new MotorHatException("Stepper motor driverB must be between 1 and 4.");
            }
            if (driverA == driverB)
            {
                throw new MotorHatException("Stepper motor driverA and driverB must be different.");
            }

            this._controller    = controller;
            this._steps_per_rev = steps;
            this._sec_per_step  = 0.1;
            this._currentstep   = 0;

            this._MICROSTEPS = 8;
            var m = new List <double>();

            for (int i = 0; i <= _MICROSTEPS; i++)
            {
                m.Add(Math.Sin(Math.PI / 180 * 90 * i / this._MICROSTEPS));
            }
            this._MICROSTEP_CURVE = m.ToArray();

            int pwmapin, ain1pin, ain2pin;
            int pwmbpin, bin1pin, bin2pin;

            switch (driverA)
            {
            case 1:
                pwmapin = 8;
                ain2pin = 9;
                ain1pin = 10;
                break;

            case 2:
                pwmapin = 13;
                ain2pin = 12;
                ain1pin = 11;
                break;

            case 3:
                pwmapin = 2;
                ain2pin = 3;
                ain1pin = 4;
                break;

            case 4:
                pwmapin = 7;
                ain2pin = 6;
                ain1pin = 5;
                break;

            default:
                throw new InvalidOperationException();
            }
            switch (driverB)
            {
            case 1:
                pwmbpin = 8;
                bin2pin = 9;
                bin1pin = 10;
                break;

            case 2:
                pwmbpin = 13;
                bin2pin = 12;
                bin1pin = 11;
                break;

            case 3:
                pwmbpin = 2;
                bin2pin = 3;
                bin1pin = 4;
                break;

            case 4:
                pwmbpin = 7;
                bin2pin = 6;
                bin1pin = 5;
                break;

            default:
                throw new InvalidOperationException();
            }
            this._PWMA = this._controller.OpenPin(pwmapin);
            this._AIN2 = this._controller.OpenPin(ain2pin);
            this._AIN1 = this._controller.OpenPin(ain1pin);
            this._PWMB = this._controller.OpenPin(pwmbpin);
            this._BIN2 = this._controller.OpenPin(bin2pin);
            this._BIN1 = this._controller.OpenPin(bin1pin);
            this._PWMA.Start();
            this._PWMB.Start();
            this._AIN1.Start();
            this._AIN2.Start();
            this._BIN1.Start();
            this._BIN2.Start();
            _coilpins = new Windows.Devices.Pwm.PwmPin[]
            {
                this._AIN2,
                this._BIN1,
                this._AIN1,
                this._BIN2
            };
        }