Write an sFunction from an arduino code with simulink builder sfunction?

2 views (last 30 days)
hello evrybody;
I'm working in a project to connect an arduino with CANbus shield with SPI communication in simulink in order to receive canBUS frames in simulink; I've build an sFunction for the code arduino below:
#include <SPI.h>
#include "IDs.h"
#include <mcp_can.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
//Serial.println(" Init CAN BUS Shield again");
delay(100);
}
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
Serial.print(millis()); // enregistrement du temps
Serial.print(',');
Serial.print(canId, DEC);
Serial.print(',');
// recopie du message CAN
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i], DEC);
if (i<len-1)
{
Serial.print(',');
}
}
Serial.println();
//delay(500);
}
}
like you see in this code, I have two methods : getCanId() and readMsgBuf(); who permits to me to receive the data, so it's my outputs. in my sfunction I have declared my libraries like this :
#ifndef MATLAB_MEX_FILE
#define ARDUINO 100
#include <Arduino.h>
#include "SPI.h"
#include "SPI.cpp"
#include "mcp_can.h"
#include "mcp_can.cpp"
MCP_CAN CAN(pin9);
endif
and in the discrete update pane, I declared my initialization function :
#ifndef MATLAB_MEX_FILE
if (xD[0]!=1){
while (CAN_OK != CAN.begin(CAN_500KBPS))
{
delay(100);
}
}
endif
and in the output pane I declared my output function like this :
#ifndef MATLAB_MEX_FILE
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
ID[0]=canId; //first output
// recopie du message CAN
for(int i = 0; i<len; i++) // print the data
{
frame[0]=buf[i]; // second output
}
}
#endif
when i build this sfunction it says that the build is done succesfully but when I run the programm it generate for me errors like in this :
../sfunc_sim_arduino_wrapper.cpp:23:13: error: 'pin9' was not declared in this scope
MCP_CAN CAN(pin9);
^
../sfunc_sim_arduino_wrapper.cpp: In function 'void sfunc_sim_arduino_Outputs_wrapper(real_T*, real_T*, const real_T*, const uint8_T*, int_T)':
../sfunc_sim_arduino_wrapper.cpp:80:8: error: expected unqualified-id before string constant
extern "C" void sfunc_sim_arduino_Update_wrapper(real_T *ID,
^
../sfunc_sim_arduino_wrapper.cpp:104:1: error: expected '}' at end of input
}
^
gmake: *** [sfunc_sim_arduino_wrapper.o] Error 1
D:\semestre4_stageM2_evry\SIMULINK\arduno_simulnk_ert_rtw>echo The make command returned an error of 2
The make command returned an error of 2
D:\semestre4_stageM2_evry\SIMULINK\arduno_simulnk_ert_rtw>An_error_occurred_during_the_call_to_make
'An_error_occurred_during_the_call_to_make' n'est pas reconnu en tant que commande interne
ou externe, un programme exécutable ou un fichier de commandes.
### Creating HTML report file arduno_simulnk_codegen_rpt.html
### Build procedure for model: 'arduno_simulnk' aborted due to an error.
Error(s) encountered while building "arduno_simulnk":
### Failed to generate all binary outputs.
so if someone have worked in this already, I'dlike some suggestions for my code if there is any imperfections please; any comments are welcome.

Answers (0)

Categories

Find more on Simulink Supported 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!