-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregulation.cpp
More file actions
164 lines (131 loc) · 3.96 KB
/
Copy pathregulation.cpp
File metadata and controls
164 lines (131 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* regulation.c
*
* Created on: 18 sept. 2019
* Author: Valentin
*/
#include "temperature.h"
#include "regulation.h"
#include "display.h"
#include <arduino.h>
#include "src/hardware/PID_v1.h"
int time_palier1_extern;
int time_palier2_extern;
int pente1_extern;
int pente2_extern;
int temp_1_extern;
int temp_2_extern;
unsigned long start_time;
int step;
#define ambiant_temp 30
#define relais_pin 5
//Specify the links and initial tuning parameters
double Setpoint, Input, Output;
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int WindowSize = 2000;
unsigned long windowStartTime;
void init_pid(void) {
myPID.SetOutputLimits(0, WindowSize);
windowStartTime = millis();
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void burn_regulation(void)
{
static int temp;
static uint32_t run_time, next_time = 0, time_step_two, time_step_four;
int temp_consigne;
char tmp[20];
if (!timeout_1s_flag)
return;
temp = int(read_temp());
//temp = temp + 10;
run_time = millis() - start_time;
refresh_temp_burn(temp, int(run_time/60000), step);
switch(step) {
case step_one :
if (pente1_extern)
temp_consigne = (int)((float)(temp_1_extern) * (float)(run_time)/((float)(pente1_extern)*60000)) + ambiant_temp;
if (temp_consigne > temp_1_extern)
temp_consigne = temp_1_extern;
Setpoint = (double)(temp_consigne);
Input = (double)(temp);
myPID.Compute();
if ((temp > temp_1_extern) || (pente1_extern == 0))
{
time_step_two = run_time + (time_palier1_extern * 60000); //en ms
step = step_two;
change_page(step);
refresh_temp_burn(temp, int(run_time/60000), step);
Serial.print(F("step Two !!"));
Serial.println(time_step_two);
}
break;
case step_two :
temp_consigne = temp_1_extern;
if (run_time > time_step_two)
{
step = step_three;
Serial.println(F("step Three !!"));
change_page(step);
refresh_temp_burn(temp, int(run_time/60000), step);
}
break;
case step_three :
if (pente2_extern)
temp_consigne = (int)((float)(temp_2_extern-temp_1_extern) * (float)(run_time)/((float)(pente2_extern)*60000)) + temp_1_extern;
if (temp_consigne > temp_2_extern)
temp_consigne = temp_2_extern;
if ((temp > temp_2_extern) || (pente2_extern == 0))
{
time_step_four = run_time + (time_palier2_extern * 60000); //en ms
step = step_four;
change_page(step);
refresh_temp_burn(temp, int(run_time/60000), step);
Serial.print(F("step four !!"));
Serial.println(time_step_four);
}
break;
case step_four :
temp_consigne = temp_2_extern;
if (run_time > time_step_four)
{
step = step_five;
change_page(step);
refresh_temp_burn(temp, int(run_time/60000), step);
Serial.println(F("step Five !!"));
}
break;
case step_five :
burn_stop();
flamme_display(false);
break;
default:
break;
}
Serial.print(F("time "));
Serial.print(run_time);
Serial.print(F(";T :"));
Serial.print(temp);
Serial.print(F(";Tc :"));
Serial.println(temp_consigne);
timeout_1s_flag = false;
}
void compute_pid(void) {
static bool on_off_heat;
if ((millis() - windowStartTime) > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if (Output < (millis() - windowStartTime))
on_off_heat = 0;
else
on_off_heat = 1;
digitalWrite(relais_pin, on_off_heat);
flamme_display(on_off_heat);
}
void burn_stop(void)
{
digitalWrite(relais_pin, false);
}