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

HXT 900 Servo


  • Please log in to reply
20 replies to this topic

#1 Jason Smith

Jason Smith

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationEnglewood, CO

Posted 12 August 2010 - 04:59 AM

All,

I was able to successfully control an HXT 900 servo using the Netduino board this evening and wanted everyone's thoughts on the code.

Basically, to figure out how to use the servo I read through a beginners guide to C# and the micro framework created by GHI located Here.

..after attempting to apply this code to the Netduino I realized that the logic for using the PWM.SetPulse method must be different on the Netduino, or I am just completely using it wrong...although was still able to get this to work. I know there are also different methods I could have used to get this to work, but this one really does seem to work well. I ran this servo for over an hour tonight without any issues.


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

namespace Servo1
{
    public class Program
    {
        static PWM Servo1 = new PWM(Pins.GPIO_PIN_D9);
        static uint _left = 1000;
        static uint _center = 1500;
        static uint _right = 2000;
        static uint _lastposition = 0;

        public static void Main()
        {
            while (true)
            {
                MoveServo(Servo1,_left, 100);
                MoveServo(Servo1,_center,100);
                MoveServo(Servo1,_right, 100);
            }
        }

        //Move the servo between 1000(left) on 2000(right).
        static void MoveServo(PWM whatservo, uint whereto, int sleep = 0) 
        {
            uint HowLong = 0;
            //exit the void to avoid servo damage if the value's are not within range.
            if (whereto > 2000 || whereto < 1000) {return;}
            //Begin sending the pulse to the servo.
            whatservo.SetPulse(20000, whereto);
            //if the lastposition of the servo is unknown (the default 0 value set above),
            //default the value to 300, otherwise calculate how longe the servo shoud run.
            if (_lastposition == 0) { HowLong = 300; }
            else { HowLong = _lastposition > whereto ? (_lastposition - whereto) / 3 : (whereto - _lastposition) / 3; }
            //continue sending the pulse using the howlong calculation.
            Thread.Sleep((int)HowLong);
            //stop the pulse.
            whatservo.SetPulse(0, whereto);
            //reset the last position after the move.
            _lastposition = whereto;
            //sleep the specified amount of time if applicable.
            if (sleep != 0) { Thread.Sleep(sleep); }
        }
    }
}


ON a side note, the above code works for both 3.3v and 5v, but the servo seems to operate a lot smoother on 3.3v because of the "HowLong" calculation I am using.. a small adjustment to the calculation would fix this.

You see a video of the servo in action below:

http://www.youtube.com/watch?v=yUgzQP85qc4

Jason

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 August 2010 - 05:32 AM

Very cool! I don't want to hijack this thread, but it brings up a quick thought. We're looking to add some additional functionality in the next version of the PWM classes. On the table are a few ideas: assignment of the PWM channels to one of two clocks; setting individual clock frequencies; possible interrupt-based sine and triangle wave functions; possible software PWM on hardware pins. Would any of those be useful in your projects? BTW, just curious: what was the difference you noted between our PWM implementation and the GHI one? Chris

#3 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 12 August 2010 - 05:42 AM

I too think this is very cool. I was curious about how you'd expose servo support in the Netduino as opposed to the Arduino. It would be nice for servos to be able to express movement in degrees rather than arbitrary 1000, 1500, and 2000's.

#4 Jason Smith

Jason Smith

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationEnglewood, CO

Posted 12 August 2010 - 05:58 AM

Very cool!

I don't want to hijack this thread, but it brings up a quick thought. We're looking to add some additional functionality in the next version of the PWM classes. On the table are a few ideas: assignment of the PWM channels to one of two clocks; setting individual clock frequencies; possible interrupt-based sine and triangle wave functions; possible software PWM on hardware pins. Would any of those be useful in your projects?

BTW, just curious: what was the difference you noted between our PWM implementation and the GHI one?

Chris


Chris,

It looks like the difference is that the FEZ PWM.SetPulse method uses nanoseconds as seen in the below code... or its 100% possible I am using the Netduino Setpulse method incorrectly as I am very new to microcontroller's. The Netduino is my first board. I really need to get my "Google" degree in a lot of this stuff. On that note, and after "Googleing" the changes you have on the table to the PWM classes, I think all those additions would be very useful.



using System;
using System.Threading;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
class Program
{
    public static void Main()
    {
        PWM servo = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5);
        while (true)
        {
            // 0 degrees. 20ms period and 1.25ms high pulse
            servo.SetPulse(20 * 1000 * 1000, 1250 * 1000);
            Thread.Sleep(1000);//wait for a second
            
            // 90 degrees. 20ms period and 1.50ms high pulse
            servo.SetPulse(20 * 1000 * 1000, 1500 * 1000);
            Thread.Sleep(1000);//wait for a second
            
            // 180 degrees. 20ms period and 1.75ms high pulse
            servo.SetPulse(20 * 1000 * 1000, 1750 * 1000);
            Thread.Sleep(1000);//wait for a second
        }
        Thread.Sleep(-1);
    }
}

