我试图让我的28BYJ-48步进电机旋转四分之一转然后停止,但我在代码中实现它时遇到了麻烦。我发现无论我的代码中的数字有多小,甚至以一种让我认为它能正常工作的方式改变它,我无法让它停止旋转。以下是代码的有用部分。
class MyFormSet(forms.BaseInlineFormSet):
def _should_delete_form(self, form):
"""Return whether or not the form should be deleted."""
if form.cleaned_data.get(forms.formsets.DELETION_FIELD_NAME):
return True # marked for delete
fields = ('name', 'question', 'amount', 'measure', 'comment')
if not any(form.cleaned_data[i] for i in fields):
return True
return False
在这里调用Stepper_Seek ......
#define STEPPER (*((volatile uint32_t *)0x4000703C))
// Move 1.8 degrees clockwise, delay is the time to wait after each step
void Stepper_CW(uint32_t delay) {
Pt = Pt->Next[clockwise]; // circular
STEPPER = Pt->Out; // step motor
if(Pos==199) { // shaft angle
Pos = 0; // reset
}
else {
Pos--; // CW
}
SysTick_Wait(delay);
}
// Move 1.8 degrees counterclockwise, delay is wait after each step
void Stepper_CCW(uint32_t delay) {
Pt = Pt->Next[counterclockwise]; // circular
STEPPER = Pt->Out; // step motor
if(Pos==0) { // shaft angle
Pos = 199; // reset
}
else {
Pos++; // CCW
}
SysTick_Wait(delay); // blind-cycle wait
}
// Initialize Stepper interface
void Stepper_Init(void) {
SYSCTL_RCGCGPIO_R |= 0x08; // 1) activate port D
SysTick_Init();
Pos = 0;
Pt = &fsm[0];
// 2) no need to unlock PD3-0
GPIO_PORTD_AMSEL_R &= ~0x0F; // 3) disable analog functionality on PD3-0
GPIO_PORTD_PCTL_R &= ~0x0000FFFF; // 4) GPIO configure PD3-0 as GPIO
GPIO_PORTD_DIR_R |= 0x0F; // 5) make PD3-0 out
GPIO_PORTD_AFSEL_R &= ~0x0F; // 6) disable alt funct on PD3-0
GPIO_PORTD_DR8R_R |= 0x0F; // enable 8 mA drive
GPIO_PORTD_DEN_R |= 0x0F; // 7) enable digital I/O on PD3-0
}
// Turn stepper motor to desired position
// (0 <= desired <= 199)
// time is the number of bus cycles to wait after each step
void Stepper_Seek(uint8_t desired, uint32_t time) {
short CWsteps;
if((CWsteps = (desired-Pos))<0) {
CWsteps+=200;
}
// CW steps is > 100
if(CWsteps > 100) {
while(desired != Pos) {
Stepper_CCW(time);
}
}
else {
while(desired != Pos) {
Stepper_CW(time);
}
}
}
我认为可能是因为它一直在重置它的位置并在重置后重新开始,但即使在注释掉线后它也无法修复。
提前谢谢大家!
答案 0 :(得分:1)
您的环绕式逻辑在Stepper_CW()
和Stepper_CCW()
中都是向后的。以前者为例。假设你试图达到198,而Pos
最初是1:
Pos
递减为0.这不等于198,因此再次调用该函数。Pos
递减到199.这不等于198,因此再次调用该函数。Pos
设置为0.这不等于198 ...... 步骤(3)之后的状态与步骤(1)之后的状态相同 - 无限循环。