Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

wood's Content

There have been 2 items by wood (Search limited from 30-March 23)


By content type

See this member's

Sort by                Order  

#23220 RC6 Decoder Class

Posted by wood on 24 January 2012 - 01:42 AM in Project Showcase

I'm only getting the "Code=0x00000000" output as well. Any suggestions?



#13130 Netduino with Adafruit Motor Shield

Posted by wood on 11 May 2011 - 03:08 PM in Netduino 2 (and Netduino 1)

First of all: many, many, many, many, many thanks to Qwindelzorf. Without the code supplied earlier, I would have been dead in the water for much longer than just a day (or two).

Also instrumental:


When attempting to use the ONBOARD_LED to blink on interval, the Netduino threw a CLR_E_PIN_UNAVAILABLE exception. Taking that out also helped uncover that I was "browning out" the Netduino... My solution was to remove the PWR header on the MotorShield and add a 5xAA battery pack to the EXT PWR headers. Keep in mind that there are no protection diodes on the MotorShield.

Here's my code. Simple demo to get the platform spinning in circles, reversing every second.

public static void Main()
{
    DcMotor leftWheel = new DcMotor(MotorHeaders.M4);
    DcMotor rightWheel = new DcMotor(MotorHeaders.M3);
    
    leftWheel.SetSpeed(0);
    rightWheel.SetSpeed(0);
    
    leftWheel.Run(MotorDirection.Forward);
    rightWheel.Run(MotorDirection.Forward);
    
    leftWheel.SetSpeed(90);
    rightWheel.SetSpeed(90);

    while (true)
    {
        leftWheel.Run(MotorDirection.Reverse);
        rightWheel.Run(MotorDirection.Forward);

        Thread.Sleep(1000);

        leftWheel.Run(MotorDirection.Forward);
        rightWheel.Run(MotorDirection.Reverse);
        
        Thread.Sleep(1000);
    }
}

Here are the modified MotorShield and DcMotor classes. The main problem with the original was that the PWM and MotorBits values were static, so when I created another instance of the DcMotor class, it overwrote the values from the previous instance. The MotorBits for 3A/B and 4A/B were also swapped.

No warranty implied.

public sealed class MotorShield
{
    public const Cpu.Pin PWM_0A = Pins.GPIO_PIN_D6; // M4
    public const Cpu.Pin PWM_0B = Pins.GPIO_PIN_D5; // M3

    private static MotorShield _instance = new MotorShield();
    public static MotorShield Instance { get { return _instance; } }

    private OutputPort _motorLatch;
    private OutputPort _motorClock;
    private OutputPort _motorEnable;
    private OutputPort _motorData;

    internal byte LatchState = 0x00;

    private MotorShield()
    {
        _motorLatch = new OutputPort(Pins.GPIO_PIN_D12, false);
        _motorClock = new OutputPort(Pins.GPIO_PIN_D4, false);
        _motorEnable = new OutputPort(Pins.GPIO_PIN_D7, false);
        _motorData = new OutputPort(Pins.GPIO_PIN_D8, false);
    }

    internal void LatchTx()
    {
        //LATCH_PORT &= ~_BV(LATCH);
        _motorLatch.Write(false);

        //SER_PORT &= ~_BV(SER);
        _motorData.Write(false);

        for (int i = 0; i < 8; i++)
        {
            //CLK_PORT &= ~_BV(CLK);
            _motorClock.Write(false);

            int mask = (1 << (7 - i));
            if ((LatchState & mask) != 0)
            {
                //SER_PORT |= _BV(SER);
                _motorData.Write(true);
            }
            else
            {
                //SER_PORT &= ~_BV(SER);
                _motorData.Write(false);
            }
            //CLK_PORT |= _BV(CLK);
            _motorClock.Write(true);
        }
        //LATCH_PORT |= _BV(LATCH);
        _motorLatch.Write(true);
    }
}

public sealed class DcMotor
{
    private PWM _pwm;
    private byte _motorBitA, _motorBitB;

    public DcMotor(MotorHeaders header)
    {
        switch (header)
        {
            case MotorHeaders.M3:
                _motorBitA = (int)MotorBits.Motor3_A;
                _motorBitB = (int)MotorBits.Motor3_B;
                _pwm = new PWM(MotorShield.PWM_0B);
                break;
            case MotorHeaders.M4:
                _motorBitA = (int)MotorBits.Motor4_A;
                _motorBitB = (int)MotorBits.Motor4_B;
                _pwm = new PWM(MotorShield.PWM_0A);
                break;
            default:
                throw new InvalidOperationException("Invalid motor header specified.");
        }

        MotorShield.Instance.LatchState &= (byte)(~(1 << _motorBitA) & ~(1 << _motorBitB));
        MotorShield.Instance.LatchTx();

        _pwm.SetPulse(100, 0);
    }

    public void Run(MotorDirection dir)
    {
        switch (dir)
        {
            case MotorDirection.Release:
                MotorShield.Instance.LatchState &= (byte)(~(1 << _motorBitA));
                MotorShield.Instance.LatchState &= (byte)(~(1 << _motorBitB));
                break;
            case MotorDirection.Forward:
                MotorShield.Instance.LatchState |= (byte)(1 << _motorBitA);
                MotorShield.Instance.LatchState &= (byte)(~(1 << _motorBitB));
                break;
            case MotorDirection.Reverse:
                MotorShield.Instance.LatchState &= (byte)(~(1 << _motorBitA));
                MotorShield.Instance.LatchState |= (byte)(1 << _motorBitB);
                break;
            default:
                throw new InvalidOperationException("Invalid motor direction specified");
        }

        MotorShield.Instance.LatchTx();
    }

    public void SetSpeed(uint speed)
    {
        if (speed > 100)
        {
            speed = 100;
        }

        _pwm.SetDutyCycle(speed);
    }
}

public enum MotorBits
{
    Motor4_A = 0,
    Motor4_B = 6,
    Motor3_A = 5,
    Motor3_B = 7
}

public enum MotorDirection
{
    Release, Forward, Reverse
}

public enum MotorHeaders
{
    M3, M4
}


How do I control it so it runs continous and change the speed?




home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.