Using 'SIMULINK Support Package for Arduino Hardware' like 'LabVIEW'

5 次查看(过去 30 天)
Hello,
I am working on implementing LabVIEW-like functionality using the <SIMULINK Support Package for Arduino Hardware> and uploading it to an Arduino.
My goal is to implement hard constraints in Simulink (as I understand that Simulink's default behavior is soft constraint).
Here's my understanding of the concepts:
Hard constraint: In a 1kHz loop, if a loop started at time 't ms' exceeds 1ms, it stops processing the current sensor value (t ms) and moves to the next sensor value at 't+1 ms'.
Soft constraint: If the loop exceeds 1ms, it completes processing the current loop(t ms) and skips the next cycle(t+1 ms), moving to the loop with value at 't+2 ms'.
In LabVIEW, I understand that the `Deadline` command allows for implementing hard constraints.(Link: https://www.ni.com/docs/en-US/bundle/labview-api-ref/page/structures/timed-loop.html)
I tried to achieve this using `tic` and `toc` in the following code, but it failed to upload on the Arduino board
%% matlab code %%
function [num, y] = fcn(a,b)
persistent t
if isempty(t)
t = 0;
end
t = t+1;
num = t;
startTime = tic;
A = rand(a,b);
y = toc(startTime);
end
%%
Does anyone know a solution to this problem?
Thank you in advance.

回答(1 个)

UDAYA PEDDIRAJU
UDAYA PEDDIRAJU 2024-8-26
Hi 순호 권,
To implement hard constraints in Simulink for an Arduino using the Simulink Support Package, you can follow these steps:
Use Rate Transition Blocks: Ensure data consistency and timing by using Rate Transition blocks for data transfer between different rates.
Configure Solver Settings:
  • Set your model to use a fixed-step solver for deterministic execution.
  • Choose a step size that corresponds to your desired loop frequency (e.g., 1ms for 1kHz).
Use a Real-Time Task:
  • Create a Function-Call Subsystem or a Triggered Subsystem to execute tasks at a specified rate.
  • If available, use a Hardware Interrupt block to trigger execution based on external events.
Monitor Execution Time:
  • Implement a mechanism to measure execution time within the loop, similar to "tic" and 'toc' in MATLAB.
  • Compare execution time against your 1ms deadline, and implement logic to skip processing if the deadline is exceeded.
Generate and Deploy Code:
  • Use Simulink Coder to generate optimized code for real-time performance.
  • Deploy the generated code to the Arduino using the Simulink Support Package.
Example Implementation:
Create a Function-Call Subsystem:
  • Use a Clock block to measure elapsed time.
  • Compare elapsed time with the 1ms deadline using a Compare To Constant block.
  • Use a Switch block to decide whether to process current data or skip to the next cycle.
Model Configuration:
  • Set the solver to a fixed-step size of 0.001 seconds (1ms).
  • Use a Timer block (if available) to ensure precise execution intervals.
Deploy to Arduino:
  • Use the "Deploy to Hardware" feature to upload your model to the Arduino.
  1 个评论
순호 권
순호 권 2024-8-26
Thank you for your response.
I have a few questions regarding your answer, which I'd like to clarify:
Use a Real-Time Task:
Q1. In the context of using a Real-Time Task, what exactly is the Hardware Interrupt block that you mentioned?
Generate and Deploy Code:
Q2. When you referred to using Simulink Coder in the "Generate and Deploy Code" section, were you suggesting that I should use Embedded Coder to upload the program to the Arduino?
I look forward to your response.
Thank you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Development Computer Setup 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by