teemuatlut / TMC2130Stepper

Arduino library for Trinamic TMC2130 Stepper driver
MIT License
159 stars 50 forks source link

How to create function to control motor speed by RPMs? #64

Open conn250 opened 5 years ago

conn250 commented 5 years ago

I'm pretty new to stepper motors in general so I probably just don't know the basic principles behind this yet. So currently I just know about the way of controlling the motor via the basic commands to set the STEP_PIN HIGH or LOW with some amount of delay between them. It's not a very easy way to precisely set the speed of the motor. How can I translate those commands into something easier to use like simply inputting an RPM?

digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(10);

I think it would be similar to the step() function in the Arduino Stepper.h library, but that library writes commands to the individual stepper coils. Ideas?

teemuatlut commented 5 years ago

If your stepper motor has 200 steps per revolution and you're using 16 microstep setting, you therefore need to send 200 * 16 = 3200 step pulses to make a full rotation. 1rpm = 3200 pulses per minute = 53 pulses per second. Time between pulses = 1s / 53 = 0.019s. Note that a step is only executed on the rising edge of the pulse.

For more precise timing of the pulses look into Arduino timers and interrupts.

You can also use AccelStepper to control the pulses.