Main Content


This example shows how to implement an AUTOSAR-based field-oriented control (FOC) algorithm to run a permanent magnet synchronous motor (PMSM).

Automotive open system architecture (AUTOSAR), is an open and standardized automotive software architecture, jointly developed by automobile manufacturers, suppliers, and tool developers. AUTOSAR defines a standardized software architecture for automotive electronic control units (ECUs). A standardized software architecture helps original equipment manufacturers (OEMs) and software suppliers in creating reusable software with scalability across different vehicles and automotive platforms. The framework also enables smooth and seamless collaboration between manufacturing partners and suppliers. In addition, compliance with AUTOSAR standards reduces software maintenance efforts throughout the product life cycle. AUTOSAR standards ensure safety and increases robustness of automotive applications.

AUTOSAR-based motor control system software enables electrical vehicle manufacturers, suppliers, and partners to take advantage of these benefits. For more information, see AUTOSAR Standard (AUTOSAR Blockset) and What is AUTOSAR? (AUTOSAR Blockset).

This example shows how a motor control system that you model in Simulink® using Motor Control Blockset™ integrates with the AUTOSAR classic platform (CP).

This image shows where you can position the motor control algorithm within AUTOSAR's classic platform solution.

This example uses Motor Control Blockset™, AUTOSAR Blockset and System Composer to develop the software architecture using model-based design in Simulink. You can use these products to create system composition models. A system composition model is a group of components. Each component acts as a model reference to a Simulink model.

The generated AUTOSAR code can be used to integrate it with the basic software (BSW) stack using an AUTOSAR authoring tool.

The example supports simulation of the system composition model. You can also use the model to generate AUTOSAR code for these layers and integrate the code with AUTOSAR basic software components with minimum changes.

  • Application layer — Encloses higher-level speed control algorithm of the motor control system.

  • Complex drivers — Encloses lower-level torque or current control algorithm of the motor control system.

Integration of this algorithm requires standard application program interface (API) calls to and from the AUTOSAR code of the neighbouring components and BSW layers.

Note: The system composition model in this example includes only the components required to implement a motor control algorithm.

What are complex drivers?

A complex device drivers (CDD) is a specific module located in the complex drivers layer of the BSW that interacts with the standard BSW modules or run time environment (RTE).

The complex drivers layer can integrate with other AUTOSAR layers using standard AUTOSAR interfaces as well as basic software modules (BSM) APIs.

With direct access to a microcontroller, you can use this layer to perform complex sensor read and evaluation operations as well as actuator control operations using specific interrupts, complex microcontroller peripherals, and external devices (such as communication transceivers and ASIC) to fulfil specialized functional and timing-related requirements.

Why is motor current control part of complex drivers layer?

The current control loop in any motor control algorithm is very time sensitive. The loop needs precise synchronization with hardware peripherals such as analog-to-digital converter (ADC) and pulse-width modulator (PWM) for accurate current and position sensor measurements.

With direct access to the microcontroller abstraction layer (MCAL), the complex drivers layer enables the motor control algorithm to configure ADC-PWM synchronization accurately. Using BSM APIs from other layers does not always provide this access.

Open MATLAB Project

The example is packaged as a MATLAB® project. Use one of these methods to open the MATLAB project window:

  • Click Open Project.

  • Run the following command to access the supporting files of the example and open the MATLAB project.



The MATLAB project has an integModels folder that includes the model mcb_AUTOSAR_System.slx.

This model contains the following entities:

  • MCU — System composition that encloses software for the microcontroller.

  • InverterAndMotor — System composition that encloses a plant model containing a motor and an inverter. The composition acts as a replica of an actual inverter and motor setup for simulation purposes.

Simulating a real plant helps you perform extensive tests and validation with ease, which helps you identify and fix issues prior to deploying the algorithm to hardware and operating the motor. Simulating a real plant also helps you troubleshoot problems and reproduce operating conditions that are hard to reproduce using a physical hardware setup.

MCU (microcontroller software) contains these entities:

  • AUTOSAR — The AUTOSAR-based controller software that includes these software components:

          - motorControlApplication — The application layer containing the high-level motor speed control algorithm. This acts as the outer field-oriented control (FOC) loop that controls the speed of a permanent magnet synchronous motor (PMSM) using a proportional integral (PI) controller. This loop usually runs at a slower rate than the inner loop controlling the motor torque.

          - ComplexDeviceDriver — The complex drivers layer containing the low-level motor torque or current control algorithm. This acts as the inner FOC loop of PMSM that controls the torque by controlling the motor currents using two separate PI controllers (for d- and q-axis currents). This loop usually runs at a faster rate than the outer loop controlling the motor speed.

          - ECUAbstraction — The electronic control unit abstraction layer.

  • MCAL — The microcontroller abstraction layer that helps AUTOSAR directly interface with microcontroller.

For more information about FOC, see Field-Oriented Control (FOC).

Required MathWorks Products

To simulate the system composition model, you need these products:

  • Motor Control Blockset

  • AUTOSAR Blockset

  • System Composer

To generate code from the system composition model, you need these products:

  • Motor Control Blockset

  • AUTOSAR Blockset

  • System Composer

  • Embedded Coder®

  • Fixed-Point Designer™ (needed only for optimized code generation)


1. Obtain the motor and inverter parameters. The MATLAB project uses default motor and inverter parameters that you can replace with values from the motor and inverter datasheets or from other sources.

Optionally, if you have the actual motor, you can estimate the parameters for the motor that you want to use with the motor control hardware by using the Motor Control Blockset parameter estimation tool. For instructions, see Estimate PMSM Parameters Using Recommended Hardware. The parameter estimation tool updates the motorParam variable (in the MATLAB workspace) with the estimated motor parameters.

2. Update the motor and inverter parameters in the StartUp.m parameter script (available in the scripts folder in the Project window) associated with the system composition model.

3. Click Run on the Editor tab to run the parameter script.

Simulate Model

Complete these steps to simulate the system composition model included in the project:

1. Open the system composition model mcb_AUTOSAR_System.slx from the model folder included in the MATLAB project.

2. Click Run on the Simulation tab to validate the motor operation.

Generate Code for Integration with AUTOSAR Compliant Software

This section shows how to generate AUTOSAR code (.c, .h, .arxml files) for the motorControlApplication and ComplexDeviceDrivers software components that you can integrate with your AUTOSAR basic software components for a motor control application such as that of an electric vehicle.

1. Open the system composition model mcb_AUTOSAR_System.slx from the model folder included in the MATLAB project.

2. Navigate to mcb_AUTOSAR_System/MCU/AUTOSAR (mcb_AUTOSAR)/ComplexDeviceDrivers and select the CCD_TorqueControl software component.

3. In the Modeling tab of the Simulink toolstrip, click Export > Generate Code and ARXML.

4. Select the export options in the Export Component window and click OK to generate the AUTOSAR code.

5. Navigate to mcb_AUTOSAR_System/MCU/AUTOSAR (mcb_AUTOSAR)/motorControlApplication and select the CruiseControl software component.

6. Repeat steps 3 and 4 for the CruiseControl software component.

To integrate the generated code manually with your AUTOSAR basic software components, you can use standard API calls to and from the AUTOSAR code of the neighbouring components and layers.