本帖最后由 蓝天-彭 于 2014-9-10 11:55 编辑
源码:
int adjust1Pin = A2; //用来调节A的电位器
int adjust2Pin = A3; //用来调节B的电位器
int read1Pin = A4; //用来连接输入A传感器
int read2Pin = A5; //用来连接输入B传感器
int i1Pin = 7; //连接电机驱动板的I1接口
int i2Pin = 8; //连接电机驱动板的I2接口
int i3Pin = 12; //连接电机驱动板的I3接口
int i4Pin = 13; //连接电机驱动板的I4接口
int power1Pin = 6; //连接电机驱动板的EA接口
int power2Pin = 5; //连接电机驱动板的EB接口
boolean debug = true;
boolean writeLog = true;
double setKd1 = 30;
double setKd2 = 30;
double setKp = 4;
int offset = 80;
int delayMs = 1;
int tick = 0;
int myLog[3500];
typedef struct {
double target;
double aver;
double Kp;
double Kd;
int preError;
int power;
boolean flag;
double v;
} PID;
PID Pid1, Pid2;
void setup()
{
pinMode(i1Pin, OUTPUT); //I1和I2都是数字信号
pinMode(i2Pin, OUTPUT); //通过设置I1和I2来控制电流方向
pinMode(i3Pin, OUTPUT); //I1和I2都是数字信号
pinMode(i4Pin, OUTPUT); //通过设置I1和I2来控制电流方向
pinMode(power1Pin, OUTPUT); //按占空比方式输出的模拟信号
pinMode(power2Pin, OUTPUT); //按占空比方式输出的模拟信号
Serial.begin(9600); //设置波特率
TCCR0B = 0x01; // Timer 0: PWM 5 & 6 @ 16 kHz
TCCR1B = 0x01; // Timer 1: PWM 9 & 10 @ 32 kHz
TCCR2B = 0x01; // Timer 2: PWM 3 & 11 @ 32 kHz
Pid1.Kp = setKp;
Pid1.preError = 0;
Pid1.Kd = setKd1;
Pid1.power = 0;
Pid1.flag = true;
Pid1.target = 300;
Pid1.aver = 0;
Pid1.v = 0;
Pid2.Kp = setKp;
Pid2.preError = 0;
Pid2.Kd = setKd2;
Pid2.power = 0;
Pid2.flag = true;
Pid2.target = 300;
Pid2.aver = 0;
Pid2.v = 0;
tick = 0;
}
int tick2 = 0;
//boolean rotateFlag = true;
void loop()
{
//=======第一组电位器和传感器========
int readValue1 = 0;
for(int i = 0; i < 4; i++) readValue1 += analogRead(read1Pin);
readValue1 >>= 2;
//readValue1 += (Pid1.flag ? 1 : -1) * Pid1.power / 17;
int adjustValue1 = analogRead(adjust1Pin); //410 analogRead(adjust1Pin);
Pid1.aver = Pid1.aver * 0.9995 + readValue1 * 0.0005;
Pid1.target = Pid1.target + (Pid1.target - Pid1.aver) / 100.0;
Pid1.target = max(0, max(adjustValue1 - offset, Pid1.target));
Pid1.target = min(755, min(adjustValue1 + offset, Pid1.target));
//=======第二组电位器和传感器=======
int readValue2 = 0;
for(int i = 0; i < 4; i++) readValue2 += analogRead(read2Pin);
readValue2 >>= 2;
//readValue2 += (Pid2.flag ? 1 : -1) * Pid2.power / 6;
int adjustValue2 = analogRead(adjust2Pin); //240 analogRead(adjust2Pin);
Pid2.aver = Pid2.aver * 0.9995 + readValue2 * 0.0005;
Pid2.target = Pid2.target + (Pid2.target - Pid2.aver) / 1000.0;
Pid2.target = max(0, max(adjustValue2 - offset, Pid2.target));
Pid2.target = min(755, min(adjustValue2 + offset, Pid2.target));
//Calculate power values
double v, error;
error = readValue1 - Pid1.target;
v = error - Pid1.preError;
Pid1.v = (Pid1.v * 6 + v) / 7;
Pid1.power = (int)error * Pid1.Kp + Pid1.v * Pid1.Kd;
Pid1.flag = Pid1.power > 0;
Pid1.power = abs(Pid1.power);
if(Pid1.power>255) Pid1.power = 255;
Pid1.preError = error;
error = readValue2 - Pid2.target;
v = error - Pid2.preError;
Pid2.v = (Pid2.v * 6 + v) / 7;
Pid2.power = (int)error * Pid2.Kp + Pid2.v * Pid2.Kd;
Pid2.flag = Pid2.power < 0;
Pid2.power = abs(Pid2.power);
if(Pid2.power>255) Pid2.power = 255;
Pid2.preError = error;
//Write PMW to control the floa
digitalWrite(i1Pin, Pid1.flag);
digitalWrite(i2Pin, !Pid1.flag);
analogWrite(power1Pin, Pid1.power);
digitalWrite(i3Pin, Pid2.flag);
digitalWrite(i4Pin, !Pid2.flag);
analogWrite(power2Pin, Pid2.power);
// Serial.println(adjustValue1);
// Serial.println(adjustValue2);
//Serial.println(readValue1);
//Serial.println(readValue2);
//delay(32000);
}
最后几行是利用串口查看电位器和霍尔传感器的读数。可以取消注释来查看,注意实际实验时要注释掉这几行,不然有延时,浮子是不稳定的
|