Esempio n. 1
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}");
        }
Esempio n. 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;
                }
            }
        }