Main Content

Parameter Tuning and Signal Logging with Serial Communication

This example shows how to perform parameter tuning and data logging with a Simulink® model running on ARM® Cortex®-based VEX® Microcontroller targets.

Required Hardware:

  • ARM Cortex-based VEX Microcontroller

  • USB A-A Cable

Model

The following figure shows the vexarmcortex_external_mode model.

Description

In this example, you will learn how to run a Simulink model, tune parameters and log data.

Configure the Hardware and Model for Parameter Tuning

Set up the hardware

Connect the ARM Cortex-based VEX Microcontroller to the computer using a USB A-A Cable. The connection should automatically enable serial connection between the Microcontroller and the host computer.

Set up the model

1. Open the Example vexarmcortex_external_mode model.

2. In the Modeling tab, click Model Settings. In the Configuration Parameters dialog box, navigate to the Hardware Implementation pane.

3. Select Hardware board as ARM Cortex-based Vex Microcontroller and click Apply.

4. In the Simulink model, go to Simulation tab and specify a desired value for the Stop time. Specify the stop time as 'inf' to run the model continuously on the target hardware.

Run the model for signal monitoring and parameter tuning

When you perform the Monitor and Tune action for the model, the host computer communicates with the target to log data and tune parameters. To run the Example Model(vexarmcortex_external_mode) for signal monitoring and parameter tuning:

1. Go to the Hardware tab and click Monitor & Tune. Simulink generates the code, loads the executable and connects to the target. View the Diagnostic Viewer to track progress.

2. While the model is running, open the Scope connected to the Gain block to monitor its output.

3. Click Stop in the Hardware tab to terminate the Monitor and Tune action.

Parameter Tuning and Data Logging

Parameter Tuning

The Monitor and Tune feature in Simulink provides the option for parameter tuning 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 immediately.

To tune parameters:

1. In the Hardware tab, click Monitor & Tune.

2. While the model is running, double click on the Gain block and change the Gain value. View the result on the Scope. The amplitude of the sine waveform changes accordingly.

3. To switch the input source, double click on the Manual Switch block. View the result on the Scope. If the Verbose is selected in the Model Configuration Parameters > Hardware Implementation > Target Hardware Resources > External mode, the status of the parameter change is displayed on the MATLAB Command Window.

4. Stop the Monitor and Tune action (click Stop in the Hardware tab).

Data Logging

To log External mode data to a MAT file, you can either use the Scope or the To Workspace block. Simulink supports multiple data logging schemes. The below steps explain, the default, manually triggered scheme.

1. Click the Data Archiving button of the External Mode Control Panel (Hardware > Control Panel ). Check the Enable Archiving option. Use the Directory parameters to specify the destination of your log file, and File to specify the name of your log file.

In Data Archiving, new data sets are saved in new MAT files (log files). Each data set is of the size of the Duration (Code > External Mode Control Panel > Signal & Triggering > Duration). Duration specifies the minimum contiguous data to be collected by External mode.

2. Open the Scope connected to the Gain block and click on the Configuration Properties button.

3. 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 corresponding time instants along with the logged data, select Save Format as Structure With Time.

Note: If you do not check the Log data to workspace option, empty MAT-files will be created.

4. In the Hardware tab, click Monitor & Tune. Navigate to the folder specified for saving the logged data(path mentioned in step 1). Several MAT files will be created, each containing a structure with 25 contiguous data samples. You can change the size of the contiguous data samples by changing Duration (Code > External Mode Control Panel > Signal & Triggering > Duration).

5. Stop the Monitor and Tune action (click Stop in the Hardware tab).

Limitations

1. There might be problem cases in which Monitor and Tune action on the Vex microcontroller doesn't start. In such cases, the serial Port (COM Port) may not have been closed properly. To overcome this, connect and disconnect the USB A-A cable.

2. Increasing the Duration allows MAT file logging with large contiguous data sets. A high value of Duration may result in Not enough memory on the target to process the packet warning, and consequently cause no data to be logged. If you face this warning, either decrease the number of scopes or decrease the Duration (Hardware > Control Panel > Signal & Triggering > Duration).