.netmf pwm example EMX

I was hoping for help with the example code (.netmf getting started) for PWM. I’m trying to run it on an EMX board and I’m not sure what I’m doing wrong. Can you please change as needed to run on an EMX? Any PWM pin is fine.

using System.Threading;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
class Program
{
public static void Main()
{
PWM servo = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5);
while (true)
{
// 0 degrees. 20ms period and 1.25ms high pulse
servo.SetPulse(20 * 1000 * 1000, 1250 * 1000);
Thread.Sleep(1000);//wait for a second
// 90 degrees. 20ms period and 1.50ms high pulse
servo.SetPulse(20 * 1000 * 1000, 1500 * 1000);
Thread.Sleep(1000);//wait for a second
// 180 degrees. 20ms period and 1.75ms high pulse
servo.SetPulse(20 * 1000 * 1000, 1750 * 1000);
Thread.Sleep(1000);//wait for a second
}
Thread.Sleep(-1);
}
}

using GHIElectronics.NETMF.FEZ;

needs to be removed. That is for the specific FEZ device family.

What references have you added to the project? You may find a Fez one there incorrectly, and you need to make sure you have the EMX one added.

What actual errors are you seeing?

Please use code tags so your code is more readable :slight_smile:

I noticed that the first line of the code is also missing.

using System;

This might be a copy and paste error. ???

BTW, please use the code button,
the one that have number 101010 on top of the Message box,
So that will make the code more readible.

Io5 is used by the touch screen?

The problem is that he didn’t say what the problem is! Or at least, what pin you want to generate pwm on?

Ok. will use the code button in the future and thanks for the reply. I can’t recall the error but it seems to not like

PWM servo = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5);

Actually, I’ve tried changing this line many ways for EMX and I’m not able to get it to work. I did change the using line from FEZ to EMX. I’m using VS 2010 and the newest .netmf. I was hoping you would change the code to what you know will work. I will go home at lunch and get the exact error.( Obviously don’t do this for a living ) I don’t care what PWM pin, I’m just trying to get it to work. Thanks!

Here’s a simple way to learn from this, to try to suss out what is going on.

Open Visual Studio C# Express
Select “New Project”
Select “EMX Application” template.
Add the following references (left pane):

after the debug.print line, start typing the following:

            PWM servo = new PWM((PWM.Pin)EMX.Pin.IO5); 

As you type this you can see auto-complete suggesting things for you, and you can see what options you have at each point. You should substitute the appropriate PWM capable pin that you wish to use in place of IO5.

This shows two things. A simple project using a pre-defined template will always help you figure out what you really need when you don’t have an exact example program for what you need. It also shows you how the actual code is built up.

Once you get to that point you can either go back to the original code and work on it, or start pasting code pieces back into this shell.

Hopefully if nothing else this question has also helped you realise that we’re all happy to help, but we need to know what you saw (where errors were reported etc, what they were) so that we can help walk through them with you. Not everyone has an EMX board around to try stuff on and not everyone has tried to do the same things you seem to be doing with servos, so you may well find that we don’t just have a piece of code equivalent to what you need sitting around.

Thanks. I’ll try to be more specific. When following your instructions I’m getting the following error. “An unhandled exception of type ‘System,ArgumentException’ occured in GhiElectronics.NetMF.Hardware.dll”. This occurs when I use IO48, IO13 or IO14 which are designated PWM pins on EMX. When I use IO5 (not PWM) I dont get the error. In all cases the build works with no errors. It’s only when I debug that it happens.

What references exactly did you add to your project?

This is only used on OTHER versions of FEZ not Cobra! It should throw an exception that Cobra is not detected, did you get this exception at start up?

I can confirm on a Cobra that this failure occurs if you use a PWM pin. I was using IO48 (the LED).


using System;
using System.Threading;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;

namespace MFConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            PWM servo = new PWM((PWM.Pin)EMX.Pin.IO48);

            servo.SetPulse(20 * 1000 * 1000, 1250 * 1000);

            Thread.Sleep(Timeout.Infinite);
        }

    }
}

