Chris Elsholz / Mbed 2 deprecated Quadrocopter

Dependencies:   mbed TextLCD

Fork of Quadrocopter by Marco Friedmann

Committer:
chriselsholz
Date:
Thu Sep 21 16:15:45 2017 +0000
Revision:
28:f9349474a553
Parent:
27:67b388f6ac2e
Child:
29:3efe34986347
Rauschen: Motorbegrenzung

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MarcoF89 13:5f0a2103c707 1 #include <Timer.h>
MarcoF89 13:5f0a2103c707 2 #include <math.h>
MarcoF89 15:742683a8efda 3 #include "mbed.h"
MarcoF89 15:742683a8efda 4 #include "stdio.h"
MarcoF89 13:5f0a2103c707 5 #include "deklaration.h"
MarcoF89 13:5f0a2103c707 6 #include "messen.h"
chriselsholz 18:78e7de067ddb 7 #include "filter/Kalman.h"
MarcoF89 25:a8a3cbc57c61 8
MarcoF89 15:742683a8efda 9 #define RAD 57.29577951
MarcoF89 25:a8a3cbc57c61 10
MarcoF89 6:27a09e8bebfb 11 int main()
MarcoF89 15:742683a8efda 12 {
MarcoF89 15:742683a8efda 13 z_off = 0;
MarcoF89 15:742683a8efda 14 drift_z = 0;
MarcoF89 15:742683a8efda 15
MarcoF89 13:5f0a2103c707 16 Motor1.period_ms(2);
MarcoF89 11:8457b851e3e1 17 Motor2.period_ms(2);
MarcoF89 11:8457b851e3e1 18 Motor3.period_ms(2);
MarcoF89 11:8457b851e3e1 19 Motor4.period_ms(2);
MarcoF89 12:4a4dad7a3432 20 initialisierung_gyro();
chriselsholz 16:59d80bf88bf8 21 initialisierung_acc();
MarcoF89 25:a8a3cbc57c61 22 Kalman_pitch();
MarcoF89 25:a8a3cbc57c61 23 Kalman_yaw();
MarcoF89 25:a8a3cbc57c61 24 Kalman_roll();
MarcoF89 25:a8a3cbc57c61 25 aktuell_roh(&z_g, &x_g, &y_g, &z_a, &x_a, &y_a);
MarcoF89 25:a8a3cbc57c61 26 wait(1);
chriselsholz 23:c99a4bd60609 27 if (taster2)
chriselsholz 23:c99a4bd60609 28 {
chriselsholz 23:c99a4bd60609 29 viberationen(&rauschen, &Motor1, &Motor2, &Motor3, &Motor4, &taster4);
chriselsholz 23:c99a4bd60609 30 }
chriselsholz 23:c99a4bd60609 31 if (taster3)
chriselsholz 23:c99a4bd60609 32 {
chriselsholz 23:c99a4bd60609 33 anlernen(&Motor1, &Motor2, &Motor3, &Motor4, &taster1, &taster2, &taster4);
chriselsholz 23:c99a4bd60609 34 }
chriselsholz 23:c99a4bd60609 35
MarcoF89 27:67b388f6ac2e 36 pc.printf("Druecke Taster1 fuer den Start und Taster2 fuers rauschen\n\r");
chriselsholz 23:c99a4bd60609 37 n1=n2=n3=n4=700;
chriselsholz 23:c99a4bd60609 38 Motor1.pulsewidth_us(n1);
chriselsholz 23:c99a4bd60609 39 Motor2.pulsewidth_us(n2);
chriselsholz 23:c99a4bd60609 40 Motor3.pulsewidth_us(n3);
chriselsholz 23:c99a4bd60609 41 Motor4.pulsewidth_us(n4);
chriselsholz 23:c99a4bd60609 42
MarcoF89 13:5f0a2103c707 43 while(1)
MarcoF89 13:5f0a2103c707 44 {
chriselsholz 18:78e7de067ddb 45 if (taster1)
MarcoF89 11:8457b851e3e1 46 {
MarcoF89 25:a8a3cbc57c61 47 while(1)
MarcoF89 25:a8a3cbc57c61 48 {
MarcoF89 15:742683a8efda 49 pc.printf("\n\rOffset und Driftberechnung wird durchgefuehrt, halte die Drohne still");
MarcoF89 15:742683a8efda 50 offset_gyro(&z_off, &x_off, &y_off);
MarcoF89 24:aaa5b4703555 51 //drift_gyro(&drift_z, &drift_x, &drift_y, &timer, &timer2, &gain_g, &roll_g, &pitch_g, &z_off, &x_off, &y_off);
MarcoF89 15:742683a8efda 52 pc.printf("\n\rOffgesamt:\n\rZ = %3.5f\tY = %3.5f\tZ = %3.5f\t\n\r", z_off, x_off, y_off);
MarcoF89 15:742683a8efda 53 pc.printf("\n\rDrift:Z: %3.10f\tX: %3.10f\tY: %3.10f\n\r", drift_z, drift_x, drift_y);
MarcoF89 25:a8a3cbc57c61 54
MarcoF89 15:742683a8efda 55 timer.reset();
MarcoF89 15:742683a8efda 56 timer.start();
MarcoF89 27:67b388f6ac2e 57 timer2.reset();
MarcoF89 27:67b388f6ac2e 58 timer2.start();
MarcoF89 15:742683a8efda 59 int i = 0;
MarcoF89 15:742683a8efda 60 while(1)
MarcoF89 15:742683a8efda 61 {
MarcoF89 15:742683a8efda 62 i++;
MarcoF89 25:a8a3cbc57c61 63 dt = timer.read_us() * 0.000001; //Zeit zwischen zwei Messpunkten
MarcoF89 15:742683a8efda 64 timer.reset();
MarcoF89 25:a8a3cbc57c61 65 aktuell_roh(&z_g, &x_g, &y_g, &z_a, &x_a, &y_a); //Rohdaten einlesen
MarcoF89 25:a8a3cbc57c61 66
MarcoF89 25:a8a3cbc57c61 67 y = y_a / 16384.00; //Umwandlung in G-Kraft
MarcoF89 25:a8a3cbc57c61 68 x = x_a / 16384.00; //Umwandlung in G-Kraft
MarcoF89 25:a8a3cbc57c61 69 z = z_a / 16384.00; //Umwandlung in G-Kraft
MarcoF89 25:a8a3cbc57c61 70
MarcoF89 25:a8a3cbc57c61 71 newAngle_pitch = atan2(-x, z) * RAD; //Umwandlung der G-Kraft in °
MarcoF89 25:a8a3cbc57c61 72 newRate_pitch = ((y_g - y_off) * 1/16.4); //Offset subtrahiert +++ Umwandlung in °/s
MarcoF89 24:aaa5b4703555 73
MarcoF89 25:a8a3cbc57c61 74 newAngle_roll = atan2(y, sqrt(x * x + z * z)) * RAD; //Umwandlung der G-Kraft in °
MarcoF89 25:a8a3cbc57c61 75 newRate_roll = ((x_g - x_off) * 1/16.4); //Offset subtrahiert +++ Umwandlung in °/s
MarcoF89 25:a8a3cbc57c61 76
MarcoF89 25:a8a3cbc57c61 77 newAngle_yaw = ((z_g - z_off) * 1/16.4); //Umwandlung der G-Kraft in °
MarcoF89 25:a8a3cbc57c61 78 newRate_yaw = ((z_g - z_off) * 1/16.4); //Offset subtrahiert +++ Umwandlung in °/s
MarcoF89 25:a8a3cbc57c61 79
MarcoF89 25:a8a3cbc57c61 80 pitch = getPitch(&newAngle_pitch, &newRate_pitch, &dt);
MarcoF89 25:a8a3cbc57c61 81 yaw = getYaw(&newAngle_yaw, &newRate_yaw, &dt);
MarcoF89 25:a8a3cbc57c61 82 roll = getRoll(&newAngle_roll, &newRate_roll, &dt);
MarcoF89 25:a8a3cbc57c61 83
MarcoF89 27:67b388f6ac2e 84 if (i == 1000)
MarcoF89 15:742683a8efda 85 {
MarcoF89 27:67b388f6ac2e 86 printf("%f2.3 \t%f2.3 \t%f2.3 \t%f2.3 \t%f2.3 \t%f2.3 \t%fd \t", pitch, yaw, roll, newAngle_pitch, newRate_pitch, newAngle_roll, newRate_roll, newAngle_yaw, n1);
MarcoF89 15:742683a8efda 87 i = 0;
MarcoF89 15:742683a8efda 88 }
MarcoF89 25:a8a3cbc57c61 89 Motorsteurung(&Motor1, &Motor2, &Motor3, &Motor4, &taster2, &taster3, &taster4, &n1, &n2, &n3, &n4);
MarcoF89 15:742683a8efda 90 }
MarcoF89 11:8457b851e3e1 91 }
MarcoF89 11:8457b851e3e1 92 }
MarcoF89 27:67b388f6ac2e 93 if (taster2)
MarcoF89 27:67b388f6ac2e 94 {
MarcoF89 27:67b388f6ac2e 95 while(1)
MarcoF89 27:67b388f6ac2e 96 {
MarcoF89 27:67b388f6ac2e 97 pc.printf("\n\rOffset und Driftberechnung wird durchgefuehrt, halte die Drohne still");
MarcoF89 27:67b388f6ac2e 98 offset_gyro(&z_off, &x_off, &y_off);
MarcoF89 27:67b388f6ac2e 99 //drift_gyro(&drift_z, &drift_x, &drift_y, &timer, &timer2, &gain_g, &roll_g, &pitch_g, &z_off, &x_off, &y_off);
MarcoF89 27:67b388f6ac2e 100 pc.printf("\n\rOffgesamt:\n\rZ = %3.5f\tY = %3.5f\tZ = %3.5f\t\n\r", z_off, x_off, y_off);
MarcoF89 27:67b388f6ac2e 101 pc.printf("\n\rDrift:Z: %3.10f\tX: %3.10f\tY: %3.10f\n\r", drift_z, drift_x, drift_y);
MarcoF89 27:67b388f6ac2e 102
MarcoF89 27:67b388f6ac2e 103 timer.reset();
MarcoF89 27:67b388f6ac2e 104 timer.start();
MarcoF89 27:67b388f6ac2e 105 timer2.reset();
MarcoF89 27:67b388f6ac2e 106 timer2.start();
MarcoF89 27:67b388f6ac2e 107 int i = 0;
MarcoF89 27:67b388f6ac2e 108 while(1)
MarcoF89 27:67b388f6ac2e 109 {
MarcoF89 27:67b388f6ac2e 110 i++;
MarcoF89 27:67b388f6ac2e 111 dt = timer.read_us() * 0.000001; //Zeit zwischen zwei Messpunkten
MarcoF89 27:67b388f6ac2e 112 timer.reset();
MarcoF89 27:67b388f6ac2e 113 aktuell_roh(&z_g, &x_g, &y_g, &z_a, &x_a, &y_a); //Rohdaten einlesen
MarcoF89 27:67b388f6ac2e 114
MarcoF89 27:67b388f6ac2e 115 y = y_a / 16384.00; //Umwandlung in G-Kraft
MarcoF89 27:67b388f6ac2e 116 x = x_a / 16384.00; //Umwandlung in G-Kraft
MarcoF89 27:67b388f6ac2e 117 z = z_a / 16384.00; //Umwandlung in G-Kraft
MarcoF89 27:67b388f6ac2e 118
MarcoF89 27:67b388f6ac2e 119 newAngle_pitch = atan2(-x, z) * RAD; //Umwandlung der G-Kraft in °
MarcoF89 27:67b388f6ac2e 120 newRate_pitch = ((y_g - y_off) * 1/16.4); //Offset subtrahiert +++ Umwandlung in °/s
MarcoF89 27:67b388f6ac2e 121
MarcoF89 27:67b388f6ac2e 122 newAngle_roll = atan2(y, sqrt(x * x + z * z)) * RAD; //Umwandlung der G-Kraft in °
MarcoF89 27:67b388f6ac2e 123 newRate_roll = ((x_g - x_off) * 1/16.4); //Offset subtrahiert +++ Umwandlung in °/s
MarcoF89 27:67b388f6ac2e 124
MarcoF89 27:67b388f6ac2e 125 newAngle_yaw = ((z_g - z_off) * 1/16.4); //Umwandlung der G-Kraft in °
MarcoF89 27:67b388f6ac2e 126 newRate_yaw = ((z_g - z_off) * 1/16.4); //Offset subtrahiert +++ Umwandlung in °/s
MarcoF89 27:67b388f6ac2e 127
MarcoF89 27:67b388f6ac2e 128 pitch = getPitch(&newAngle_pitch, &newRate_pitch, &dt);
MarcoF89 27:67b388f6ac2e 129 yaw = getYaw(&newAngle_yaw, &newRate_yaw, &dt);
MarcoF89 27:67b388f6ac2e 130 roll = getRoll(&newAngle_roll, &newRate_roll, &dt);
MarcoF89 27:67b388f6ac2e 131
MarcoF89 27:67b388f6ac2e 132 if (i == 1000)
MarcoF89 27:67b388f6ac2e 133 {
MarcoF89 27:67b388f6ac2e 134 printf("%f2.3 \t%f2.3 \t%f2.3 \t%f2.3 \t%f2.3 \t%f2.3 \t%fd \t", pitch, yaw, roll, newAngle_pitch, newRate_pitch, newAngle_roll, newRate_roll, newAngle_yaw, n1);
MarcoF89 27:67b388f6ac2e 135 i = 0;
MarcoF89 27:67b388f6ac2e 136 }
MarcoF89 27:67b388f6ac2e 137 if (timer2.read_ms() >= 2500)
MarcoF89 27:67b388f6ac2e 138 {
MarcoF89 27:67b388f6ac2e 139 n1+=200;
MarcoF89 27:67b388f6ac2e 140 n2+=200;
MarcoF89 27:67b388f6ac2e 141 n3+=200;
MarcoF89 27:67b388f6ac2e 142 n4+=200;
MarcoF89 27:67b388f6ac2e 143 Motor1.pulsewidth_us(n1);
MarcoF89 27:67b388f6ac2e 144 Motor2.pulsewidth_us(n2);
MarcoF89 27:67b388f6ac2e 145 Motor3.pulsewidth_us(n3);
MarcoF89 27:67b388f6ac2e 146 Motor4.pulsewidth_us(n4);
MarcoF89 27:67b388f6ac2e 147 timer2.reset();
MarcoF89 27:67b388f6ac2e 148 }
chriselsholz 28:f9349474a553 149 Motorsteurung(&Motor1, &Motor2, &Motor3, &Motor4, &taster2, &taster3, &taster4, &n1, &n2, &n3, &n4);
chriselsholz 28:f9349474a553 150 if (n1>1400)
chriselsholz 28:f9349474a553 151 {
chriselsholz 28:f9349474a553 152 n1=n2=n3=n4=700;
chriselsholz 28:f9349474a553 153 Motor1.pulsewidth_us(n1);
chriselsholz 28:f9349474a553 154 Motor2.pulsewidth_us(n2);
chriselsholz 28:f9349474a553 155 Motor3.pulsewidth_us(n3);
chriselsholz 28:f9349474a553 156 Motor4.pulsewidth_us(n4);
chriselsholz 28:f9349474a553 157 while(1);
chriselsholz 28:f9349474a553 158 }
MarcoF89 27:67b388f6ac2e 159 }
MarcoF89 27:67b388f6ac2e 160 }
MarcoF89 27:67b388f6ac2e 161 }
MarcoF89 10:16ca5e9ee0dc 162 }
MarcoF89 25:a8a3cbc57c61 163 }