Netduino home hardware projects downloads community

Jump to content


Photo

Netduino servo class


  • Please log in to reply
56 replies to this topic

#41 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 10 April 2012 - 04:30 AM

Wow, a year later at least and people are still getting a bunch of use out of this thing. :rolleyes:

#42 DForce

DForce

    New Member

  • Members
  • Pip
  • 3 posts

Posted 24 June 2012 - 10:02 AM

Wow, a year later at least and people are still getting a bunch of use out of this thing. :rolleyes:


So am I :) I was going to make my own, but then I was searching for the PWM specs I just saw your post for controlling servo's. Thanks! Going to make much use of it!

Btw. Might it be handy to make some sort of reference for servo's which maximum setRange to use?

I've got a Robbe FS100 here and the settings for this are 250 for left and 2350 for right. It might vary sometimes but this could be a good indicator?

#43 DForce

DForce

    New Member

  • Members
  • Pip
  • 3 posts

Posted 24 June 2012 - 10:14 AM

I've tested another Servo the :"Multiplex MS-X6" and the setRange i'm using is (550,2300) As for a question that came up while testing servo's, the value for the left rotation is actually for the right. Then I've got to set the inverted boolean, while actually everything is connected the normal way. Am I the only one with this problem? Greetzz, Matthijs

#44 Cole Prince

Cole Prince

    New Member

  • Members
  • Pip
  • 8 posts
  • LocationJonesboro, AR, USA

Posted 27 June 2012 - 03:48 AM

Hopefully this thread is not completely dead. I have tried to use the class from both Chris Seto and the one that OZ modified and I am having the same issue on both. I have created the Servo_API.dll and I have added it is a reference to my project. I am receiving no errors from the code (all references to 'servo' show to be working. However, when I try to debug the application it gives me a error with a description of "0xfd000000". When I click on the error a popup appears that says "This document cannot be opened. It has been renamed, deleted, or moved." If I remove the Servo_API references (and all comment out my code of course) I have no issues whatsoever in loading my project to my Netduino Plus. If I add it back, the problem comes back. I am sure that there is something obvious that I am overlooking. I appreciate any help that you can give.

#45 Stefan

Stefan

    Moderator

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

Posted 27 June 2012 - 06:18 AM

Hopefully this thread is not completely dead. I have tried to use the class from both Chris Seto and the one that OZ modified and I am having the same issue on both. I have created the Servo_API.dll and I have added it is a reference to my project. I am receiving no errors from the code (all references to 'servo' show to be working. However, when I try to debug the application it gives me a error with a description of "0xfd000000". When I click on the error a popup appears that says "This document cannot be opened. It has been renamed, deleted, or moved." If I remove the Servo_API references (and all comment out my code of course) I have no issues whatsoever in loading my project to my Netduino Plus. If I add it back, the problem comes back. I am sure that there is something obvious that I am overlooking. I appreciate any help that you can give.


Try removing the underscore in the namespace. Class Libraries have way stricter naming rules.
"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

#46 Pak0loko

Pak0loko

    New Member

  • Members
  • Pip
  • 1 posts

Posted 26 September 2012 - 07:05 PM

Hi Guys.
I´m new on this kind of thigs like programing using netduino and even on the use of c# :S.
I´ve been trying a lot to make this code works on firmware 4.2, you know the PWM changes and some other things.
Can anyone of you please help me on this?
I´ll appreciate it a lot.
Thanks in Advance.

This is a class I use on my other NETMF hardware to run my RC car based robot. It took very little effort to port to the Netduino, hopefully someone find it useful :)

/*
 * 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)
 * 
 * 
 * */

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

namespace Servo_API
{
	public class Servo : IDisposable
	{
		/// <summary>
		/// PWM handle
		/// </summary>
		private PWM servo;

		/// <summary>
		/// Timings range
		/// </summary>
		private int[] range = new int[2];
		
		/// <summary>
		/// Set servo inversion
		/// </summary>
		public bool inverted = false;

		/// <summary>
		/// Create the PWM pin, set it low and configure timings
		/// </summary>
		/// <param name="pin"></param>
		public Servo(Cpu.Pin pin)
		{
			// Init the PWM pin
			servo = new PWM((Cpu.Pin)pin);

			servo.SetDutyCycle(0);

			// Typical settings
			range[0] = 1000;
			range[1] = 2000;
		}

		public void Dispose()
		{
			disengage();
			servo.Dispose();
		}

		/// <summary>
		/// Allow the user to set cutom timings
		/// </summary>
		/// <param name="fullLeft"></param>
		/// <param name="fullRight"></param>
		public void setRange(int fullLeft, int fullRight)
		{
			range[1] = fullLeft;
			range[0] = fullRight;
		}

		/// <summary>
		/// Disengage the servo. 
		/// The servo motor will stop trying to maintain an angle
		/// </summary>
		public void disengage()
		{
			// See what the Netduino team say about this... 
			servo.SetDutyCycle(0);
		}

		/// <summary>
		/// Set the servo degree
		/// </summary>
		public double Degree
		{
			set
			{
				/// Range checks
				if (value > 180)
					value = 180;

				if (value < 0)
					value = 0;

				// Are we inverted?
				if (inverted)
					value = 180 - value;

				// Set the pulse
				servo.SetPulse(20000, (uint)map((long)value, 0, 180, range[0], range[1]));
			}
		}

		/// <summary>
		/// Used internally to map a value of one scale to another
		/// </summary>
		/// <param name="x"></param>
		/// <param name="in_min"></param>
		/// <param name="in_max"></param>
		/// <param name="out_min"></param>
		/// <param name="out_max"></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;
		}
	}
}

