I am officially lost with S-Function builder

8 views (last 30 days)
I was trying to make a simulink S-Function to connect Arduino and it Sensor Adafruit INA219 to simulink, I needed to add the library for the INA219. However it is been frustrating to understand how to do it using matlab. I tried many different sources to understand.
I would appreciate if someone could explain me step by step this procedure.
The wrapper, and the errors:
/*
* 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
#define ARDUINO 100
#include <math.h>
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_INA219.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 INA219_Outputs_wrapper(uint32_T *busvoltage,
uint32_T *shuntvoltage,
uint32_T *loadvoltage,
uint32_T *current_mA,
uint32_T *power_mW,
const real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
/* This sample sets the output equal to the input
y0[0] = u0[0];
For complex signals use: y0[0].re = u0[0].re;
y0[0].im = u0[0].im;
y1[0].re = u1[0].re;
y1[0].im = u1[0].im;
*/
#ifdef MATLAB_MEX_FILE
/*
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
*/
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getPower_mW();
loadvoltage = busvoltage + (shuntvoltage / 1000);
#endif
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
}
/*
* Updates function
*
*/
void INA219_Update_wrapper(uint32_T *busvoltage,
uint32_T *shuntvoltage,
uint32_T *loadvoltage,
uint32_T *current_mA,
uint32_T *power_mW,
real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Update_Changes_BEGIN --- EDIT HERE TO _END */
/*
* Code example
* xD[0] = u0[0];
*/
#ifdef MATLAB_MEX_FILE
Adafruit_INA219 ina219;
ina219.begin();
ina219.setCalibration_16V_400mA();
#endif
/* %%%-SFUNWIZ_wrapper_Update_Changes_END --- EDIT HERE TO _BEGIN */
}
S-function Builder 1 Clear
10:21 PM Elapsed: 0.42 sec
INA219_wrapper.c
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(65): error C2065: 'ina219': undeclared identifier
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(65): error C2224: left of '.getShuntVoltage_mV' must have struct/union type
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(66): error C2065: 'ina219': undeclared identifier
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(66): error C2224: left of '.getBusVoltage_V' must have struct/union type
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(67): error C2065: 'ina219': undeclared identifier
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(67): error C2224: left of '.getCurrent_mA' must have struct/union type
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(68): error C2065: 'ina219': undeclared identifier
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(68): error C2224: left of '.getPower_mW' must have struct/union type
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(69): error C2296: '/': illegal, left operand has type 'uint32_T *'
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(93): error C2065: 'Adafruit_INA219': undeclared identifier
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(93): error C2146: syntax error: missing ';' before identifier 'ina219'
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(93): error C2065: 'ina219': undeclared identifier
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(94): error C2065: 'ina219': undeclared identifier
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(94): error C2224: left of '.begin' must have struct/union type
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(95): error C2065: 'ina219': undeclared identifier
D:\1 - Main\UWF\Office of Undergraduate Research (OUR)\SURP\SURP 2018\MATLAB\INA219\INA219_wrapper.c(95): error C2224: left of '.setCalibration_16V_400mA' must have struct/union type
Component:S-function Builder | Category:Build error

Answers (1)

Madhu Govindarajan
Madhu Govindarajan on 6 Aug 2018
The S-function builder approach is quite old and a little difficult, however if you are really set on using that here is a video that I found quite helpful - https://www.youtube.com/watch?v=_OLctOFjjYQ
The recommended approach by MathWorks for the same is to use the System Objects workflow. Here is a detailed documentation on how to build custom support using this workflow - https://www.mathworks.com/help/supportpkg/arduino/device-driver-blocks.html
I have also found this video to be helpful for the recommended workflow - https://www.mathworks.com/videos/creating-a-simulink-block-using-matlab-code-98734.html

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!