Arduino simulink gets wrong timing with pulse generator

조회 수: 4 (최근 30일)
Francesco Brescia
Francesco Brescia 2024년 4월 27일
답변: Abhas 2024년 9월 4일
Hi, I'm facing a problem with the arduino package for simulink. I set up the model to fixed step discrete with a step size of 0.001 s. In the model I'm trying to do 2 things: blink an led each second(for 1 s the led is on and for 1 s is off); Print to the serial a number coming from a constant block of simulink. I tried setting the pulse generator pulse type time based, specifying the 2 seconds period with 50% pulse width, and also sample based, specifiyng the sample time of 0.001s (as the model) and imposing period of 2000 samples and pulse width of 1000 samples, to get the correct timing. Both ways i cannot get the led to blink with the correct timing, it seems like it is taking much longer, while i can see that the arduino is continuously printing the number on the serial, through the serial monitor of arduino ide after i deployed the code. Interestingly, if i delete the serial print block, the led blinks correctly, with both pulse types (time based and sample based). What am i not getting right? Thanks
I leave attached the simulink model
  댓글 수: 2
Monalisha
Monalisha 2024년 4월 29일
What is the reason behind setting the solver as discrete (non continuous state), with a step size of 0.001s.
Can you please try updating the solver as auto(Atomatic Solver selection).
Requirement is to toggle a LED pin every one second, you can configure the pulse generator with
Pulse Type:Sample Based
Period: 2
Pulse Width:1
Sample time :1
Francesco Brescia
Francesco Brescia 2024년 4월 30일
Thank you for your answer. I used the 0.001 s to specify the loop time of the arduino. I tried to set the solver to "type = fixed-step" and "solver = auto(automatic solver selection)". I think i need to leave type as fixed step otherwise i get an error saying to change the setting to fixed-step. I also set the pulse generator as you suggested, while the constant block outputting to serial is set with a sample time of 0.01s, to publish to serial every 0.01s. With this configuration the Led is taking approximately 1.40 s to blink, not 1. If I delete the serial communication block, I can see the led blinking with pulses of 1 second as it should, but i would like to do both blink and serial communication. Any suggestions? Thank you

댓글을 달려면 로그인하십시오.

답변 (1개)

Abhas
Abhas 2024년 9월 4일
Hi Francesco,
It sounds like you're encountering a timing issue when trying to perform both LED blinking and serial communication simultaneously on your Arduino using Simulink. This issue can possibly happen due to high frequency. Here are a few suggestions to help resolve the problem:
  1. Adjust Sample Times: First, let's tweak the sample times. Serial communication can be a bit slow, so try increasing the sample time for the Serial Transmit block. Instead of every 0.01 seconds, maybe bump it up to 0.1 or even 0.5 seconds. This should reduce the frequency of data transmission and might help with the timing. Ensure the Pulse Generator block is configured correctly for your desired blinking rate. For a 1-second on/off cycle, the sample-based configuration should have a period of 2000 samples and a pulse width of 1000 samples, matching your current setup.
  2. Consider Hardware Limitations: Remember, the Arduino board has limited processing power. Running high-frequency tasks at the same time can cause timing issues. The following documentation suggests using longer sample times for PWM and serial communication to avoid overruns: https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/pwmcommon.html#:~:text=If%20you%20use%20the%20PWM%20block%20along%20with%20the%20Serial%20Receive%20and%20Serial%20Transmit%20blocks%20in%20a%20Simulink%20model%2C%20use%20longer%20sample%20times%20to%20avoid%20
  3. Verify Pin configurations: Review the provided documentation link to ensure you are not exceeding PWM frequency limits or encountering conflicting pin configurations: https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/pwmcommon.html#:~:text=to%20avoid%20overruns.-,This%20table%20specifies%20the%20upper%20limit%20of%20the%20output%20frequency%20that,980.4,-Ports
Hope this helps!

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by