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

Servos in NETMF 4.2


  • Please log in to reply
21 replies to this topic

#1 aalmada

aalmada

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts
  • LocationPortugal

Posted 09 September 2012 - 04:39 PM

Hi,

I recently upgraded my Netduino to 4.2.0 firmware. I'm now updating my code and I'm having an issue with the new PWM class and servos.

Servos expect a PWM with 20ms period and high signal duration from 1 ms to 2ms. I wrote the following using the new native PWM:

var servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false);
while (true)
{
    Thread.Sleep(1000);
    servo.Duration = 2000; // 2 ms
    Thread.Sleep(1000);
    servo.Duration = 1000; // 1ms
}

And the following using the SecretLabs PWM implementation:

var pwm = new SecretLabs.NETMF.Hardware.PWM(Pins.GPIO_PIN_D10);
uint period = 20000;
while (true)
{
    pwm.SetPulse(period, 1000); // 1 ms
    Thread.Sleep(1000);
    pwm.SetPulse(period, 2000); // 2 ms
    Thread.Sleep(1000);

}

Both examples make the servo move but, it doesn't move 90º and the 2 ms position forces the servo. Am I doing something wrong?

Thanks!

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 09 September 2012 - 05:06 PM

Hi aalmada, There's a small timing issue with the new PWM classes. We're working on it, and should have an update in about a week. Chris

#3 aalmada

aalmada

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts
  • LocationPortugal

Posted 09 September 2012 - 06:04 PM

Hi aalmada,

There's a small timing issue with the new PWM classes. We're working on it, and should have an update in about a week.

Chris


Thanks!

#4 chungenhung

chungenhung

    Member

  • Members
  • PipPip
  • 27 posts

Posted 16 September 2012 - 07:21 PM

Added
var servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false);

It errors on PWM, saying "The type of namespace name 'PWN' does not exist in the namespace 'Microsoft.SPOT.Hardware'
Is the namespace PWN not included in 4.2?

#5 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 16 September 2012 - 07:27 PM

Hi chungenhung,

Added

var servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false);

It errors on PWM, saying "The type of namespace name 'PWN' does not exist in the namespace 'Microsoft.SPOT.Hardware'
Is the namespace PWN not included in 4.2?

Be sure to add the Microsoft.SPOT.Hardware.PWM.dll assemble as a reference in your project.

Does that fix things for you?

Chris

#6 chungenhung

chungenhung

    Member

  • Members
  • PipPip
  • 27 posts

Posted 17 September 2012 - 12:57 AM

Hi chungenhung,


Be sure to add the Microsoft.SPOT.Hardware.PWM.dll assemble as a reference in your project.

Does that fix things for you?

Chris

Got it fixed!
I thought "using Microsoft.SPOT.Hardware" would include the PWM, but it didn't.

Now, where do I find how to use Microsoft.SPOT.Hardware.PWM?
Like, what each param means "Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false)".
How to set default position, starting position, ending position, etc.

The servo that I have is "Parallax Standard Servo":
http://learn.paralla...Start/900-00005
It says that "To enable the servo to move a full 180° use pulse durations of approximately 750 to 2250 μs. Exercise care to not exceed these lower and upper limits.".
What numbers to use in "Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false)"? What does 20000 and 1000 mean?

Thanks!

#7 aalmada

aalmada

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts
  • LocationPortugal

Posted 17 September 2012 - 08:08 AM

It says that "To enable the servo to move a full 180° use pulse durations of approximately 750 to 2250 μs. Exercise care to not exceed these lower and upper limits.".
What numbers to use in "Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false)"? What does 20000 and 1000 mean?


Hi chungenhung,

