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

Help with a Servo code


  • Please log in to reply
19 replies to this topic

#1 ulterior

ulterior

    New Member

  • Members
  • Pip
  • 7 posts

Posted 23 October 2012 - 01:22 PM

I am new to this. I am trying to program a Servo motor the spin in one direction and then spin in the other direction. I just can't seem to get the coding right. Can anyone help me with a code that will get this done?

#2 Stefan

Stefan

    Moderator

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

Posted 23 October 2012 - 01:50 PM

Hi ulterior and welcome to the Netduino Community! I'm curious, do you already have code? On which part do you need help? Here you can see some code already: http://forums.netdui...no-servo-class/
"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

#3 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 23 October 2012 - 01:58 PM

ulterior, I've seen you pop in the chat room a few times, but I keep missing you. If you need a hand drop on by again and we'll see if we can get you setup and spinning. Cheers, Gutworks (Steve)

#4 ulterior

ulterior

    New Member

  • Members
  • Pip
  • 7 posts

Posted 23 October 2012 - 02:01 PM

Thank you for the greeting and the fast response. I tried that code but got an error when I tried to debug it. this is the error message. Error 1 Program 'C:\Users\Owner\AppData\Local\Temporary Projects\NetduinoApplication1\obj\Debug\NetduinoApplication1.exe' does not contain a static 'Main' method suitable for an entry point NetduinoApplication1 Sorry for being stupid when it comes to this, but like I said I am new.

#5 Stefan

Stefan

    Moderator

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

Posted 23 October 2012 - 02:05 PM

The code is just an additional class. In a Visual Studio project you can load it as additional file in your project. So you still have the project template, and to that, you can add several other pieces of code in additional files.
"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

#6 ulterior

ulterior

    New Member

  • Members
  • Pip
  • 7 posts

Posted 23 October 2012 - 02:15 PM

Not sure what you are talking about. Sorry

#7 ulterior

ulterior

    New Member

  • Members
  • Pip
  • 7 posts

Posted 23 October 2012 - 02:53 PM

Do you have an example of a project template that would work for this project?

#8 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 23 October 2012 - 02:54 PM

Hi ulterior,

I remember when I first started out and had no idea where to actually start. I think a great place to get the basics down would be to buy the book "Getting Started with Netduino" by Chris Walker. I found it incredibly helpful and went over all the general topics such as servos and motor control in Chapter 7. The book is also available as an eBook which I find very useful and often have it open when needing a quick reference.

In addition to the book, I have also often used the tutorials found on the Netduino Wiki. Community member ItsDan converted some awesome Arduino tutorials over to the Netduino platform and I have found myself referencing those many times. Many of the parts there can be purchase as kits from Sparkfun, or you can buy the components as needed from somewhere like your local electronics store, or eBay.

Here is the servo tutorial that also explains what is happening. Once you finish that one I would suggest you post some of your questions here and we'll try to answer and explain them as best as possible. Also don't forget to check the datasheets on the servo you have. Each servo may be configured differently and to have it operating smoothly you may need to alter your code slightly.

In any case, feel free to ask away here or in the chat room. Good luck and have fun!

Cheers,
Steve

#9 ulterior

ulterior

    New Member

  • Members
  • Pip
  • 7 posts

Posted 23 October 2012 - 06:52 PM

looks like this might work. Thanks so much for the help! Just one more question (for now). The program says the following: preparing to deploy assemblies to the device. And it is stuck there. Been like that for about 10 minutes now. Is this normal?

#10 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 23 October 2012 - 07:28 PM

looks like this might work. Thanks so much for the help! Just one more question (for now). The program says the following: preparing to deploy assemblies to the device. And it is stuck there. Been like that for about 10 minutes now. Is this normal?


I have seen this happen from time to time with Visual Studio. It's not something that should be happening, and depending on your version of firmware and usb drivers, you can cause a BSOD (computer restart) if you unplug your Netduino. The latest firmware release and Netduino SDK seem to have fixed the BSOD, at least for me.

So, to stop the deployment you should stop the Build process. To do this press Control-Break or in the Visual Studio menu go to Build> Cancel. After this, disconnect your Netduino from the USB port and plug it back in again. To make sure your VS project has found the device, right click on the Solution (not the Project) and select Properties. Then in the .Net Micro Framework tab, make sure in the Transport dropdown you see USB as the transport type, and you see the Netduino listed in the Device dropdown.

Keep us posted!
Steve

#11 ulterior

ulterior

    New Member

  • Members
  • Pip
  • 7 posts

Posted 14 November 2012 - 02:01 PM

Any clue as to what this means? Error 1 Program 'C:\Users\Owner\AppData\Local\Temporary Projects\NetduinoApplication1\obj\Debug\NetduinoApplication1.exe' does not contain a static 'Main' method suitable for an entry point NetduinoApplication1 Still ain't got this thing working right!

#12 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 14 November 2012 - 03:13 PM

Any clue as to what this means?

Error 1 Program 'C:\Users\Owner\AppData\Local\Temporary Projects\NetduinoApplication1\obj\Debug\NetduinoApplication1.exe' does not contain a static 'Main' method suitable for an entry point NetduinoApplication1

Still ain't got this thing working right!

Hi ulterior,

If you are using the class that Chris Seto provided, you will need to add it to your Netduino Project. The easiest way is to create a new Netduino project (I assume you are using the Netduino classic), and add open Program.cs. Inside of the Main method, add the following just below where is says

// write your code here


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);
	}
}

The Main method is the starting point for your application. This is where you will initialize and setup what you want to do in your application.

