Exemple #1
0
        /// <summary>
        /// Move a stepper. Only one param between steps and msec but be set different from 0.
        /// Blocking call.
        /// </summary>
        /// <param name="which">Which stepper to drive</param>
        /// <param name="mode">Stepping mode</param>
        /// <param name="speed">Number of ms between each step (0 : no pause=max speed)</param>
        /// <param name="direction">True = foward, False = backward</param>
        /// <param name="steps">If non-zero, number of steps to run</param>
        /// <param name="msec">If non-zero, number of ms to run</param>
        /// <param name="hold">True if stepper shall stay energized when task done</param>
        /// <returns>Number of steps ran</returns>
        //public uint StepperMove(Steppers which, BipolarStepping mode, int speed, bool direction, int steps, int msec, bool hold = false)
        //{
        //    byte[] MotorSteps = BipolarSteppingWaveDrive[(byte)which]; // Get the stepping pattern
        //    if (mode == BipolarStepping.HiTorque) MotorSteps = BipolarSteppingHiTorque[(byte)which];
        //    if (mode == BipolarStepping.HalfStep) MotorSteps = BipolarSteppingHalfStep[(byte)which];

        //    // Find where we stopped last time
        //    byte last;
        //    int pos;
        //    uint step = 0;
        //    if (which == 0) last = (byte)(latch_state & 0x1E);
        //    else last = (byte)(latch_state & 0xE1);
        //    for (pos = 0; pos < MotorSteps.Length; pos++) if (MotorSteps[pos] == last) break;
        //    if (pos == MotorSteps.Length) pos = 0;

        //    if (which == 0) { Motor1A.Write(true); Motor1B.Write(true); }
        //    else { Motor2A.SetDutyCycle(100); Motor2B.SetDutyCycle(100); }

        //    byte Mask = (byte)((which == 0) ? 0xE1 : 0x1E);

        //    if (steps > 0) // user chose to move from a number of steps
        //    {
        //        for (; step < steps; step++)
        //        {
        //            if (direction) pos = (pos + 1) % MotorSteps.Length;
        //            else if (--pos < 0) pos = MotorSteps.Length - 1;
        //            latch_state = (byte)((latch_state & Mask) | MotorSteps[pos]);
        //            latch_tx();
        //            if (speed > 0) Thread.Sleep(speed);
        //        }

        //    }
        //    else // So no number of steps, the user must have given a time to run
        //    {
        //        long endtime = DateTime.Now.Ticks + msec * TimeSpan.TicksPerMillisecond;
        //        for (; DateTime.Now.Ticks < endtime; step++)
        //        {
        //            if (direction) pos = (pos + 1) % MotorSteps.Length;
        //            else if (--pos < 0) pos = MotorSteps.Length - 1;
        //            latch_state = (byte)((latch_state & Mask) | MotorSteps[pos]);
        //            latch_tx();
        //            if (speed > 0) Thread.Sleep(speed);
        //        }
        //    }

        //    if (!hold) // Remove energy from stepper
        //    {
        //        if (which == 0) { Motor1A.Write(false); Motor1B.Write(false); }
        //        else { Motor2A.SetDutyCycle(0); Motor2B.SetDutyCycle(0); }
        //    }

        //    return step;
        //}

        ///// <summary>
        ///// Control a motor; Non-blocking call. Set speed to 0 to stop.
        ///// Frequency might need to be adjusted deppending on motor, or to match other PWMs
        ///// </summary>
        ///// <param name="which">Which motor to drive</param>
        ///// <param name="speed">Steed, between 0 and 255 (0 to stop, 255 = max speed)</param>
        ///// <param name="direction">True = foward, False = backward</param>
        ///// <param name="freq">Frequency in Hz of the PWM controlling the motor</param>
        //public void MotorControl(Motors which, byte speed, bool direction, int freq = 100)
        //{
        //    uint period = (uint)(1000000D / (double)freq);
        //    switch (which)
        //    {
        //        case Motors.M1:
        //            latch_state = (byte)((latch_state & 0xF3) | (direction ? 4 : 8));
        //            latch_tx();
        //            Motor1A.Write(speed > 0);
        //            break;

        //        case Motors.M2:
        //            latch_state = (byte)((latch_state & 0xED) | (direction ? 2 : 16));
        //            latch_tx();
        //            Motor1B.Write(speed > 0);
        //            break;
        //        case Motors.M3: // This motor can have its speed controlled through PWM
        //            if (speed == 0) Motor2A.SetDutyCycle(0);
        //            else
        //            {
        //                latch_state = (byte)((latch_state & 0xBE) | (direction ? 1 : 64));
        //                latch_tx();
        //                Motor2A.SetPulse(period, (uint)(period * (double)speed / 255D));
        //            }
        //            break;
        //        case Motors.M4: // This motor can have its speed controlled through PWM
        //            if (speed == 0) Motor2B.SetDutyCycle(0);
        //            else
        //            {
        //                latch_state = (byte)((latch_state & 0x5F) | (direction ? 32 : 128));
        //                latch_tx();
        //                Motor2B.SetPulse(period, (uint)(period * (double)speed / 255D));
        //            }
        //            break;
        //    }
        //}

        public void BothMotors(int motor3Speed, int motor4Speed)
        {
            if (motor3Speed == 0)
            {
                Motor2A.SetDutyCycle(0);
            }
            if (motor4Speed == 0)
            {
                Motor2A.SetDutyCycle(0);
            }

            var motor3Dir = motor3Speed > 0;
            var motor4Dir = motor4Speed > 0;

            //var motor3latch_state = (byte) ((latch_state & 0xBE) | (motor3Dir ? 1 : 64));

            //var motor4latch_state = (byte) ((latch_state & 0x5F) | (motor4Dir ? 32 : 128));


            //latch_state = (byte) (motor3latch_state & motor4latch_state);

            latch_state = (byte)((latch_state & 0xBE) | (motor3Dir ? 1 : 64));
            latch_tx();
            Motor2A.SetDutyCycle((uint)Math.Abs(motor3Speed));

            latch_state = (byte)((latch_state & 0x5F) | (motor4Dir ? 32 : 128));
            latch_tx();

            Motor2B.SetDutyCycle((uint)Math.Abs(motor4Speed));
        }
