Help with a Servo code
#1
Posted 23 October 2012 - 01:22 PM
#2
Posted 23 October 2012 - 01:50 PM
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#3
Posted 23 October 2012 - 01:58 PM
Twiiter: https://twitter.com/Gutworks
#4
Posted 23 October 2012 - 02:01 PM
#5
Posted 23 October 2012 - 02:05 PM
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#6
Posted 23 October 2012 - 02:15 PM
#7
Posted 23 October 2012 - 02:53 PM
#8
Posted 23 October 2012 - 02:54 PM
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
- Stefan likes this
Twiiter: https://twitter.com/Gutworks
#9
Posted 23 October 2012 - 06:52 PM
#10
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
Twiiter: https://twitter.com/Gutworks
#11
Posted 14 November 2012 - 02:01 PM
#12
Posted 14 November 2012 - 03:13 PM
Hi ulterior,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!
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:
AddClass.gif 86.32KB 9 downloads
Name your new class Servo, and click Add.
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.
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.
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
Twiiter: https://twitter.com/Gutworks
#13
Posted 14 November 2012 - 09:55 PM
#14
Posted 15 November 2012 - 02:58 AM
Hi David,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
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.
Twiiter: https://twitter.com/Gutworks
#15
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
Posted 17 November 2012 - 06:54 PM
#17
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.
#19
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
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