Main Content

C2000 Speed Measurement

Calculate motor speed


Embedded Coder® Support Package for Texas Instruments™ C2000™ Processors/ Optimization/ C28x DMC

  • C2000 Speed Measurement block


This block calculates the motor speed based on the rotor position when the direction information is available. The inputs are the electrical angle (theta) and the direction of rotation (dir) from the encoder. The outputs are the speed normalized from 0 to 1 in the Q format (freq) and the speed in revolutions per minute (rpm).


  • To generate optimized code from this block, enable the TI C28x or TI C28x (ISO) Code Replacement Library.

  • This block does not call the corresponding Texas Instruments library function during code generation. Instead, the MathWorks® code uses the TI functions global Q setting to adjust dynamically the Q format based on the block input. See Using the IQmath Library for more information.

Understanding the Theta Input to the Block

To indicate the rotational position of your motor, the block expects a 32-bit, fixed-point value that varies from 0 to 1.

Block input theta is defined by the following relations:

  • A theta input signal equal to 0 indicates 0 degrees of rotation.

  • A theta input signal equal to 1 indicates 360 degrees of rotation (one full rotation).

When the motor spins at a constant speed, theta (in counts) from your position sensor (encoder) should increase linearly from 0 to 1 and then abruptly return to 0, like a saw-shaped signal. Adjust the theta signal output from your encoder to get the input signal range for the Speed Measurement block. Then, convert your encoder signal to 32-bit fixed-point Q format that meets your resolution needs.

For example, if you are using a position sensor that generates 8000 counts for one full revolution of the motor, (0.0450 degrees per count), you need to reset your counter to 0 after your counter reaches 8000. Each time you read your encoder position, you need to convert the position to a 32-bit, fixed-point Q format value knowing that 8000 is represented as a 1.0. In this example your format could be Q31.

The Base Speed Parameter

Base speed is the maximum motor rotation rate to measure. This value is probably not the maximum speed the motor can achieve.

The Speed Measurement block calculates motor speed from two successive theta readings of the motor position, thetanew and thetaold (the base speed of the motor; and the time between readings). The maximum speed the block can calculate occurs when the difference between two successive samples [abs(thetanew-thetaold)] is 1.0—one full motor revolution occurs between theta samples.

Therefore, the value you provide for the Base speed (in revolutions per minute) parameter is the speed, in revolutions per minute, at which your motor position signal reports one full revolution during one sample time. While the motor may spin faster than the base speed, the block cannot calculate the rotation rate in that case. If the motor completes more than one revolution in one sample time, the calculated speed may be wrong. The block does not know that between samples thetanew and thetaold, theta wrapped from 1 back to 0 and started counting up again.

The time difference between the two theta readings is the sample time. The Speed Measurement block inherits the sample time from the upstream block in your model. You set the sample time in the upstream block and then the Speed Measurement block uses that sample time to calculate the rotation rate of the motor.

The Sample Time Calculation

Motor speed measurements depend on the sample time you set in the model. Your sample time must be short enough to measure the full speed of the motor.

Two parameters drive your sample time—motor base speed and encoder counts per revolution. To be able to measure the maximum rotation rate, you must take at least one sample for each revolution. For a motor with base speed equal to 1000 rpm, which is 16.67 rps, you need to sample at 1/16.67 s, which is 0.06 s/sample. This sample rate of 16.67 samples per second is the maximum sample time (lowest sample rate) so that you can measure the full speed of the motor.

Using the same sample rate assumption, the minimum speed the block can measure depends on the encoder counts per revolution. At the minimum measurable motor speed, the encoder generates one count per sample period—16.67 counts per second. For an encoder that generates 8000 counts per revolution, this results in being able to measure a speed of [(16.67 counts/s) * (0.045 degrees/count)] = 0.752 degrees per second, or about 45 degrees per minute—one-eighth RPM.

The Differentiator Constant

The differentiator constant is a scalar value applied to the block output. For example, setting it to 1 does not alter the output. Setting the constant to 1/4 multiplies the frequency and revolutions per minute outputs by 0.25. This setting can be useful when your motor has multiple pole pairs, and one electrical revolution is not equal to one mechanical revolution. The constant lets you account for the difference between electrical and mechanical rotation rates.

The Low-Pass Filter Constant

This block includes filtering capability if your position signal is noisy. Setting the filter constant to 0 disables the filter. Setting the filter constant to 1 filters out the entire signal and results in a block output equal to 0. Use a simulation to determine the best filter constant for your system. Your goal is to filter enough to remove the noise on your signal but not so much that the speed measurements cannot react to abrupt speed changes.


Base speed

Maximum speed of the motor to measure in revolutions per minute.

Differentiator constant

Constant used in the differentiator equation that describes the rotor position.

Low-pass filter constant

Constant to apply to the lowpass filter. This constant is 1/(1+T*(2πfc)), where T is the sampling period and fc is the cutoff frequency. The 1/(2πfc) term is the lowpass filter time constant. This block uses a lowpass filter to reduce noise generated by the differentiator.


The following example demonstrates how you configure the Speed Measurement block.

Configuring the Speed Measurement Block to Measure Motor Speed

Use the following process to set up the Speed Measurement block parameters.

  1. Add the block to your model.

  2. Open the block dialog box to view the block parameters.

  3. Set the value for Base Speed to the maximum speed to measure, in revolutions per minute.

  4. Enter values for Differentiator and Low-Pass Filter Constant.

  5. Click OK to close the dialog box.

Setting the Sample Time to Measure Motor Speed

Use the following process to set the sample time for measuring the motor speed.

  1. Open the block dialog box for the block before the Speed Measurement block in your model (the upstream or driving block).

  2. Set the sample time parameter in the upstream block according to the sample time guidelines described in The Sample Time Calculation.

  3. Click OK to close the dialog box.


For detailed information on the DMC library, see C/F 28xx Digital Motor Control Library, SPRC080, available at the Texas Instruments Web site.