Exemple #2
0
        public void Dispose()
        {
            latch_state = 0;
            latch_tx();

            //if (UsedDriver == Drivers.Driver1 || UsedDriver == Drivers.Both)
            //{
            //    Motor1A.Dispose();
            //    Motor1B.Dispose();
            //}
            //if (UsedDriver == Drivers.Driver2 || UsedDriver == Drivers.Both)
            //{
            Motor2A.Dispose();
            Motor2B.Dispose();
            //}
            MotorLatch.Dispose();
            MotorEnable.Dispose();
            MotorClk.Dispose();
            MotorData.Dispose();
        }
Exemple #3
0
        public void MotorControl(Motors which, uint speed, bool direction)
        {
            if (speed >= 100)
            {
                speed = 100;
            }

            switch (which)
            {
            case Motors.M3:     // This motor can have its speed controlled through PWM
                if (speed == 0)
                {
                    Motor2A.SetDutyCycle(0);
                }
                else
                {
                    latch_state = (byte)((latch_state & 0xBE) | (direction ? 1 : 64));
                    latch_tx();
                    Motor2A.SetDutyCycle(speed);
                }
                break;

            case Motors.M4:     // This motor can have its speed controlled through PWM
                if (speed == 0)
                {
                    Motor2B.SetDutyCycle(0);
                }
                else
                {
                    latch_state = (byte)((latch_state & 0x5F) | (direction ? 32 : 128));
                    latch_tx();
                    Motor2B.SetDutyCycle(speed);
                }
                break;
            }
        }