Integration of a C++ Library in Simulink and deployment to the Arduino UNO using automated code generation

35 visualizaciones (últimos 30 días)
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 these files 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 the model, 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! :)

Respuestas (1)

Aravind
Aravind el 21 de Nov. de 2024 a las 5:25
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
  3. Example of implementing the ADXL343 sensor: https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/adxl343-read-acceleration-io-device-builder.html
  4. Video tutorial on creating custom sensor blocks for Arduino in Simulink: https://www.mathworks.com/videos/how-to-build-custom-sensor-blocks-for-arduino-in-simulink-1704432957396.html
I hope this helps resolve your issue!

Categorías

Más información sobre Modeling en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by