MATLAB Answers

How to fix <<Error evaluating registered method 'InitializeConditions' of MATLAB S-Function 'msfun_ard​uino_analo​gread' in 'Mechatron​icsFYP/ARD​UINO/Ardui​no Analog Read7'>>?

25 views (last 30 days)
david tekra
david tekra on 29 Apr 2019
Answered: JACKO on 22 Mar 2020
hello! i am trying to recreate a project usign an arduino mega 2560, an adxl377 and 5 flex sensors to move a 3d arm. i am having issues with the following error :
Error evaluating registered method 'InitializeConditions' of MATLAB S-Function 'msfun_arduino_analogread' in 'MechatronicsFYP/ARDUINO/Arduino Analog Read7'. The following is the MATLAB call stack (file names and line numbers) that produced this error:
['C:\Users\bujie\Desktop\ArduinoIO\simulink\getSetupBlockUserData.m'] [23]
['C:\Users\bujie\Desktop\ArduinoIO\simulink\msfun_arduino_analogread.m'] [71]
Cannot find Arduino IO Setup Block for 'Temporary arduino variable: Arduino1' at top level of model
i have already installed the Arduino IO Package and run MATLAB in administrator mode. i am very new to matlab, i'm running the r2015a version.
the code of msfun_arduino_analogread is :
function msfun_arduino_analogread(block)
% Level-2 M-File S-Functions, Copyright 2014, The MathWorks, Inc.
% instance variables
myArduino = [];
myPin = 0;
%% ---------------------------------------------------------
function setup(block)
% Register the number of ports.
block.NumInputPorts = 0;
block.NumOutputPorts = 1;
block.OutputPort(1).Dimensions = 1;
block.OutputPort(1).SamplingMode = 'sample';
% Set up the states
block.NumContStates = 0;
block.NumDworks = 0;
% Register the parameters.
block.NumDialogPrms = 3; % arduinoVar, arduinoPin
block.DialogPrmsTunable = {'Nontunable', 'Nontunable', 'Nontunable'};
% Set the sample time
block.SampleTimes = [block.DialogPrm(3).Data 0];
block.SetAccelRunOnTLC(false); % run block in interpreted mode even w/ Acceleration
block.SimStateCompliance = 'DefaultSimState';
% the ArduinoIO Setup block uses the Start method to initialize the arduino
% connection; by using InitConditions, we ensure that we don't access
% the variable before it is created
block.RegBlockMethod('CheckParameters', @CheckPrms); % called during update diagram
% block.RegBlockMethod('Start', @Start); % called first
block.RegBlockMethod('PostPropagationSetup', @PostPropSetup);
block.RegBlockMethod('InitializeConditions', @InitConditions); % called second
block.RegBlockMethod('Outputs', @Output); % called first in sim loop
% block.RegBlockMethod('Update', @Update); % called second in sim loop
block.RegBlockMethod('Terminate', @Terminate);
function CheckPrms(block)
validateattributes(block.DialogPrm(1).Data, {'char'}, {'nonempty'}); % Name of arduino instance
validateattributes(block.DialogPrm(2).Data, {'numeric'}, {'real', 'scalar', 'nonnegative'}); % pin
errstr=arduino.checknum(block.DialogPrm(2).Data,'analog input pin number',0:15);
if ~isempty(errstr), disp(errstr); error(errstr); end
validateattributes(block.DialogPrm(3).Data, {'numeric'}, {'real', 'scalar', 'nonzero'}); % sample time
catch %#ok<CTCH>
error('Simulink:ArduinoIO:invalidParameter', 'Invalid value for a mask parameter');
function PostPropSetup(block)
st = block.SampleTimes;
if st(1) == 0
error('The ArduinoIO library blocks can only handle discrete sample times');
function InitConditions(block)
% fprintf('%s: InitConditions\n', getfullname(block.BlockHandle));
customData = getSetupBlockUserData(bdroot(block.BlockHandle), block.DialogPrm(1).Data);
myArduino = customData('arduinoHandle');
myPin = block.DialogPrm(2).Data;
% myArduino.pinMode(myPin, 'input');
function Output(block)
% fprintf('%s: Output\n', getfullname(block.BlockHandle));
analogValue = myArduino.analogRead(myPin);
if isempty(analogValue)
analogValue = 0;
block.OutputPort(1).Data = analogValue;
function Terminate(block) %#ok<INUSD>
% The ArduinoIO Setup block handles cleanup of myArduino
help please!!!!


Sign in to comment.