目录
S形:
/*
* ********
* *
* ********
* *
* S型加速 ********
*
*/
#define accel1_time 1000 //第一段加速时间
#define accel2_time 1000 //第二段加速时间
#define accel3_time 1000 //第三段加速时间
#define accel4_time 3000 //第四段匀速时间
#define accel5_time 1000 //第五段减速时间
#define accel6_time 1000 //第六段减速时间
#define accel7_time 1000 //第七段减速时间
class Motor{
public:
unsigned long velocity; //电机转速,主要反应脉冲发射速度(频率)
int dir=2; //电机旋转方向控制IO口
int stp=3; //电机脉冲信号发射IO口
int enable=4; //电机使能,看接线方法,这里是高电平使能
long accel=1; //加速度系数
unsigned long edge;
Motor();
void stp_motor() //发送脉冲
{
digitalWrite(stp,HIGH);
delayMicroseconds(int(1.00/2*float(velocity))*1000000);
digitalWrite(stp,LOW);
delayMicroseconds(int(1.00/2*float(velocity))*1000000);
}
};
Motor::Motor()
{
for(int i=2;i<=4;i++)
pinMode(i,OUTPUT);
digitalWrite(enable,HIGH);
digitalWrite(dir,HIGH);
}
Motor mymotor; //定义自己的电机
unsigned long last_time;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //设置通信波特率,查看结果
}
void loop() {
// put your main code here, to run repeatedly:
last_time=millis();
mymotor.velocity=1;
//加加速代码,加速1秒
while((millis()-last_time)<=accel1_time)
{
mymotor.velocity=mymotor.accel*(millis()-last_time)*(millis()-last_time);
if(mymotor.velocity==0)
mymotor.velocity=1;
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
mymotor.edge=mymotor.velocity;
//匀加速代码,加速1秒
last_time=millis();
while((millis()-last_time)<=accel2_time)
{
mymotor.velocity=mymotor.edge+100*(millis()-last_time);
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
mymotor.edge=mymotor.velocity;
//减加速代码,加速1秒
last_time=millis();
while((millis()-last_time)<=accel3_time)
{
mymotor.velocity=mymotor.edge+(1000000-mymotor.accel*(1000-millis()+last_time)*(1000-millis()+last_time));
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
mymotor.edge=mymotor.velocity;
//减速代码,1秒
last_time=millis();
while((millis()-last_time)<=accel4_time)
{
mymotor.velocity=mymotor.edge;
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
mymotor.edge=mymotor.velocity;
//加减速代码,1秒
last_time=millis();
while((millis()-last_time)<=accel5_time)
{
mymotor.velocity=mymotor.edge-mymotor.accel*(millis()-last_time)*(millis()-last_time);
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
mymotor.edge=mymotor.velocity;
//匀减速代码,减速1秒
last_time=millis();
while((millis()-last_time)<=accel6_time)
{
mymotor.velocity=mymotor.edge-100*(millis()-last_time);
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
mymotor.edge=mymotor.velocity;
//匀减速代码,减速1秒
last_time=millis();
while((millis()-last_time)<=accel7_time)
{
mymotor.velocity=mymotor.accel*(accel7_time-(millis()-last_time))*(accel7_time-(millis()-last_time));
if(mymotor.velocity<=1)
mymotor.velocity=1;
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
mymotor.edge=mymotor.velocity;
}
梯形:
/*
*
* ******
* * *
* **********
* T型加速
*
*/
#define accel_time 1000
class Motor{
public:
unsigned long velocity; //电机转速,主要反应脉冲发射速度(频率)
int dir=2; //电机旋转方向控制IO口
int stp=3; //电机脉冲信号发射IO口
int enable=4; //电机使能,看接线方法,这里是高电平使能
long accel=10; //加速度系数
Motor();
void stp_motor() //发送脉冲
{
digitalWrite(stp,HIGH);
delayMicroseconds(int(1.00/2*float(velocity))*1000000);
digitalWrite(stp,LOW);
delayMicroseconds(int(1.00/2*float(velocity))*1000000);
}
};
Motor::Motor()
{
for(int i=2;i<=4;i++)
pinMode(i,OUTPUT);
digitalWrite(enable,HIGH);
digitalWrite(dir,HIGH);
}
Motor mymotor; //定义自己的电机
unsigned long last_time;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //设置通信波特率,查看结果
}
void loop() {
// put your main code here, to run repeatedly:
last_time=millis();
//匀加速代码,加速10秒
while((millis()-last_time)<=accel_time)
{
mymotor.velocity=mymotor.accel*(millis()-last_time);
if(mymotor.velocity==0)
mymotor.velocity=1;
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
//匀速代码
last_time=millis();
while((millis()-last_time)<=10000)
{
mymotor.velocity=accel_time*mymotor.accel;
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
//匀减速代码
last_time=millis();
while((millis()-last_time)<=accel_time)
{
mymotor.velocity=accel_time*mymotor.accel-mymotor.accel*(millis()-last_time);
if(mymotor.velocity==0)
mymotor.velocity=1;
mymotor.stp_motor();
Serial.println(mymotor.velocity);
}
}