-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiz.ino
69 lines (52 loc) · 1.69 KB
/
iz.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
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
float x = -47;
float y = 0.02;
float dt = 0.1;
const float a = 0.02;
const float b = 0.2;
const float c = -50.0;
const float d = 2.0;
float I = 10;
int i = 0;
int mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (int)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
}
float tomapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// Lectura del pin del potenciometro
sensorValue = analogRead(analogInPin);
// Tranformación para obtener el valor real leido del potenciometro
I = tomapfloat(sensorValue, 0, 1023, 0, 20);
// Integracion del modelo
i++;
float x_new, y_new, f, g;
f = 0.04*x*x + 5*x + 140 - y + I;
g = a*(b*x - y);
x_new = x + f * dt;
y_new = y + g * dt;
if(x_new >= 30){
x_new = c;
y_new = y_new + d;
}
x = x_new;
y = y_new;
//Serial.println(x); // Para comprobar validez del valor de la variable
// Tranformación para pin de salida
outputValue = mapfloat(x, -90, 45, 0, 255);
// Escritura al pin de salida
analogWrite(analogOutPin, outputValue);
// Espera al siguiente periodo de calculo. 5 ms
delay(5);
}