arduino控制步进电机/电缸/电推杆接线+控制【5】PWM代码(1)
https://blog.csdn.net/RickyWasYoung/article/details/132482643?spm=1001.2014.3001.5501
想要使电机的 时间-位移关系 满足 三角函数 ,需要调整 脉冲步数 与 delay时长。
并且
但是后面Arduino程序需要修改的就越多
比如上一篇文章内UN=20时, 需要添加/修改20处语句:
for (int x = 0; x < A; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(B);//Microseconds是微秒
digitalWrite(STEPPIN, LOW);
delayMicroseconds(B);//Microseconds是微秒
这样太麻烦了
因此写了个自动化语句系统
在上一篇文章的matlab代码运行之后运行以下代码
%% 2.自动化语句生成代码(1)正向转
clc
Result_bc_str=num2str(Result_bc(1));
Result_mc_str=num2str(Result_mc(1));
%% 正向转
for iii=1:length(Result_bc)/2
%记录步数
s11=' for (int x = 0; x <';
s21=num2str(Result_mc(iii));
s31='; x ++) {';
s1 = strcat(s11,s21,s31);
%记录delay时长
s12=' delayMicroseconds(';
s22=num2str(Result_bc(iii));
s32=');';
s2 = strcat(s12,s22,s32);
%合成语句
disp([s1 newline ' digitalWrite(STEPPIN, HIGH);' newline s2 newline ' digitalWrite(STEPPIN, LOW);' newline s2 newline ' }'])
end
disp([' delay(1000); //暂停1秒' newline])
然后在Matlab的命令行窗口内自动生成语句
//3.
Serial.println("Forward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, HIGH);//方向引脚高电位,正转
// 正向转
的后面即可
%% 2.自动化语句生成代码(2)逆向转
clc
Result_bc_str=num2str(Result_bc(1));
Result_mc_str=num2str(Result_mc(1));
%% 正向转
for iii=length(Result_bc)/2+1:length(Result_bc)
%记录步数
s11=' for (int x = 0; x <';
s21=num2str(Result_mc(iii));
s31='; x ++) {';
s1 = strcat(s11,s21,s31);
%记录delay时长
s12=' delayMicroseconds(';
s22=num2str(Result_bc(iii));
s32=');';
s2 = strcat(s12,s22,s32);
%合成语句
disp([s1 newline ' digitalWrite(STEPPIN, HIGH);' newline s2 newline ' digitalWrite(STEPPIN, LOW);' newline s2 newline ' }'])
end
disp([' delay(1000); //暂停1秒' newline])
#define STEPPIN 9 //脉冲位为9
#define DIRPIN 8 //方向位为8
//#define bushu 300 //循环时的脉冲步数
void setup() {
pinMode(STEPPIN, OUTPUT);
pinMode(DIRPIN, OUTPUT);
Serial.begin(9600);
//不论电机初始位置如何,使电机超量程运动,以达到复位效果
//1.正转1001步
Serial.println("Forward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, HIGH);//方向引脚高电位,正转
// 正向转(1001步)
for (int x = 0; x < 1001; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(500);//Microseconds是微秒
digitalWrite(STEPPIN, LOW);
delayMicroseconds(500);//Microseconds是微秒
}
Serial.println("Forward Ends");//串口监视器输出引号内容
delay(1000); // 暂停1秒
//2.逆转1001步
Serial.println("Backward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, LOW);//方向引脚低电位,逆转
// 反向转(bushu脉冲)
for (int x = 0; x < 1001; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(500);//Microseconds是微秒
digitalWrite(STEPPIN, LOW);
delayMicroseconds(500);//Microseconds是微秒
}
Serial.println("Backward Ends");//串口监视器输出引号内容
delay(1000); //暂停1秒
//3.
Serial.println("Forward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, HIGH);//方向引脚高电位,正转
// 正向转,下面插入MATLAB正向转的语句
//4.
Serial.println("Backward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, LOW);//方向引脚低电位,逆转
// 逆向转,下面插入MATLAB逆向转的语句
}
//循环
void loop() {
}
首先复制arduino语句,然后运行matlab代码1、2,再复制命令行窗口语句,粘贴到arduino语句的相应位置,然后上传即可
arduino控制步进电机接线+控制【6】视频1
效果还可以,比较丝滑
arduino控制步进电机接线+控制【6】视频2
周期为2秒
#define STEPPIN 9 //脉冲位为9
#define DIRPIN 8 //方向位为8
//#define bushu 300 //循环时的脉冲步数
void setup() {
pinMode(STEPPIN, OUTPUT);
pinMode(DIRPIN, OUTPUT);
Serial.begin(9600);
//不论电机初始位置如何,使电机超量程运动,以达到复位效果
//1.正转1001步
Serial.println("Forward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, HIGH);//方向引脚高电位,正转
// 正向转(1001步)
for (int x = 0; x < 1001; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(500);//Microseconds是微秒
digitalWrite(STEPPIN, LOW);
delayMicroseconds(500);//Microseconds是微秒
}
Serial.println("Forward Ends");//串口监视器输出引号内容
delay(1000); // 暂停1秒
//2.逆转1001步
Serial.println("Backward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, LOW);//方向引脚低电位,逆转
// 反向转(bushu脉冲)
for (int x = 0; x < 1001; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(500);//Microseconds是微秒
digitalWrite(STEPPIN, LOW);
delayMicroseconds(500);//Microseconds是微秒
}
Serial.println("Backward Ends");//串口监视器输出引号内容
delay(1000); //暂停1秒
//3.
Serial.println("Forward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, HIGH);//方向引脚高电位,正转
// 正向转,下面插入MATLAB正向转的语句
for (int x = 0; x <25; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(4000);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(4000);
}
for (int x = 0; x <72; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(1389);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(1389);
}
for (int x = 0; x <111; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(901);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(901);
}
for (int x = 0; x <140; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(714);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(714);
}
for (int x = 0; x <155; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(645);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(645);
}
for (int x = 0; x <154; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(649);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(649);
}
for (int x = 0; x <139; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(719);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(719);
}
for (int x = 0; x <110; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(909);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(909);
}
for (int x = 0; x <70; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(1429);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(1429);
}
for (int x = 0; x <24; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(4167);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(4167);
}
delay(1000); //暂停1秒
//4.
Serial.println("Backward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, LOW);//方向引脚低电位,逆转
// 逆向转,下面插入MATLAB逆向转的语句
for (int x = 0; x <25; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(4000);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(4000);
}
for (int x = 0; x <72; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(1389);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(1389);
}
for (int x = 0; x <111; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(901);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(901);
}
for (int x = 0; x <140; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(714);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(714);
}
for (int x = 0; x <155; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(645);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(645);
}
for (int x = 0; x <154; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(649);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(649);
}
for (int x = 0; x <139; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(719);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(719);
}
for (int x = 0; x <110; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(909);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(909);
}
for (int x = 0; x <70; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(1429);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(1429);
}
for (int x = 0; x <24; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(4167);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(4167);
}
delay(1000); //暂停1秒
}
//循环
void loop() {
//3.
Serial.println("Forward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, HIGH);//方向引脚高电位,正转
// 正向转,下面插入MATLAB正向转的语句
for (int x = 0; x <25; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(4000);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(4000);
}
for (int x = 0; x <72; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(1389);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(1389);
}
for (int x = 0; x <111; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(901);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(901);
}
for (int x = 0; x <140; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(714);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(714);
}
for (int x = 0; x <155; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(645);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(645);
}
for (int x = 0; x <154; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(649);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(649);
}
for (int x = 0; x <139; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(719);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(719);
}
for (int x = 0; x <110; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(909);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(909);
}
for (int x = 0; x <70; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(1429);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(1429);
}
for (int x = 0; x <24; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(4167);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(4167);
}
delay(1000); //暂停1秒
//4.
Serial.println("Backward Begins");//串口监视器输出引号内容
digitalWrite(DIRPIN, LOW);//方向引脚低电位,逆转
// 逆向转,下面插入MATLAB逆向转的语句
for (int x = 0; x <25; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(4000);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(4000);
}
for (int x = 0; x <72; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(1389);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(1389);
}
for (int x = 0; x <111; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(901);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(901);
}
for (int x = 0; x <140; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(714);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(714);
}
for (int x = 0; x <155; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(645);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(645);
}
for (int x = 0; x <154; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(649);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(649);
}
for (int x = 0; x <139; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(719);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(719);
}
for (int x = 0; x <110; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(909);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(909);
}
for (int x = 0; x <70; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(1429);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(1429);
}
for (int x = 0; x <24; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(4167);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(4167);
}
delay(1000); //暂停1秒
}