Jason

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 12 August 2010 - 06:08 AM

BTW, just curious: what was the difference you noted between our PWM implementation and the GHI one?

I have also noted differences in feature implementations among various .NET Micro Framework SDKs, in this very example of PWM:

SecretLabs.NETMF.Hardware
PWM.SetDutyCycle(uint dutyCycle);
PWM.SetPulse(uint period, uint duration);

GHIElectronics.NETMF.Hardware
PWM.Set(bool pinState);
PWM.Set(int freq_Hz, byte dutyCycle);
PWM.SetPulse(uint period_ns, uint highTime_ns);

DeviceSolutions.SPOT.Hardware
Meridian.PWM.Configure(uint Frequency, byte Duty); // Note the nested class

There are several other MF SDKs, which will probably have yet another variants, but I think it is enough to get the picture.

#6 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 12 August 2010 - 07:02 AM

Very nice! I have a servo and ESC driver I wrote for the FEZ Domino. I'll port the code over and post it tomorrow.

#7 Flores

Flores

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 12 August 2010 - 02:41 PM

Did you directly attach the servo to the outputs? How can that not break your board? I've read somewhere that the max output I is 17mA.. surely the servo is more than that?

#8 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 12 August 2010 - 03:08 PM

Yes, the servo can be attached directly to the board. Ground, signal, power (not necessarily in that order) As for the current question, the only time a servo really draws much current is when it's driving something. His was spinning freely, so it wasn't drawing much. I don't know if that 17mA figure is correct, but I would assume the limit is more than that -- although the other Chris will need to confirm.

#9 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 August 2010 - 03:19 PM

The signals to the servos are just pulses which direct the servo's position. It's the power from the 5V/3.3V header which is providing the power. The Netduino can provide 100s of mA of power. The on-board power regulators are rated at 800mA. You can draw a few 100 mA of power for your projects...not a problem. Just remember to use an external power supply (via the power barell or VIN connector) if you're drawing a bunch of current. Chris

#10 Flores

Flores

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 12 August 2010 - 05:35 PM

The signals to the servos are just pulses which direct the servo's position. It's the power from the 5V/3.3V header which is providing the power.

The Netduino can provide 100s of mA of power. The on-board power regulators are rated at 800mA. You can draw a few 100 mA of power for your projects...not a problem. Just remember to use an external power supply (via the power barell or VIN connector) if you're drawing a bunch of current.

Chris


I was reffering to the maximum output of a digital output. That is 17mA right?

#11 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 12 August 2010 - 06:11 PM

I was reffering to the maximum output of a digital output. That is 17mA right?

There are four I/O lines PA0, PA1, PA2, PA3 (Netduino pins D2,D3,-,D7) that can drive up to 16 mA permanently, the remaining lines can draw only 8 mA. The total current drawn by all the I/O lines cannot exceed 200 mA.

#12 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 August 2010 - 07:24 PM

To add to what CW2 said, Analog pins 0-3 are rated to drive 2mA when used in digital mode.

#13 dm3281

dm3281

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts

Posted 30 October 2010 - 03:00 AM

You mind telling me where I can buy the HXT 900?

#14 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 31 October 2010 - 12:29 AM

www.hobbycity.com has lots and lots of servos at very low prices.

#15 dm3281

dm3281

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts

Posted 31 October 2010 - 01:46 AM

Thanks -- does anyone know what standard size Servo will work with netduino? I just purchased the HXT500 and 900, but wanted to get a couple full-size just to have them. Everything I see shows 5v??

#16 Karashnaho

Karashnaho

    New Member

  • Members
  • Pip
  • 2 posts

Posted 10 November 2010 - 04:21 PM

Very nice!

I have a servo and ESC driver I wrote for the FEZ Domino. I'll port the code over and post it tomorrow.



Hi there - First post here.
Did you ever do a port from the FEZ?

Many thanks

#17 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 10 November 2010 - 08:31 PM

Hi there - First post here.
Did you ever do a port from the FEZ?

Many thanks

Yes, I have a port.

See: http://files.chrisseto.com/1Vu

#18 Karashnaho

Karashnaho

    New Member

  • Members
  • Pip
  • 2 posts

Posted 10 November 2010 - 09:50 PM

Yes, I have a port.

See: http://files.chrisseto.com/1Vu

Many thanks Chris - I will try and get my head round it :-)

#19 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 10 November 2010 - 11:15 PM

Many thanks Chris - I will try and get my head round it :-)


No problem. It's pretty simple stuff. The code that you see there is the complete package to drive an RC car.

#20 Arbiter

Arbiter

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationBrisbane, Australia

Posted 19 July 2012 - 09:51 AM

Did you directly attach the servo to the outputs?

How can that not break your board? I've read somewhere that the max output I is 17mA.. surely the servo is more than that?



Assuming Netduino overcurrent protection works, the servo will simply produce less torque.
One day, all this too shall parse.




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.