Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Quadrocopter by
main.cpp@15:742683a8efda, 2017-08-23 (annotated)
- Committer:
- MarcoF89
- Date:
- Wed Aug 23 11:53:22 2017 +0000
- Revision:
- 15:742683a8efda
- Parent:
- 13:5f0a2103c707
- Child:
- 16:59d80bf88bf8
Sensoren zusammengef?gt
Who changed what in which revision?
User | Revision | Line number | New 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" |
MarcoF89 | 15:742683a8efda | 7 | |
MarcoF89 | 15:742683a8efda | 8 | #define RAD 57.29577951 |
MarcoF89 | 15:742683a8efda | 9 | |
MarcoF89 | 15:742683a8efda | 10 | uint16_t zeit; |
MarcoF89 | 15:742683a8efda | 11 | uint32_t zeit2; |
MarcoF89 | 15:742683a8efda | 12 | |
MarcoF89 | 15:742683a8efda | 13 | uint8_t k; |
MarcoF89 | 15:742683a8efda | 14 | |
MarcoF89 | 15:742683a8efda | 15 | |
MarcoF89 | 15:742683a8efda | 16 | |
MarcoF89 | 15:742683a8efda | 17 | |
MarcoF89 | 13:5f0a2103c707 | 18 | |
MarcoF89 | 6:27a09e8bebfb | 19 | int main() |
MarcoF89 | 15:742683a8efda | 20 | { |
MarcoF89 | 15:742683a8efda | 21 | gain_g = 0; |
MarcoF89 | 15:742683a8efda | 22 | z_off = 0; |
MarcoF89 | 15:742683a8efda | 23 | zeit = 0; |
MarcoF89 | 15:742683a8efda | 24 | k = 0; |
MarcoF89 | 15:742683a8efda | 25 | drift_z = 0; |
MarcoF89 | 15:742683a8efda | 26 | |
MarcoF89 | 13:5f0a2103c707 | 27 | Motor1.period_ms(2); |
MarcoF89 | 11:8457b851e3e1 | 28 | Motor2.period_ms(2); |
MarcoF89 | 11:8457b851e3e1 | 29 | Motor3.period_ms(2); |
MarcoF89 | 11:8457b851e3e1 | 30 | Motor4.period_ms(2); |
MarcoF89 | 12:4a4dad7a3432 | 31 | initialisierung_gyro(); |
MarcoF89 | 13:5f0a2103c707 | 32 | initialisierung_acc(); |
MarcoF89 | 12:4a4dad7a3432 | 33 | if (taster2) |
MarcoF89 | 12:4a4dad7a3432 | 34 | { |
MarcoF89 | 13:5f0a2103c707 | 35 | viberationen(&rauschen, &Motor1, &Motor2, &Motor3, &Motor4, &taster4); |
MarcoF89 | 12:4a4dad7a3432 | 36 | } |
MarcoF89 | 12:4a4dad7a3432 | 37 | if (taster3) |
MarcoF89 | 12:4a4dad7a3432 | 38 | { |
MarcoF89 | 13:5f0a2103c707 | 39 | anlernen(&Motor1, &Motor2, &Motor3, &Motor4, &taster1, &taster2, &taster4); |
MarcoF89 | 11:8457b851e3e1 | 40 | } |
MarcoF89 | 15:742683a8efda | 41 | pc.printf("Druecke Taster1 fuer den Start\n\r"); |
MarcoF89 | 13:5f0a2103c707 | 42 | n1 = n2 = n3 = n4 = 700; |
MarcoF89 | 11:8457b851e3e1 | 43 | Motor1.pulsewidth_us(n1); |
MarcoF89 | 11:8457b851e3e1 | 44 | Motor2.pulsewidth_us(n2); |
MarcoF89 | 11:8457b851e3e1 | 45 | Motor3.pulsewidth_us(n3); |
MarcoF89 | 13:5f0a2103c707 | 46 | Motor4.pulsewidth_us(n4); |
MarcoF89 | 13:5f0a2103c707 | 47 | while(1) |
MarcoF89 | 13:5f0a2103c707 | 48 | { |
MarcoF89 | 15:742683a8efda | 49 | if (1)//(taster1) |
MarcoF89 | 11:8457b851e3e1 | 50 | { |
MarcoF89 | 15:742683a8efda | 51 | while(1)//(!taster4) |
MarcoF89 | 15:742683a8efda | 52 | { |
MarcoF89 | 15:742683a8efda | 53 | pc.printf("\n\rOffset und Driftberechnung wird durchgefuehrt, halte die Drohne still"); |
MarcoF89 | 15:742683a8efda | 54 | offset_gyro(&z_off, &x_off, &y_off); |
MarcoF89 | 15:742683a8efda | 55 | 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 | 56 | 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 | 57 | pc.printf("\n\rDrift:Z: %3.10f\tX: %3.10f\tY: %3.10f\n\r", drift_z, drift_x, drift_y); |
MarcoF89 | 15:742683a8efda | 58 | |
MarcoF89 | 15:742683a8efda | 59 | /********//******/ |
MarcoF89 | 15:742683a8efda | 60 | /*Messen*//*Gyro*/ |
MarcoF89 | 15:742683a8efda | 61 | /********//******/ |
MarcoF89 | 15:742683a8efda | 62 | timer.stop(); |
MarcoF89 | 15:742683a8efda | 63 | timer2.stop(); |
MarcoF89 | 15:742683a8efda | 64 | timer.reset(); |
MarcoF89 | 15:742683a8efda | 65 | timer2.reset(); |
MarcoF89 | 15:742683a8efda | 66 | gain_g = 0; |
MarcoF89 | 15:742683a8efda | 67 | pitch_g = 0; |
MarcoF89 | 15:742683a8efda | 68 | roll_g = 0; |
MarcoF89 | 15:742683a8efda | 69 | timer.start(); |
MarcoF89 | 15:742683a8efda | 70 | timer2.start(); |
MarcoF89 | 15:742683a8efda | 71 | int i = 0; |
MarcoF89 | 15:742683a8efda | 72 | while(1) |
MarcoF89 | 15:742683a8efda | 73 | { |
MarcoF89 | 15:742683a8efda | 74 | i++; |
MarcoF89 | 15:742683a8efda | 75 | zeit = timer.read_us(); |
MarcoF89 | 15:742683a8efda | 76 | timer.reset(); |
MarcoF89 | 15:742683a8efda | 77 | aktuell_roh(&z_g, &x_g, &y_g, &z_a, &x_a, &y_a); |
MarcoF89 | 15:742683a8efda | 78 | gain_g = gain_g + ((z_g - z_off) * zeit * 0.000001 * 1/16.4); |
MarcoF89 | 15:742683a8efda | 79 | pitch_g = pitch_g + ((y_g - y_off) * zeit * 0.000001 * 1/16.4); |
MarcoF89 | 15:742683a8efda | 80 | roll_g = roll_g + ((x_g - x_off) * zeit * 0.000001 * 1/16.4); |
MarcoF89 | 15:742683a8efda | 81 | y = y_a / 16384.00; |
MarcoF89 | 15:742683a8efda | 82 | x = x_a / 16384.00; |
MarcoF89 | 15:742683a8efda | 83 | z = z_a / 16384.00; |
MarcoF89 | 15:742683a8efda | 84 | roll_a = atan2(y, sqrt(x * x + z * z)) * RAD; |
MarcoF89 | 15:742683a8efda | 85 | pitch_a = atan2(-x, z) * RAD; |
MarcoF89 | 15:742683a8efda | 86 | if (timer2.read_ms() >= 2000) |
MarcoF89 | 15:742683a8efda | 87 | { |
MarcoF89 | 15:742683a8efda | 88 | gain_g -= drift_z; |
MarcoF89 | 15:742683a8efda | 89 | pitch_g -= drift_y; |
MarcoF89 | 15:742683a8efda | 90 | roll_g -= drift_x; |
MarcoF89 | 15:742683a8efda | 91 | timer2.reset(); |
MarcoF89 | 15:742683a8efda | 92 | } |
MarcoF89 | 15:742683a8efda | 93 | gain = gain_g; |
MarcoF89 | 15:742683a8efda | 94 | pitch = pitch_g * 0.9 + pitch_a * 0.1; |
MarcoF89 | 15:742683a8efda | 95 | roll = roll_g * 0.9 + roll_a * 0.1; |
MarcoF89 | 15:742683a8efda | 96 | if (i == 2000) |
MarcoF89 | 15:742683a8efda | 97 | { |
MarcoF89 | 15:742683a8efda | 98 | pc.printf("gain: %2.5f\tpitch: %2.5f\troll: %2.5f\t\n\r",pitch, roll, gain); |
MarcoF89 | 15:742683a8efda | 99 | i = 0; |
MarcoF89 | 15:742683a8efda | 100 | } |
MarcoF89 | 15:742683a8efda | 101 | |
MarcoF89 | 15:742683a8efda | 102 | } |
MarcoF89 | 11:8457b851e3e1 | 103 | } |
MarcoF89 | 11:8457b851e3e1 | 104 | } |
MarcoF89 | 10:16ca5e9ee0dc | 105 | } |
MarcoF89 | 13:5f0a2103c707 | 106 | } |