At this point you will still seem some red underlines beneath any mention of Servo. This is because the Servo class has not been added to your project.

To do this, we will need to add a Class to our Netduino project. To do this go to the Solution Explorer in Visual Studio and right click on your Project. Then select Add and then Class. (Use can also use Shift+Alt+C). You should now see a window like this:
Attached File  AddClass.gif   86.32KB   9 downloads

Name your new class Servo, and click Add.
Attached File  Name_the_class.gif   93.95KB   16 downloads

Now copy and paste the code from Chris Seto's post, found here, into your new class.
/*
 * 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;
                }
        }
}

Once you have this added you'll notice that we will still have some red underlines beneath the PWM class. This is because by default the SecretLabs PWM class has not been added to your project, and the Microsoft PWM class is a little different and expects a different constructor.

To add the SL PWM class, right click on Reference in the Solutions Explorer and select Add Reference. Then in the .Net tab, you'll find the SecretLabs.NETMF.Hardware.PWM class, and add it to your project.
Attached File  AddReference.gif   115.08KB   10 downloads

The next step will be to resolve the confusion between the MS PWM class and the SL PWM class. To do this right click where ever you see the red underline PWM class, and click Resolve. Then select the SecretLabs.NETMF.Hardware.PWM. This will add the namespace in front of the PWM class and allow the compiler to know which PWM class it should use. We could have also just removed the MS PWM class from our References as a shorter alternative.
Attached File  ResolveSLPWM.gif   87.9KB   11 downloads

We should now have a complete Servo project that we can run. To do so, connect your servos balck wire to GND, the Red to 5V and the yellow or signal wire (colour may differ) to Digital pin 9 (D9). Then connect your Netduino via USB to your computer. Once connected, press F5 to Start Debugging. And voila, you should have a spinning servo!

Hope this helps and gets you your way!

Cheers,
Steve

#13 DavidBev

DavidBev

    New Member

  • Members
  • Pip
  • 6 posts

Posted 14 November 2012 - 09:55 PM

Have a quick question, new to all of this but I have used Chris's servo class with continuous rotation servos. My issue now is that after mounting my servos I realize I need one to rotate opposite the other. Looking through Chris's code is that what the "inverted" command will do and how do I change that in the main program? Thanks

#14 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 15 November 2012 - 02:58 AM

Have a quick question, new to all of this but I have used Chris's servo class with continuous rotation servos. My issue now is that after mounting my servos I realize I need one to rotate opposite the other. Looking through Chris's code is that what the "inverted" command will do and how do I change that in the main program? Thanks

Hi David,

Yes, you are correct. You can reverse the movement by setting the inverted property of the servo. By default this property is set to false. To change it you can do so with the following code:
Servo servo = new Servo(Pins.GPIO_PIN_D9);
servo.inverted = true;  //Reverse the rotation

To change it back, you can set the inverted property back to false.

Hope this helps, and happy spinning!

Steve

Edited by Gutworks, 15 November 2012 - 03:00 AM.


#15 DavidBev

DavidBev

    New Member

  • Members
  • Pip
  • 6 posts

Posted 15 November 2012 - 01:58 PM

Hi David,

Yes, you are correct. You can reverse the movement by setting the inverted property of the servo. By default this property is set to false. To change it you can do so with the following code:

Servo servo = new Servo(Pins.GPIO_PIN_D9);
servo.inverted = true;  //Reverse the rotation

To change it back, you can set the inverted property back to false.

Hope this helps, and happy spinning!

Steve


That was exactly what I was looking for. Thanks!

#16 ulterior

ulterior

    New Member

  • Members
  • Pip
  • 7 posts

Posted 17 November 2012 - 06:54 PM

I do not see the " SecretLabs.NETMF.Hardware.PWM " when I try to add a reference. I see other secretlabs hardware but not that one. Do I need to download something else?

#17 c2rosa

c2rosa

    Member

  • Members
  • PipPip
  • 15 posts

Posted 27 May 2014 - 01:38 PM

Regarding:

"Here is the servo tutorial that also explains what is happening. Once you finish that one I would suggest you post some of your questions here and we'll try to answer and explain them as best as possible. Also don't forget to check the datasheets on theservo you have. Each servo may be configured differently and to have it operating smoothly you may need to alter your code slightly. "

 

The servo tutorial you reference above is great for Netduino.

Unfortunately, for the Netduino 2, they seem to have taken away the:

servo.SetPulse(20000, pos);

method of:

PWM servo = new PWM(Pins.GPIO_PIN_D9);

In addition, the constructor for the PWM object has changed as well.

 

Do you know whether there are examples/tutorials that help those of us using Netduino 2?

 

Thanks for any insights.



#18 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 06 June 2014 - 04:11 AM

Hi c2rosa,

If you add the "SecretLabs.NETMF.Hardware.PWM.dll" assembly to your project, you'll be able to control PWM pins using that code. Please note that you may want to use the full namespace of the class when you create your objects.

Chris

#19 c2rosa

c2rosa

    Member

  • Members
  • PipPip
  • 15 posts

Posted 22 June 2014 - 11:38 PM

Chris,

Where do I go to find and install "SecretLabs.NETMF.Hardware.PWM.dll"....

 

Once this is installed, will Microsoft Visual Studio C# 2010 Express be able to find it?



#20 c2rosa

c2rosa

    Member

  • Members
  • PipPip
  • 15 posts

Posted 23 June 2014 - 12:08 AM

Forget the last question.....I got the dll ("SecretLabs.NETMF.Hardware.PWM.dll") figured out....and the servo code is working great now.

Thanks!






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.