# Using the Servo Expansion

Last edited by greenbreakfast, 2016-07-13 18:48:06

The Servo (PWM) Expansion allows you to generate up to 16 different Pulse Width Modulated (PWM) signals to control anything from Servo Motors, DC Motor speed, LED brightness, etc.

# PWM Signals

Pulse Width Modulated signals can be described by duty cycle or periods.

## Duty Cycle

Indicates what percentage of the time a signal is on or high voltage. So a PWM signal with a 25% duty cycle will be high for 25% of the time, and low 75% of the time. The duty cycle can be calculated as follows:

$$DutyCycle = {\frac{T_{on}}{T_{CompleteCycle}}}\times100\%$$

## Period

Indicates the amount of time (usually in milliseconds) for each part of the cycle. The Time On, Ton in the diagram above is the time the signal is high. This is also known as pulse width. Similarly, Time Off, or Toff is the time the signal is low.

The Complete Cycle time corresponds to the overall period of the PWM. Changing the period will also change the frequency of the PWM signal:

$$Frequency = {\frac{1}{Period}}$$

# The Hardware

The Servo Expansion has 16 channels to generate PWM signals. The 16 channels are broken out so that servo-motors can be directly connected to the headers.

It also comes with an onboard DC barrel jack connector that can be used to supply power to any connected servos. If this connector is not used, the Expansion Dock will provide power to the servos, however, it is not recommended to run more than one or two servos under light load with this method. Note that the Omega cannot be powered through this connector, it requires its power own power supply.

The onboard chip has an oscillator that can generate PWM signals with a frequency in the range of 24 Hz to 1526 Hz. Most servos operate at 50 Hz, that is the default frequency.

# Using the Command Line

Make sure that your Omega has the latest firmware!

We've developed a tool called pwm-exp that should make generating PWM signals a breeze.

Also available are a C library and a Python module that allow you to develop your own programs to control the Servo Expansion.

## Command Usage

For a print-out of the command's usage, run it with just a -h argument

pwm-exp -h

## Initialization

After every boot, the chip and oscillator on the Servo Expansion have to be initialized in order for the PWM signals to be generated correctly. The driver application will automatically detect if the oscillator is not running and will run the required sequence, so this command should only be run if you wish to initialize the oscillator but not generate any PWM signals.

To perform the initialization, run the following command:

pwm-exp -i

This can be run by itself or in conjunction with any commands below.

## Setting the PWM based on Duty Cycle

The tool allows you to define the PWM signal to be generated by specifying which channel on the expansion to use and the duty cycle

pwm-exp [options] <channel> <duty cycle percentage>

A few examples:

Generating a 10% duty cycle signal on channel 0

pwm-exp 0 10

Generating a 33.33% duty cycle with a signal frequency of 100 Hz on channel 1

pwm-exp -f 100 1 33.33

Performing the initialization sequence and then generating a 95% duty cycle signal on channel 2

pwm-exp -i 2 95

Note that the duty cycle must be between 0 and 100.

## Setting the PWM based on Period

The PWM signal can also be generated by specifying the pulse width and total period in milliseconds:

pwm-exp -p <channel> <pulse width> <total period>

Some examples:

Generating a PWM signal that is high for 1ms and low for 19ms on channel 3. This makes the overall period 20ms, corresponding to a 50Hz frequency

pwm-exp -p 3 1 20

Generating a PWM signal that is high for 1.65ms with an overall period of 5.4ms (corresponds to 185.19 Hz) on channel 4

pwm-exp -p 5 1.65 5.4

## Setting the PWM signal frequency

When setting the signal based on duty cycle, the frequency can also be adjusted in each command by using the -f option.

pwm-exp -f <frequency> <channel> <duty>

An example Generating a 15% duty pwm signal at 220Hz on channel 5

pwm-exp -f 220 5 15

The default frequency is 50 Hz since most Servos operate on this frequency.

If the desired frequency is different from the default, the frequency must be specified in each pwm-exp command, otherwise the expansion will reset to the default frequency.

Also note that all channels run on the same frequency, it is not possible to generate PWM signals with different frequencies on the same Servo Expansion.

When setting the signal based on period, the frequency is dependent on the total period:

$$Frequency = {\frac{1}{Period}}$$

## Selecting a Channel

The Servo Expansion has 16 channels, pwm-exp allows you to program each individually or all at once.

Programming each individually:

Seen in the examples above:

pwm-exp 12 97
pwm-exp -p 15 1.65 20

Programming all at once:

Instead of specifying a channel number, the keyword all can be entered

Setting all of the channels to a 50% duty cycle signal at the default 50Hz:

pwm-exp all 50

Setting all of the channels to a 66% duty cycle at 123Hz:

pwm-exp -f 123 all 66

Setting all of the channels to a 5% duty cycle at 50Hz using the period times

pwm-exp -p all 1 20

## Setting a Delay

It is also possible to create PWM signals where the pulse is delayed by some time.

The following image shows a 20% duty cycle signal with a 10% delay:

So for the first 10% of the time, the signal is low, then it will be high for the next 20% of the time, and low for the remaining 70% of the time.

Setting a delay with pwm-exp can only be done when setting the PWM signal using the duty cycle:

pwm-exp <channel> <duty cycle> <delay percentage>

Examples

Generating a signal like the one above, 20% duty cycle with a 10% delay, on all channels:

pwm-exp all 20 10

Generating a signal with 33% and a delay of 50% on channel 9:

pwm-exp 9 33 50

## Disabling the Oscillator

The oscillator can be put into sleep mode, immediately disabling generation of all PWM signals. This might be useful in any application where you want to disable all connected servos or LEDs but keep the Omega powered on. For instance, we use the sleep mode to rest the servos on Oliver, our robotic arm, after he's been powered on for too long.

To put the oscillator in sleep mode:

pwm-exp -s

To enable the oscillator again, there are a few methods:

Run the initialization sequence to enable the oscillator and generate no pwm signals:

pwm-exp -i

Generate a PWM signal on one or all channels, the software will detect the oscillator is in sleep mode and will run the initialization sequence:

pwm-exp <channel> <duty>
or
pwm-exp -p <channel> <pulse width> <total period>

# Using the Libraries

The C library and Python module will allow you to develop your own programs that can control the Servo Expansion.