Example code:
using System.Threading;
using SecretLabs.NETMF.Hardware.Netduino;
using Servo_API;

namespace NetduinoServoDemo
{
	public class Program
	{
		public static void Main()
		{
			Servo servo = new Servo(Pins.GPIO_PIN_D9);

			while (true)
			{
				for (int i = 0; i <= 180; i++)
				{
					servo.Degree = i;
					Thread.Sleep(10);
				}

				for (int i = 180; i >= 0; i--)
				{
					servo.Degree = i;
					Thread.Sleep(10);
				}
			}
		}

	}
}


Pako Gomez
"Live and Let Live"

#47 BrandonW6

BrandonW6

    New Member

  • Members
  • Pip
  • 8 posts

Posted 30 September 2012 - 02:00 AM

I updated the sample for the new PWM classes in 4.2.0.1
Changes include PWM constructor code with new parameters and code to set angle.

/*
 * 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)
 * 
 * 
 * */

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

namespace Servo_API
{
    public class Servo : IDisposable
    {
        /// <summary>
        /// PWM handle
        /// </summary>
        private PWM servo;

        /// <summary>
        /// Timings range
        /// </summary>
        private int[] range = new int[2];

        /// <summary>
        /// Set servo inversion
        /// </summary>
        public bool inverted = false;

        /// <summary>
        /// Create the PWM Channel, set it low and configure timings
        /// </summary>
        /// <param name="pin"></param>
        public Servo(Cpu.PWMChannel channelPin) 
        {
            // Init the PWM pin
            servo = new PWM((Cpu.PWMChannel)channelPin, 20000, 1500,PWM.ScaleFactor.Microseconds, false);

            servo.DutyCycle = 0;
            // Typical settings
            range[0] = 1000;
            range[1] = 2000;
        }

        public void Dispose()
        {
            disengage();
            servo.Dispose();
        }

        /// <summary>
        /// Allow the user to set cutom timings
        /// </summary>
        /// <param name="fullLeft"></param>
        /// <param name="fullRight"></param>
        public void setRange(int fullLeft, int fullRight)
        {
            range[1] = fullLeft;
            range[0] = fullRight;
        }

        /// <summary>
        /// Disengage the servo. 
        /// The servo motor will stop trying to maintain an angle
        /// </summary>
        public void disengage()
        {
            // See what the Netduino team say about this... 
            servo.DutyCycle = 0; //SetDutyCycle(0);
        }

        /// <summary>
        /// Set the servo degree
        /// </summary>
        public double Degree
        {
            set
            {
                /// Range checks
                if (value > 180)
                    value = 180;

                if (value < 0)
                    value = 0;

                // Are we inverted?
                if (inverted)
                    value = 180 - value;

                // Set the pulse
                //servo.SetPulse(20000, (uint)map((long)value, 0, 180, range[0], range[1]));
                servo.Duration = (uint)map((long)value, 0, 180, range[0], range[1]);
            }
        }

        /// <summary>
        /// Used internally to map a value of one scale to another
        /// </summary>
        /// <param name="x"></param>
        /// <param name="in_min"></param>
        /// <param name="in_max"></param>
        /// <param name="out_min"></param>
        /// <param name="out_max"></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;
        }
    }
}

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

