Servo class in 4.3.1

I’m using the new version of the class of Chris:

/*
 * 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;
        }
    }
}
Servo servo = new Servo(PWMChannels.PWM_PIN_D5);
 servo.Degree = 30; // 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);
                 }
             }

I connecting servo in
3.3v
gnd
port 5 digital in Netduino

my servo is:
http://www.hobbyking…5g_0_14sec.html

not working! :-[

most servos need 5v not 3v3.

Why don’t you explaining what happens? Does your code compile, does the servo do anything? What about trying to simplify, and just add an LED and see if the PWM works as you’d expect, varying the intensity of the LED.

1 Like

Link to the servo information is not working, so I do not exactly know what servos you have, but could it be you are using a digital servo?

Also the thing to remember with servo, the hobby servos most likely will not give the full 180 degrees of rotation. You are most likely to get 120 degrees of rotation with cheapish ones.

As Brett said, most hobby servos are 5V, so that is likely your issue. I have experienced an issue with one type of servo that could use a voltage range, at the lower end of range they did not function correctly.

1 Like