What is Model-Based Development – and How to Use it For Generating Production-Ready Code?
Share
Model-Based Development Title

Model-Based Development (MBD) offers a way to design embedded systems by using visual models instead of traditional coding. Model-based development tools generate production-ready code, reducing manual errors and compliance issues. This guide will walk you through the basics of Model-Based Development, its advantages, challenges, risks and a tutorial for developing embedded systems from start to finish.

What are the Challenges in Classic Development Approach?

Senior Developer

  • Late Testing: Issues often emerge late in integration testing, leading to costly fixes.
  • Manual Coding: Increases risk of errors and inconsistencies, especially with multiple contributors.
  • Slow Iteration: Updates are time-consuming and often require physical prototypes.
  • Limited Early Validation: Design flaws may only be detected during later stages.
  • Compliance Requirement: Compliance demands developer effort to ensure code meets standards and best practices.

What are the Benefits of Adopt Model-Based Development?

Benefits

  1. Faster Development: Model-based development toolkits allow you to simulate both the model and the generated C code, helping to identify issues early and reduce late-stage fixes.
  2. Improved Quality: Early defect detection leads to fewer errors and higher product quality.
  3. Better Collaboration: Common visual models enhance team communication.
  4. Automatic Code Generation: Model-based development tools generate production-ready code, reducing manual errors and compliance issues.

What’s the Catch?

The most commonly-used model-based development tool is MATLAB Simulink, and it costs a fortunate. And if you’d like to add additional toolboxes e.g., Signal Processing Toolbox, this may incur even additional costs.

This makes it unsuitable for startups and freelancers with limited budgets.

What Happens to the Legacy Code?

Legacy Code

When transitioning to model-based development, you don’t need to discard your existing code:

  • Integration: Legacy code can be integrated with new models using wrappers or interfaces, allowing both to work together.
  • Incremental Migration: Move parts of your system to model-based development gradually, focusing on less complex components first.
  • Refactoring: Use this opportunity to clean up and improve your existing codebase.

Are Developers Going to Lose their Jobs to Model-Based Development?

It’s an enhancement, not a replacement. Model-based design enhances, not replaces, engineering expertise in software design. Software engineers focus on modeling architecture, coding platform software, and system integration, rather than manually writing code for everything.

Step-by-Step Guide: Model-Based Development in MATLAB for Embedded Systems:

Step-by-Step Guide

1. Build the Model in Simulink

Use the Simulink Editor to build your models.

Simulink Editor

  • Simulink provides a set of block libraries, organized by functionality in the Library Browser.

block libraries

  • For example, find the Pulse Generator block using the search box. Press Enter or Drag & Drop the block to add it to the model.

Generator block

  • Find the Scope block and Press Enter or Drag & Drop the block to add it to the model.

Scope block

  • Define the block parameters and connect the output port of the pulse generator with the input port of the scope.

block parametersblock parameters

2. Simulate the System

  • In the Simulink Toolstrip, on the Simulation tab, enter the value in the Stop Time field.

Simulate the System

  • To run the simulation, click RunRun.

run the simulation

3. Refine the Model

Based on simulation results, tweak the model and block parameters until desired performance is achieved.

4. Prepare for Code Generation

Select your target hardware and configure code generation settings for C/C++ code.

Go to the Apps tab in Simulink and select Embedded Coder.

Specify the part of your model you want to generate code for.

Specify the part of model

Choose between C or C++.

Choose between C or C++.

Select the processor you’re targeting:

Select the processor you're targeting

Indicate your priorities (e.g., performance):

Indicate your priorities

After answering the questions, click Generate Code

5. Generate and Deploy Code

The generated code can be deployed directly to your microcontroller:

Generate and Deploy Code

6. Test on Real Hardware

Test the generated code on hardware in real-world conditions. Make adjustments in the model until desired performance is achieved:

Test the generated code

Additional Considerations:

  • Model Configuration: Ensure your model is properly configured for code generation. This includes setting data types, solver options, and other relevant parameters.
  • Hardware Support Packages: If you’re targeting specific hardware, you might need to install and configure hardware support packages.
  • Testing and Debugging: After generating code, thoroughly test it in your target environment, debug issues, and compare outputs from the model, simulated C code, and hardware.

Real-World Examples of Model-Based Design

Iveco’s Shift Range Inhibitor System

Shift Range Inhibitor System

Iveco developed a shift range inhibitor system using Model-Based Design in Simulink. They created and tested a detailed model, automatically generating structured text code. This approach helped them with timely delivery and high-quality performance.

Ather Energy’s Intelligent Electric Scooter

Intelligent Electric Scooter

Ather Energy utilized Model-Based Design to build their Ather 450 electric scooter. By simulating various scenarios in a detailed plant model, they optimized design choices and validated control algorithms.

Ponsse’s Wood Harvester Control

Wood Harvester Control

Ponsse enhanced their Scorpion wood harvester’s control system with Model-Based Design. They generated real-time code from a control model, which streamlined debugging and integration, resulting in faster development and better quality.

Challenges and Risks in Model-Based Design

  • Integration Issues: Adapting new workflows to existing processes can be complex.
  • Learning Curve: Teams must become proficient with new tools and methodologies.
  • Model Accuracy: Ensuring that models remain accurate and consistent throughout development is crucial.

Conclusion

Model-Based Design (MBD) transforms development by using models throughout design, implementation, and testing.  Model-based design enables automatic code generation, simulations, and other capabilities, leading to fewer errors, and faster time to market. While model-based design offers major benefits, challenges like workflow integration and initial learning must be managed. Using model-based design effectively can lead to more efficient, higher-quality products.

Key references:

INCOSE: https://www.incose.org/

Was this article of help to you?
Subscribe to our newsletter. We write about developing embedded and electronic systems.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe Our Newsletter