简体   繁体   English

Arduino Uno步进电机问题

[英]Arduino Uno Stepper Motor Issues

I am working with a stepper motor hooked up to pins 9, 10, 11, and 12 on an Arduino Uno. 我正在使用连接到Arduino Uno的引脚9、10、11和12的步进电机。 In order to rotate the stepper motor, I wrote a helper method. 为了旋转步进电机,我写了一个辅助方法。 This particular stepper motor rotates 1.8 degrees per step. 这种特殊的步进电机每步旋转1.8度。 The method is: 方法是:

void rotateStepperBy(float deg) {
  int steps = deg / 1.8;
  motor.step(steps);
}

The method works fine for small degree measures, but behaves in unexpected ways (under rotating, rotating back and forth) if I give it larger degree measures like 45 and 90. Here's an example I was trying: 该方法适用于较小程度的测量,但是如果我给它较大程度的测量(如45和90),则它的行为方式会异常(在旋转,来回旋转时)。这是我尝试的示例:

#include <Stepper.h>

Stepper motor(200, 9, 10, 11, 12);

void setup() {
  rotateStepperBy(360);
}

void loop() {
  rotateStepperBy(90);
  delay(10);
}

void rotateStepperBy(float deg) {
  int steps = deg / 1.8;
  motor.step(steps);
}

Does motor.step complete and then the rest of the program resumes or does there need to be a longer delay for bigger degree measurements to allow the motor to finish stepping? 是否完成motor.step,然后继续执行程序的其余部分,或者是否需要较长的延迟以进行更大程度的测量,以使电机完成步进?

Does motor.step complete and then the rest of the program resumes.. motor.step是否完成,然后其余程序继续。

Yes, motor.step() is a blocking function : 是的, motor.step()一个阻止函数

This function is blocking; 该功能正在阻止; that is, it will wait until the motor has finished moving to pass control to the next line in your sketch. 也就是说,它将等到电动机完成移动,以将控制权传递给草图中的下一行。

But you probably have to set the speed in setup() , for example motor.setSpeed(30); 但是您可能必须在setup()setup()速度,例如motor.setSpeed(30); .

Looking at the code for stepper it looks like step_delay stays undefined (or zero) until setSpeed() is called (ie it doesn't get a default value in the constructor..) 查看stepper代码,好像step_delay保持未定义(或为零),直到setSpeed()为止(即,在构造函数中未获取默认值。)

unsigned long step_delay; // delay between steps, in ms, based on speed

This value only changes in setSpeed() : 此值仅在setSpeed()更改:

/*
 * Sets the speed in revs per minute
 */
void Stepper::setSpeed(long whatSpeed)
{
  this->step_delay = 60L * 1000L * 1000L / this->number_of_steps / whatSpeed;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM