In my goal to get my robot controller up and running, I want to know more about the use of PWM on the Netduino.
My question for today is, How do you setup and use the Netduino PWM pins?
I'll give just a brief explanation of how Pulse Width Modulation (PWM) is done from a hardware standpoint, for non-hardware people. Usually there is a triangle wave (most commonly from a 555) that is then compared with a reference voltage (using a comparator). Comparing the reference voltage (orange) with the triangle wave (magenta), you get a square wave out (blue). The width of this square wave compared to the TOTAL width (leading edge to leading edge) between the two square waves is called the "duty cycle", which can be from 0-100%.
So from the hardware you have some variables that you have control over, such as:
- Frequency of the triangle wave
- Reference voltage (this becomes comparing bits)
- Duty cycle
These are some very important parameters to control when making a circuit. So how do I configure these parameters on the Netduino?
Can you more experienced guys help me fill in my code?
public static void Main() { AnalogInput xAxisInput = new AnalogInput(Pins.GPIO_PIN_A1); //reading an analog input this will by my "VRef" as it were int xAxisValue= xAxisInput.Read(); //int for that analog input //How is the PWM properly setup here?? PWM.frequency(150Hz); //setting the frequency of of my "triangle wave" How is this done??? int PwmDutyCycle; //value for my PWM duty cycle //I have made the range for directions 0-1023, with a 128bit "dead zone" if (xAxisValue > 576) { PwmDutyCycle = xAxisValue/1023 //setting my duty cycle 50-100%. Is this right??? Direction1.Write(PwmDutyCycle); //gives the motor 1, direction 1 a true reading, this is forward with speed } else if(xAxisValue < 448) { PwmDutyCycle = (1023-xAxisValue)/1023 //setting my duty cycle 50-100%. Is this right??? Direction2.Write(PwmDutyCycle); //gives the motor 1 direction 2 a true reading, this is reverse with speed }