-
Notifications
You must be signed in to change notification settings - Fork 0
/
smdiff.c
56 lines (56 loc) · 1.19 KB
/
smdiff.c
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
#include <stdio.h> //printf
#include <stdlib.h> //malloc
#include <math.h> //pow
#include "smdiff.h"
//#include "controller.h" //sign
float sign(float n)
{
if(n < 0) return -1;
else if(n > 0) return 1;
else return 0;
}
float potencia(int n)
{
return n/(n+1.0);
}
void init_DiffOrdN(DiffOrdN *self, int n, float *gains)
{
int i;
self->orden = n;
self->diffs = (Diff*) malloc(n * 5 * sizeof(float));
for(i=0; i < n + 1; i++)
{
Diff tmp;
tmp.orden = i;
printf("\nIntroduce la ganancia de orden %d: ",n-i);
scanf("%f",&tmp.gain);
tmp.integral = 0;
tmp.preintval = 0;
self->diffs[i] = tmp;
}
printf("Derivador de orden %d creado\n",n);
}
int update_DiffOrdN(DiffOrdN *self, float val, double dt)
{
int i;
for(i = self->orden; i >= 0; i--)
{
float zm1;
if(i == 0) zm1 = 0;
else zm1 = self->diffs[i-1].integral;
val = f_Diff(&self->diffs[i], val, zm1);
self->diffs[i].integral += (self->diffs[i].tmp + val)/2 * dt;
self->diffs[i].tmp = val;
}
return 0;
}
float f_Diff(Diff *self, float val, float zm1)
{
float v,e,pot,abse,sig;
e = self->integral - val;
abse = fabs(e);
sig = sign(e);
pot = pow(abse,potencia(self->orden));
v = -sig*self->gain * pot;
return v + zm1;
}