Arduino からSPI経由SDcard を使ってloggerを作りたい

SDmodule を使いたいです。
SPI WritRead block の help にSDcardへの store の記載がありますが、data form が分かりません。
Arduino IDE の #include <SC.h> のライブラリを使えればいいのだと思いますが、
Simulink のモデル中にどのように取り込めばいいのか教えていただけるとありがたいです。

Answers (1)

Toshinobu Shintai
Toshinobu Shintai on 31 Jan 2023
Edited: Toshinobu Shintai on 31 Jan 2023

0 votes

一般的には、以下で説明されている MATLAB System ブロックを使ったワークフローを推奨しています。

4 Comments

ご連絡ありがとうございます。
返答が遅くて申し訳ありません。
正直、理解力がだいぶ不足していて、全く進むことができていません。
このページに沿って DigitalWrite.m を作成してみているのですが、その段階で滞っています。
(記載内容を理解していないだろうからですが、2行目'&' が無効な構文になるエラー)
DigitalWrite.m
classdef DigitalWrite < matlab.System & coder.ExternalDependency
& coder.ExternalDependency ...
& matlab.system.mixin.Propagates ...
& matlab.system.mixin.CustomIcon
%
% System object template for a sink block.
%
% This template includes most, but not all, possible properties,
% attributes, and methods that you can implement for a System object in
% Simulink.
%
% NOTE: When renaming the class name Sink, the file name and
% constructor name must be updated to use the class name.
%
% Copyright 2016-2018 The MathWorks, Inc.
%#codegen
%#ok<*EMCA>
properties
% Public, tunable properties.
end
properties (Nontunable)
% Public, non-tunable properties.
end
properties (Access = private)
% Pre-computed constants.
end
methods
% Constructor
function obj = DigitalWrite(varargin)
% Support name-value pair arguments when constructing the object.
setProperties(obj,nargin,varargin{:});
end
end
methods (Access=protected)
function setupImpl(obj) %#ok<MANU>
if isempty(coder.target)
% Place simulation setup code here
else
% Call C-function implementing device initialization
% coder.cinclude('digitalio_arduino.h');
coder.ceval('digitalIOSetup', 9, u);
end
end
end
methods(Access=protected)
...
function stepImpl(~,u)
if isempty(coder.target)
% Place simulation output code here
else
% Call C-function implementing device output
coder.ceval('writeDigitalPin', 9, u);
end
end
...
end
methods(Access=protected)
...
function releaseImpl(obj) %#ok<MANU>
if isempty(coder.target)
% Place simulation termination code here
else
% Call C-function implementing device termination
%coder.ceval('sink_terminate');
end
end
...
end
methods (Access=protected)
%% Define input properties
function num = getNumInputsImpl(~)
num = 1;
end
function num = getNumOutputsImpl(~)
num = 0;
end
function flag = isInputSizeMutableImpl(~,~)
flag = false;
end
function flag = isInputComplexityMutableImpl(~,~)
flag = false;
end
function validateInputsImpl(~, u)
if isempty(coder.target)
% Run input validation only in Simulation
validateattributes(u,{'double'},{'scalar'},'','u');
end
end
function icon = getIconImpl(~)
% Define a string as the icon for the System block in Simulink.
icon = 'Sink';
end
end
methods (Static, Access=protected)
function simMode = getSimulateUsingImpl(~)
simMode = 'Interpreted execution';
end
function isVisible = showSimulateUsingImpl
isVisible = false;
end
end
methods (Static)
function name = getDescriptiveName()
name = 'Sink';
end
function b = isSupportedContext(context)
b = context.isCodeGenTarget('rtw');
end
function updateBuildInfo(buildInfo, context)
if context.isCodeGenTarget('rtw')
% Update buildInfo
srcDir = fullfile(fileparts(mfilename('fullpath')),'src');
includeDir = fullfile(fileparts(mfilename('fullpath')),'include');
addIncludePaths(buildInfo,includeDir);
% Use the following API's to add include files, sources and linker flags
%addIncludeFiles(buildInfo,'source.h',includeDir);
%addSourceFiles(buildInfo,'source.c',srcDir);
%addLinkFlags(buildInfo,{'-lSource'});
%addLinkObjects(buildInfo,'sourcelib.a',srcDir);
%addCompileFlags(buildInfo,{'-D_DEBUG=1'});
%addDefines(buildInfo,'MY_DEFINE_1')
addSourceFiles(buildInfo,'digitalio_arduino.cpp', srcDir);
end
end
...
end
以下のように、1行目の末尾に「...」が不足しているのではないでしょうか。
ありがとうございます。
エラーマークが消えました。
続けてみます。
根本的に skill 不足で解決できないエラーだらけです。
***
Recommended Skills
To develop a device driver block for Arduino hardware boards, familiarity with the following skills is recommended:
***
↑他に s-function を使う方法が公開されていて試していますが、
ビルド時、以下のログが出ました。
・・・正常に作成されたが結局 failed の原因がわかりません。
Arduino IDE のサンプルスケッチ(.ino)をそのまま書き込む方法があると助かるのですが・・・
### 'SD_logger.c' は正常に作成されました
### 'SD_logger_wrapper.c' は正常に作成されました
### 'SD_logger.tlc' は正常に作成されました
Compile of 'SD_logger.c' failed.
<SD_logger_wrapper.c>
/*
* 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 */
#include <math.h>
#include <SPI.h>
#include <SD.h>
/* %%%-SFUNWIZ_wrapper_includes_Changes_END --- EDIT HERE TO _BEGIN */
#define u_width 1
/*
* Create external references here.
*
*/
/* %%%-SFUNWIZ_wrapper_externs_Changes_BEGIN --- EDIT HERE TO _END */
/* extern double func(double a); */
const int chipSelect =53;
/* %%%-SFUNWIZ_wrapper_externs_Changes_END --- EDIT HERE TO _BEGIN */
/*
* Start function
*
*/
void SD_logger_Start_wrapper(real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Start_Changes_BEGIN --- EDIT HERE TO _END */
/*
* カスタム開始コードをここに配置します。
// void setup(){
// Open serial communications and wait for port to open:
Serial.begin(31250);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
//see if the dcard is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed,or not present");
//don't do anythingmore:
while(1);
}
Serial.println("card initialized.");
*/
/* %%%-SFUNWIZ_wrapper_Start_Changes_END --- EDIT HERE TO _BEGIN */
}
/*
* Output function
*
*/
void SD_logger_Outputs_wrapper(const uint16_T *Time,
const real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
/* このサンプルは、出力を入力と等しく設定します
//make a stringf for assembling the data to log:
String dataString = "";
// read inputs:
for (uint16 Time){
}
// open the file. note that only one file can be open at a time,
// lso you have to vlose this one file can be opening another.
File dataFile = SD.open("datalog.txt,FILE_WRITE);
// if the file is available, wtrite to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file is'nt open, pop up an error;
else{
Serial.println("error opening datalog.txt")
}
*/
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
}
/*
* Updates function
*
*/
void SD_logger_Update_wrapper(const uint16_T *Time,
real_T *xD)
{
/* %%%-SFUNWIZ_wrapper_Update_Changes_BEGIN --- EDIT HERE TO _END */
/* %%%-SFUNWIZ_wrapper_Update_Changes_END --- EDIT HERE TO _BEGIN */
}

Sign in to comment.

Products

Release

R2022a

Asked:

on 17 Jan 2023

Commented:

on 15 Feb 2023

Community Treasure Hunt

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

Start Hunting!