Main Content

Improve Accuracy of Time Values Read by Generated Entry-Point Functions

This example shows that for a component model that is configured to use a service code interface, you can improve the accuracy of the time values read by generated entry-point functions by configuring the model with a specific clock resolution. The aperiodic function Integrator in example model ComponentDeploymentFcn includes the Discrete Time Integrator block, which requests the clock tick. Model configuration parameter Fixed-step size (fundamental sample time) is set to auto.

Generate and inspect the code for the model with the Clock resolution (seconds, -1 inherited) parameter set to the default value -1. In this case, the code generator initializes the clock resolution based on scheduling properties for the model style and the type of entry-point function.

Open example model ComponentDeploymentFcn.

open_system("ComponentDeploymentFcn.slx");

Use the default clock resolution to generate code, then inspect the code.

  1. Open the Embedded Coder app.

  2. Configure the Integrator function to use a timer service interface that is configured for during-execution communication. In the Code Mappings Editor – C, on the Functions tab, select the row for function Exported Funciton:Aperiodic. Click the pencil button mapping_inspector_icon.png. In the dialog box that appears, set the Timer Service property to get_tick_during. Save the model.

  3. In the toolstrip, in the Modeling tab, click Modeling Settings.

  4. In the Configuration Parameters dialog box, search for and select the Clock resolution (seconds, -1 inherited) parameter. The parameter is set to the default value -1. Close the dialog box.

  5. Generate code.

  6. In the code generation report, select the Code Interface Report and navigate to the Timer Service Interfaces section. The report shows that the clock resolution is set to 1 second.

  7. In the generated file ComponentDeployentFcn.c, inspect the code for entry-point function cd_integrator. The code generator uses the clock resolution to calculate the integer value in the assignment statement for variable tmp. The value 1.25 is the product of the clock resolution (1) and the gain value specified for the Discrete Time Integrator block (1.25) and is the frequency at which the function requests the elapsed time.

tmp = 1.25 * (real_T)Integrator_ELAPS_T;

Configure the model with a clock resolution of 0.5. Then, generate and reinspect the code.

  1. Set model configuration parameter Clock resolution (seconds, -1 inherited) to 0.5.

  2. Generate code.

  3. Check that the Code Interface Report shows that the clock resolution is set to 0.5 seconds.

  4. In the generated file ComponentDeployentFcn.c, reinspect the code for entry-point function cd_integrator. In this case, the code generator uses the specified clock resolution 0.5 to calculate the integer value in the assignment statement for variable tmp. The value 0.625 is the product of the clock resolution (0.5) and the gain value specified for the Discrete Time Integrator block (1.25). The generated code calls the timer service more frequently, which results in the function reading time values that are more accurate.

tmp = 0.625 * (real_T)Integrator_ELAPS_T;

See Also

|

Related Topics