MATLAB Answers

1

Device Driver Arduino CAN Bus Shield Simulink s-function

stivi888 님이 질문을 제출함. 21 Jan 2015
최근 활동 Lukas 님이 답변함. 5 Dec 2019 13:41
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

Do you solve this problem?
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

로그인 to comment.

답변 수: 4

Answer by 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

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

로그인 to comment.


回答者: Nebojsa
2015 年 2 月 22 日

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

로그인 to comment.


Answer by marie qian on 13 Nov 2017

I have same problem when try to make driver for Arduino, anyone solved this problem?

  댓글 수: 0

로그인 to comment.


回答者: Lukas
2019 年 12 月 5 日 13:41

Try using this library: https://github.com/II43/arduino_CAN

  댓글 수: 0

로그인 to comment.



Translated by