|
以前学的是信号处理,完全没接触过单片机,现在接到一个关于arduino温度控制的项目,控制tec元器件,温度从0度升到100度,然后再从100降到0.每次升温或降温都是10度。误差要求是+-3%。看了一下pid控制,试着写了下局部的升温或降温程序,不知道对不对,求大神指点。我的思路是用PT100测温,arduino输出两路PWM用来控制tec元件加热或降温,PID的输入是测得的温度,输出直接赋值给一路PWM,通过实时温度和设定温度的差值来判断是来加热或降温。
电路硬件方面:
1.温度测量pt100用了一个adafruit max31865电路 已测试过可以正常读取温度 https://learn.adafruit.com/adafruit-max31865-rtd-pt100-amplifier/overview
2.两路PWM输出分别是arduino的pin口5和6
请教大神,如果以后做出了完整的从0升温到100然后降温到0的程序,是不是每次升温或降温都得调节PID的参数?如果可以用自整定,应该怎么办?
代码如下
[kenrobot_code]
#include <Adafruit_MAX31865.h>
#include <stdlib.h>
#include <PID_v1.h>
float set_temp=0 ;
float delta_temp=0;
float temp_sensor=0;
String inString= "";
int pwm=0;
//pins defined
//const int fan_pin= ;
const int pwm_H1= 5;
const int pwm_H2= 6;
//MAX31865 defined
Adafruit_MAX31865 max = Adafruit_MAX31865(10, 11, 12, 13);
#define RREF 430.0
//PID
double Setpoint, Input, Output;
double kp = 10,ki = 0,kd =0;
PID myPID(&Input, &Output, &Setpoint,kp,ki,kd, DIRECT);
void setup() {
Serial.begin( 9600 );
/* MAX31865 initialize. */
max.begin(MAX31865_4WIRE);
//initialize the variables we're linked to
Setpoint =set_temp;
Input=temp_sensor;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop() {
temp_sensor=0;
//sensor temperature read
for(int i=0; i<10; ++i)
{
float temperature =max.temperature(100, RREF);
temp_sensor+=temperature;
}
temp_sensor=temp_sensor/10;
while (Serial.available() > 0) {
int inChar = Serial.read();
inString += (char)inChar;
set_temp = inString.toFloat();
delay(1000);
}
inString = "";
Serial.println(temp_sensor);
Serial.println(set_temp);
//Serial.println(delta_temp);
//PID
Setpoint =set_temp;
Input=temp_sensor;
delta_temp=temp_sensor-set_temp;
myPID.Compute( );
//pwm=map(Output,0, 1023, 0, 255);
pwm=Output;
//heat up or cool down
//cool down
if(delta_temp>0)
{
// analogWrite(fan_pin,pwm);
analogWrite(pwm_H2, pwm);
digitalWrite(pwm_H1,LOW);
}
//heat up
else if(delta_temp<0)
{
// analogWrite(fan_pin, 0);
analogWrite(pwm_H1, pwm);
digitalWrite(pwm_H2,LOW);
}
}[/kenrobot_code]
|
-
-
|