Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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.

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.

This function requires the use of the following functions at some point of your .ino file in order to work properly:

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 type unsigned char.

  • minPulseWidth: The minimum pulse width, in µs, of your servo motor. Must be of the type int.

  • maxPulseWidth: The maximum pulse width, in µs, of your servo motor. Must be of the type int.

  • reverse: Whether or not to invert the rotation direction of the servo. Must be of the type bool.

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 type unsigned 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 type unsigned char.

  • reverse: Whether or not to invert the rotation direction of the servo. Must be of the type bool.

#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 type unsigned char.

  • minPulseWidth: The minimum pulse width, in µs, of your servo motor. Must be of the type int.

  • maxPulseWidth: The maximum pulse width, in µs, of your servo motor. Must be of the type int.

Examples

 #1 We want to initialize a continuous servo hooked up to PWM Port #5 and another one hooked up to PWM Port #6. We want the rotation direction to be reversed from one another. For readability, we also want the motor on Port 5 to be referred as "RIGHT_FEEDER_MOTOR".
#include <CrcLib.h>

#define RIGHT_FEEDER_MOTOR CRC_PWM_5

using namespace Crc;

void setup() {
    CrcLib::Initialize();

    CrcLib::InitializePwmOutput(RIGHT_FEEDER_MOTOR);
    CrcLib::InitializePwmOutput(CRC_PWM_6, true);
    
    /* 
    The rest of your setup code
    ...
     */
}
void loop() {
    CrcLib::Update();

    /* 
    The rest of your looping code
    ...
    */
}
 #2 We want to initialize a hobby type standard servo (Manufacturer: FEETECH, Part number: FS5115M) hooked up to PWM Port #12. An internet search reveals the required pulse of that Servo is 0.5ms-2.5ms.
#include <CrcLib.h>

using namespace Crc;

void setup() {
    CrcLib::Initialize();
    
    CrcLib::InitializePwmOutput(CRC_PWM_12, 500, 2500);
    
    /* 
    The rest of your setup code
    ...
     */
}
void loop() {
    CrcLib::Update();

    /* 
    The rest of your looping code
    ...
    */
}

More on this function

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.

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…

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.

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.

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

  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.