- This method provides precise timing for motor control and reduces dependency on Simulink's scheduling.
- During the initialization phase of the S-function (e.g., in the `mdlStart` function), configure the timer interrupt.
- This setup allows the motor control to be managed independently by the interrupt.
Simulink stepper motor control
3 ビュー (過去 30 日間)
古いコメントを表示
I have written an S-function for my university project to control a stepper motor using simulink. I needed an S-function because it needs to do other things as well and also needs to be upladed to the Arduino so I can't use the model based examples Simulink provides. My question is when I run the simulation in external mode I need to set the sample time to a very low number (0.001 s) or the motor won't turn at the requested RPM. It seems that the S-function only runs when a sample is taken because if I set the sample time to 1 s the motor only steps every other second doesn't matter what speed it is set to. Is it how it should work? I would like to think that the code should run on the Arduino countinously the sample time shouldn't matter. Also it wouldn't be a problem if setting the sample time so low didn't slow down Simulink's clock.
0 件のコメント
回答 (1 件)
MULI
2024 年 11 月 11 日
Hi Zsombor,
To achieve continuous motor control independent of Simulink's sample time, you can use hardware interrupts or timers on the Arduino.
You can set up a timer interrupt on the Arduino to handle motor stepping.
For example, use the `TimerOne` library to configure a timer interrupt:
#include <TimerOne.h>
void setup() {
Timer1.initialize(1000); // Set timer to trigger every 1000 microseconds
Timer1.attachInterrupt(stepMotor); // Attach the ISR
}
void stepMotor() {
// Logic to step the motor
}
For more detailed guidance on generating interrupt service routines (ISRs), you can refer to the below MathWorks documentation:
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Peripherals についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!