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.
I recently upgraded my Netduino to 4.2.0 firmware. I'm now updating my code and I'm having an issue with the new PWM class and servos.
Servos expect a PWM with 20ms period and high signal duration from 1 ms to 2ms. I wrote the following using the new native PWM:
var servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false);
while (true)
{
Thread.Sleep(1000);
servo.Duration = 2000; // 2 ms
Thread.Sleep(1000);
servo.Duration = 1000; // 1ms
}
And the following using the SecretLabs PWM implementation:
var pwm = new SecretLabs.NETMF.Hardware.PWM(Pins.GPIO_PIN_D10);
uint period = 20000;
while (true)
{
pwm.SetPulse(period, 1000); // 1 ms
Thread.Sleep(1000);
pwm.SetPulse(period, 2000); // 2 ms
Thread.Sleep(1000);
}
Both examples make the servo move but, it doesn't move 90º and the 2 ms position forces the servo. Am I doing something wrong?
var servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false);
It errors on PWM, saying "The type of namespace name 'PWN' does not exist in the namespace 'Microsoft.SPOT.Hardware'
Is the namespace PWN not included in 4.2?
var servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false);
It errors on PWM, saying "The type of namespace name 'PWN' does not exist in the namespace 'Microsoft.SPOT.Hardware'
Is the namespace PWN not included in 4.2?
Be sure to add the Microsoft.SPOT.Hardware.PWM.dll assemble as a reference in your project.
Be sure to add the Microsoft.SPOT.Hardware.PWM.dll assemble as a reference in your project.
Does that fix things for you?
Chris
Got it fixed!
I thought "using Microsoft.SPOT.Hardware" would include the PWM, but it didn't.
Now, where do I find how to use Microsoft.SPOT.Hardware.PWM?
Like, what each param means "Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false)".
How to set default position, starting position, ending position, etc.
The servo that I have is "Parallax Standard Servo": http://learn.paralla...Start/900-00005
It says that "To enable the servo to move a full 180° use pulse durations of approximately 750 to 2250 μs. Exercise care to not exceed these lower and upper limits.".
What numbers to use in "Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false)"? What does 20000 and 1000 mean?
It says that "To enable the servo to move a full 180° use pulse durations of approximately 750 to 2250 μs. Exercise care to not exceed these lower and upper limits.".
What numbers to use in "Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1000, PWM.ScaleFactor.Microseconds, false)"? What does 20000 and 1000 mean?
Hi chungenhung,
Servos expect a 20ms second period (20000μs), that's the value you see on the constructor.
To change position you should then change the Duration value. The typical values are from 1000 to 2000, with 1500 for the middle position. In your case, use values from 750 to 2250, 1500 should still be from the middle position.
The constructor requires an initial duration value (that's the 1000 value in the example).
Servos expect a 20ms second period (20000μs), that's the value you see on the constructor.
To change position you should then change the Duration value. The typical values are from 1000 to 2000, with 1500 for the middle position. In your case, use values from 750 to 2250, 1500 should still be from the middle position.
The constructor requires an initial duration value (that's the 1000 value in the example).
aalmada
When I use Microseconds, and use (20000, 1000) in the constructor, the servo would try to move out of the range.
Trying to get this to work. Short code snippet.
What I am trying to accomplish is, when the onboard button is pressed, move the servo.
It is complaining that "servo" is not in the "static void button_OnInterrupt" context.
Servo is declared in 'public static void Main()".
How do I make it visible in "static void button_OnInterrupt"?
Or maybe another way to do this? Thanks.
namespace NetduinoPlusApplication1
{
public class Program
{
public static void Main()
{
//********************************************************
// Row pins. The keypad have 4 rows.
Cpu.Pin[] RowPins = { Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D4 };
// Col pins. The keypad have 3 columns.
Cpu.Pin[] ColPins = { Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D2, Pins.GPIO_PIN_D1 };
// Initializes the new keypad
MatrixKeyPad kb = new MatrixKeyPad(RowPins, ColPins);
// Bind both events
kb.OnKeyDown += new NativeEventHandler(kb_OnKeyDown);
kb.OnKeyUp += new NativeEventHandler(kb_OnKeyUp);
InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); //On Board Button. Interrrupt when buttom is pressed down.
button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
//SERVO
var servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1500, PWM.ScaleFactor.Microseconds, false); //Declare servo. 20,000 means 20ms, 1500 means the starting position.
//The servo have a range of 750-2250ms.
// Lets wait forever for events to occur
Thread.Sleep(Timeout.Infinite);
}
static void button_OnInterrupt(uint data1, uint data2, DateTime time)
{
Debug.Print("OnBoard Button Pressed");
servo.Duration = 1500; //THIS DOESN'T WORK.
servo.start(); //THIS DOESN'T WORK.
}
}
}
The button_OnInterrupt event can't see the servo object because it was created in the Main function...and its scope is therefore limited to the Main function unless you pass a reference around.
The easiest thing to do here is to add this line of code right before your Main function:
var servo;
You'll then want to remove the word "var" from "var servo = ..." in your Main function, so that you know it's referencing the static class-level variable and not creating a new one.
namespace NetduinoPlusApplication1
{
public class Program
{
var servo; //ERROR "The type or namespace name 'var' could not be found."
public static void Main()
{
//********************************************************
// Row pins. The keypad have 4 rows.
Cpu.Pin[] RowPins = { Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D4 };
// Col pins. The keypad have 3 columns.
Cpu.Pin[] ColPins = { Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D2, Pins.GPIO_PIN_D1 };
// Initializes the new keypad
MatrixKeyPad kb = new MatrixKeyPad(RowPins, ColPins);
// Bind both events
kb.OnKeyDown += new NativeEventHandler(kb_OnKeyDown);
kb.OnKeyUp += new NativeEventHandler(kb_OnKeyUp);
InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); //On Board Button. Interrrupt when buttom is pressed down.
button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
//SERVO. ERROR "An object reference is required for the non-static field, method, or property 'NetduinoPlusApplication1.Program.servo' ".
servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1500, PWM.ScaleFactor.Microseconds, false); //Declare servo. 20,000 means 20ms, 1500 means the starting position.
//The servo have a range of 750-2250ms.
// Lets wait forever for events to occur
Thread.Sleep(Timeout.Infinite);
}
static void button_OnInterrupt(uint data1, uint data2, DateTime time)
{
Debug.Print("OnBoard Button Pressed");
servo.Duration = 1500; //THIS DOESN'T WORK.
servo.start(); //THIS DOESN'T WORK.
}
}
}
In "Public class Program", I have "static Microsoft.SPOT.Hardware.PWM _servo;".
In "public static void Main()", I have "_servo = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 20000, 1500, PWM.ScaleFactor.Microseconds, false);"
In "static void button_OnInterrupt", I have "_servo.duration = 1500;". Now it's complaining about "duration".
Error message "Microsoft.SPOT.Hardware.PWM does not contain a definition for duration."