-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid_control.ino
203 lines (153 loc) · 4.58 KB
/
pid_control.ino
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
#############################################################
#
# Control PID
#
# Description:
# Implementacion de un control PID en un Arduino Nano con
# parametros ajustables a traves de una GUI
#
# Author:
# Hugo Arboleas <harboleas@citedef.gob.ar>
#
#############################################################
#
# Copyright 2015 Hugo Arboleas
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
*/
//////////////////////////////
///// Definicion de pines
#define pin_mot_A 9
#define pin_mot_B 10
#define pin_pv 3
#define pin_led 13
/////////////////////////////////////
// Variables y parametros
bool pid_on = 0;
unsigned char Ts = 15; // Tiempo de muestreo en miliseg
union byte_double
{
byte data[4];
double value;
} Kp, Ki, Kd; // Ganancias del PID
union byte_int
{
byte data[2];
int value;
} sp, pv; // Set point y process variable
double e, e_1; // Error actual y anterior
double acum_e; // Acumulacion de los errores ponderados (integral)
double d_e; // Variacion del error (derivada)
double u; // Senal de control
volatile unsigned int cont_rpm;
// Serial port data
unsigned char data[16];
unsigned char i = 0;
///////////////////////////////////////
unsigned long t = 0, t_1 = 0; // para la medicion del delta t de muestreo
////////////////////////////////////////
/////////////////////////////////
// Limites de la senal de control
#define U_MAX 255
#define U_MIN -255
void setup()
{
Serial.begin(115200); // Inicializar puerto serie
pinMode(pin_mot_A, OUTPUT);
pinMode(pin_mot_B, OUTPUT);
pinMode(pin_led, OUTPUT);
pinMode(pin_pv, INPUT);
attachInterrupt(1, contar, RISING);
}
void contar()
{
cont_rpm++;
}
void pid()
{
t = millis(); // tiempo actual
if ((t - t_1) >= Ts) // Muestrea cada Ts
{
t_1 = t; // Actualiza t_1 para determinar la siguiente muestra
pv.value = 60 * (cont_rpm / 15.0) / (Ts*1e-3); // obtiene la process variable (rpm del motor)
cont_rpm = 0;
e = sp.value - pv.value; // error actual
if (pid_on)
{
acum_e = acum_e + Ki.value * e; // Acumula los errores ponderados
if (acum_e > U_MAX)
acum_e = U_MAX;
else if (acum_e < U_MIN)
acum_e = U_MIN;
d_e = e - e_1; // Delta error
e_1 = e; // Actualiza el error anterior para la sig. muestra
// Senal de control
u = Kp.value * e + acum_e + Kd.value * d_e;
if (u > U_MAX)
u = U_MAX;
else if (u < U_MIN)
u = U_MIN;
if (u >= 0)
{
analogWrite(pin_mot_B, 0);
analogWrite(pin_mot_A, u);
}
else
{
analogWrite(pin_mot_A, 0);
analogWrite(pin_mot_B, -u);
}
}
else // PID Off
{
analogWrite(pin_mot_A, 0);
analogWrite(pin_mot_B, 0);
e_1 = e; // Actualiza error anterior
acum_e = 0;
d_e = 0;
}
Serial.write(pv.data, 2); // Envia a la GUI el valor de pv
}
}
void loop()
{
pid();
}
void serialEvent()
{
// Obtiene los parametros de la GUI
data[i] = Serial.read();
i++;
if(i == 16)
{
i = 0;
pid_on = (bool) data[0];
Ts = data[1];
for(int j=0; j < 4; j++)
Kp.data[j] = data[2+j];
for(int j=0; j < 4; j++)
Ki.data[j] = data[6+j];
for(int j=0; j < 4; j++)
Kd.data[j] = data[10+j];
for(int j=0; j < 2; j++)
sp.data[j] = data[14+j];
Ki.value = Ki.value * (Ts * 1e-3);
Kd.value = Kd.value / (Ts * 1e-3);
digitalWrite(pin_led, pid_on);
}
}
/* vim: set ts=8 sw=4 tw=0 et :*/