Stepping into the code, the app starts, then as soon as the PWM NEW is hit the first chance exception is hit.


The thread '<No Name>' (0x2) has exited with code 0 (0x0).
    #### Exception System.ArgumentException - 0xfd000000 (1) ####
    #### Message: 
    #### GHIElectronics.NETMF.Hardware.PWM::.ctor [IP: 0000] ####
    #### MFConsoleApplication1.Program::Main [IP: 0006] ####
A first chance exception of type 'System.ArgumentException' occurred in GHIElectronics.NETMF.Hardware.dll
An unhandled exception of type 'System.ArgumentException' occurred in GHIElectronics.NETMF.Hardware.dll

Checked Im running the latest firmware and validated the SDK versions match:


ClrInfo.clrVersion:                     4.1.2821.0
ClrInfo.clrVendorInfo:                  Microsoft Copyright (C) Microsoft Corporation.  All rig
ClrInfo.targetFrameworkVersion:         4.1.2821.0
SolutionReleaseInfo.solutionVersion:    4.1.3.0
SolutionReleaseInfo.solutionVendorInfo: GHI Electronics, LLC
SoftwareVersion.BuildDate:              Oct 15 2010

Houston, looks like we have a problem…

You can’t use the IO number to control the PWM. You need the actual PWM number not the IO number

Your right! If I use IO4, I can modulate PWM 4, which is IO48. On the EMX IO48 is an LED. Evidently my servo draws to much current to connect directly to a PWM pin so I can’t test it with a servo, but modulating the LED is test enough. Thanks Gus and Bret. Mostly for your patience!

HI, I am having the same problem and you have said we need to use the PWM number not the Pin Number, my code uses PWM 2 (which is IO50):

PWM servo = new PWM((PWM.Pin)EMX.Pin.IO50); // PWM 2

could you please give the correct EMX reference for use of PWM pin rather than the CPU pin.

Please disregard last question: I found the PWM enumerations. Just in case anyone else cant read the documentation (like me ::slight_smile: ) here is the line using the PWM enumerations:
PWM Servo = new PWM(PWM.Pin.PWM4);

Sorry Guys,
The above code compiled and deployed without any errors or exceptions, but it doesnt appear to be sending the PWM signal to the EMX PWM4 pin?? When I connect a servo (and I tried a few) it was like random movements rather than the 1.25 ms (90 degree), 1.75ms (180 deg) etc.
Could you please give the code for a working example of PWM for the EMX Dev Kit?

The above should have read PWM2 pin (not PWM4). For the listed code in my previous post.

Servos aren’t all going to be “the same”. You may need tweaks in the timing. More importantly, you probably need to deal with power issues separately, so you’re better off describing how you’ve dealt with that here before worrying about code. Power issues tent to manifest themselves in poorly tracking servos (classic modellers symptom of poor or flattening batteries is a servo that jitters a lot )

EDIT: Why don’t you set up PWM on the LED (or any LED) and see if you can test it that way? You can even use a voltmeter to measure voltage on a PWM pin, so a 50% duty cycle will give you 50% of your expected voltage.

Brett thanks for the speedy reply:

I am powering the servos independantly I have a 12 V wall adapter, powering other gear and a 5V regulator to power the Servo. The Servo (and I have tried many brands) jiiter around and then settle on one of the end stops like they are trying to go further, I shut it down at that point to stop killing the servo.
I have tried all range of up times from 1 ms through to 1.75 ms, same every time. I did the fade in fade out PWM on the LED and it works.
I will try the volt meter to test the duty cycle, if that fails I will have to find a scope somewhere. Have you driven servos direct off the PWM signal? If so what brand servos?

PWM is definitely working: checked with voltmeter for 100% duty cycle (20ms) gave me the 3.3V, checked @ 50% (10ms) got 1.65V. Then just to make sure looked at the ratios for 1ms, 1.5ms and 2 ms over the 20ms period and got the average voltages of 165 mV, ~250mV and 330mV: nothing wrong with PWM signal.
Guess I better get another power supply for the Servo power.

Thanks for your assistance.