[英]How to for loop a stepper motor with half steps
I have four digital pins stored in the array inPins[4]
which power on different phases of my stepper motor.我有四个数字引脚存储在数组
inPins[4]
中,它们为步进电机的不同相位供电。 The wave function was simple enough, because there is never more than one phase on.波 function 很简单,因为只有一个相位。 My wave function looks like so:
我的波 function 看起来像这样:
void waveClockwise()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(inPins[i], HIGH);
delay(delayTime);
digitalWrite(inPins[i], LOW);
}
return;
}
This ^ is called repetitively in the loop.这个 ^ 在循环中被重复调用。 In implementing a halfStep function however, I am having a hard time wrapping my head around how this could be done in for loop.
然而,在实现半步 function 时,我很难理解如何在 for 循环中完成。
For the halfStep, the sequence goes:对于 halfStep,顺序如下:
... and so on as it half steps around the phases. ......等等,因为它在阶段周围走了半步。 I got it done with the following code, but am just curious how I could do this in a loop.
我用下面的代码完成了它,但我只是好奇我怎么能在一个循环中做到这一点。 Any tips, or ideas?
任何提示或想法?
void halfStepClockwise()
{
digitalWrite(inPins[0], HIGH);
delay(delayTime);
digitalWrite(inPins[1], HIGH);
delay(delayTime);
digitalWrite(inPins[0], LOW);
delay(delayTime);
digitalWrite(inPins[2], HIGH);
delay(delayTime);
digitalWrite(inPins[1], LOW);
delay(delayTime);
digitalWrite(inPins[3], HIGH);
delay(delayTime);
digitalWrite(inPins[2], LOW);
delay(delayTime);
digitalWrite(inPins[0], HIGH);
delay(delayTime);
digitalWrite(inPins[3], LOW);
delay(delayTime);
}
Put the indices of that inPins
array and the polarities into their own arrays:将该
inPins
数组的索引和极性放入它们自己的 arrays 中:
int indices[] = { 0, 1, 0, 2, 1, 3, 2, 0, 3 };
// Assuming HIGH and LOW are defined as ints
int polarities = { HIGH, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW };
int size = sizeof(indices) / sizeof(int);
You can then:然后您可以:
for (int i = 0; i < size; i++)
{
digitalWrite(inPins[indices[i]], polarities[i]);
delay(delayTime);
}
Firstly, divide the sequence of pin handling to find some pattern:首先,划分引脚处理的顺序以找到一些模式:
void halfStepClockwise()
{
digitalWrite(inPins[0], HIGH);
delay(delayTime);
digitalWrite(inPins[1], HIGH);
delay(delayTime);
digitalWrite(inPins[0], LOW);
delay(delayTime);
digitalWrite(inPins[2], HIGH);
delay(delayTime);
digitalWrite(inPins[1], LOW);
delay(delayTime);
digitalWrite(inPins[3], HIGH);
delay(delayTime);
digitalWrite(inPins[2], LOW);
delay(delayTime);
digitalWrite(inPins[0], HIGH);
delay(delayTime);
digitalWrite(inPins[3], LOW);
delay(delayTime);
}
Except for the first block, the lines are turning pin n mod 4
high, then turning pin n-1
low.除了第一个块,这些线将引脚
n mod 4
转为高电平,然后将引脚n-1
转为低电平。
Let's code this:让我们这样编码:
void halfStepClockwise()
{
digitalWrite(inPins[0], HIGH);
delay(delayTime);
for (int i = 1; i <= 4; i++)
{
digitalWrite(inPins[i % 4], HIGH);
delay(delayTime);
digitalWrite(inPins[i - 1], LOW);
delay(delayTime);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.