drivefusion.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef DRIVE_REGLER_H
  2. #define DRIVE_REGLER_H
  3. #include "Arduino.h"
  4. #include "globals.h"
  5. #include "secrets.h"
  6. float sollRichtung, istRichtung;
  7. double pidIst, pidSoll, output;
  8. double Kp=1, Ki=50, Kd=0.11;
  9. float flat, flon;
  10. unsigned long age;
  11. unsigned long start = 0;
  12. float Longitudealt, Latitudealt, dist, bestwert;
  13. unsigned long chars = 0;
  14. unsigned short sentences = 0, failed = 0;
  15. PID myPID(&pidIst, &output, &pidSoll, Kp, Ki, Kd, DIRECT);
  16. TinyGPS gps;
  17. const char header[] = "Mode \tGas \tServo \tistW° \tsollW° \tLenk \tLat \tLng \tDist \tAge \tDatenfusion";
  18. void initAuto() {
  19. myPID.SetMode(AUTOMATIC);
  20. sollRichtung = 0;
  21. Serial3.println(header);
  22. Serial.println(header);
  23. }
  24. void driveAuto() {
  25. bool newdata = false;
  26. while(Serial2.available()) {
  27. char c = Serial2.read();
  28. if (gps.encode(c)) {
  29. newdata = true;
  30. }
  31. }
  32. if (newdata || (millis() - start > 1000)) {
  33. gps.f_get_position(&flat, &flon, &age);
  34. gps.stats(&chars, &sentences, &failed);
  35. Serial.print("Auto: \t");
  36. if(age > 1000){
  37. Serial.print("valid sentences = ");
  38. Serial.print(sentences);
  39. Serial.print(" Satellit wird gesucht...");
  40. }
  41. else {
  42. dist = gps.distance_between(flat, flon, s_lat, s_lon);
  43. Serial.print("Latitude: ");
  44. Serial.print(flat,10);
  45. Serial.print(" \tLongitude: ");
  46. Serial.print(flon,10);
  47. Serial.print(" \tDist: ");
  48. Serial.print(dist);
  49. Serial.print(" \tTime: ");
  50. Serial.print(age);
  51. Serial.print("ms. ");
  52. sollRichtung = 359 - gps.course_to(flat, flon, s_lat, s_lon); // Koordinaten zum Sportplatz !
  53. if (Latitudealt != flat && Longitudealt != flon) {
  54. istRichtung = 359 - gps.course_to(Latitudealt, Longitudealt, flat, flon);
  55. Longitudealt = flon;
  56. Latitudealt = flat;
  57. const float s1 = 0.31;
  58. const float s2 = 0; // 0.95
  59. bestwert = (mpu.winkelZ) * (s2*s2 / (s1*s1 + s2*s2)) + istRichtung * (s1*s1 / (s1*s1 + s2*s2));
  60. mpu.winkelZ = bestwert;
  61. }
  62. //istRichtung = gps.f_course(); // Korrigieren der Lenkung zum Ziel
  63. }
  64. start = millis();
  65. Serial3.print("Auto: \t");
  66. Serial3.print(gasVal);
  67. Serial3.print(" \t");
  68. Serial3.print(servoVal);
  69. Serial3.print(" \t");
  70. Serial3.print(istRichtung);
  71. Serial3.print(" \t");
  72. Serial3.print(sollRichtung);
  73. Serial3.print(" \t");
  74. Serial3.print(output);
  75. Serial3.print(" \t");
  76. Serial3.print(flat,10);
  77. Serial3.print(" \t");
  78. Serial3.print(flon,10);
  79. Serial3.print(" \t");
  80. Serial3.print(dist);
  81. Serial3.print(" \t");
  82. Serial3.print(age);
  83. Serial3.print(" \t");
  84. Serial3.print(bestwert);
  85. Serial3.print(" \t");
  86. Serial3.print(mpu.winkelZ);
  87. Serial3.print(" \t");
  88. Serial3.print(failed);
  89. Serial3.println();
  90. Serial.print(gasVal);
  91. Serial.print(" \t");
  92. Serial.print(servoVal);
  93. Serial.print(" \t");
  94. Serial.print(istRichtung);
  95. Serial.print("\t");
  96. Serial.print(sollRichtung);
  97. Serial.print("\t");
  98. Serial.println(output);
  99. }
  100. //istRichtung = mpu.winkelZ;
  101. //sollRichtung = spm.data[1] * 180;
  102. if(spm.hasData()) {
  103. setMotor(spm.data[2]*0.3);
  104. int winkelDiff = sollRichtung - mpu.winkelZ; // -360 - 360
  105. // 0 - 180 -> rechts fahren
  106. // -180 - 0 -> links
  107. // 180 - 360 -> links
  108. // -180 - -360 -> rechts
  109. winkelDiff += 180;
  110. //180 hizufuegen
  111. // 180 - 360 -> rechts fahren
  112. // -0 - 180 -> links
  113. // 360 - 360+180 -> links
  114. // 0 - -180 -> rechts
  115. winkelDiff = (winkelDiff+360) % 360;
  116. //0 - 360 modulo nur positiv
  117. // 180 - 360 -> rechts fahren
  118. // -0 - 180 -> links
  119. // 0 - 180 -> links
  120. // 360 - 180 -> rechts
  121. output = 1.0 * (winkelDiff - 180) / 180.0;
  122. // -1 - +1
  123. setServo(output);
  124. }
  125. }
  126. #endif