MATLAB Answers

Device Driver Arduino CAN Bus Shield Simulink s-function

22 views (last 30 days)
stivi888
stivi888 on 21 Jan 2015
Commented: Lukas on 17 Jun 2020
Hi, I've tried to send a CAN Message with Arduino CAN Bus Shield. I've tried to implement the MCP Can lib (mcp_can.cpp mcp_can.h) in my Simulink Model for my Arduino uno.
I work with the device driver lib of Giampiero Campa as example. I can build the s-function without error. But download the model to arduino is not possible.
See attached message(textfile).
my s-function code is below.
Inputs
stmp (defined as array 8 x uint8);
ID (defined as uint8)
DLC (defined as uint8)
FrameMode (defined as uint8)
Libraries
# ifndef MATLAB_MEX_FILE
#define ARDUINO 100 // using wire, and twi from arduino 1.0.3
#include <Arduino.h>
#include "mcp_can.h"
#include "mcp_can_dfs.h"
#include "SPI.h"
#include "mcp_can.cpp"
#include "SPI.cpp"
MCP_CAN CAN(10);
#endif
Output:
if(xD[0] == 1)
{
#ifndef MATLAB_MEX_FILE
CAN.sendMsgBuf(ID[0], FrameMode[0], DLC[0], stmp);
#endif
}
Discrete Update
if true
if (xD[0]!=1) {
/* don't do anything for mex file generation */
# ifndef MATLAB_MEX_FILE
//CAN.begin();
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
}
# endif
/* initialization done */
xD[0]=1;
}
after compiling the s-function I've added "extern "C" " in the wrapper file and changed ...wrapper.c to ...wrapper.cpp.
I don't understand what's wrong in this code and what is the reason for this error? Can someone help me please?
Thanks for your help
Stefan Neumüller

  2 Comments

AFETTOUCHE Massinissa
AFETTOUCHE Massinissa on 6 Feb 2019
Hello,
I have a question please:
These labaries that you have used are supported by Matlab or simulink only ?
and when you write the code, you do it arduino IDE or in matlab file ??
Thank you

Sign in to comment.

Answers (4)

KDN
KDN on 26 Jan 2015
I am also working on same project but using dspic I used s-function builder but not able to generate CAN block.

  1 Comment

Quan Zhou
Quan Zhou on 19 Sep 2016
You can try to include the SPI.h AND SPI.cpp in your folder. but I also have a error message said "Error occurred while executing External Mode MEX-file 'ext_serial_win32_comm_rtt':ExtTargetPktPending() call failed while checking for target pkt"
here is my report Simulation ### Starting build procedure for model: can_interface Code Generation 1 Elapsed: 9 sec ### Generating code into build folder: C:\Users\Quan.Zhou\Desktop\CAN_S_FUN\can_interface_ert_rtw ### Generated code for 'can_interface' is up to date because no structural, parameter or code replacement library changes were found. ### Evaluating PostCodeGenCommand specified in the model ### Using toolchain: Arduino AVR v1.6.1 | gmake (64-bit Windows) ### 'C:\Users\Quan.Zhou\Desktop\CAN_S_FUN\can_interface_ert_rtw\can_interface.mk' is up to date ### Building 'can_interface': C:\PROGRA~1\MATLAB\R2015b\bin\win64\gmake -f can_interface.mk all "### Invoking postbuild tool "Binary Converter" ..." C:/MATLAB/SupportPackages/R2015b/arduino-1.6.1/hardware/tools/avr/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 ../can_interface.elf ../can_interface.eep "### Done invoking postbuild tool." "### Invoking postbuild tool "Hex Converter" ..." C:/MATLAB/SupportPackages/R2015b/arduino-1.6.1/hardware/tools/avr/bin/avr-objcopy -O ihex -R .eeprom ../can_interface.elf ../can_interface.hex "### Done invoking postbuild tool." "### Successfully generated all binary outputs." AVR Memory Usage ---------------- Device: atmega2560
Program: 23808 bytes (9.1% Full) (.text + .data + .bootloader)
Data: 1450 bytes (17.7% Full) (.data + .bss + .noinit) ### Successful completion of build procedure for model: can_interface Build process completed successfully Error occurred while executing External Mode MEX-file 'ext_serial_win32_comm_rtt': ExtTargetPktPending() call failed while checking for target pkt

Sign in to comment.


Nebojsa
Nebojsa on 22 Feb 2015
I have same problem when try to make driver for Arduino DS1302 real time block. I write code on same way and same strange error log occurs. I am using Matlab 2013b.

  0 Comments

Sign in to comment.


marie qian
marie qian on 13 Nov 2017
I have same problem when try to make driver for Arduino, anyone solved this problem?

  0 Comments

Sign in to comment.


Lukas
Lukas on 5 Dec 2019
Try using this library: https://github.com/II43/arduino_CAN

  2 Comments

Henry Julian Pineda Rueda
Henry Julian Pineda Rueda on 15 Jun 2020
Hello, hope you doing good.
I'm checking the library you cited. It's interesting but not well explained though I'm not pro on this.
I'm running the simple example, but I've gotta type a TLC code or something like that, here's the code printed in the command window:
"-dc switch
00001: %% SYSTLC: Embedded Coder TMF: ert_default_tmf MAKE: make_rtw \
TLC-DEBUG>"
Hope, you can help me buddy.
Good day.
Lukas
Lukas on 17 Jun 2020
Hey,
I know this one - you are stuck in the TLC debugger. The simple example has the TLC debugger switched ON. I suggest to switch this OFF. See the screenshot. The best is to submit this on github as an issue. Otherwise I find the documentation not so bad.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by