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.
Photo

MotorButton loop error? Update: Now more about Motor Driving.


  • Please log in to reply
23 replies to this topic

#1 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 08 November 2010 - 05:20 AM

Sorry to bother you all, but I seem to have run in to a bit of trouble. I get a strange exception windows when I press the button after I load the program on the netduino. The button is supposed to start the motor as long as it is being held down. I am not sure what I did wrong here... I seem to have forgotten my password to my youtube account, and to recover it you have to go to your email, which ironically I also forget. So, I can't upload the video of the error. Here is my code though.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1
{
    public class Program

    {
        static OutputPort motor = new OutputPort(Pins.GPIO_PIN_D6, false);
        public static void Main()
        {
            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, ResistorModes.Disabled, Port.InterruptMode.InterruptEdgeBoth);
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            Thread.Sleep(Timeout.Infinite);
        }

        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            PWM[] motor = new PWM[] { new PWM(Pins.GPIO_PIN_D6) };

            while (data2 == 0)
            {
                //Starts Motor 1
                motor[0].SetDutyCycle(100);

            };
        }

    }
}

Credits go to Oz-Solutions and whoever does the How-To's on the website section "Projects"

Sincerely,
CwbhX
-Cwbh

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 08 November 2010 - 06:01 AM

CwbhX,

First of all, the event handler button_OnInterrupt is called when the button is pushed. Your loop that checks "while data2 = 0" will use the value of data2 passed into the event handler--not the actual state of the button.

I think what you really want there is:
while (button.Read() == 0)

In which case you'll also need the make the button a static variable and create it in the class instead of the Main() function.

When you say that you get strange exception errors, can you take a screenshot of what you're seeing and post it?

Chris

#3 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 08 November 2010 - 07:36 AM

In addition to what Chris said, it would be good idea to enable glitch filter (pass 'true' to the InterruptPort constructor), and the 'while' loop is not really needed because the interrupt is trigerred by edges:

public class Program
{
  // Member variable to prevent garbage collection
  static InterruptPort button;

  // Instantiate here to keep interrupt handler code small
  static PWM motor = new PWM(Pins.GPIO_PIN_D6); 
   
  public static void Main()
  {
    button = new InterruptPort(Pins.ONBOARD_SW1, true, ...);
    ...
  }

  static void button_OnInterrupt(uint pin, uint state, DateTime time)
  {
    if(state == 0)
    {
      // Falling Edge
      motor.SetDutyCycle(100); // On
    }
    else
    {
      // Rising Edge
      motor.SetDutyCycle(0); // Off
    }
}


#4 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 08 November 2010 - 12:48 PM

I have to go soon, but here is the error. I think I am becoming a bit more familiar with C# and Mr. Netduino. NOTE: That I do get this right after I press the button. Thank you both very much! Sincerely, Cwbh

Attached Files


-Cwbh

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 08 November 2010 - 01:07 PM

I have to go soon, but here is the error.

In all likelihood, the cause of the exception is that you have already used the pin D6 in OutputPort instance (line #13 in the screenshot), so it cannot be used again in PWM object. Certain features require exclusive access to the pin, it'd have to be released (disposed) for reuse.

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 08 November 2010 - 06:08 PM

CW2 is correct. You're already using the pin when you create the OutputPort up farther up in your code.

If you change your code over to what CW2 posted three posts back, you'll be fine.

Either way, you need to remove the following line of code:
static OutputPort motor = new OutputPort(Pins.GPIO_PIN_D6, false);

That is creating an OutputPort using pin D6. This ties up pin D6--so the call to use it as a PWM further down won't work at all.

Chris

#7 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 08 November 2010 - 11:41 PM

Okay, I got the code down with no errors, just two things... It doesn't turn the motor (Kind of a problem). I am not sure what CW2 means when it says "Falling Edge", "Rising Edge" and "Garbage collection"

Thanks a lot for your help guys.

Sincerely,
Cwbh

CODE:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1
{
    public class Program
    {
        static InterruptPort button;
        static PWM motor = new PWM(Pins.GPIO_PIN_D6);

        public static void Main()
        {
            // write your code here
            button = new InterruptPort(Pins.ONBOARD_SW1, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);

            button.OnInterrupt +=new NativeEventHandler(button_OnInterrupt);
        }

        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            if (data2 == 0)
            {
                motor.SetDutyCycle(100);
            }
            else
            {
                motor.SetDutyCycle(0);
            }

        }

    }
}

-Cwbh

#8 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 08 November 2010 - 11:48 PM

It doesn't turn the motor (Kind of a problem).

Just out of curiosity, what kind of motor do you have? How is it connected to Netduino?

#9 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 09 November 2010 - 12:03 AM

Simple 1.5 volt motor. Looks like this Posted Image I have one wire to the D6 pin and one to ground on the same side.

*Could this be a power related issue or is the code broken?

Sincerely,
Cwbh
-Cwbh

#10 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 09 November 2010 - 12:36 AM

Changed the code a bit; did I forgot to sleep it? Here is the new code. I made it so it would also light up the led when pressed.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1
{
    public class Program
    {
        static InterruptPort button;
        static PWM motor = new PWM(Pins.GPIO_PIN_D6);
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        public static void Main()
        {
            // write your code here
            button = new InterruptPort(Pins.ONBOARD_SW1, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);

            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            Thread.Sleep(Timeout.Infinite);

        }

        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            led.Write(data2 == 0);
            if (data2 == 0)
            {
                motor.SetDutyCycle(100);
            }
            else
            {
                motor.SetDutyCycle(0);
            }

        }

    }
}

