#include <AccelStepper.h>
// 定义电机控制用常量
const int enablePin = 8; // 使能控制引脚
const int xdirPin = 5; // x方向控制引脚
const int xstepPin = 2; // x步进控制引脚
const int ydirPin = 6; // y方向控制引脚
const int ystepPin = 3; // y步进控制引脚
const int zdirPin = 7; // z方向控制引脚
const int zstepPin = 4; // z步进控制引脚
AccelStepper stepper1(1,xstepPin,xdirPin);//建立步进电机对象1
AccelStepper stepper2(1,ystepPin,ydirPin);//建立步进电机对象2
AccelStepper stepper3(1,zstepPin,zdirPin);//建立步进电机对象3
char mode;
long data,xp,yp,zp,xa;
float xmax,ymax,xjia,yjia;
void setup()
{
stepper1.setMaxSpeed(8000.0);
stepper1.setAcceleration(16000.0);
stepper2.setMaxSpeed(8000.0);
stepper2.setAcceleration(16000.0);
Serial.begin(1000000);
Serial.println(F("Please input motor command:"));
}
void loop()
{
if (Serial.available()) { // 检查串口缓存是否有数据等待传输
mode = Serial.read();
data = Serial.parseInt();
switch (mode){
case 'x':
xmax=float(data);
Serial.print("最大速度设置为");
Serial.println(xmax);
stepper1.setMaxSpeed(xmax);
break;
case 'a':
xjia=float(data);
Serial.print("最大加速度设置为");
Serial.println(xjia);
stepper1.setAcceleration(xjia);
break;
case 'q':
xa=data;
if(xa!=xp){
stepper1.stop();
xp=xa;
}
Serial.print("位置为");
Serial.println(xp);
break;
default:
Serial.println("error");
break;
}
}
stepper1.moveTo(xp);
xp+=1;
stepper1.moveTo(xp);
xp-=1;
stepper1.run();
if (stepper1.currentPosition()!=stepper1.targetPosition()){
zp+=1;
if (zp>200){
Serial.print("最大速度");
Serial.print(stepper1.maxSpeed());
Serial.print("当前速度");
Serial.println(stepper1.speed());
Serial.print("当前加速度");
Serial.println(xjia);
zp=0;
}
}
else if (stepper1.currentPosition()==xp){
}
stepper2.run();
}
// 定义电机控制用常量
const int enablePin = 8; // 使能控制引脚
const int xdirPin = 5; // x方向控制引脚
const int xstepPin = 2; // x步进控制引脚
const int ydirPin = 6; // y方向控制引脚
const int ystepPin = 3; // y步进控制引脚
const int zdirPin = 7; // z方向控制引脚
const int zstepPin = 4; // z步进控制引脚
AccelStepper stepper1(1,xstepPin,xdirPin);//建立步进电机对象1
AccelStepper stepper2(1,ystepPin,ydirPin);//建立步进电机对象2
AccelStepper stepper3(1,zstepPin,zdirPin);//建立步进电机对象3
char mode;
long data,xp,yp,zp,xa;
float xmax,ymax,xjia,yjia;
void setup()
{
stepper1.setMaxSpeed(8000.0);
stepper1.setAcceleration(16000.0);
stepper2.setMaxSpeed(8000.0);
stepper2.setAcceleration(16000.0);
Serial.begin(1000000);
Serial.println(F("Please input motor command:"));
}
void loop()
{
if (Serial.available()) { // 检查串口缓存是否有数据等待传输
mode = Serial.read();
data = Serial.parseInt();
switch (mode){
case 'x':
xmax=float(data);
Serial.print("最大速度设置为");
Serial.println(xmax);
stepper1.setMaxSpeed(xmax);
break;
case 'a':
xjia=float(data);
Serial.print("最大加速度设置为");
Serial.println(xjia);
stepper1.setAcceleration(xjia);
break;
case 'q':
xa=data;
if(xa!=xp){
stepper1.stop();
xp=xa;
}
Serial.print("位置为");
Serial.println(xp);
break;
default:
Serial.println("error");
break;
}
}
stepper1.moveTo(xp);
xp+=1;
stepper1.moveTo(xp);
xp-=1;
stepper1.run();
if (stepper1.currentPosition()!=stepper1.targetPosition()){
zp+=1;
if (zp>200){
Serial.print("最大速度");
Serial.print(stepper1.maxSpeed());
Serial.print("当前速度");
Serial.println(stepper1.speed());
Serial.print("当前加速度");
Serial.println(xjia);
zp=0;
}
}
else if (stepper1.currentPosition()==xp){
}
stepper2.run();
}