Netduino home hardware projects downloads community

Jump to content


Photo

Netduino servo class


  • Please log in to reply
56 replies to this topic

#21 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 03 September 2010 - 04:23 AM

Hi Arc, Maybe a stupid question, but the servo does work under a regular RC RX, correct? Unless you have it wired up wrong, I don't know how it could misbehave. Can you post your code? :)

#22 SkippyFiRe

SkippyFiRe

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationMA, USA

Posted 26 September 2010 - 03:48 AM

Hi guys, I'm a newb (when it comes to hardware) and I'm trying to hook up this parallax servo. The red wire is hooked into the 5V pin, the black is hooked into the ground pin, and the white is hooked into the Digital I/O 7 pin. I tried my own code to make the servo move, but was getting an exception. So I tried the Servo class from above (which essentially does the same thing) and I get the same error: "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in SecretLabs.NETMF.Hardware.dll" I get the System.ArgumentOutOfRangeException when it tries to new up the Servo class, and I get the same exception when I try and instantiate the PWM with the digital I/O 7 pin. I assume that this means that it doesn't see anything connected to the pin, or maybe it's not the right type of device? Any ideas what the problem might be? FYI, the servo does power on, as it jitters a little bit then I plug it in, I've also tried different digital pins to no avail.
- SkippyFiRe

#23 hari

hari

    Advanced Member

  • Members
  • PipPipPip
  • 131 posts

Posted 26 September 2010 - 05:24 AM

Hi guys,
I'm a newb (when it comes to hardware) and I'm trying to hook up this parallax servo. The red wire is hooked into the 5V pin, the black is hooked into the ground pin, and the white is hooked into the Digital I/O 7 pin. I tried my own code to make the servo move, but was getting an exception. So I tried the Servo class from above (which essentially does the same thing) and I get the same error:

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in SecretLabs.NETMF.Hardware.dll"

I get the System.ArgumentOutOfRangeException when it tries to new up the Servo class, and I get the same exception when I try and instantiate the PWM with the digital I/O 7 pin.

I assume that this means that it doesn't see anything connected to the pin, or maybe it's not the right type of device? Any ideas what the problem might be?

FYI, the servo does power on, as it jitters a little bit then I plug it in, I've also tried different digital pins to no avail.


I've made that same mistake before. Posted Image
R/C Servos use Pulse Width Modulation, and PWM is only supported on digital pins 5,6,9, and 10
http://netduino.com/netduino/specs.htm
Change it from pin 7 (hardware and software) to a valid PWM pin and it'll work.

#24 SkippyFiRe

SkippyFiRe

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationMA, USA

Posted 26 September 2010 - 05:09 PM

Ahhhh, I see that now on the spec sheet! I didn't realize there was a difference in the functionality of the pins! I'll be sure to check that the next time I'm using something. Thanks! So now the servo works great! Mission accomplished! Thanks again!
- SkippyFiRe

#25 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 05 December 2010 - 04:13 PM

I'll use this in my TankBot. I'll have to change it up a bit to fit my own style, but I'll link to this thread so people know where the original is. I'll note it in the header of the code. Very nice code though, well done. Thanks

#26 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 05 December 2010 - 04:37 PM

Alright so I changed it around a bit, tell me what you think of the changes.

/*
 * Servo NETMF Driver
 *      Coded by Chris Seto August 2010
 *      <chris@chrisseto.com> 
 *      
 * Use this code for whatveer you want. Modify it, redistribute it, I don't care.
 * I do ask that you please keep this header intact, however.
 * If you modfy the driver, please include your contribution below:
 * 
 * Chris Seto: Inital release (1.0)
 * Chris Seto: Netduino port (1.0 -> Netduino branch)
 * Chris Seto: bool pin state fix (1.1 -> Netduino branch)
 * 
 * Omar (OZ): I've changed a decent amount of code... original is here: http://forums.netduino.com/index.php?/topic/160-netduino-servo-class/
 * 
 * */

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

namespace Servo_API
{
    public class Servo : IDisposable
    {
        private PWM servoControl;
        private uint[] _range = new uint[2];

        private const uint IN_MAX = 180, IN_MIN = 0;


        private bool _reverse = false;

        public Servo(Cpu.Pin pin)
        {
            servoControl = new PWM(pin);

            servoControl.SetDutyCycle(0); // Start stopped.

            // Set default timing settings
            _range[0] = 1000;
            _range[1] = 2000;
        }

        public void SetDegree(double _degree)
        {
            if (_degree > IN_MAX) _degree = IN_MAX; // If greater than 180, then make it 180
            if (_degree < IN_MIN) _degree = IN_MIN; // If less than 0, then make it 0
            if (_reverse) _degree = IN_MAX - _degree; // Modify it to be reversed.

            servoControl.SetPulse(20000, (uint)map((long)_degree, IN_MIN, IN_MAX, _range[0], _range[1]));
        }

