예제 #1
0
        /// <summary>
        /// Creates a <see cref="ServoMotor"/> from one of the 4 available PWM channels  in the MotorHat
        /// </summary>
        /// <param name="channelNumber">A valid PWM channel (0, 1, 14 or 15)</param>
        /// <param name="maximumAngle">The maximum angle the servo motor can move represented as a value between 0 and 360.</param>
        /// <param name="minimumPulseWidthMicroseconds">The minimum pulse width, in microseconds, that represent an angle for 0 degrees.</param>
        /// <param name="maximumPulseWidthMicroseconds">The maxnimum pulse width, in microseconds, that represent an angle for maximum angle.</param>
        /// <remarks>
        /// The channelNumber refers to ont of the available PWM channel numbers available in the Motor Hat (0, 1, 14, 15) printed in the device.
        /// </remarks>
        public ServoMotor.ServoMotor CreateServoMotor(int channelNumber, double maximumAngle = 180, double minimumPulseWidthMicroseconds = 1000, double maximumPulseWidthMicroseconds = 2000)
        {
            var pwmChannel = CreatePwmChannel(channelNumber);

            var servo = new ServoMotor.ServoMotor(pwmChannel, maximumAngle, minimumPulseWidthMicroseconds, maximumPulseWidthMicroseconds);

            return(servo);
        }
예제 #2
0
        private static void CalibratePulseWidth(ServoMotor.ServoMotor servo, ref int pulseWidthMicroSeconds)
        {
            void SetPulseWidth(ref int pulseWidth)
            {
                pulseWidth = Math.Max(pulseWidth, 0);
                servo.WritePulseWidth(pulseWidth);
            }

            Console.WriteLine("Use A/Z (1x); S/X (10x); D/C (100x)");
            Console.WriteLine("Press enter to accept value");

            while (true)
            {
                SetPulseWidth(ref pulseWidthMicroSeconds);
                Console.WriteLine($"Current value: {pulseWidthMicroSeconds}");

                switch (Console.ReadKey().Key)
                {
                case ConsoleKey.A:
                    pulseWidthMicroSeconds++;
                    break;

                case ConsoleKey.Z:
                    pulseWidthMicroSeconds--;
                    break;

                case ConsoleKey.S:
                    pulseWidthMicroSeconds += 10;
                    break;

                case ConsoleKey.X:
                    pulseWidthMicroSeconds -= 10;
                    break;

                case ConsoleKey.D:
                    pulseWidthMicroSeconds += 100;
                    break;

                case ConsoleKey.C:
                    pulseWidthMicroSeconds -= 100;
                    break;

                case ConsoleKey.Enter:
                    return;
                }
            }
        }
예제 #3
0
        private static void CalibrateServo(ServoMotor.ServoMotor servo)
        {
            int maximumAngle = 180;
            int minimumPulseWidthMicroseconds = 520;
            int maximumPulseWidthMicroseconds = 2590;

            Console.WriteLine("Searching for minimum pulse width");
            CalibratePulseWidth(servo, ref minimumPulseWidthMicroseconds);
            Console.WriteLine();

            Console.WriteLine("Searching for maximum pulse width");
            CalibratePulseWidth(servo, ref maximumPulseWidthMicroseconds);

            Console.WriteLine("Searching for angle range");
            Console.WriteLine(
                "What is the angle range? (type integer with your angle range or enter to move to MIN/MAX)");

            while (true)
            {
                servo.WritePulseWidth(maximumPulseWidthMicroseconds);
                Console.WriteLine("Servo is now at MAX");
                if (int.TryParse(Console.ReadLine(), out maximumAngle))
                {
                    break;
                }

                servo.WritePulseWidth(minimumPulseWidthMicroseconds);
                Console.WriteLine("Servo is now at MIN");

                if (int.TryParse(Console.ReadLine(), out maximumAngle))
                {
                    break;
                }
            }

            servo.Calibrate(maximumAngle, minimumPulseWidthMicroseconds, maximumPulseWidthMicroseconds);
            Console.WriteLine($"Angle range: {maximumAngle}");
            Console.WriteLine($"Min PW [uS]: {minimumPulseWidthMicroseconds}");
            Console.WriteLine($"Max PW [uS]: {maximumPulseWidthMicroseconds}");
        }