Purpose
Enables the use of a PWM pin by initializing it with information relating to its characteristics.
When to use
Once for each PWM Pin used on the board. If you connect anything to a PWM pin, its mode should be set.
Where to use
At the beginning of your code execution, in the void setup()
part of your .ino
file, so that the pin is initialized before it is referred to by your code.
Info |
---|
Click here to learn more on the PWM outputs of the CrcDuino board. It explains in particular why PWM port #1 to #4 can only be used with power motor controller. |
Note |
---|
CrcLib Error mode is triggered if a given PWM pin is initialized more than once. Make sure to only use |
Note |
---|
This function requires the use of the following functions at some point of your |
Returns
This function does not return a value once it has completed its tasks.
Main prototype and parameters
static void Crc::CrcLib::InitializePwmOutput(unsigned char pin, int minPulseWidth, int maxPulseWidth, bool reverse)
The following parameters must be passed to the function for it to work properly:
pin
: The name of the PWM pin you want to initialize. Must be of the typeunsigned char
.minPulseWidth
: The minimum pulse width, in µs, of your servo motor. Must be of the typeint
.maxPulseWidth
: The maximum pulse width, in µs, of your servo motor. Must be of the typeint
.reverse
: Whether or not to invert the rotation direction of the servo. Must be of the typebool
.
Overloads
#1: Using default pulse width values, without the reverse feature
static void Crc::CrcLib::InitializePwmOutput(unsigned char pin)
It is the easiest, minimal way to use this function. A 1000µs min pulse width and 2000µs max pulse width will be used by default. The following parameters must be passed to the function for it to work properly:
pin
: The name of the PWM pin you want to initialize. Must be of the typeunsigned char
.
#2: Using default pulse width values, with the reverse feature
static void Crc::CrcLib::InitializePwmOutput(unsigned char pin, bool reverse)
A 1000µs min pulse width and 2000µs max pulse width will be used by default. The following parameters must be passed to the function for it to work properly:
pin
: The name of the PWM pin you want to set. Must be of the typeunsigned char
.reverse
: Whether or not to invert the rotation direction of the servo. Must be of the typebool
.
#3: Setting pulse width values, without the reverse feature
static void Crc::CrcLib::InitializePwmOutput(unsigned char pin, int minPulseWidth, int maxPulseWidth)
The following parameters must be passed to the function for it to work properly:
pin
: The name of the PWM pin you want to initialize. Must be of the typeunsigned char
.minPulseWidth
: The minimum pulse width, in µs, of your servo motor. Must be of the typeint
.maxPulseWidth
: The maximum pulse width, in µs, of your servo motor. Must be of the typeint
.
Examples
Expand | |||||
---|---|---|---|---|---|
| |||||
|
Expand | |||||
---|---|---|---|---|---|
| |||||
|
More on this function
Info |
---|
What is a pulse width, how it relates to a PWM output, and why would I need to set it ?PWM stands for Pulse width modulation. PWM outputs use ON/OFF pulses to control servo motor (standard or continuous) and power motor controllers. The ON time of the pulse (its width, or duration) indicates the desired position or speed. On a standard hobby servo, the minimum pulse width is decoded as a to full left position command and the maximum pulse width corresponds to full right position. The neutral position of the servo corresponds to a pulse width half-way between the two extremes. On a continuous hobby servo or a power motor controller, the minimum pulse width is decoded as a full speed command in a direction and the maximum pulse width corresponds to full speed in the other direction. Zero speed corresponds to a pulse width half-way between the two extremes. CrcLib defaults the minimum pulse width at 1000µs and the maximum pulses width at 2000µs since it’s the value used by many popular hobby standard servos and continuous servos, such as a 3-Wire Vex Servo, 3-Wire Vex motor or Vex 2 Wire Motor 393 equipped with a Motor Controller 29. |
Info |
---|
Why would I use the reverse feature?The reverse feature comes handy when want to quickly switch the rotation direction of a continuous hobby type servo or of the the power motor controlled by a motor controller, or swap the left and right position of a standard hobby servo. This can spare some rewiring or long code modifications… |
Info |
---|
How can I know the minimum and maximum pulse width of the servos I use?The minimum and maximum pulse width are usually indicated in the servo specs sheet or user manual. You can also look for the “required pulse”, “pulse length” or “signal characteristics”. When in doubt, use 1ms and 2ms as the min and max value, since almost every hobby servos are centered at a 1.5ms pulse lengh. Some servos will also have a little screw that can be used to adjust the neutral value of the PWM signal. |
Info |
---|
How can I know the minimum and maximum pulse width of the motor controller I use?Although the motor controller specs sheet or user manual typically states the min and max pulse width of the PWM signal needed, these devices typically include a calibration mode. Calibrating a motor controller makes it automatically adjust to the PWM signal used. Refer to the motor controller specs sheet or user manual for specific details. |
Info |
---|
Can I use the native Arduino function analogWrite() to control the PWM outputs ?You could, but we strongly recommend against it if you don’t know what you are doing and don’t have a precise reason to do so. The analogWrite() native Arduino function controls PWM pins using the “normal” definition of PWM. Although PWM pins on the CrcDuino are PWM capable pins, they are used as Servo PWM pins by CrcLib, since the CRC Robotics Competition robots mostly use hobby type standard servos, continuous servo and PWM compatible power motor controllers. For more info on this, read this. |
Articles liés
Filter by label (Content by label) | ||||||
---|---|---|---|---|---|---|
|