How to access simulink arduino instance inside matlab function from stateflow?

11 views (last 30 days)
Sorry this is a wierd question as i don't really even know what the problem is and im a total beginner and feel totally lost.
I have a basic simulink model that uses stateflow.
This simulink model targets my MKR 1000 arduino.
I want to use the following function:
writeDigitalPin(a,'6',1);
But it takes as it's first parameter 'a', which should be an instance of the arduino.
Simulink already has an instance of arduino, so how do i access this from my matlab model?
i have tried using the code below:
function blnk
a = arduino();
writeDigitalPin(a,'6',1);
pause(1);
writeDigitalPin(a,'6',0);
pause(1);
writeDigitalPin(a,'6',1);
pause(1);
writeDigitalPin(a,'6',0);
end
But these gives me a huuuge number of errors:
### Starting build procedure for model: roverClosed_states_hw
Code Generation 7
Elapsed: 1 sec
Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs.
Component:MATLAB Function | Category:Coder error
The 'dynamicprops' class does not support code generation.
P-code function 'base.p' produced an error.
Component:MATLAB Function | Category:Coder error
Parsing failed.
Function 'master_base.m' (#140.21.32), line 2, column 21:
"master_base"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Parsing failed.
Function 'master.m' (#141.37325.37419), line 784, column 9:
"function obj = master()"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Parsing failed.
Function 'arduino.m' (#142.5603.6662), line 127, column 9:
"function obj = arduino(varargin)"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Parsing failed.
Function 'blnk' (#138.19.26), line 3, column 5:
"arduino"
Launch diagnostic report.
Component:Stateflow | Category:Coder error
Error while generating code for chart StateLogic.
Component:Stateflow | Category:Coder error
### Build procedure for model: 'roverClosed_states_hw' aborted due to an error.
Error while generating code for chart StateLogic.
Component:Simulink | Category:Block diagram error
Error in port widths or dimensions. Output port 1 of 'roverClosed_states_hw/StateLogic/angCtrl' is a one dimensional vector with 1 elements.
Component:Simulink | Category:Block diagram error
How do i fix this!?

Answers (1)

Altaïr
Altaïr on 11 Feb 2025
The issue might be related to code generation when using arduino and writeDigitalPin within the MATLAB Function block. In release R2019b, an alternative approach is to output signals from the Stateflow chart to Simulink and utilize the Digital Input and Digital Output blocks to read and write the logical values of the digital pins.
If certain blocks are reused multiple times, consider creating a function using the Simulink Function block.
The documentation for release R2020a (and R2024b) indicates the capability to use Arduino functions within the MATLAB Function block.
I have attached an example model, arduino_matlab_codegen, demonstrating the use of Arduino-related MATLAB functions in a MATLAB Function block. The model can also be found in the latest documentation linked below:
Please note that the R2020a documentation mentions that both MATLAB and Simulink support packages for Arduino are required.
For further details, refer to the documentation specific to the release being used, which can be accessed by running the doc command in the MATLAB command window.

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!