angle3

Dependencies:   LSM9DS1 TB5649

Committer:
gr66
Date:
Tue May 26 12:04:42 2020 +0000
Revision:
11:9d83bb9a611c
Parent:
10:2a1abbd2c84c
Child:
12:ab387ced85ab
V1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gr66 2:952d41c26b43 1 // test avec RTOS
gr66 11:9d83bb9a611c 2 #define viti 1 // 1 montage viti ; 0 montage sur table
gr66 0:a2296270a125 3 #include "mbed.h"
gr66 0:a2296270a125 4 #include "LSM9DS1.h"
gr66 0:a2296270a125 5
gr66 0:a2296270a125 6
gr66 0:a2296270a125 7
gr66 0:a2296270a125 8 #include "TB6549.h"
gr66 0:a2296270a125 9
gr66 9:2a16e3930e1d 10 #define dt 0.01 // pas d'integration
gr66 5:d51ba8e93d10 11 #define FDC_PLUS 0.9 // fin de course +
gr66 5:d51ba8e93d10 12 #define FDC_MOINS 0.1 // din de course -
gr66 5:d51ba8e93d10 13 #define ZM 2 // zone morte +/- zm
gr66 5:d51ba8e93d10 14 #define SP 0.5 // pwm moteur en % (0-1)
gr66 5:d51ba8e93d10 15
gr66 8:41752698aa35 16 void calcul(void);
gr66 0:a2296270a125 17 DigitalOut Led0(LED1);
gr66 0:a2296270a125 18
gr66 0:a2296270a125 19 Serial pc(SERIAL_TX, SERIAL_RX,115200);
gr66 5:d51ba8e93d10 20
gr66 0:a2296270a125 21 LSM9DS1 DOF(PB_9, PB_8, 0xD4, 0x38);
gr66 4:b40027cb3012 22 Thread thread (osPriorityAboveNormal);
gr66 8:41752698aa35 23 EventQueue queue;
gr66 9:2a16e3930e1d 24 Mutex pr;
gr66 0:a2296270a125 25 AnalogIn verin(PC_3); // lecture pos verin
gr66 0:a2296270a125 26 AnalogOut ana (PA_5); // pour debug analogique ISR
gr66 0:a2296270a125 27 Motor motor(PB_4,PA_1,PA_4,PC_7);
gr66 8:41752698aa35 28 Ticker tic;
gr66 5:d51ba8e93d10 29
gr66 0:a2296270a125 30 int flag_affich=0;
gr66 0:a2296270a125 31 int flag_imu=0;
gr66 0:a2296270a125 32 double pi= 3.1415926535897932;
gr66 0:a2296270a125 33 double gx_off=0,gy_off=0,gz_off=0;
gr66 0:a2296270a125 34 double ang_off=0;
gr66 8:41752698aa35 35 //
gr66 3:d552b7419f51 36 double ang;
gr66 3:d552b7419f51 37 // fitres complémentaires
gr66 3:d552b7419f51 38 double Fc=0.05;
gr66 5:d51ba8e93d10 39 double RC ; //calcul de RC
gr66 5:d51ba8e93d10 40 double a0; //calcul du coefficient a du filtre
gr66 5:d51ba8e93d10 41 double b0; //calcul du coefficient b du filtre
gr66 5:d51ba8e93d10 42 double a1; //calcul du coefficient a du filtre
gr66 0:a2296270a125 43
gr66 3:d552b7419f51 44 double angle_acce_pred=0.0f;
gr66 3:d552b7419f51 45 double angle_acce=0.0f;
gr66 3:d552b7419f51 46 double angle_acce_f_pred=0.0f;
gr66 3:d552b7419f51 47 double angle_acce_f=0.0f;
gr66 3:d552b7419f51 48 //
gr66 3:d552b7419f51 49 double gyroy_pred=0.0f;
gr66 3:d552b7419f51 50 double gyroy=0.0f;
gr66 3:d552b7419f51 51 double angle_gyroy_f_pred=0.0f;
gr66 3:d552b7419f51 52 double angle_gyroy_f=0.0f;
gr66 3:d552b7419f51 53 //
gr66 3:d552b7419f51 54 double angle_final;
gr66 6:65c511f6f1fc 55 //
gr66 3:d552b7419f51 56 void calcul(void)
gr66 0:a2296270a125 57 {
gr66 8:41752698aa35 58 ana=0.3;
gr66 8:41752698aa35 59 Fc=0.05;
gr66 8:41752698aa35 60 RC=1./(Fc*2*pi); //calcul de RC
gr66 8:41752698aa35 61 a0=1./(1+(2*RC/dt)); //calcul du coefficient a du filtre
gr66 8:41752698aa35 62 b0=(1-(2.*RC/dt))*a0; //calcul du coefficient b du filtre
gr66 8:41752698aa35 63 a1=a0*RC*1.0; //calcul du coefficient a du filtre
gr66 8:41752698aa35 64 DOF.readAccel();
gr66 8:41752698aa35 65 DOF.readGyro();
gr66 6:65c511f6f1fc 66 #if viti
gr66 8:41752698aa35 67 ang=((180/pi)*atan2((double)DOF.ax,-(double)DOF.ay)-90-ang_off); // sur site
gr66 6:65c511f6f1fc 68 #else
gr66 8:41752698aa35 69 ang=((180/pi)*atan2((double)DOF.ay,(double)DOF.az)-ang_off); // sur table
gr66 6:65c511f6f1fc 70 #endif
gr66 8:41752698aa35 71 // filtres complémentaires
gr66 8:41752698aa35 72 angle_acce_pred = angle_acce;
gr66 8:41752698aa35 73 angle_acce=ang;
gr66 8:41752698aa35 74 angle_acce_f_pred = angle_acce_f;
gr66 8:41752698aa35 75 angle_acce_f=a0*angle_acce+a0*angle_acce_pred-b0*angle_acce_f_pred; //filtrage accéléromètre
gr66 10:2a1abbd2c84c 76 //
gr66 8:41752698aa35 77 gyroy_pred = gyroy;
gr66 6:65c511f6f1fc 78 #if viti
gr66 8:41752698aa35 79 gyroy=DOF.gz-gz_off; //sur site
gr66 6:65c511f6f1fc 80 #else
gr66 8:41752698aa35 81 gyroy=-DOF.gx-gx_off; //sur table
gr66 6:65c511f6f1fc 82 #endif
gr66 10:2a1abbd2c84c 83 //
gr66 8:41752698aa35 84 angle_gyroy_f_pred = angle_gyroy_f;
gr66 8:41752698aa35 85 angle_gyroy_f=a1*gyroy+a1*gyroy_pred-b0*angle_gyroy_f_pred;
gr66 8:41752698aa35 86 //
gr66 8:41752698aa35 87 angle_final=angle_acce_f+angle_gyroy_f;
gr66 8:41752698aa35 88 ana=0.0;
gr66 0:a2296270a125 89 }
gr66 10:2a1abbd2c84c 90 //
gr66 0:a2296270a125 91 int main()
gr66 0:a2296270a125 92 {
gr66 0:a2296270a125 93 wait(1);
gr66 0:a2296270a125 94 DOF.begin();
gr66 0:a2296270a125 95 wait(1);
gr66 0:a2296270a125 96 DOF.calibration();
gr66 0:a2296270a125 97 wait(1);
gr66 8:41752698aa35 98 thread.start(callback(&queue,&EventQueue::dispatch_forever));
gr66 9:2a16e3930e1d 99 tic.attach(queue.event(&calcul),dt);
gr66 0:a2296270a125 100 while(1) {
gr66 6:65c511f6f1fc 101 ana=0.6;
gr66 3:d552b7419f51 102 //moteur
gr66 3:d552b7419f51 103 float x=verin.read();
gr66 3:d552b7419f51 104 float s=0.0;
gr66 5:d51ba8e93d10 105 if((angle_final<-ZM)&&(x>FDC_MOINS)) s=SP;
gr66 5:d51ba8e93d10 106 else if((angle_final>ZM)&&(x<FDC_PLUS)) s=-SP;
gr66 5:d51ba8e93d10 107 else motor.speed(0.0);
gr66 5:d51ba8e93d10 108 //
gr66 3:d552b7419f51 109 motor.speed(s);
gr66 9:2a16e3930e1d 110 //pr.lock();
gr66 9:2a16e3930e1d 111 pc.printf("$%6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f;\r\n",ang,angle_acce_f,angle_gyroy_f,angle_final,gyroy,x,s);
gr66 9:2a16e3930e1d 112 //pr.unlock();
gr66 3:d552b7419f51 113 ana=0.0;
gr66 7:f5c5cf499311 114 wait(0.095);
gr66 0:a2296270a125 115 }
gr66 0:a2296270a125 116 }
gr66 0:a2296270a125 117