极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16182|回复: 5

新人求教,求指点。

[复制链接]
发表于 2016-1-6 09:49:47 | 显示全部楼层 |阅读模式
/********************************************************
* PID RelayOutput Example
* Same as basic example, except that this time, the output
* is going to a digital pin which (we presume) is controlling
* a relay.  the pid is designed to Output an analog value,
* but the relay can only be On/Off.
*
*   to connect them together we use "time proportioning
* control"  it's essentially a really slow version of PWM.
* first we decide on a window size (5000mS say.) we then
* set the pid to adjust its output between 0 and that window
* size.  lastly, we add some logic that translates the PID
* output into "Relay On Time" with the remainder of the
* window being "Relay Off Time"
  
PID继电器输出范例
与基本范例相同,这一次输出是一个数字引脚控制的继电器。PID被设计成
输出一个模拟值,但是继电器只有开关状态。
为了联系上两者,我们使用时间比例控制,它本质上是一个很慢的PWM。
首先我们决定一个窗口时间(比如5000ms)。
然后设置PID适应它的输出在0到窗口时间的范围。
最后我们添加一些逻辑,把PID输出转换成“继电器接通时间”和剩余的
“继电器断开时间”
********************************************************/

#include <PID_v1.h>
#define RelayPin 8
// 定义我们将要使用的变量
//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 = 2000;
unsigned long windowStartTime;
void setup()
{
  windowStartTime = millis();
  //初始化变量
  //initialize the variables we're linked to
  Setpoint = 100;
  //告诉PID在从0到窗口大小的范围内取值
  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);
  //开启PID
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(0);
  myPID.Compute();

  /************************************************
   * turn the output pin on/off based on pid output 基于PID输出,打开或关闭端口输出
   ************************************************/
  if(millis() - windowStartTime>WindowSize)
  { //time to shift the Relay Window 继电器窗口时间
    windowStartTime += WindowSize;
  }
  if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
  else digitalWrite(RelayPin,LOW);

}

这是网上的pid控制代码,其中红字部分为读取温度传感器,因为它只需要控制100度。所以用的是lm35之类的传感器。
我需要控制的300度,所以用max6675加k型热电偶
#include "max6675.h"

int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;
  
void setup() {
  Serial.begin(9600);
  // use Arduino pins
  pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
  pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
  
  Serial.println("MAX6675 test");
  // wait for MAX chip to stabilize
  delay(500);
}

void loop() {
  // basic readout test, just print the current temp
  
   Serial.print("C = ");
   Serial.println(thermocouple.readCelsius());
   Serial.print("F = ");
   Serial.println(thermocouple.readFahrenheit());

   delay(1000);
}
这是6675库文件自带的。蓝色部分是输出温度值。
那么问题来了。
我想要设置温度范围之类的时候,却不会用变量来储存6675的数据。请大侠指点。

(就是能让上面的代码能读取下面代码的热电偶的值)
回复

使用道具 举报

 楼主| 发表于 2016-1-6 10:33:26 | 显示全部楼层
我刚才又想了一下,好像想到点什么。原来我是用int来设定变量来存储热电偶的值,结果输出的全是乱码。后来用double 来设定变量储存热电偶的值就好了。

又一个问题。就是红线处的语句是不是设定加热的目标值吗?

如果我要加热到200度是不是把该处的值改为200呢?
请指教

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2016-1-6 10:47:46 | 显示全部楼层
Ateman3145 发表于 2016-1-6 10:33
我刚才又想了一下,好像想到点什么。原来我是用int来设定变量来存储热电偶的值,结果输出的全是乱码。后来用 ...

要注意:继电器是有吸合和释放时间的!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-6 16:00:43 | 显示全部楼层
hubertdong 发表于 2016-1-6 10:47
要注意:继电器是有吸合和释放时间的!

我知道。所以我准备用ssr固态继电器来控制,
关键问题是我想控制的温度是200度,可是应该在哪里改呢
是不是如下图红线处的参数呢?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2016-1-7 08:30:27 | 显示全部楼层
Ateman3145 发表于 2016-1-6 16:00
我知道。所以我准备用ssr固态继电器来控制,
关键问题是我想控制的温度是200度,可是应该在哪里改呢
是 ...

1、固态继电器分过零型和随机型,要注意;
2、我没有看你的程序,在温度控制中,设定温度值与你所使用的温度传感器有关。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-7 19:42:05 | 显示全部楼层
谢谢楼上帮助。继续顶
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 08:28 , Processed in 0.063313 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表