Custom battery on Simulink

Hi, I would like to use a custom battery model on my CM4SM project, but i dont know where is the right place to put it. I tried just put it on simulink and use a CM Write Dict block to replace the values from CM but that doesnt give the right result, it just replaces the value on IPG Control but is not used on simulation.

Note that the HV1 voltage drops with current (as expected), but the HV2 is kept constant (changing only with SOC) as is Motor Voltage too.
How can I succesfully replace the battery model from CM on Simulink?
Any help is appreciated!

Hello !

Thank you for your question!

In the PT.Control interface in Simulink, there is no reasonable way to just replace the battery model without changing the overall architecture.
If you create a Simulink plugin with your current model, the program will be able to just replace the battery and calculate the rest of the powertrain as usual.

Try to proceed as follows:

  • open Simulink library browser and search for the block „Create Plug-in Model“
  • select the model class „PTBattery“ and give your model a name
  • copy your model in the now opened interface and chose your desired in and output values
  • click on the tab „Simulink coder“ and then on the button „build“
  • after that, you should be able to simulate using your battery model when opening the CM GUI via Simulink (generic → Open CarMaker GUI)
  • select your battery model via CM GUI → Parameters → Car → Powertrain → Power Supply → Battery model

Also, for more condensed information on the Simulink Plug-Ins, please see chapter 8.3 “Integration of Simulink models with CarMaker’s Model Plug-in” in the Programmer’s Guide

I hope I could help you! Please let me know if this worked or if further help is needed!

Hi, thanks for the help. I’ve tried to create the plug-in model, but I’m having trouble compilanting. When I select simulink coder>Build i get these warnings/errors

=== Build (Elapsed: 1:02 min) ===
    CarMaker Plug-in Model Target
    ### Generating code and artifacts to 'Model specific' folder structure
    ### Generating code into build folder: C:\Users\israe\Documents\Tupa\PT_Control\FCM_Projects\src_cm4sl\The_Icy_Touch_CarMaker_rtw
    Warning:This Simscape(TM) model contains both dynamic and algebraic equations even after model simplification. To improve performance and robustness for this class of model an implicit solver is recommended.
    ### Generating code for Physical Networks associated with solver block 'The_Icy_Touch/Solver Configuration' ...
    ### Invoking Target Language Compiler on The_Icy_Touch.rtw
    ### Using System Target File: C:\IPG\carmaker\win64-9.1.1\Matlab\R2020a\CarMaker.tlc
    ### Loading TLC function libraries
    ### Initial pass through model to cache user defined code
    ### Caching model source code
    ### Generating TLC interface API.
        ### Writing source file The_Icy_Touch_capi.c
        ### Writing header file The_Icy_Touch_capi.h
        ### Writing header file The_Icy_Touch_types.h
        ### Writing header file The_Icy_Touch.h
        ### Writing header file rtwtypes.h
        ### Writing header file builtin_typeid_types.h
        ### Writing header file multiword_types.h
        ### Writing source file The_Icy_Touch.c
        ### Writing header file The_Icy_Touch_private.h
        ### Writing header file rtmodel.h
    ### TLC code generation complete.
    ### Processing Template Makefile: C:\IPG\carmaker\win64-9.1.1\Matlab\R2020a\CarMaker_unix-win64.tmf
    ### C:\Users\israe\Documents\Tupa\PT_Control\FCM_Projects\src_cm4sl\The_Icy_Touch_CarMaker_rtw\ which is generated from C:\IPG\carmaker\win64-9.1.1\Matlab\R2020a\CarMaker_unix-win64.tmf is up to date
    ### Building The_Icy_Touch: .\The_Icy_Touch.bat
    C:\Users\israe\Documents\Tupa\PT_Control\FCM_Projects\src_cm4sl\The_Icy_Touch_CarMaker_rtw>set MATLAB=C:\Program Files\MATLAB\R2020a 
     CC       The_Icy_Touch_4f5f8f11_1_ds.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_zc.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_duy.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_y.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_dp_r.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_dp_l.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_dxy.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_obs_il.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_obs_all.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_obs_act.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_obs_exp.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_log.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_tdxf_p.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_dxf.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_mode.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_f.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_passert.o
     CC       The_Icy_Touch_4f5f8f11_1_ds_assert.o
     CC       The_Icy_Touch_4f5f8f11_1.o
     CC       The_Icy_Touch_4f5f8f11_1_gateway.o
     CC       pm_printf.o
     AR x     rt_backsubrr_dbl.o
     AR x     rt_forwardsubrr_dbl.o
     AR x     rt_lu_real.o
     AR x     rt_matrixlib_dbl.o
     CC       The_Icy_Touch_capi.o
     CC       The_Icy_Touch.o
     CC       The_Icy_Touch_wrap.o
     MKLIB    ../libThe_Icy_Touch_win64.a
     MKENTRY  PTBattery The_Icy_Touch 1
     HOOKUP   The_Icy_Touch
    Executing 'make' in the project source directory...
    make -C ".." V=0
    make[1]: Entering directory `/c/Users/israe/Documents/Tupa/PT_Control/FCM_Projects/src_cm4sl'
    make[1]: *** No targets specified and no makefile found.  Stop.
    make[1]: Leaving directory `/c/Users/israe/Documents/Tupa/PT_Control/FCM_Projects/src_cm4sl'
    make: *** [ModelMainLib] Error 2
    Error(s) encountered while building "The_Icy_Touch"

I have no idea on how to fix it. Sounds like the makefile is corrupted.
I have attached the plug-in files.


If indeed there is something with the Makefile (which the error message indicates), you can try to switch it out with or compare to our template version. You can find it in \IPG\carmaker\win64-10.0\Templates\Car\src_cm4sl\Makefile

To get some simplicity and narrow the troubleshooting down, I would also suggest trying to create a new Project (CM Main GUI->File->Project Folder->Create Project; make sure to check the box “Sources/Build environment”). So you will have a new src_cm4sl folder with all necessary and unaltered files. Open this folder in Matlab, run cmenv.m to initialize and set the correct paths (the error is also indicating something could be wrong with that) and try to create, compile and integrate a Plug-In (the Plug-In can be empty for this). If this works, which I hope, you can work from there and compare your file and folder architecture. If not, please write again.

PS: Great model name!! :ice_cube: