Parameter Tuning and Signal Logging with Serial Communication
This example shows how to perform parameter tuning and data logging with a Simulink® model running in Texas Instruments™ C2000 targets.
Any of the Texas Instruments C2000 - based controlCARDS with docking station or Spectrum Digital eZdsp boards that have a serial interface with SCI_A.
Note: F281x based boards do not support parameter tuning over a serial communication interface. Refer to CAN based parameter tuning for these boards.
A USB serial cable if your hardware provides serial over USB capabilities, or an RS-232 cable connected to the COM1 port of your computer. The CCS tool is closed before running the program in external mode. You can use a tool such as PuTTY to test the basic working of Rx and Tx before trying the external mode. For more, see Set Up Serial Communication with Target Hardware
This figure shows the example model. SDI(Simulation Data Inspector) and HMI (Human Machine Interface) blocks in the model will work with External mode over XCP protocol.
Configure the Hardware and Model for Monitoring and Tuning
Set Up the Hardware
This example uses Texas Instruments F28335 controlCARD with docking station and USB serial cable to connect the host computer and the target hardware. You can also use the COM1 port of your computer to establish an RS-232 serial connection with the board. See Set Up Serial Communication with Target Hardware for details on establishing a serial connection between the target and the host computer.
After establishing a serial connection, find the COM port associated with the target hardware:
Open Device Manager in Windows.
Expand the Ports tab.
Note the COM port associated with the target board.
Set up the model
The Example Model is configured for the Texas Instruments F28335 controlCARD with docking station, but you can follow the procedure for any other hardware board mentioned in Required Hardware.
1. Open the Example Model.
2. Open the Modeling tab and press Ctrl+E to open Configuration Parameters dialog box and navigate to the Hardware Implementation pane.
3. Select your target hardware from the Hardware board drop-down list.
Note: To run the Example Model on targets with small amounts of RAM such as the F28027 or F28035, enable the Boot From Flash (stand alone execution) option in Hardware Implementation > Target Hardware Resources > Build options tab and increase the heap size as mentioned in the Limitations section.
4. Navigate to the Hardware Implementation > Target Hardware Resources > External mode.
Select the Communication interface as serial or XCP on Serial.
Select the SCI module. By default SCI_A module is selected for Controlcards and Launchpads. For custom boards, select other serial modules to connect to FTDI.
Select the Serial port in MATLAB preference with the COM port number associated with your target hardware.
The Verbose option enables viewing the execution progress of the simulation on the Diagnostic Viewer and on the MATLAB Command Window.
Navigate to SCI_A and specify the baud rate in Desired baud rate in bits/sec.
The default baud rate is
115200. You can increase the baud of the serial over USB of your Launchpad or controlCARD. On Launchpads and controlCARDs using FTDI 2232H, you can select any baud less than or equal to 6 Mbps, or exactly 9 or 12 Mbps. On controlCARDs using FTDI 2232D, you can select any baud less than or equal to 1.5 Mbps, or exactly 2 or 3 Mbps.
5. Open the Simulation tab and specify a value in the Stop Time text box. You can specify the stop time as 'inf' to run the model continuously on the target hardware.
Monitor and Tune the Model
When you perform Monitor and Tune action for the model, the host computer communicates with the target, on which the generated executable runs. To perform Monitor and Tune in the Example Model:
1. Open the Hardware tab and click Monitor & Tune. You can observe from the Diagnostic Viewer that the code is generated for the model and the host connects to the target after loading the generated executable.
2. While the model is running, open the Scope connected to the Gain block to monitor its output.
3. In the Hardware tab, click Stop button to terminate the simulation.
You can tune parameter values while the generated executable is running on the target hardware. When the parameter values are changed in the Simulink model, the modified values are communicated to the target hardware.
1. While the Monitor and Tune action is in progress, double click the Gain block and change the value of the gain. You can use slider to change the gain value if using XCP protocol. You can observe that the amplitude of the sine waveform has changed according to the new gain value. Change the value of the Constant block to
0 to switch the input source and observe the result on the Scope. If the Verbose option is selected in Configuration Parameters > Hardware Implementation > External mode, the status of the parameter change is displayed on the MATLAB command window.
2. Stop the simulation.
To modify more than one parameter and communicate the changes to the target hardware at once, use the Batch download option in the External Mode Control Panel. To open the External Mode Control Panel, go to the Hardware tab and click Control Panel. Refer to Parameter Downloading for details on the Batch Download option.
While the Monitor and Tune action is in progress, you can log data from the model to a file. You can either make use of the Scope or the To Workspace block.
Follow the steps below to manually trigger data logging or trigger data logging from a signal.
Logging Signals with a Manual Trigger
You can use the Arm trigger button on the External Mode Control Panel to trigger data logging. The Arm when connecting to target option enables the trigger automatically when the host connects to the target and data uploading begins. Otherwise, to start uploading the data, you must manually arm the trigger by clicking the Arm trigger button located in the External Mode Control Panel. Triggers can be useful when the communication channel speed does not allow live visualization of the desired signals. In this task, you will learn how to manually trigger data uploading from the target to the host computer.
1. Open the External Mode Control Panel and click on the Signal & Triggering button, which opens the External Signal & Triggering dialog box.
a. By default, the Source option is set to
manual. Set the Mode to
normal to collect contiguous data samples. External mode allocates sufficient memory to collect data for the length of the Duration for each signal. Depending on the communication speed and the processing time given to the External mode engine that is running as a background task, you may see a continuous data logging stream, or a stream of data containing gaps corresponding to the time required to send the acquired buffers.
b. Specify the Duration as
15 to collect 15 data samples for a base rate signal. Since the signal from the
Gain block has the same sample rate as that of the model, the number of samples collected in each data set is 15. Uncheck the Arm when connecting to target option to enable manual triggering to upload data.
2. To view the sample time of the model, navigate to Display > Sample Time and select Sample Time Legend. For this example model, the sample time is 0.01 s.
3. Click on the Data Archiving button of the External Mode Control Panel to enable logging data to a MAT file from the Enable Data Archiving dialog box.
4. Check the Enable archiving option. Use the File and Directory parameters to specify the destination of your log file. In normal mode, file name incrementing happens automatically and new data sets are saved in new MAT files.
5. Open the Scope connected to the Gain block and click on the Configuration Properties button. Navigate to the Logging tab and check the Log data to workspace option. Specify a name in Variable name. The logged data is saved in this variable. To save the time instant and the signal data values, select
Structure With Time for Save format.
Note: If you do not select the Save data to workspace option, the MAT files for data logging are created, but they will be empty.
6. Click Monitor & Tune. Click the Arm Trigger button on the External Mode Control Panel to trigger data logging and the Cancel Trigger button to stop logging the data. Navigate to the folder specified for saving the logged data. Several MAT files are in this location, each containing a structure with 15 contiguous data samples.
7. Press the Stop button to terminate the simulation.
To collect only a single set of contiguous data samples, select
One-shot. In this mode, check the option Increment file after one-shot in the Enable Data Archiving dialog box to save new data sets in new MAT files.
Logging Signal Data using a Signal Trigger
In signal trigger mode, Monitor and Tune uses a signal as a trigger to start logging data. The data uploading begins when the trigger event occurs. To analyze a signal when an error or fault condition occurs while the model is running, use a signal trigger to log data.
In this task, you will learn how to trigger data logging to a file when certain signal conditions are met. In signal trigger mode, depending on the Delay specified, you can choose to log data samples of a signal immediately at trigger, a few seconds after the occurrence of the trigger, or both before and after the occurrence of the trigger.
1. Open the External Signal & Triggering dialog box from the External Mode Control Panel.
2. In this dialog box, select the Scope block connected to the Step input and click the Trigger Signal button to set the selected block signal as the trigger signal.
signal as the trigger Source. This enables the options in the Trigger signal. Set the Mode to
normal to collect contiguous data samples.
b. Specify the Duration as
15 to collect 15 base rate samples whenever the trigger conditions are satisfied.
c. Specify the Delay as
5. This causes data logging to begin 5 base rate samples after the occurrence of the signal trigger. Because the sample time for this model is 0.01 s and the step input is applied at the time instant 26 s, data logging begins from the time instant 26.05 s , (i.e 5 * 0.01 s after the trigger event at 26 s).
d. In the Trigger signal section, choose
rising as the Direction and set Level to
1. This causes the signal trigger conditions to be met whenever the signal connected to Scope1 increases in magnitude and crosses the threshold value of 1.
3. Specify a folder and a file name in the Enable Data Archiving dialog box to save the logged data.
4. Click Monitor & Tune.
5. The trigger conditions are met at the time instant 26 s, so stop the simulation after, about 40 s. Navigate to the folder where the data is saved and load the MAT file into MATLAB workspace by double-clicking on it. If you examine the contents of the MAT file, the structure member
time indicates, that data is logged from the time instant 26.05 s.
If the Delay
0, logging of the signal data begins immediately when the signal trigger conditions are met. The Delay option can also be used with negative values to center the data acquisition around the event of interest. To log data samples before the trigger conditions occur for the Example Model, specify a negative value,
-8 for example in the Delay field. This captures 8 base rate samples before the occurrence of the trigger and the remaining data samples after the occurrence of trigger.
To collect a data set of base rate samples only when the signal trigger conditions are met for the first time, choose the Mode as
One Shot. This mode is useful for viewing fast-changing data on a slow communication channel. To achieve this, increase the value of the Duration and set the signal trigger to acquire relevant data based on an event of interest, like a fault signal, an over voltage/current signal or a temperature warning signal.
Logging Data in SDI
XCP supports logging data in Simulation Data Inspector. For more, see Save and Share Simulation Data Inspector Data and Views (Simulink)
If you observe breaks in the scope, you can try the following to improve logging:
Serial Baud Rate
Decrease the number of signals selected for logging
Decrease the model base rate