Simulink stepper motor control

10 次查看(过去 30 天)
Barabás Zsombor
Barabás Zsombor 2019-12-4
回答: MULI 2024-11-11
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.

回答(1 个)

MULI
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
}
  • 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.
For more detailed guidance on generating interrupt service routines (ISRs), you can refer to the below MathWorks documentation:

类别

Help CenterFile Exchange 中查找有关 Run on Target Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by