top of page

Automating Specification Documentation Generation: C Code to MATLAB Model Transformation for Charging Control

Mar 5

4 min read

0

4

0


#Automotive #EV充電 #ModelBasedDevelopment #MATLAB #Simulink #組み込みシステム #AutomatedDocumentation #電気自動車 #ChargingControl #CtoMATLAB #ソフトウェア開発 #自動ドキュメント生成 #EmbeddedSystems #充電制御 #自動車
iJbridge Incorporation
Introduction
With the increasing complexity of electric vehicle (EV) charging control systems, transitioning from traditional C-based implementations to MATLAB/Simulink models is essential for improving maintainability, validation, and integration with model-based development (MBD) workflows. However, this transformation requires meticulous translation of functional logic while ensuring compliance with automotive standards such as ISO 26262. Additionally, the automation of specification documentation generation plays a critical role in reducing manual effort, ensuring consistency, and improving traceability throughout the development cycle.
This article provides a highly technical breakdown of the structured approach to converting C code into a MATLAB/Simulink model for charging control systems, alongside an automated documentation generation process.

Charging Control System Overview
The EV charging control system is responsible for regulating power flow between the charging station and the vehicle battery, ensuring safety, efficiency, and compliance with standardized protocols. Key control functions include:
  • State Machine Implementation: Governs charge initiation, active charging, balancing, and termination sequences.
  • Power Electronics Control: Regulates voltage, current, and power levels through closed-loop feedback mechanisms.
  • Communication with Charging Stations: Implements protocols such as CCS (Combined Charging System), CHAdeMO, and GB/T to negotiate charging parameters.
  • Thermal and Fault Management: Monitors temperature variations, detects faults, and implements emergency shut-off mechanisms.
  • Battery Management System (BMS) Interaction: Ensures proper interfacing with the BMS for SoC (State of Charge) and SoH (State of Health) monitoring.
Legacy implementations in C are often optimized for real-time embedded platforms, necessitating a structured approach for migrating to MATLAB.

C Code to MATLAB Model Transformation Process
1. Static and Dynamic Code Analysis
The initial step involves extracting functional elements from C code. A static code analysis is performed using tools like Polyspace or Clang static analyzer to detect:
  • Control flow complexity
  • Data dependencies
  • Global variable usage
  • Function call hierarchy
  • Pointer dereferencing and memory accesses
A dynamic analysis using execution profiling tools identifies:
  • Time-sensitive operations such as interrupt handlers
  • Execution bottlenecks in computational loops
  • Task scheduling priorities
  • Signal processing mechanisms

2. Functional Decomposition and Block Representation
Once the functional architecture is defined, the equivalent MATLAB model is structured using:
  • Stateflow for control logic representation: Charge control states (e.g., Idle, Pre-Charge, Charging, Post-Charge) are modeled with state transition conditions derived from the original C state machine logic.
  • Simulink function blocks: Core algorithms such as PID-based current control and voltage regulation are converted into Simulink subsystems.
  • Lookup tables (LUTs): Predefined mappings (e.g., temperature-dependent charging rates) are extracted from C arrays and implemented using Simulink LUT blocks.
  • Custom MATLAB functions: Any mathematical transformations or filtering logic found in the original C code are encapsulated within MATLAB function blocks.

3. Mapping Embedded C Parameters to Simulink Data Objects
C macros and constants that define system behavior (e.g., max charging current, overvoltage thresholds) are converted to Simulink.Parameter objects with the appropriate data types and tunability attributes:
  • #define MAX_CHARGING_CURRENT 50.0 → Simulink.Parameter('MAX_CHARGING_CURRENT', 50.0)
  • static const float overvoltage_threshold = 4.2; → Simulink.Parameter('overvoltage_threshold', 4.2)

4. Signal Routing and Interface Definition
  • I/O Ports Configuration: Simulink Inports/Outports are assigned corresponding signal names and bus structures.
  • Signal Logging: Data Store Memory blocks or Data Logging configurations are used to capture simulation behavior.
  • External Hardware Integration: When interfacing with hardware peripherals, S-Functions or Simulink Real-Time blocks are used.

5. Model Verification using MIL (Model-in-the-Loop) Testing
Once the Simulink model is built, it is verified using a series of Model-in-the-Loop (MIL) validation steps:
  • Back-to-Back Testing: MATLAB scripts execute equivalent input test cases on both C and Simulink models, ensuring functional equivalency.
  • Simulation of Corner Cases: Edge conditions such as voltage fluctuations, thermal stress, and current surges are simulated.
  • Floating-Point Precision Analysis: MATLAB’s Fixed-Point Designer is used to validate numerical stability compared to the original fixed-point C implementation.

Automating Specification Documentation Generation
1. Extracting Metadata from MATLAB Models
Specification documentation requires structured details on functional behavior, signal interactions, and verification results. MATLAB provides APIs to extract metadata such as:
  • Block-Level Information: Simulink.findBlocks retrieves functional blocks, parameters, and connectivity.
  • Signal Flow Analysis: Simulink.BlockDiagram.getGraphicalInfo extracts signal paths, allowing automatic generation of interface diagrams.
  • Stateflow Transition Details: sfroot API is used to retrieve state transition conditions and logic flow.

2. Automating Document Formatting
Using the Simulink Report Generator, structured reports are automatically compiled with:
  • System Architecture Diagrams: Auto-generated diagrams highlighting subsystem interconnections.
  • Functional Descriptions: Extracted annotations and block descriptions.
  • Verification Test Results: MIL test execution reports with success/failure metrics.
  • Parameter Tables: Listing of tunable parameters, default values, and constraints.

3. Version Control and Change Management
For iterative development, Simulink Requirements enables:
  • Tracking model changes across versions.
  • Automatic cross-referencing between requirements and model blocks.
  • Continuous validation by linking test cases with functional specifications.
Conclusion
Transitioning from C-based charging control implementations to MATLAB/Simulink models significantly enhances validation, maintainability, and compliance adherence. However, manual transformation is prone to errors and inefficiencies. Automating the process using static and dynamic code analysis, structured function decomposition, and parameterized Simulink modeling ensures correctness and traceability.
Furthermore, automating specification documentation generation eliminates manual inconsistencies, improves documentation quality, and streamlines regulatory compliance efforts. By leveraging MATLAB’s powerful API-driven metadata extraction, reporting, and change management features, engineering teams can establish a fully automated workflow from C code translation to validated MATLAB models and structured documentation.
As the EV industry continues to evolve, future advancements in AI-assisted model generation and real-time verification through hardware-in-the-loop (HIL) testing will further optimize the transition process, enabling faster and more robust software development for next-generation automotive charging systems.

For more enquiry visit www.ijbridge.com

Mar 5

4 min read

0

4

0

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page