-Cwbh
-Cwbh

#11 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 09 November 2010 - 12:52 AM

I'm pretty sure you're going to need at least a transistor and a diode to drive a motor -- i don't think the netduino (or most microcontrollers for that matter) can safely drive a motor.

#12 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 09 November 2010 - 01:00 AM

I have a motor controller (shield), just as of right now I don't know how to program it... Here is the manual http://www.robotshop...ual-dri0009.pdf it is L298N. -Cwbh
-Cwbh

#13 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 09 November 2010 - 01:48 AM

do you have or can you get at least a transistor and some resistors? I think you risk hurting your netduino hooking it up directly.

#14 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 09 November 2010 - 02:00 AM

Well, I don't have my tech kits with me and won't for 2 weeks, the earliest being in 2 days. I can pick some up though; I live about 2 blocks from a Radioshack {Lucky me :)} P.S. I am now thinking I am doing this completely wrong, just I am not sure why I am thinking this. I need another breadboard... I think this is getting off thread topic... If that is okay with Chris -Cwbh
-Cwbh

#15 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 09 November 2010 - 07:18 AM

I am not sure what CW2 means when it says "Falling Edge", "Rising Edge" and "Garbage collection"

The falling edge is name for high to low (1 -> 0) transition of a [digital] signal, rising edge mean transition from low to high (0 -> 1). The value of Netduino pin SW1 is logical high (3.3 V) when the switch is not pressed. When you press it, it causes the signal to change to logical low (0V) - this transition is the falling edge, the pin circuitry detects it and triggers the interrupt, which is then handled by .NET Micro Framework runtime that in turn calls the interrupt event handler. Similarly, when you release the switch, the signal level gets back to logical high, the rising edge is detected and the interrupt event handler is executed (because you have set InterruptMode.InterruptEdgeBoth).

The popular explanation of garbage collection is that it is a mechanism in .NET Framework that manages allocation and automatically releases unused memory. If you are interested in details (including the 'correct' definition), please have a look at articles by Raymond Chen.

I'm pretty sure you're going to need at least a transistor and a diode to drive a motor -- i don't think the netduino (or most microcontrollers for that matter) can safely drive a motor.

bill.french is right, you'd need to use a transistor (plus base resistor and flyback protection diode) to switch inductive load such as DC motor. Please have a look at the fourth image in Using transistor as a switch, or MOSFET switch example (replace lamp with the motor, you could use general purpose NPN transistor instead of MOSFET, but make sure it can handle the current).

#16 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 09 November 2010 - 02:19 PM

Okay. I have looked at both links for diagrams. I see what it is doing, just I don't know what the variables are; this isn't a Legends. If I know what to buy I can put it together later today. Anyways, thanks a lot guys! Sincerely, Cwbh
-Cwbh

#17 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 09 November 2010 - 06:29 PM

Here's what I would do. I am not an expert on anything so... you're on your own.

Looking at the below diagram, look at the circuit on the right, since a motor is an inductive load. I would skip RC, because I am hardcore. RB i would start with a 1k Ohm. I would use just about any NPN resistor, RadioShack should definitely have them, I think they sell a 10 count variety pack. For such a small motor almost any diode would do, something like this should be fine: http://www.radioshac...oductId=2036269

Is it safe to assume you understand the symbols in the diagram for the diode, transistor, etc?

Posted Image

#18 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 09 November 2010 - 06:42 PM

I thought that I would be this one, for motors. Posted Image.I am not sure what Vbe is Ib and the other objects like RC. I can see the symbols like the diode, resistor, etc...

I can build circuits; I have done quite a few. Just I am not sure what those variables are. :/

Thanks a lot for your help though.

Sincerely,
Cwbh

P.S. Maybe this could go onto the "Motor control" in the project section? Just a thought.
-Cwbh

#19 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 09 November 2010 - 07:12 PM

Yeah, that diagram is the same basically as the right side of what I posted.

Vbe is the base to emmitter voltage.
Vce is the collector to emmiter voltage.
Ib is the current to base
Ic is the current through the collector
RC is the resistor to the collector <--i just edited this, i had it wrong!

Since you're just going to be using the transistor as a switch, you really don't need to worry so much about any of those to get started. They will be whatever they are. All you need to really worry about is RB and maybe the diode. Starting with an RB of 1k is a good starting point.

If you want to think of the transistor as something more than a switch (because they are so much more) in this case, think of the transistor as a current multiplier. Different transistors have different multiplication factors (the real name of which escapes me...) .. so, if you put in say 1ma into the base (Ib), that will allow say 100x (which is typical of common transistors at radioshack) the current to move from the collector to the emmiter (Ic). If you use a 1k resistor for RB, that gives you .0005 amps for Ib, which should multiply to .5 amps Ic, which should be enough to drive your motor.

Some transistors:
http://www.radioshac...oductId=2062586

Edited by bill.french, 10 November 2010 - 12:43 AM.


#20 CwbhX

CwbhX

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationNYC

Posted 09 November 2010 - 09:39 PM

Okay, I just bought the TIP 120, Assorted Rectifier Diodes, and 2n3906 PNP Transistors. I will get my breadboard tomorrow, and my resistors to see if it will work :). Thanks a lot for your guys help! Sincerely, Cwbh
-Cwbh




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.