Servos expect a 20ms second period (20000μs), that's the value you see on the constructor.
To change position you should then change the Duration value. The typical values are from 1000 to 2000, with 1500 for the middle position. In your case, use values from 750 to 2250, 1500 should still be from the middle position.
The constructor requires an initial duration value (that's the 1000 value in the example).

aalmada

#8 chungenhung

chungenhung

    Member

  • Members
  • PipPip
  • 27 posts

Posted 17 September 2012 - 01:27 PM

Hi chungenhung,

Servos expect a 20ms second period (20000μs), that's the value you see on the constructor.
To change position you should then change the Duration value. The typical values are from 1000 to 2000, with 1500 for the middle position. In your case, use values from 750 to 2250, 1500 should still be from the middle position.
The constructor requires an initial duration value (that's the 1000 value in the example).

aalmada

When I use Microseconds, and use (20000, 1000) in the constructor, the servo would try to move out of the range.

#9 aalmada

aalmada

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts
  • LocationPortugal

Posted 17 September 2012 - 02:58 PM

When I use Microseconds, and use (20000, 1000) in the constructor, the servo would try to move out of the range.


Chris Walker mentions above that there is a "small timing issue with the new PWM classes". I'm waiting for the fix too.

#10 chungenhung

chungenhung

    Member

  • Members
  • PipPip
  • 27 posts

Posted 17 September 2012 - 03:13 PM

Chris Walker mentions above that there is a "small timing issue with the new PWM classes". I'm waiting for the fix too.

Would it be possible to use the pwm from the old 4.1 even if my netduino is running 4.2?
I am thinking it is not possible.

#11 aalmada

aalmada

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts
  • LocationPortugal

Posted 19 September 2012 - 04:21 PM

There's a small timing issue with the new PWM classes. We're working on it, and should have an update in about a week.


Any news on the update? ;)

#12 aalmada

aalmada

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts
  • LocationPortugal

Posted 20 September 2012 - 08:23 AM

There's a small timing issue with the new PWM classes. We're working on it, and should have an update in about a week.


The issue is fixed with v4.2.0 Update 1 (4.2.0.1)
http://forums.netdui...-v420-update-1/
Thanks Chris! :D

#13 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 20 September 2012 - 08:39 AM

The issue is fixed with v4.2.0 Update 1 (4.2.0.1)
http://forums.netdui...-v420-update-1/
Thanks Chris! :D

Thanks for the update. Glad it's working for you now :)

Chris

#14 chungenhung

chungenhung

    Member

  • Members
  • PipPip
  • 27 posts

Posted 20 September 2012 - 02:05 PM

Thanks for the update. Glad it's working for you now :)

Chris

http://forums.netdui...-v420-update-1/
That shows to install Netduino 4.2.0.1 SDK.
The file from http://forums.netdui...?showtopic=5117
Direct download link http://www.netduino....ETMF42_QFE2.msi
That is still the same file as the old one. The page wasn't updated. Last time I use this, the SDK actually shows 4.2.0.0, not 4.2.0.1.
Would this still work with the new 4.2.0.1 firmware?

#15 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 20 September 2012 - 02:11 PM

Would this still work with the new 4.2.0.1 firmware?

Yes, the SDK remains unchanged, only the firmware is changed.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#16 chungenhung

chungenhung

    Member

  • Members
  • PipPip
  • 27 posts

Posted 21 September 2012 - 01:49 AM

Trying to get this to work. Short code snippet.
What I am trying to accomplish is, when the onboard button is pressed, move the servo.
It is complaining that "servo" is not in the "static void button_OnInterrupt" context.
Servo is declared in 'public static void Main()".
How do I make it visible in "static void button_OnInterrupt"?
Or maybe another way to do this? Thanks.

namespace NetduinoPlusApplication1
{
    public class Program
    {
        public static void Main()
        {
            //********************************************************
            // Row pins. The keypad have 4 rows.
            Cpu.Pin[] RowPins = { Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D4 };

            // Col pins. The keypad have 3 columns.
            Cpu.Pin[] ColPins = { Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D2, Pins.GPIO_PIN_D1 };

            // Initializes the new keypad
            MatrixKeyPad kb = new MatrixKeyPad(RowPins, ColPins);

            // Bind both events
            kb.OnKeyDown += new NativeEventHandler(kb_OnKeyDown);
            kb.OnKeyUp += new NativeEventHandler(kb_OnKeyUp);


            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); //On Board Button. Interrrupt when buttom is pressed down.
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            //SERVO
            var servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1500, PWM.ScaleFactor.Microseconds, false); //Declare servo. 20,000 means 20ms, 1500 means the starting position. 
            //The servo have a range of 750-2250ms.    

            // Lets wait forever for events to occur
            Thread.Sleep(Timeout.Infinite);
        }


        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {

            Debug.Print("OnBoard Button Pressed");
            servo.Duration = 1500; //THIS DOESN'T WORK.
            servo.start(); //THIS DOESN'T WORK.
        }

    }

}


#17 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 21 September 2012 - 01:58 AM

Hi chungenhung,

The button_OnInterrupt event can't see the servo object because it was created in the Main function...and its scope is therefore limited to the Main function unless you pass a reference around.

The easiest thing to do here is to add this line of code right before your Main function:
var servo;
You'll then want to remove the word "var" from "var servo = ..." in your Main function, so that you know it's referencing the static class-level variable and not creating a new one.

Chris

#18 chungenhung

chungenhung

    Member

  • Members
  • PipPip
  • 27 posts

Posted 21 September 2012 - 02:24 AM

Now I have new errors.

namespace NetduinoPlusApplication1
{
    public class Program
    {
        var servo; //ERROR "The type or namespace name 'var' could not be found."

        public static void Main()
        {
            //********************************************************
            // Row pins. The keypad have 4 rows.
            Cpu.Pin[] RowPins = { Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D4 };

            // Col pins. The keypad have 3 columns.
            Cpu.Pin[] ColPins = { Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D2, Pins.GPIO_PIN_D1 };

            // Initializes the new keypad
            MatrixKeyPad kb = new MatrixKeyPad(RowPins, ColPins);

            // Bind both events
            kb.OnKeyDown += new NativeEventHandler(kb_OnKeyDown);
            kb.OnKeyUp += new NativeEventHandler(kb_OnKeyUp);


            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); //On Board Button. Interrrupt when buttom is pressed down.
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            //SERVO. ERROR "An object reference is required for the non-static field, method, or property 'NetduinoPlusApplication1.Program.servo' ".
            servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1500, PWM.ScaleFactor.Microseconds, false); //Declare servo. 20,000 means 20ms, 1500 means the starting position. 
            //The servo have a range of 750-2250ms.    

            // Lets wait forever for events to occur
            Thread.Sleep(Timeout.Infinite);
        }


        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {

            Debug.Print("OnBoard Button Pressed");
            servo.Duration = 1500; //THIS DOESN'T WORK.
            servo.start(); //THIS DOESN'T WORK.
        }

    }

}


#19 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 21 September 2012 - 02:38 AM

Sorry, try this:

static Microsoft.SPOT.Hardware.PWM servo;

'var' needs to know what you're creating...thus the error. Also, you'll need the keyword static in there to make sure it's allocated properly.

Chris

P.S. We usually put a _ in front of class-level variables. In other words, we'd name it "_servo".

#20 chungenhung

chungenhung

    Member

  • Members
  • PipPip
  • 27 posts

Posted 21 September 2012 - 02:50 AM

In "Public class Program", I have "static Microsoft.SPOT.Hardware.PWM _servo;". In "public static void Main()", I have "_servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1500, PWM.ScaleFactor.Microseconds, false);" In "static void button_OnInterrupt", I have "_servo.duration = 1500;". Now it's complaining about "duration". Error message "Microsoft.SPOT.Hardware.PWM does not contain a definition for duration."




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.