Esempio n. 1
0
        static void WriteAngle(PwmChannel pwmChannel, ServoMotor servoMotor)
        {
            servoMotor.Start();

            while (true)
            {
                Console.WriteLine("Enter an angle ('Q' to quit). ");
                string?angle = Console.ReadLine();

                if (angle?.ToUpper() is "Q" || angle?.ToUpper() is null)
                {
                    break;
                }

                if (!int.TryParse(angle, out int angleValue))
                {
                    Console.WriteLine($"Can not parse {angle}.  Try again.");
                }

                servoMotor.WriteAngle(angleValue);
                Console.WriteLine($"Duty Cycle: {pwmChannel.DutyCycle * 100.0}%");
            }

            servoMotor.Stop();
        }
Esempio n. 2
0
        static void WritePulseWidth(PwmChannel pwmChannel, ServoMotor servoMotor)
        {
            servoMotor.Start();
            int pulseWidthValue = 1000;

            while (true)
            {
                //Console.WriteLine("Enter a pulse width in microseconds ('Q' to quit). ");
                //string? pulseWidth = Console.ReadLine();

                //if (pulseWidth?.ToUpper() is "Q" || pulseWidth?.ToUpper() is null)
                //{
                //    break;
                //}

                //if (!int.TryParse(pulseWidth, out int pulseWidthValue))
                //{
                //    Console.WriteLine($"Can not parse {pulseWidth}.  Try again.");
                //}

                servoMotor.WritePulseWidth(pulseWidthValue);
                Console.WriteLine($"Duty Cycle: {pwmChannel.DutyCycle * 100.0}%");
                pulseWidthValue = (pulseWidthValue == 1000) ? 2000 : 1000;
                Thread.Sleep(5000);
            }

            servoMotor.Stop();
        }
Esempio n. 3
0
        private static void WritePulseWidth(PwmChannel pwmChannel, ServoMotor servoMotor)
        {
            servoMotor.Start();

            while (true)
            {
                Console.WriteLine("Enter a pulse width in microseconds ('Q' to quit). ");
                string pulseWidth = Console.ReadLine();

                if (pulseWidth.ToUpper() == "Q")
                {
                    break;
                }

                if (!int.TryParse(pulseWidth, out int pulseWidthValue))
                {
                    Console.WriteLine($"Can not parse {pulseWidth}.  Try again.");
                }

                servoMotor.WritePulseWidth(pulseWidthValue);
                Console.WriteLine($"Duty Cycle: {pwmChannel.DutyCycle * 100.0}%");
            }

            servoMotor.Stop();
        }
Esempio n. 4
0
        private static void WriteAngle(PwmChannel pwmChannel, ServoMotor servoMotor)
        {
            servoMotor.Start();

            while (true)
            {
                Console.WriteLine("Enter an angle ('Q' to quit). ");
                string angle = Console.ReadLine();

                if (angle.ToUpper() == "Q")
                {
                    break;
                }

                if (!int.TryParse(angle, out int angleValue))
                {
                    Console.WriteLine($"Can not parse {angle}.  Try again.");
                }

                servoMotor.WriteAngle(angleValue);
                Console.WriteLine($"Duty Cycle Percentage: {pwmChannel.DutyCyclePercentage}");
            }

            servoMotor.Stop();
        }
Esempio n. 5
0
        public void Pwm_Channel_Should_Start_And_Stop_When_Starting_And_Stopping_Servo()
        {
            Mock <PwmChannel> mockPwmChannel = new Mock <PwmChannel>();
            ServoMotor        servo          = new ServoMotor(mockPwmChannel.Object);

            servo.Start();
            mockPwmChannel.Verify(channel => channel.Start());
            mockPwmChannel.VerifyNoOtherCalls();

            mockPwmChannel.Reset();

            servo.Stop();
            mockPwmChannel.Verify(channel => channel.Stop());
            mockPwmChannel.VerifyNoOtherCalls();
        }
Esempio n. 6
0
        static void TestRotate()
        {
            Console.WriteLine("Testing a servo motor controlled by a rotary encoder");
            var       angle = 0;
            const int step  = 10;

            using (var left = Pi.Gpio.Pin(leftPin, PinKind.InputPullUp))
                using (var right = Pi.Gpio.Pin(rightPin, PinKind.InputPullUp))
                    using (var servo = new ServoMotor(maxAngle))
                    {
                        servo.PulseWidths(0.5, 2.5);
                        servo.Start();
                        // Connect rotate and click events
                        left.OnRisingEdge += Rotate;
                        Console.WriteLine("Click the button to stop this test");
                        Pi.Gpio.Pin(buttonPin, PinKind.InputPullUp).WaitForEdge(PinEdge.Rising);
                        left.OnRisingEdge -= Rotate;
                        servo.Angle        = 0;
                        Pi.Wait(2000);
                        servo.Stop();

                        // Called on the falling edge of the left pin
                        void Rotate(object sender, PinEventHandlerArgs args)
                        {
                            if (args.Bounced)
                            {
                                return;
                            }
                            var a = angle;

                            a += left.Value == right.Value ? -step : step;
                            if (a < 0)
                            {
                                a = 0;
                            }
                            if (a > maxAngle)
                            {
                                a = maxAngle;
                            }
                            if (a != angle)
                            {
                                Console.WriteLine("servo angle at {0}°", servo.Angle = angle = a);
                            }
                        }
                    }
        }
Esempio n. 7
0
 /// <summary>
 /// Stops the servo motor.
 /// </summary>
 public void Stop() => _servoMotor.Stop();
Esempio n. 8
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            using PwmChannel pwmChannel1 = PwmChannel.Create(0, 0, 50);
            using ServoMotor servoMotor1 = new ServoMotor(
                      pwmChannel1,
                      180,
                      700,
                      2400);

            using PwmChannel pwmChannel2 = PwmChannel.Create(0, 1, 50);
            using ServoMotor servoMotor2 = new ServoMotor(
                      pwmChannel2,
                      180,
                      700,
                      2400);

            using SoftwarePwmChannel pwmChannel3 = new SoftwarePwmChannel(27, 50, 0.5);
            using ServoMotor servoMotor3         = new ServoMotor(
                      pwmChannel3,
                      180,
                      900,
                      2100);

            servoMotor1.Start();
            servoMotor2.Start();
            servoMotor3.Start();

            connection = new HubConnectionBuilder()
                         .WithUrl("https://192.168.1.162:5001/chathub", conf =>
            {
                conf.HttpMessageHandlerFactory = (x) => new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
                };
            })
                         .Build();

            try
            {
                connection.On <string, string>("ReceiveMessage", (user, message) =>
                {
                    if (user == "servo1")
                    {
                        MoveToAngle(servoMotor1, Int32.Parse(message));
                    }
                    else if (user == "servo2")
                    {
                        MoveToAngle(servoMotor2, Int32.Parse(message));
                    }
                    else if (user == "servo3")
                    {
                        MoveToAngle(servoMotor3, Int32.Parse(message));
                    }
                    Console.WriteLine($"{message} posted by: {user}");
                });

                await connection.StartAsync();
            }
            catch (System.Exception)
            {
                throw;
            }


            //
            // Keep the code running...
            //
            while (true)
            {
            }

            servoMotor1.Stop();
            servoMotor2.Stop();
            servoMotor3.Stop();
        }