Integration of a C++ Library in Simulink and deploy it to the Arduino UNO with automated Code generation

24 ビュー (過去 30 日間)
Johannes
Johannes 2024 年 11 月 15 日 13:15
回答済み: Aravind 2024 年 11 月 29 日 6:17
Dear Community, I have the following problem:
I need to read out a VL53L1X distance sensor for a project using an Arduino UNO. The programming of the Arduino should work via Simulink with automatic code generation.
I have tried the whole thing using a s-function builder block. I include the following libraries in it like this:
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#ifndef MATLAB_MEX_FILE
#include <Arduino.h>
#include <Wire.h>
#include <twi.h>
#include <WString.h>
#include <VL53L1X.h>
#endif
There is an existing code that works in the Arduino IDE to read out the sensor. The header files are written in C++. I currently have a s function block with the following Code:
/* Includes_BEGIN */
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#ifndef MATLAB_MEX_FILE
#include <Arduino.h>
#include <Wire.h>
#include <twi.h>
#include <WString.h>
#include <VL53L1X.h>
#endif
/* Includes_END */
/* Externs_BEGIN */
#ifndef MATLAB_MEX_FILE
VL53L1X sensor; // Instanz global deklarieren
#endif
/* Externs_END */
void Sensor_Init_Start_wrapper(void)
{
/* Start_BEGIN */
/*
* Custom Start code goes here.
*/
/* Start_END */
}
void Sensor_Init_Outputs_wrapper(boolean_T *Error)
{
/* Output_BEGIN */
#ifndef MATLAB_MEX_FILE
VL53L1X sensor; // Erstellen Sie eine Instanz der VL53L1X-Klasse
if (!sensor.init()) { // Aufruf über das Objekt
*Error = true; // Fehler setzen
return;
}
sensor.setDistanceMode(VL53L1X::Short); // Den Modus korrekt setzen
sensor.startContinuous(20); // Starten mit der gewünschten Verzögerung
#endif
/* Output_END */
}
void Sensor_Init_Terminate_wrapper(void)
{
/* Terminate_BEGIN */
/*
* Custom Terminate code goes here.
*/
/* Terminate_END */
}
This s-function block also compiles without any problems.
Now the question is, how can I use the generated mexw64 file and how can I integrate this file generated by the s function into my Simulink model?
If I use the whole model outside the s-function builder block and try to compile and deploy it, I get error messages depending on the compiler used.
If I use the C compiler, I get a lot of error messages regarding the libraries themselves (namemangling) and if I use the C++ compiler, I get the error message that C++ is not supported for the Arduino hardware.
Thank you very much for your help! :)

回答 (1 件)

Aravind
Aravind 2024 年 11 月 29 日 6:17
Hello @Johannes,
It looks like you are trying to use an "S-function" block to integrate third-party C/C++ source files for reading a sensor in a code deployment on an Arduino UNO board.
However, using S-functions for this purpose is not the recommended approach, as it might lead to errors or unexpected outcomes. Instead, it is advisable to use the "IO Device Builder" app, which is part of the Simulink Support Package for Arduino. More information about the "IO Device Builder" app can be found here: https://www.mathworks.com/help/simulink/io-device-builder-arduino.html.
The "IO Device Builder" app allows you to incorporate Arduino's C++ libraries into MATLAB System objects, which can then be used to create a Simulink block. This block can be deployed to the Arduino Uno with code generation.
Below are some resources to help you get started with the "IO Device Builder" app:
  1. Getting started with the "IO Device Builder" app: https://www.mathworks.com/help/simulink/supportpkg/arduino_ug/io-device-builder.html
  2. Example of implementing the DHT11 sensor: https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/dht11-relative-humidity-temperature-io-device-builder.html
I hope this helps resolve your issue!

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by