Professional Documents
Culture Documents
This example shows how to develop a motor control application Texas Instruments™ C2000™ processors using
Simulink®. The steps illustrated in this example, depicted as incremental model refinements, suggest an example
workflow for designing and implementing a motor control application on an embedded platform. This example
highlights advanced processor-specific capabilities of code generation software.
Optional hardware:
Ia and Ib inputs in the Field Oriented Control model shown below represent the phase currents for phase A and B
of the system. They are also represented in normalized fixed-point format, where 1 corresponds to the maximum
Page
2
Page
1. Open the Field Oriented Motor Control template.
2. Open the C28x Digital Motor Control Library and drag the "Clarke transformation", "Park transformation",
"Inverse Park Transformation", and "Space Vector Generator" blocks to your template model.
3. Connect the signal lines to the corresponding input and output ports of the blocks introduced in the model. If
you prefer, use a pre-configured FOC model to execute this task.
4. Start the simulation by pressing the Run button of the Simulink model and observe the simulation results. Note
that D-axis current becomes zero when the current Ia crosses the zero axis on its way down, when the position
signal equals 0. This is the reference position. The current Ib is lagging the current Ia by 120 electrical degrees or
by 1/3 of the normalized electrical angle. The voltage outputs are of the same amplitude as the current input in the
normalized format.
5. You can now observe the effects of changing the simulation inputs on the controlled PMSM system. Increase
the amplitudes of Ia and Ib to 1. Observe that this creates a Q-axis current of 1 and that the voltage outputs have
3
the same amplitude. To create non-zero D-axis current, click the Ia block and change "Phase Offset" to 12500,
Page
then click the Ib block and change the "Phase Offset" to 12500-20000/3. The generated sine waves have 20000
samples per period. Advancing the phase of Ia and Ib by 2500 samples corresponds to 2*pi/8, or 1/8 of the per
unit angle, which leads to a D-axis current with -amplitude*sqrt(2)/2 and a Q-axis current with amplitude*sqrt(2)/2.
2. Set the target hardware resources of the model to one of the supported configurations specified at the top of
this example. For more information on setting the target hardware resources, seethis section in the IDE Link
documentation.
3. Open the Simulink Model Explorer by selecting "View" > "Model Explorer" (or by entering Ctrl+H). In Model
Explorer, go to "Configuration" > "Solver" and set the "Fixed step Size" to 10 microseconds.
4. While in Model Explorer, open the "Simulation Configuration" > "Code Generation". In the "Coder Target" pane,
select "Tool Chain Automation" tab and enable "Profile real-time execution".
5. Generate code by clicking the "Build Model" button on the toolbar of your model (Ctrl+B). Once code has been
generated, let it run for a few seconds and then halt the execution on the C2000 processor from the CCS IDE. At
the MATLAB® prompt enter: profile(CCS_Obj, 'execution', 'report').
6. Inspect the profiling report to see the execution time for the Field Oriented Control algorithm.
7. You can automate the profiling operation with a MATLAB script. This script can be run on the current active
model.
2. Open the Model Configuration Parameters dialog and select "Coder Target" under "Code Generation". Click
"Target Hardware Resources" and set "IDE/Tool Chain" and the "Board" to match one of the supported
configurations specified at the top of this example. Click OK.
If you use F28335 eZdsp board and CCS is set up as simulator, do following before going to step 3:
Open the Model Configuration Parameters dialog and select "Coder Target" under "Code Generation". Next, click
"Target Hardware Resources".
Click on ".adc_cal" from "Compiler sections" on "Section" tab;
Choose "ZONE7P" from "Placement";
Click "OK".
3. Open the Simulink Model Explorer by selecting "View" > "Model Explorer" (or by entering Ctrl+H). In Model
Explorer, go to "Configuration" > "Code Generation". Under the "Coder Target" pane, change the "Build Action" to
"Create_Processor_In_the_Loop_project".
4. Generate code for the "Algorithm Subsystem" by right-clicking the "Algorithm Subsystem" and selecting "C/C++
Code" > "Build This Subsystem" > "Build". Once code has been generated in CCS, you should see a new
"untitled" Simulink model with an automatically generated PIL block. Drag this block to your original model.
Connect the signal lines to the corresponding input and output ports in the model.
5. Start the simulation by clicking the "Run" button on the Simulink model. Simulink will execute the generated
code on the embedded processor and the algorithm simulation in synchronous fashion. Observe that the results
are identical, verifying the generated code against the original simulation design.
Depending on your execution platform, you may be able to use serial communication interface for PIL simulation.
PIL simulation using serial communication interface is generally much faster than the PIL simulation using IDE
debugger.
For steps to enable serial communication interface for PIL simulation, see Enabling Serial Communication PIL
Simulation.
Open Field Oriented Control with inputs and profile the execution of this model using the steps in Task #2. Note
that the execution is slower by a factor of 10 compared to the execution result obtained in Task #2. The only
difference between the two models is that we added sine wave inputs. This is because the Simulink Sine Wave
block generates ANSI® C code that is not optimized for the selected processor.
To generate more efficient code, replace the Sine Wave blocks with the optimized IQMath sine function blocks
from the C28x IQmath Library. Open the Inputs Comparison Model and run the model in simulation to confirm that
these source blocks are emitting signals comparable to the Sine Wave blocks.
Open Field Oriented Control with optimized inputs. Follow the steps described in Task #2 to profile the execution
of this model. Note that the execution time is not much closer to the results obtained in Task #2.
2. Optimize Code by Using Compiler-Specific Intrinsics
Using compiler-specific intrinsics can improve the performance of the generated code even further. This is
accomplished by replacing standard C constructs for certain fixed-point numerical operations with processor and
compiler-specific instructions. You will use the Code Replacement Library (CRL) feature to specify the
replacement scheme that matches your processor family. More information about CRL can be found in the CRL
Help.
For this step, you will include PID controllers in the FOC algorithm. The DMC library provides an efficient PID
controller. However, in this exercise we are going to create a PID controller using generic Simulink blocks.
Open the Field Oriented Control with PID controllers template.
Open the Simulink Model Explorer (Ctrl+H). Go to "Configuration" > "Code Generation". Under the "Interface" tab,
change the Code Replacement Library to "TI C28x". When possible, this option replaces all generic fixed-point
operations in Simulink with TI IQMath optimized functions.
Follow the steps described in Task #2 to profile the execution of this model. You can enable and disable CRL and
compare the profiling results to measure the performance effect of these optimizations.
3. Optimize Code by Using Simulink® Coder™ Optimizations
Open the Simulink Model Explorer (Ctrl+H). Go to "Configuration" > "Optimization". Enable "Inline Parameters" to
inline block parameters for increased efficiency.
4. Optimize Code by Using Compiler Optimizations
Texas Instruments C2000 compiler can apply various optimizations while compiling the generated code. This can
lead to substantial execution performance gains. You can control the CCS compiler optimization flags from
Simulink.
In this task, you will validate your Field Oriented Control algorithm with closed-loop simulation using a motor
6
model. The Id current request will be set to 0, while the Iq current request will come from the speed controller. The
Page
requested speed is fixed while external torque applied on the motor simulation varies with time.
1. Open the C2808 Field Oriented Motor Control Plant-Controller or the equivalent C2812 Field Oriented Motor
Control Plant-Controller.
2. Start the simulation by pressing the "Run" button of the Simulink model.
Note that the outputs of the motor have been scaled to match the values that you will get from physical devices on
the hardware. The following task describes how this scaling is being done.
There are three signals we want to sample from the ADC, Ia and Ib coming from the current sensors on phase A
and B of the motor and a third signal used as a speed request. These three signals are respectively connected to
ADCINA0, ADCINA1 and ADCINA7.
Block Mask Settings:
Leave the settings on the "ADC Control" tab unchanged. On the "Input Channels" tab, set the number of inputs to
8
3 and select ADCINA0, ADCINA1 and ADCINA7 for conversion #1, #2, and #3. For more information on ADC
Page
block parameters, see c280x ADC or c281x ADC help. The ADC block outputs 12-bit conversion values.
The current sensors measure positive and negative values, calibrated so the 0 A mark corresponds to half the
range of the ADC (2048 counts). 4095 counts represents the maximum current that can be measured. 0 counts
represents the maximum negative current that can be measured.
An efficient way to scale these inputs is to shift the 12-bit ADC values for the current sensors by 6 bits to the left.
The result is a normalized representation of the current in fixed-point Q17 format, where -1 is the maximum
negative current and +1 is the maximum positive current. This operation is executed in the "Scaling" subsystem.
2. QEP
One way to get a position signal is to use an incremental encoder. You can use the QEP decoder to translate
signals from the encoder into an angle value that can feed the FOC algorithm. This example shows how to
decode signals coming from a 2000-slit optical encoder disk.
The FOC algorithm needs the absolute position of the rotor. To achieve this, the QEP index pulse is used to
establish a reference position, and the QEP counter to find a position relative to the reference position. For start-
up the strategy in this example uses a ramp signal generated for the position until the index pulse is hit. During
that initialization period, the init_latch signal is set to 0 and a small amount of torque is applied to spin the shaft of
the motor until the reference position is found. This operation is performed in "Scaling/CAP3INT1". This
subsystem will also output the value of the counter saved on each hit of the index pulse.
"Scaling/CAP3INT1/QEP_scaling" converts the counter value into a per-unit position signal, given the counter
value of the last index pulse.
Block Mask Settings:
c281x QEP: Open the block's mask and set the sample time to 50 microseconds.
c280x/c28x3x eQEP: Open the block's mask and set the sample time to 50 microseconds.
c280x/c28x3x eQEP uses a 32-bit counter. To get the same behavior as the c2812, you can configure the counter
to reset when it reaches 65535. You can also configure the block to outputs a latched value of the counter on
every index pulse. To do these operations, open the eQEP block and:
On the "Position counter" tab of c280x/c28x3x eQEP:
Using a PWM frequency close to the frequency used for the FOC algorithm, you will see in task #7 how to initiate
the ADC conversion and the FOC algorithm based on a PWM event. It is desirable for the PWM frequency to be a
multiple of the clock cycles by a power of 2 in order to switch from a per-unit signal to number of clock cycles with
minimal processing.
Scaling
The output of the FOC is in Q17 fixed-point format. The range of the signal is therefore from -1 to 1. Adding 1 to
this signal converts the range so it goes from 0 to 2. You only need to shift the integer value of this signal to
match the PWM period and feed the compare value of the PWM block. If the PWM period is set to 2^12, a shift by
6 to the right is needed on the [0 - 2] Q17 FOC output signal to feed the PWM compare value. This operation is
done in c2812_drivers/Motor Control Algorithm/PWM scaling and c2808_drivers/Motor Control Algorithm/PWM
scaling.
Deadbands
You can use PWM deadband option to add timing protection between transitions of the upper and lower switches
of the H bridge on the motor controller hardware.
Block Mask Settings:
c281x PWM:
10
Page
1. Open from the model constructed in task #6.
2. Put all the blocks except the "Edit Parameters" block in a "Function-Call Subsystem" that you can find in the
Simulink browser under "Simulink > Ports & Subsystems".
3. The FOC algorithm is going to be triggered using the ADC interrupt. Drag the "Hardware Interrupt" block for
your target board from the library.
For C281x, set the following Hardware Interrupt block mask parameters:
On 2812, open the PWM block's mask and go to the "ADC Control" tab. Select "Counter underflow" for the ADC
start event to launch the ADC conversion when the PWM counter is equal to 0.
On 2808/28335, open the first PWM block's mask and go to the "Event Trigger" tab. Select "Enable ADC start
module A". The default settings for the other parameters trigger the ADC conversion every time the PWM counter
reaches 0.
11
Open the ADC block's mask and set "Start of conversion" to "ePWMxA" (C2808/C28335) or EVA" (C2812).
6. If you prefer, use pre-configured models for F2808, F2812, or F28335 eZdsps.
Page
Task 8 - Monitor Target Signals Using SCI or CAN
This task shows how to use SCI and CAN to monitor target signals. This task requires additional connections
between the host and the target. For SCI, use a serial cable connected from the Target to the host via appropriate
RS232 transceivers. For CAN, use a Vector® CAN card on the host with appropriate cables and transceivers to
communicate to the target.
12
Page
1. Open the 2808 Monitor Model / 28335 Monitor Model or the corresponding 2812 Monitor Model. The baud rate
for CAN and SCI can be set in the Target Hardware Resources dialog.
2. Generate code for the model, provided that you have made the necessary hardware connections.
3. While the target model is running on the processor, open the CAN host model or the Serial host model. Start
the simulation on the host models to monitor the target signals in real-time.
This task requires extra connections between the host and the target board. You can use a variety of CAN
interface cards for your host. You will also need appropriate cables and transceivers.
13
Page
Page
14
1. Open the 2808 External Mode Model, 28335 External Mode Model or the corresponding 2812 External Mode
Model. The baud rate for CAN Calibration Protocol block can be set in the Model Configuration Parameters dialog
under "Code Generation" > "Coder Target" > "Target Hardware Resources". Baud rates for host and target should
be the same.
2. While opening the model, a script will be processed as a Pre-Load Function callback. This can be viewed by
selecting "File" > "Model Properties" > "Model Properties" > "Callbacks" > "PreLoadFcn". The script is setting
Simulink signals and parameters to define Simulink objects needed for CCP DAQ lists and Simulink tunable
parameters as described in the CCP documentation. This is required to interact with these signals in real-time
using CCP.
3. Double-click the convenience block "Switch External Mode Configuration", select the "Build an executable"
option (default value) and click "OK".
4. Generate code by clicking "Build Model" button in your model (Ctrl+B). Make sure that you have the required
hardware connections.
5. Double-click the convenience block Switch External Mode Configuration, select the "External mode" option and
click OK.
6. Run the model in External Mode by selecting "Code" > "External Mode Control Panel" > "Connect" or by
clicking "Connect To Target" button on the Simulink toolbar.
7. You can view selected signals in the Simulink scopes while the application is executing on the target board.
Summary
This example showed a workflow to generate and verify code for a motor control application. It showed how to
profile the generate code, run PIL simulation on the hardware, simulate the controller against a motor simulation,
integrate peripherals, monitor target signals on host models and generate production code using the on-chip flash
memory.
16
Page