我正在尝试使用 PID 功能配置我的 arduino mega。arduino 示例启动一个基于“millis”功能打开和关闭的继电器。但是,我想知道是否可以在定时器调用中使用 PID,所以在 6 分钟后它会检查传感器读数。基于传感器读数和参数的激进程度;这将打开或关闭继电器。我的问题是,这可以用计时器而不是“毫秒”来完成吗?下面是 arduino 提供的示例。在此之下,是我炮制的东西。请给建议。谢谢。
#include <PID_v1.h>
#define RelayPin 6
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 100;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Input = analogRead(0);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if(millis() - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
}
这是我的代码:
#include "Wire.h"
#include "DS1307RTC.h"
#include "PID_v1.h"
#include "SPI.h"
#include "Time.h"
#include "TimeAlarms.h"
#define RELAY_ON 1
#define RELAY_OFF 0
#define Relay1 2
int analogChannel0 = 0;
double Setpoint, Input, Output;
double Kp=1, Ki=0.5, Kd=0.25; //PID Tuning Parameters
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); //PID Tuning Parameters
void setup() {
digitalWrite(Relay1, RELAY_ON);
pinMode(Relay1, OUTPUT);
Serial.begin(9600);
Alarm.timerRepeat(360, Pid);
Input = analogRead(analogChannel0);
myPID.SetMode(AUTOMATIC); //turn the PID on
Setpoint = 7.0;
}
void loop() {
Alarm.delay(0);
}
void Pid()
{
Input = analogRead(analogChannel0);
myPID.SetTunings(Kp, Ki, Kd);
myPID.Compute();
////FROM HERE I DONT KNOW WHAT TO DO
}