namespace NetduinoApplication5
{
    public class Program
    {
        public static void Main()
        {
            Servo servo = new Servo(PWMChannels.PWM_PIN_D9);
           
            // Change 'While' to 'For' to limit how many time it repeats
            for (int j = 0; j < 3; j++) 
            {
                for (int i = 0; i <= 180; i++)
                {
                    servo.Degree = i;
                    Thread.Sleep(10);
                }

                for (int i = 180; i >= 0; i--)
                {
                    servo.Degree = i;
                    Thread.Sleep(10);
                }
            }

        }

    }
}


#48 rchelicopter

rchelicopter

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationSan Diego, Ca

Posted 27 December 2012 - 03:43 PM

Hello,

 

I've been playing around trying to get the PWM and analog Input stuff working on .netmf 4.2 and have been running into some problems.  So I thought I would post this to help someone else possibly, keep in mind I'm pretty new to this stuff.

I wasn't able to get any PWM output until I added servo.Start(); at the end of public Servo(Cpu.PWMChannel channelPin). Like Below

 

public Servo(Cpu.PWMChannel channelPin) { // Init the PWM pin servo = new PWM((Cpu.PWMChannel)channelPin, 20000, 1500,PWM.ScaleFactor.Microseconds, false); servo.DutyCycle = 0;

servo.Start();

// Typical settings range[0] = 1000; range[1] = 2000;

{

 

P.S.  Thank you Chris Seto for originally writing this, it has been a great deal of help to me.



#49 lancewmccarthy

lancewmccarthy

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationBoston, MA USA

Posted 06 April 2013 - 01:14 PM

I'm using the NP2 and MF 4.3, have you updated the project for 4.3? I'm having problems trying to get it work. The servo is going to operate a small hatch door, I simply want to set an "open" state and a "closed" state.

 

Update: I didn't see the end of this thread until I posted this reply. RCHelicopter's tip fixed it.

 

Thanks for sharing guys!


Lance McCarthy

Microsoft MVP (Windows Platform Development)


#50 gingemonster

gingemonster

    New Member

  • Members
  • Pip
  • 1 posts

Posted 29 June 2013 - 10:04 AM

I tried the latest code for this and as far as I can see the map function is wrong? It was giving me values not in my output range? Am I imagining this or has something changed in the most updated code?



#51 john51

john51

    New Member

  • Members
  • Pip
  • 2 posts

Posted 14 July 2014 - 12:07 AM

Hi Everyone,

  Im new to programming the netduino, but have learned a lot by pasting code and getting a few projects to work, I just got done with the Servo knob and sweep, and had very little problem getting it to work, the sweep.  But I noticed that the movement of the servo was only 90 degrees instead of the full 180+ that I should be getting with the servo.  Im using the Futaba s3003 servo, but after trying a few things with the code I still cant increase the movement range.  What part of the code determines the range of movement, and what should I do to increase it?  I will be reading up on some articles of servos while I wait for a reply. Thanks in advance for any help.



#52 perpetualKid

perpetualKid

    Member

  • Members
  • PipPip
  • 20 posts

Posted 14 July 2014 - 09:30 PM

in that code above it should be these lines defining the servo range

 

// Typical settings
range[0] = 1000;
range[1] = 2000;

 

Typically a 1500ns pulse defines the midpoint, and you may try some broader range to turn your servo the full 180 degree. I'm sure there is some spec available where you could read up the lower and upper boundaries, else just try in some steps like 950 and 2050, 900 and 2100 etc to find the full range. Some servos go as braod as 500 for the low bound (range[0]) up to 2500 for the upper bound (range[1]).



#53 john51

john51

    New Member

  • Members
  • Pip
  • 2 posts

Posted 15 July 2014 - 03:17 AM

Thanks for the info everyone, glad to have this group as a resource for problems.

#54 TareqGamal

TareqGamal

    Member

  • Members
  • PipPip
  • 19 posts

Posted 14 November 2015 - 04:52 PM

it did not work for me due Memory problem

May this program not fit with my Device ?

I have :

NetDuino3 wifi

Windows 7

VS 2013

-----------------

also at compile time :

It can not make object from servo :

Servo servo = new Servo(PWMChannels.PWM_PIN_D9);

 

I Added the code as it is and referances okay :

 

using System;

using System.Threading;

using Microsoft.SPOT;

using Microsoft.SPOT.Hardware;

using SecretLabs.NETMF.Hardware;

using SecretLabs.NETMF.Hardware.Netduino;

using Servo_API;



#55 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 14 November 2015 - 08:43 PM

Hi Tareq

 

I think you may have go a version of the code from earlier in the thread which targets an older version of .NetMF

 

Below is a version of the code with the modifications suggested in the thread.

 

Fired up a N3 with a servo and it appears to work as expected.

 

@KiwiBryn

blog.devmobile.co.nz

 

/*
 * 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)
 * 
 * 
 * */

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

namespace Servo_API
{
   public class Servo : IDisposable
   {
      /// <summary>
      /// PWM handle
      /// </summary>
      private PWM servo;

      /// <summary>
      /// Timings range
      /// </summary>
      private int[] range = new int[2];

      /// <summary>
      /// Set servo inversion
      /// </summary>
      public bool inverted = false;

      /// <summary>
      /// Create the PWM Channel, set it low and configure timings
      /// </summary>
      /// <param name="pin"></param>
      public Servo(Cpu.PWMChannel channelPin)
      {
         // Init the PWM pin
         servo = new PWM((Cpu.PWMChannel)channelPin, 20000, 1500, PWM.ScaleFactor.Microseconds, false);  

         servo.DutyCycle = 0;
         // Typical settings
         range[0] = 1000;
         range[1] = 2000;

         servo.Start(); 
      }

      public void Dispose()
      {
         disengage();
         servo.Dispose();
      }

      /// <summary>
      /// Allow the user to set cutom timings
      /// </summary>
      /// <param name="fullLeft"></param>
      /// <param name="fullRight"></param>
      public void setRange(int fullLeft, int fullRight)
      {
         range[1] = fullLeft;
         range[0] = fullRight;
      }

      /// <summary>
      /// Disengage the servo. 
      /// The servo motor will stop trying to maintain an angle
      /// </summary>
      public void disengage()
      {
         // See what the Netduino team say about this... 
         servo.DutyCycle = 0; //SetDutyCycle(0);
      }

      /// <summary>
      /// Set the servo degree
      /// </summary>
      public double Degree
      {
         set
         {
            /// Range checks
            if (value > 180)
               value = 180;

            if (value < 0)
               value = 0;

            // Are we inverted?
            if (inverted)
               value = 180 - value;

            // Set the pulse
            //servo.SetPulse(20000, (uint)map((long)value, 0, 180, range[0], range[1]));
            servo.Duration = (uint)map((long)value, 0, 180, range[0], range[1]);
         }
      }

      /// <summary>
      /// Used internally to map a value of one scale to another
      /// </summary>
      /// <param name="x"></param>
      /// <param name="in_min"></param>
      /// <param name="in_max"></param>
      /// <param name="out_min"></param>
      /// <param name="out_max"></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;
      }
   }
}


#56 TareqGamal

TareqGamal

    Member

  • Members
  • PipPip
  • 19 posts

Posted 15 November 2015 - 03:45 PM

Great thank you for care ,

I tried your code it gives me error in the following lines :

        public Servo(Cpu.PWMChannel channelPin)
        {
            // Init the PWM pin
            servo = new PWM((Cpu.PWMChannel)channelPin, 20000, 1500, PWM.ScaleFactor.Microseconds, false);

            servo.DutyCycle = 0;  //this line
            // Typical settings
            range[0] = 1000;
            range[1] = 2000;

            servo.Start(); //this line also 
        }

 Also this line :

         servo.Duration = (uint)map((long)value, 0, 180, range[0], range[1]);

 

 

error :

Error 1 'SecretLabs.NETMF.Hardware.PWM' does not contain a definition for .....

 

 

 

 

also the Main class have this error in this line:

 Servo servo = new Servo(Pins.GPIO_PIN_D9);



#57 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 16 November 2015 - 04:06 AM

Hi Tareq,

 

Issue 1 sounds like a .NetMF device and/or SDK versioning problem.  For a 4.3 project the PWM reference should be

 

Microsoft.SPOT.Hardware.PWM

 

not 

 

SecretLabs.NETMF.Hardware.PWM

 

If changing the references doesn't help, could you please check the following

 

1) What template did you use to generate your application (Universal vs. Legacy...)

2) What in NetMF version is selected in the Visual Studio in the project properties

3) Do all the references for the project match (3) as not all references get update when you change the project properties
4) The device info using the NetMF deploy tool using \Plug-in\Debug\Show device info
5) what .NetMF & Netduino SDKs do you have installed.

 

If sorting Issue 1 doesn't fix issue 2 then do you have the using statement below

 

using Servo_API;

 

Regards

 

@KiwiBryn

blog.devmobile.co.nz






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.