Arduino SPI writeRead function unavailable in Simulink

4 次查看(过去 30 天)
I am trying to execute multiple SPI writes in one solver step of a simulink program. So I enclosed all of my SPI write calls in one functions and I initialize the relevant objects before running the simulink code. However, at execution simulink complains about 'writeRead' function being undefined when it tries to execute the transfers. I have included the two program segments that I am using.
% init.m
global arduinodev; global extdac;
arduinodev = arduino('COM4', 'Due', 'Libraries', 'SPI');
extdac = device(arduinodev, 'SPIChipSelectPin', 'D52', 'SPIMode', 1, 'bitorder', 'msbfirst') ;
% MATLAB Function
function dacWrite(extdac, values)
%DACWRITE Writes the 6 values received to the Helmholtz Cage Controller
%DAC
%
l = length(values);
if ( l ~= 6 )
return
end
% code n
coden = 0b10000000 ;
for dac = 1:6
cmd = bitor(coden, dac) ;
val = uint16(values(dac));
if ( val > 0x0fff )
val = 0x0fff ;
end
low = bitshift(uint8(bitand(val,0x00ff)),4);
high = uint8(bitshift(uint16(val),-4,'uint16'));
%fprintf("0x%x 0x%x 0x%x\n", cmd, high, low);
dacout = [cmd, high, low];
writeRead(extdac, dacout);
end %for
dacout = [ 0b11000001 0x00 0x00 ] ; % loadn
writeRead(extdac, dacout);
end
Here is the image of the Simulink Code Block that I am using:
And here is the error I am getting:
Undefined function or variable 'writeRead'.
Function 'MATLAB Function' (#23.590.615), line 21, column 9:
"writeRead(extdac, dacout)"

采纳的回答

Walter Roberson
Walter Roberson 2020-1-9
编辑:Walter Roberson 2020-1-13
In the case where you are using either no acceleration or else only the first step of acceleration, then you can use coder.extrinsic() to have Simulink talk to the MATLAB engine to run the function.
However, there is currently no way to generate deployable code for the writeRead() call. Notice that https://www.mathworks.com/help/supportpkg/arduinoio/ref/writeread.html does not have any section for "extended capabilities", so there is no code generation available for it.
  1 个评论
Sunip Mukherjee
Sunip Mukherjee 2020-1-13
Thanks! I ended up writing my own Arduino server code that reads 12 bytes over serial and programs the DAC with those, while the Simulink code uses generic serial communication to send the 12 bytes to the Arduino at each cycle.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Run on Target Hardware 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by