Dydaktyka
Dependencies: FastPWM mbed-src
Fork of 2015_04_17_quadro_bez_sterowania by
main.cpp
- Committer:
- Michu90
- Date:
- 2014-12-19
- Revision:
- 5:c3caf8b83e6b
- Parent:
- 4:a5b51a651db7
- Child:
- 6:8cc6df266363
File content as of revision 5:c3caf8b83e6b:
#include "mbed.h" #include "FastPWM.h" #include "kalman.h" #include "Offsets.h" #include "stdio.h" #include "IMU.h" #define PWM_period 2500 #define M_PI 3.141592 #define M_PI2 1.570796 #define dt 0.005 //kalman // Structs for containing filter data kalman_data pitch_data; kalman_data roll_data; kalman_data pitch_data2; kalman_data roll_data2; DigitalOut myled(PTA2); DigitalOut myled2(PTA1); Serial pc(USBTX, USBRX); Serial bluetooth(D1, D0); IMU imu(PTE25,PTE24); Offsets off; FastPWM M1(D10); FastPWM M2(D11); FastPWM M3(D12); FastPWM M4(D13); Ticker triger1; //przerwanie filtracji //Ticker triger2; //przerwanie wysyłania danych float d[9]; double D[9]; float o[3]; float O[3]; char buff[160]; float r,katx,katy; float rbut,katxbut,katybut; float pitch, roll; float pitch2, roll2; double i; float offsetGyro[3]; char odczyt[20]; char znak; char znak2; double PWM1zad; double PWM2zad; double PWM3zad; double PWM4zad; double valPWM1; double valPWM2; double valPWM3; double valPWM4; void task1() { imu.readData(d); imu.filterData(d, D); off.offsetData(d,offsetGyro,o); off.offsetData2(D,offsetGyro,O); r = sqrt(pow(d[3],2) + pow(d[4],2) + pow(d[5],2)); katx = acos(d[4]/r)-M_PI2; katy = acos(d[3]/r)-M_PI2; rbut = sqrt(pow(D[3],2) + pow(D[4],2) + pow(D[5],2)); katxbut = acos(D[4]/rbut)-M_PI2; katybut = acos(D[3]/rbut)-M_PI2; //Filtr Kalmana kalman_innovate(&pitch_data, katx, o[0]); kalman_innovate(&roll_data, -katy, o[1]); pitch = pitch_data.x1; roll = roll_data.x1; //Filtr Kalmana butterworth 2nd kalman_innovate(&pitch_data2, katxbut, O[0]); kalman_innovate(&roll_data2, -katybut, O[1]); pitch2 = pitch_data2.x1; roll2 = roll_data2.x1; //sprintf(buff, "%f,%f,%f,%f\n\r", -katy*180/M_PI, roll*180/M_PI, -katybut*180/M_PI, roll2*180/M_PI); //pc.printf(buff); //sprintf(buff, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n\r", -katy, roll, -katybut, roll2, (d[0]*180/M_PI),(D[0]*180/M_PI),(d[1]*180/M_PI),(D[1]*180/M_PI),(o[0]*180/M_PI),(O[0]*180/M_PI),(o[1]*180/M_PI),(O[1]*180/M_PI)); //pc.printf(buff); //sprintf(buff, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n\r", offsetGyro[0]*180/M_PI, offsetGyro[1]*180/M_PI, offsetGyro[2]*180/M_PI, roll2, (d[0]*180/M_PI),(D[0]*180/M_PI),(d[1]*180/M_PI),(D[1]*180/M_PI),(o[0]*180/M_PI),(O[0]*180/M_PI),(o[1]*180/M_PI),(O[1]*180/M_PI)); //pc.printf(buff); //sprintf(buff, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n\r", -katy, roll, -katybut, roll2, (d[0]*180/M_PI),(D[0]*180/M_PI),(d[1]*180/M_PI),(D[1]*180/M_PI),(o[0]*180/M_PI),(O[0]*180/M_PI),(o[1]*180/M_PI),(O[1]*180/M_PI)); //pc.printf(buff); myled = !myled; } void task2() { /* sprintf(buff, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n\r", -katy*180/M_PI, roll*180/M_PI, -katybut*180/M_PI, roll2*180/M_PI, katx*180/M_PI, pitch*180/M_PI, katxbut*180/M_PI, pitch2*180/M_PI,(o[0]*180/M_PI),(O[0]*180/M_PI),(o[1]*180/M_PI),(O[1]*180/M_PI)); pc.printf(buff); myled2 = !myled2;*/ } int main() { pc.baud(115200); bluetooth.baud(19200); imu.init(); kalman_init(&pitch_data); kalman_init(&roll_data); kalman_init(&pitch_data2); kalman_init(&roll_data2); sprintf(buff, "Hello: \n\r"); pc.printf(buff); //off.setOffsets(offsetGyro, pc, imu); triger1.attach(&task1, 0.005); //triger2.attach(&task2, 0.005); i=1000; PWM1zad=10000; PWM2zad=10000; PWM3zad=10000; PWM4zad=10000; M1.period_us(PWM_period); M1.pulsewidth_us(PWM1zad); M2.period_us(PWM_period); M2.pulsewidth_us(PWM2zad); M3.period_us(PWM_period); M3.pulsewidth_us(PWM3zad); M4.period_us(PWM_period); M4.pulsewidth_us(PWM4zad); while(1) { //sprintf(buff, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n\r", -katy*180/M_PI, roll*180/M_PI, -katybut*180/M_PI, roll2*180/M_PI, katx*180/M_PI, pitch*180/M_PI, katxbut*180/M_PI, pitch2*180/M_PI,(o[0]*180/M_PI),(O[0]*180/M_PI),(o[1]*180/M_PI),(O[1]*180/M_PI)); //pc.printf(buff); //myled2 = !myled2; if(pc.readable()){ znak=pc.getc(); switch (znak){ case 'p': sprintf(buff, "odczytany znak: %c\n\r",znak); pc.printf(buff); i+=10; break; case 'm': i-=10; break; case 'u': i+=0.1; break; case 'd': i-=0.1; break; case 'q': for(i=1000;i<1500;i++){ M1.pulsewidth_us(i); wait(0.005); } break; case 'w': for(i=1500;i>1000;i--){ M1.pulsewidth_us(i); wait(0.005); } break; case 'e': for(i=1000;i<1500;i=i+0.1){ M1.pulsewidth_us(i); wait(0.001); } break; case 'r': for(i=1500;i>1000;i-=0.1){ M1.pulsewidth_us(i); wait(0.001); } break; } znak=0; M1.pulsewidth_us(i); } if(bluetooth.readable()){ znak2=bluetooth.getc(); switch (znak2){ case 'a': PWM1zad-=50; PWM2zad-=50; PWM3zad-=50; PWM4zad-=50; if(PWM1zad<10000){ PWM1zad=10000; PWM2zad=10000; PWM3zad=10000; PWM4zad=10000; } //ustawianie M1.pulsewidth_us(PWM1zad); M2.pulsewidth_us(PWM2zad); M3.pulsewidth_us(PWM3zad); M4.pulsewidth_us(PWM4zad); znak2=0; break; case 'b': PWM1zad+=50; PWM2zad+=50; PWM3zad+=50; PWM4zad+=50; if(PWM1zad>=20000){ PWM1zad=20000; PWM2zad=20000; PWM3zad=20000; PWM4zad=20000; } //ustawianie M1.pulsewidth_us(PWM1zad); M2.pulsewidth_us(PWM2zad); M3.pulsewidth_us(PWM3zad); M4.pulsewidth_us(PWM4zad); znak2=0; break; case 'x': PWM1zad=10000; PWM2zad=10000; PWM3zad=10000; PWM4zad=10000; M1.pulsewidth_us(PWM1zad); M2.pulsewidth_us(PWM2zad); M3.pulsewidth_us(PWM3zad); M4.pulsewidth_us(PWM4zad); sprintf(buff,"Odlacz silniki\n\r"); pc.printf(buff); wait(1.0f); sprintf(buff,"5 \n\r"); pc.printf(buff); wait(1.0f); sprintf(buff,"4 \n\r"); pc.printf(buff); wait(1.0f); sprintf(buff,"3 \n\r"); pc.printf(buff); wait(1.0f); sprintf(buff,"2 \n\r"); pc.printf(buff); wait(1.0f); sprintf(buff,"1 \n\r"); pc.printf(buff); wait(1.0f); sprintf(buff,"GO! \n\r"); pc.printf(buff); break; } } } }