Ejemplo n.º 1
0
 private void checkBoxServo1_CheckedChanged(object sender, EventArgs e)
 {
     if (checkBoxServo1.Checked)
     {
         pigpiodIf.set_servo_pulsewidth(SERVO_1, (UInt32)trackBarServo1.Value);
     }
     else
     {
         pigpiodIf.set_servo_pulsewidth(SERVO_1, 0);
     }
 }
Ejemplo n.º 2
0
        public void t3(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            PigpiodIf.Callback callback = null;
            try
            {
                int[] pw = new int[] { 500, 1500, 2500 };
                int[] dc = new int[] { 20, 40, 60, 80 };

                int   f, rr, v;
                float on, off;

                int t;

                Console.WriteLine("\r\nPWM/Servo pulse accuracy tests.");

                int    t3_reset = 1;
                int    t3_count = 0;
                UInt32 t3_tick  = 0;
                float  t3_on    = 0.0f;
                float  t3_off   = 0.0f;
                callback = pigpiodIf.callback(GPIO, PigpiodIf.EITHER_EDGE, (gpio, level, tick, user) =>
                {
                    UInt32 td;

                    //Console.WriteLine("pi={0} g{1} l={2} t={3}", pi, gpio, level, tick);
                    if (t3_reset != 0)
                    {
                        t3_count = 0;
                        t3_on    = 0.0f;
                        t3_off   = 0.0f;
                        t3_reset = 0;
                    }
                    else
                    {
                        td = tick - t3_tick;

                        if (level == 0)
                        {
                            t3_on += td;
                        }
                        else
                        {
                            t3_off += td;
                        }
                    }

                    t3_count++;
                    t3_tick = tick;
                });

                for (t = 0; t < 3; t++)
                {
                    pigpiodIf.set_servo_pulsewidth(GPIO, (UInt32)pw[t]);
                    v = pigpiodIf.get_servo_pulsewidth(GPIO);
                    CHECK(3, t + t + 1, v, pw[t], 0, "get servo pulsewidth", ct);

                    pigpiodIf.time_sleep(1);
                    t3_reset = 1;
                    pigpiodIf.time_sleep(4);
                    on  = t3_on;
                    off = t3_off;
                    CHECK(3, t + t + 2, (int)((1000.0 * (on + off)) / on), (int)(20000000.0 / pw[t]), 1,
                          "set servo pulsewidth", ct);
                }

                pigpiodIf.set_servo_pulsewidth(GPIO, 0);
                pigpiodIf.set_PWM_frequency(GPIO, 1000);
                f = pigpiodIf.get_PWM_frequency(GPIO);
                CHECK(3, 7, f, 1000, 0, "set/get PWM frequency", ct);

                rr = pigpiodIf.set_PWM_range(GPIO, 100);
                CHECK(3, 8, rr, 200, 0, "set PWM range", ct);

                for (t = 0; t < 4; t++)
                {
                    pigpiodIf.set_PWM_dutycycle(GPIO, (UInt32)dc[t]);
                    v = pigpiodIf.get_PWM_dutycycle(GPIO);
                    CHECK(3, t + t + 9, v, dc[t], 0, "get PWM dutycycle", ct);

                    pigpiodIf.time_sleep(1);
                    t3_reset = 1;
                    pigpiodIf.time_sleep(2);
                    on  = t3_on;
                    off = t3_off;
                    CHECK(3, t + t + 10, (int)((1000.0 * on) / (on + off)), (int)(10.0 * dc[t]), 1,
                          "set PWM dutycycle", ct);
                }
            }
            finally
            {
                pigpiodIf.set_PWM_dutycycle(GPIO, 0);

                pigpiodIf.callback_cancel(callback);
            }
        }