        public uint Full_Right
        {
            get { return _range[0]; }
            set { _range[0] = value; }
        }

        public uint Full_Left
        {
            get { return _range[1]; }
            set { _range[1] = value; }
        }

        public bool Reversed
        {
            get { return _reverse; }
            set { _reverse = value; }
        }

        // This is from http://www.arduino.cc/en/Reference/Map ... You can read more about it there.
        /// <summary>
        /// Used internally to map a value of one scale to another
        /// </summary>
        /// <param name="x">Current value input</param>
        /// <param name="in_min">Minimum input</param>
        /// <param name="in_max">Maximum input</param>
        /// <param name="out_min">Minimum output</param>
        /// <param name="out_max">Maximum ouput</param>
        /// <returns></returns>
        private long map(long x, long in_min, long in_max, long out_min, long out_max)
        {
            return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
        }

        ~Servo()
        {
            Dispose();
        }

        public void Dispose()
        {
            servoControl.SetDutyCycle(0);
            servoControl.Dispose();
        }
    }
}


#27 Mustang

Mustang

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationDetroit, Michigan

Posted 05 December 2010 - 05:56 PM

I've been experimenting with a Futaba S3003 and for some reason the limits (0 and 180), put the servo beyond its bounds. I can hear it straining to get to the 0 or 180 mark. If I use 50 and 150 as the limits, it's no problem.

In testing, I ran:
for (int i = 0; i <= 180; i++)
     {
          myServo.Degree = i;
     }

Not sure why my servo is straining to get to the outer bounds, but I'll look into it more.

#28 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 05 December 2010 - 11:52 PM

This class was tested with the S3003, but you may be using the FEZ version of the driver; there two different boards have differences in the timing parameters.

#29 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 14 December 2010 - 02:35 AM

Hey Chris S, I ran into the weirdest problem I'v ever seen! I am using your original code... When plugged in with the USBthe servo does what it is supposed to do, when I use a good old 9V battery it doesn't work! It smacks into the side and keep going that way.... I haven't been able to figure out why this is happening, do you have any suggestions?

#30 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 14 December 2010 - 04:47 PM

Not powering the servo from Vin, right?

#31 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 14 December 2010 - 08:41 PM

Not powering the servo from Vin, right?


nope, i have it powered with the netduino's 5V.

#32 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 15 December 2010 - 03:16 AM

If it has any load on it, you need to power it through some other source. Other than that, I think you need to probe the signal pin to see what it's doing.

#33 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 15 December 2010 - 08:27 PM

If it has any load on it, you need to power it through some other source. Other than that, I think you need to probe the signal pin to see what it's doing.


I think it is pulling too much power from the netduino. I'll power it externally and post the results here...

#34 Tarkon

Tarkon

    New Member

  • Members
  • Pip
  • 3 posts

Posted 21 April 2011 - 01:21 AM

I'm extremely new to netduino programming, so sorry if this is an idiotic question, but I'm getting an error when trying to compile this. I get An unhandled exception of type 'System.NotSupportedException' occurred in SecretLabs.NETMF.Hardware.dll Any idea what I'm doing wrong? :(

#35 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 21 April 2011 - 01:50 AM

Hi Tarkon, Have you selected your Netduino as the deployment target (in Project Properties > .NET Micro Framework)? If your app is running in the emulator instead, hardware features won't work :) Chris

#36 Michel Trahan

Michel Trahan

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts

Posted 21 April 2011 - 04:11 AM

Why not push this to the sandbox.netduino.com ? the projects wiki ? Thanks for sharing :)
Started with C in 1985, moved to Vb3 ... to vb6 and stopped. Now started with .Net and learning C# and VB.net and wishing VB.net was on MF !

#37 Tarkon

Tarkon

    New Member

  • Members
  • Pip
  • 3 posts

Posted 21 April 2011 - 11:29 PM

Hi Tarkon,

Have you selected your Netduino as the deployment target (in Project Properties > .NET Micro Framework)? If your app is running in the emulator instead, hardware features won't work :)

Chris



Thank you, I'm big stupid. ^_^

#38 hampee

hampee

    New Member

  • Members
  • Pip
  • 8 posts

Posted 02 January 2012 - 11:14 PM

  ~Servo()
   {
      Dispose();
   }


could you explain this peace of code?

#39 SkippyFiRe

SkippyFiRe

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationMA, USA

Posted 03 January 2012 - 02:02 AM

It's the destructor in C#: http://msdn.microsof...6x5fx1b(v=vs.80).aspx
- SkippyFiRe

#40 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 09 April 2012 - 11:35 PM

Thanks for the servo library. Going to use it soon.
Steve


My Other Hobby: Engineer Turned Baker




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.