Пример #1
0
        /// <summary>
        /// Creates a <see cref="DCMotor"/> object for the specified channel.
        /// </summary>
        /// <param name="motorNumber">A motor number from 1 to 4.</param>
        /// <remarks>
        /// The motorNumber parameter refers to the motor numbers M1, M2, M3 or M4 printed in the device.
        /// </remarks>
        public DCMotor.DCMotor CreateDCMotor(int motorNumber)
        {
            if (motorNumber < 1 || motorNumber > 4)
            {
                throw new ArgumentOutOfRangeException(nameof(motorNumber), $"Must be between 1 and 4, corresponding with M1, M2, M3 and M4. (Received: {motorNumber}");
            }

            // The PCA9685 PWM controller is used to control the inputs of two dual motor drivers.
            // These correspond to motor hat screw terminals M1, M2, M3 and M4.
            // Each motor driver circuit has one speed 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 speed, in1 and in2 variables identify which PCA9685 PWM output pins will be used to drive this DCMotor.
            // The speed variable identifies which PCA9685 output pin is used to drive the PWM input on the motor driver.
            // 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 motor driver.

            int speedPin, in1Pin, in2Pin;

            switch (motorNumber)
            {
            case 1:
                speedPin = 8;
                in2Pin   = 9;
                in1Pin   = 10;
                break;

            case 2:
                speedPin = 13;
                in2Pin   = 12;
                in1Pin   = 11;
                break;

            case 3:
                speedPin = 2;
                in2Pin   = 3;
                in1Pin   = 4;
                break;

            case 4:
                speedPin = 7;
                in2Pin   = 6;
                in1Pin   = 5;
                break;

            default:
                throw new ArgumentException($"MotorHat Motor must be between 1 and 4 inclusive. Received: {motorNumber}");
            }

            var speedPwm = _pca9685.CreatePwmChannel(speedPin);
            var in1Pwm   = _pca9685.CreatePwmChannel(in1Pin);
            var in2Pwm   = _pca9685.CreatePwmChannel(in2Pin);

            _channelsUsed.Add(speedPwm);
            _channelsUsed.Add(in1Pwm);
            _channelsUsed.Add(in2Pwm);

            return(new DCMotor3Pwm(speedPwm, in1Pwm, in2Pwm));
        }
Пример #2
0
        /// <summary>
        /// Creates a <see cref="DCMotor"/> object for the specified channel.
        /// </summary>
        /// <param name="motorNumber">A motor number from 1 to 4.</param>
        /// <remarks>
        /// The motorNumber parameter refers to the motor numbers M1, M2, M3 or M4 printed in the device.
        /// </remarks>
        public DCMotor.DCMotor CreateDCMotor(int motorNumber)
        {
            var motorPins = _pinProvider.GetPinsForMotor(motorNumber);

            var speedPwm = _pca9685.CreatePwmChannel(motorPins.SpeedPin);
            var in1Pwm   = _pca9685.CreatePwmChannel(motorPins.In1Pin);
            var in2Pwm   = _pca9685.CreatePwmChannel(motorPins.In2Pin);

            _channelsUsed.Add(speedPwm);
            _channelsUsed.Add(in1Pwm);
            _channelsUsed.Add(in2Pwm);

            return(new DCMotor3Pwm(speedPwm, in1Pwm, in2Pwm));
        }
Пример #3
0
        public ServoDriver(Pca9685 pca9685, int channel)
        {
            _pca9685 = pca9685;

            var pwmChannel = _pca9685.CreatePwmChannel(channel);

            _servo = new ServoMotor(pwmChannel, 180, 500, 2500);
        }
Пример #4
0
        public void Init(int channelNumber)
        {
            if (channelNumber < 0 || channelNumber >= MaxNumberOfMotors)
            {
                throw new Exception($"ChannelNumber can be [0...{MaxNumberOfMotors - 1}].");
            }

            if (_motor[channelNumber] != null && _motor[channelNumber].Initialized)
            {
                return;
            }

            Motor motor;

            if (_motor[channelNumber] != null)
            {
                motor = _motor[channelNumber];
            }
            else
            {
                motor = new Motor();
            }

            int busId = 1;

            if (_i2cConnectionSettings == null)
            {
                _i2cConnectionSettings = new I2cConnectionSettings(busId, _i2cAddress);
            }
            if (_i2cDevice == null)
            {
                _i2cDevice = I2cDevice.Create(_i2cConnectionSettings);
            }
            if (_pca9685 == null)
            {
                _pca9685 = new Pca9685(_i2cDevice, pwmFrequency: 50);
            }

            motor.Pwm             = _pca9685.CreatePwmChannel(channelNumber * 3);     //speed
            motor.In1             = _pca9685.CreatePwmChannel(channelNumber * 3 + 1); //in1
            motor.In2             = _pca9685.CreatePwmChannel(channelNumber * 3 + 2); //in2
            motor.Pwm.DutyCycle   = 0.0;                                              //stop motor
            motor.Initialized     = true;
            _motor[channelNumber] = motor;
        }
Пример #5
0
        public void Init()
        {
            int busId = 1;
            I2cConnectionSettings settings = new I2cConnectionSettings(busId, _i2cAddress);

            _i2cDevice = I2cDevice.Create(settings);
            _pca9685   = new Pca9685(_i2cDevice, pwmFrequency: 50);

            _servoChannel = _pca9685.CreatePwmChannel(_channelNumber);
            _servoMotor   = new ServoMotor(_servoChannel, 180, 500, 2500);
        }
Пример #6
0
 private static ServoMotor.ServoMotor CreateServo(Pca9685 pca9685, int channel)
 {
     return(new ServoMotor.ServoMotor(
                pca9685.CreatePwmChannel(channel),
                AngleRange, MinPulseWidthMicroseconds, MaxPulseWidthMicroseconds));
 }