I'm developing a S-function block in Matlab with the code of Arduino to read the value of a sensor; the code of the arduino works fine, but when I tried to send to the S-function Builder I receive this error:
/Prueba_wrapper.cpp:42:47: error: cannot call member function 'float LPS::readPressureMillibars()' without object float pressure = LPS::readPressureMillibars(); This is the part of the code from ccp file: * *// reads pressure in millibars (mbar)/hectopascals (hPa) float LPS::readPressureMillibars(void) { return (float)readPressureRaw() / 4096; }* * Some idea? Thanks
"../Prueba_wrapper.cpp"
../Prueba_wrapper.cpp: In function 'void Prueba_Outputs_wrapper(real_T*, const real_T*)':
../Prueba_wrapper.cpp:42:47: error: cannot call member function 'float LPS::readPressureMillibars()' without object
float pressure = LPS::readPressureMillibars();
^
../Prueba_wrapper.cpp:43:20: error: 'ps' was not declared in this scope
float altitude = ps.pressureToAltitudeMeters(pressure);
^
../Prueba_wrapper.cpp: In function 'void Prueba_Update_wrapper(real_T*, real_T*)':
../Prueba_wrapper.cpp:70:6: error: 'ps' was not declared in this scope
if (!ps.init())
^
../Prueba_wrapper.cpp:75:1: error: 'ps' was not declared in this scope
ps.enableDefault();
^
gmake: *** [Prueba_wrapper.o] Error 1
### Creating HTML report file untitled_codegen_rpt.html
### Build procedure for model: 'untitled' aborted due to an error.
Wrapper.cpp
/*
* Include Files
*
*/
#if defined(MATLAB_MEX_FILE)
#include "tmwtypes.h"
#include "simstruc_types.h"
#else
#include "rtwtypes.h"
#endif
/* %%%-SFUNWIZ_wrapper_includes_Changes_BEGIN --- EDIT HERE TO _END */
# ifndef MATLAB_MEX_FILE
# include <Arduino.h>
# include <math.h>
# include <Wire.h>
# include <LPS.h>
# include <LPS.cpp>
# endif
/* %%%-SFUNWIZ_wrapper_includes_Changes_END --- EDIT HERE TO _BEGIN */
#define y_width 1
/*
* Create external references here.
*
*/
/* %%%-SFUNWIZ_wrapper_externs_Changes_BEGIN --- EDIT HERE TO _END */
/* extern double func(double a); */
/* %%%-SFUNWIZ_wrapper_externs_Changes_END --- EDIT HERE TO _BEGIN */
/*
* Output functions
*
*/
extern "C" void Prueba_Outputs_wrapper(real_T *y0,
const real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
if (xD[0] == 1){
# ifndef MATLAB_MEX_FILE
float pressure = LPS::readPressureMillibars();
float altitude = ps.pressureToAltitudeMeters(pressure);
float temperature = ps.readTemperatureC();
Serial.print("p: ");
Serial.print(pressure);
Serial.print(" mbar\ta: ");
Serial.print(altitude);
Serial.print(" m\tt: ");
Serial.print(temperature);
Serial.println(" deg C");
# endif
}
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
}
/*
* Updates function
*
*/
extern "C" void Prueba_Update_wrapper(real_T *y0,
real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Update_Changes_BEGIN --- EDIT HERE TO _END */
if (xD[0] != 1){
# ifndef MATLAB_MEX_FILE
Serial.begin(9600);
Wire.begin();
if (!ps.init())
{
Serial.println("Failed to autodetect pressure sensor!");
while (1);
}
ps.enableDefault();
# endif
xD[0] = 1;
}
/* %%%-SFUNWIZ_wrapper_Update_Changes_END --- EDIT HERE TO _BEGIN */
}
Looks like "LPS::readPressureMillibars()" is not declared 'static' so you can't call it directly. You have to call it as an LPS object method, like "ps.readPressureMillibars()".
Of course you also don't have an LPS object named 'ps' so that would cause the same problems as everywhere else in that file.
...
/*
* Output functions
*
*/
LPS ps; // <--- Add this here to define an object called ps of type class LPS.
extern "C" void Prueba_Outputs_wrapper(real_T *y0,
const real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
if (xD[0] == 1){
# ifndef MATLAB_MEX_FILE
float pressure = ps.readPressureMillibars(); // <--- change this so it uses ps to call the function
float altitude = ps.pressureToAltitudeMeters(pressure);
...