Using For Loops in ECU Application Layer Model Development

It has been a while since I last updated an article on ECU application layer software development. Recently, I have frequently used for loops, so I would like to share some insights. Although for loops are generally avoided in actual projects, and most people do not often use them, there are times when they are unavoidable. Therefore, this article will introduce how to use the For Iterator subsystem in Simulink to implement looping functionality.Using For Loops in ECU Application Layer Model DevelopmentWithout further ado, let’s directly introduce some simple application examples:Example 1: Summation from 1 to 100First, create a For Iterator subsystem, then build the summation operation inside it, and set up the For loop module as shown below:Using For Loops in ECU Application Layer Model DevelopmentUsing For Loops in ECU Application Layer Model DevelopmentThe expected calculation process is as follows:1st for loop: 1 + 0 = 1, which equals 12nd for loop: 1 + 2 = 3, which is the sum of 1 from the 1st for loop and 2;3rd for loop: 3 + 3 = 6, which is the sum of 3 from the 2nd for loop and 3;Continuing this way, until the 100th for loop: 4950 + 100 = 5050, which is the sum of 4950 from the 99th for loop and 100.This is the designed model algorithm. Next, we will configure the Simulink simulation with a discrete step size of 10ms, as shown below:Using For Loops in ECU Application Layer Model DevelopmentFinally, when running, we can see that the model algorithm indeed calculates the result as 5050, which meets expectations.Using For Loops in ECU Application Layer Model DevelopmentThis simple example provides a preliminary understanding of the operation mechanism of for loops.Note: The entire for loop runs within one step cycle!Next, let’s look at two more examples:Example 2: Summing Array ElementsIf we need to sum the elements of an array, we can use a for loop to extract each element from the array for accumulation, as shown below:Using For Loops in ECU Application Layer Model DevelopmentUsing For Loops in ECU Application Layer Model DevelopmentNote that the starting point of the array index is 0, so in the for loop module, select zero-based indexing.Using For Loops in ECU Application Layer Model DevelopmentThus, the for loop can complete the summation of array elements within one step cycle:1st for loop: The for loop outputs the array element at index 0, which has a value of 1, so 1 + 0 = 1;2nd for loop: The for loop outputs the array element at index 1, which has a value of 2, so 1 + 2 = 3, which is the sum of 1 from the 1st for loop and 2;Until the 5th for loop: The for loop outputs the array element at index 4, which has a value of 5, so 10 + 5 = 15, which is the sum of 10 from the 4th for loop and 5.Using For Loops in ECU Application Layer Model DevelopmentExample 3: Comparing Array ElementsSuppose we need to count how many elements in the array are equal to a certain value. The first step in designing the logic is still to use a for loop to extract each element from the array, comparing each element with the target value in each for loop. If they are equal, the counter increases by 1; if not, the counter remains unchanged. The logic is set up as follows:Using For Loops in ECU Application Layer Model DevelopmentUsing For Loops in ECU Application Layer Model DevelopmentUsing For Loops in ECU Application Layer Model DevelopmentAs can be seen above, the for loop serves to extract each array element and can complete the operation within one step cycle.4. ConclusionThrough these three examples, I have progressively introduced the use of for loops. In automotive ECU application model development, regardless of whether the for loops used in the model are more complex, the essence remains the same:To complete multiple calculations within one step cycle,whether it is addition, subtraction, multiplication, division, or comparison logic, overall, the most common approach seen is still using the For Iterator subsystem to handle arrays.Creating content is not easy, please like, save, and follow!!

For those interested, please add the group owner: prOmiseyes, with a note: company + position to join the group. Limited to automotive industry personnel.

Articles on ECU application layer software models are continuously updated, teaching you application layer development from scratch:

  • Example 1: Building an ECU Application Layer Software Model (qq.com)
  • How to Automatically Generate Code for ECU Application Layer Software Model 2 (qq.com)
  • How to Run ECU Application Layer Software Periodically 3 (qq.com)
  • Code Generation Configuration Related to ECU Application Layer Software Model and Hardware 4 (qq.com)
  • Data Dictionary DD in ECU Application Layer Software Model 5 (qq.com)
  • Data Dictionary Storage Classes in ECU Application Layer Software Model 6 (qq.com)
  • Fixed-Point Implementation in ECU Application Layer Software Model 7 (qq.com)
  • Five Fixed-Point Implementation Methods in ECU Application Software Model 8 (qq.com)
  • Custom Storage Classes in ECU Application Layer Software Model 9 (qq.com)
  • Summary of Model Generation Code in ECU Application Layer Software Model 10 (qq.com)
  • CAN Reception in ECU Application Layer Software Model 11 (qq.com)
  • Data Stream Reception of CAN Messages in ECU Application Layer Software Model 12 (qq.com)
  • How to Leverage AI in ECU Software Development? Application of Kimi in MBD Development Environment 13 (qq.com)
  • Detailed Explanation of Unit Test Coverage CC, DC, and MCDC in ECU Application Layer Software Model 14 (qq.com)

  • Application and Principles of Calibration Quantities and Observables in ECU Application Layer Model Development 15 (qq.com)

Leave a Comment