在Windows中试用AccelStepper库,控制步进电机,太极创客官方程序
#include <AccelStepper.h>
//http://www.taichi-maker.com/homepage/reference-index/motor-reference-index/arduino-cnc-shield/
// 定义电机控制用常量
const int xdirPin = 13; // 方向控制引脚
const int xstepPin = 12; // 步进控制引脚
const int moveSteps = 600; //运行步数
const int movebackSteps = -600;
AccelStepper stepper1(1,xstepPin,xdirPin);//建立步进电机对象
void setup() {
pinMode(xstepPin,OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(xdirPin,OUTPUT); // Arduino控制A4988方向引脚为输出模式
stepper1.setMaxSpeed(600); // 设置电机最大速度300
stepper1.setAcceleration(100); // 设置电机加速度20.0
}
void loop() {
// 控制步进电机往复运动
if ( stepper1.currentPosition() == 0 )
{
stepper1.moveTo(moveSteps);
delay(20);
}
else if ( stepper1.currentPosition() == moveSteps )
{
stepper1.moveTo(0);
delay(20);
}
stepper1.run(); // 1号电机运行
}
此程序需添加AccelStepper.h库,方法如下,
按引脚接好线,细分设为200,上电可正常运行。
但是在Ubuntu系统中出现了无法导入的问题,处理方法为:
1.在Windows系统中找到对应文件
2.拉到Ubuntu系统,以下目录下
如果不能直接拉,在终端用命令行操作
cp 命令使用 -r 参数可以将 packageA 下的所有文件拷贝到 packageB 中:
cp -r /home/packageA/* /home/cp/packageB/
之后就可以用了