【发布时间】:2020-03-26 05:13:37
【问题描述】:
我想用 arduino 构建和编程来自角磨机的速度控制器。因此我买了一个电机速度控制器模块(它上面有一个电位器,我将用我的 arduino 的模拟输出代替)和一个红外避障模块(它也可以用于 rpm 测量)。
arduino 应该使用传感器测量轴的转速(因此我在代码中使用了中断函数)。
然后将 rpm 值传递给我计算的控制器(控制器使用 Timer1 功能运行,以保持循环时间恒定),并使用模拟输出将计算值传递给电机速度控制器。角磨机的实际速度也会显示在 I2C 显示器上。
现在我的问题是,arduino 是否能够同时运行中断和 Timer1 功能,或者它们会相互干扰?
(Controller的值已经用Winfact的Boris测试过了)
我的代码:
#include <LiquidCrystal_I2C.h>
#include <TimerOne.h>
//Regler: Siehe Haager S.147
//RPM Counter: Siehe https://forum.arduino.cc/index.php?topic=634139.0
const int X_input=1;
const int U_output=3;
int X=0;
int U=0, W=0;
const float kr=0.1;
const float Tn=0.12;
const float Tv=0.3;
const float T1=1.0e6;
const float T=0.01;
double w_k, u_k, e_k, e_k1, e_k2, u_k1, u_k2, x_k, d1, d2, c0, c1, c2;
float value = 0;
float rev = 0;
int rpm;
int oldtime = 0;
int time;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
//RPM Counter:
attachInterrupt(digitalPinToInterrupt (2), RPM_Count, RISING); //interrupt pin
//Regler:
Timer1.initialize(T*1.0e6);
Timer1.attachInterrupt(regler);
d1=(T+2*T1)/(T+T1);
d2=-T1/(T+T1);
c0=kr*(1+T/Tn+Tv/(T+T1));
c1=kr*((T*T+T*Tn-2*Tv*Tn)/(T*Tn+T1*Tn)-T/Tn-2);
c2=kr*(Tv+T1)/(T+T1);
e_k1=0.0, e_k2=0.0, u_k1=0.0, u_k2=0.0;
//Display:
lcd.begin();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("------Drehzahl------");
}
void regler(){
detachInterrupt(digitalPinToInterrupt(2));
time = millis() - oldtime;
rpm = (rev / time) * 60000;
oldtime = millis();
rev = 0;
attachInterrupt(digitalPinToInterrupt (2), RPM_Count, RISING);
w_k=rpm;
X=analogRead(X_input);
x_k=X*1000.0/1024-500;
e_k2=e_k1;
e_k1=e_k;
e_k=w_k-x_k;
u_k2=u_k1;
u_k1=u_k;
u_k=d1*u_k1 + d2*u_k2 + c0*e_k + c1*e_k1 + c2*e_k2;
U=256.0/320.0*u_k + 128;
if(U>255) U=255;
if(U<0) U=0;
analogWrite(U_output, U);
}
void RPM_Count() {
rev++;
}
void loop() {
lcd.setCursor(0,1);
lcd.print(rpm);
lcd.print(" U/min");
}
【问题讨论】:
-
这个问题可能更适合Arduino。
标签: arduino controller