Skip to content

Commit 2e96049

Browse files
committed
Create supertwisting.c
Flying test program ./supertwisting [time of flight secs] [factor (0=no flying, 10=normal flying, 100=10 times "stronger")]
1 parent afb20d9 commit 2e96049

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

ardrone/supertwisting.c

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* * ATRAS | ADELANTE, Theta +|-
3+
* * IZQUIERDA | DERECHA, Phi +|-
4+
* */
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <unistd.h>
8+
#include <pthread.h>
9+
#include <math.h>
10+
#include "util/type.h"
11+
#include "util/util.h"
12+
#include "attitude/attitude.h"
13+
#include "motorboard/mot.h"
14+
#include "controller.h"
15+
#include "smdiff.h"
16+
17+
int sleep(double s)
18+
{
19+
double t0 = util_timestamp();
20+
while( util_timestamp() - t0 < s);
21+
return 0;
22+
}
23+
int main(int argc, char *argv[])
24+
{
25+
att_struct att;
26+
double t0, dt, ta;
27+
float alt, th, ph, ps; // Valores de X
28+
float thp, php, psp; // Valores de Xp
29+
// Parametros del cuadricoptero
30+
float l = 0.13, Ixx = 24.1e-3, Iyy = 23.2e-3, Izz = 45.1e-2;
31+
float b = 0.0006646195542576290, d = b*9.72;
32+
float ginvth = Ixx/l, ginvph = Iyy/l, ginvps = Izz;
33+
34+
float factor;
35+
int om1, om2, om3, om4;
36+
int ttotal;
37+
int rc,i;
38+
super_twisting st;
39+
PID pid;
40+
DiffOrdN filtroth, filtroph;
41+
float gains[] = {9.5, 8, 7.5, 5, 1.5, 1};
42+
float N = 5;
43+
printf("Iniciando derivadores numericos...\n");
44+
init_DiffOrdN(&filtroth, N, &gains[0]);
45+
init_DiffOrdN(&filtroph, N, &gains[0]);
46+
for( i = 0 ; i < N +1; i++){
47+
printf("Th: Orden %d. Ganancia: %f. Integral: %f\n",i,filtroth.diffs[i].gain,filtroth.diffs[i].integral);
48+
printf("Ph: Orden %d. Ganancia: %f. Integral: %f\n",i,filtroph.diffs[i].gain,filtroph.diffs[i].integral);
49+
}
50+
printf(" Listo!\n");
51+
printf("Iniciando el controlador...");
52+
rc = init_controller(&st, &pid);
53+
if(rc) return rc;
54+
printf("Listo!\n");
55+
if( argc < 3)
56+
{
57+
printf("Es necesario ingresar el tiempo de vuelo y el factor de multiplicacion\n");
58+
return 0;
59+
}
60+
ttotal = atoi(argv[1]); // Tiempo total de vuelo
61+
factor = atoi(argv[2])/10.0; // Factor que para pruebas de vuelo
62+
printf("Prueba de vuelo de %d segundos\n", ttotal);
63+
printf("Iniciando lector de actitud...");
64+
rc = att_Init(&att);
65+
if(rc) return rc;
66+
printf("Listo!\n");
67+
68+
printf("Iniciando la escritura en los motores...");
69+
mot_Init();
70+
printf(" Listo!\n");
71+
printf("Encendiendo los motores...");
72+
mot_Run(.01, .01, .01, .01);
73+
sleep(3.0);
74+
ta = t0 = util_timestamp();
75+
pthread_yield();
76+
printf("Iniciando ciclo de vuelo\n");
77+
while( util_timestamp() - t0 < ttotal )
78+
{
79+
dt = util_timestamp() -ta;
80+
ta = util_timestamp();
81+
rc = att_GetSample(&att);
82+
if (rc)
83+
{
84+
printf("Error al actualizar los datos de actitud\n");
85+
break;
86+
}
87+
alt = att.h;
88+
ph = att.roll;
89+
th = att.pitch;
90+
ps = 0;//att.yaw;
91+
update_DiffOrdN( &filtroth, th, dt);
92+
update_DiffOrdN( &filtroph, ph, dt);
93+
th = filtroth.diffs[(int)N].integral;
94+
ph = filtroph.diffs[(int)N].integral;
95+
thp = filtroth.diffs[(int)N-1].integral;
96+
php = filtroph.diffs[(int)N-1].integral;
97+
updateatt_u(&st, th, thp, ph, php, ps, 0);
98+
updatealt_u(&pid, alt);
99+
om1 = om2 = om3 = om4 = 0;
100+
//SOLVE EQUATIONS
101+
om1 = (int)(sqrt(-(b*st.ups + d*st.uph - d*pid.ualt - d*st.uth)/(4*b*d)*factor)/2);
102+
om2 = (int)(sqrt((d*st.uph + d*st.uth + b*st.ups + d*pid.ualt)/(4*b*d)*factor)/2);
103+
om3 = (int)(sqrt(-(d*st.uth + b*st.ups - d*pid.ualt - d*st.uph)/(4*b*d)*factor)/2);
104+
om4 = (int)(sqrt((d*pid.ualt + b*st.ups - d*st.uph - d*st.uth)/(4*b*d)*factor)/2);
105+
if( om1 < 5) om1 += 5;
106+
if( om2 < 5) om2 += 5;
107+
if( om3 < 5) om3 += 5;
108+
if( om4 < 5) om4 += 5;
109+
//OUTPUT TO MOTORS
110+
mot_Run(om1/256.0, om2/256.0, om3/256.0, om4/256.0);
111+
printf("uo= %.2f %.2f %.2f %.2f\nom= %d %d %d %d\ndt= %.4f\n", pid.ualt, st.uph, st.uth, st.ups, om1, om2, om3, om4, dt);
112+
pthread_yield();
113+
}
114+
printf("Tiempo de vuelo terminado.\n");
115+
printf("Deteniendo motores y cerrando el manejador de motores y navegacion\n");
116+
mot_Stop();
117+
mot_Close();
118+
att_Close();
119+
printf("Cerrado\n");
120+
}

0 commit comments

Comments
 (0)