driveRegler.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;
  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";
  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. istRichtung = sollRichtung;
  41. }
  42. else {
  43. dist = gps.distance_between(flat, flon, s_lat, s_lon);
  44. Serial.print("Latitude: ");
  45. Serial.print(flat,10);
  46. Serial.print(" \tLongitude: ");
  47. Serial.print(flon,10);
  48. Serial.print(" \tDist: ");
  49. Serial.print(dist);
  50. Serial.print(" \tTime: ");
  51. Serial.print(age);
  52. Serial.print("ms. ");
  53. sollRichtung = gps.course_to(flat, flon, s_lat, s_lon); // Koordinaten zum Sportplatz !
  54. if (Latitudealt != flat && Longitudealt != flon) {
  55. istRichtung = gps.course_to(Latitudealt, Longitudealt, flat, flon);
  56. Longitudealt = flon;
  57. Latitudealt = flat;
  58. }
  59. //istRichtung = gps.f_course(); // Korrigieren der Lenkung zum Ziel
  60. }
  61. start = millis();
  62. Serial3.print("Auto: \t");
  63. Serial3.print(gasVal);
  64. Serial3.print(" \t");
  65. Serial3.print(servoVal);
  66. Serial3.print(" \t");
  67. Serial3.print(istRichtung);
  68. Serial3.print(" \t");
  69. Serial3.print(sollRichtung);
  70. Serial3.print(" \t");
  71. Serial3.print(output);
  72. Serial3.print(" \t");
  73. Serial3.print(flat,10);
  74. Serial3.print(" \t");
  75. Serial3.print(flon,10);
  76. Serial3.print(" \t");
  77. Serial3.print(dist);
  78. Serial3.print(" \t");
  79. Serial3.print(age);
  80. Serial3.println();
  81. Serial.print(gasVal);
  82. Serial.print(" \t");
  83. Serial.print(servoVal);
  84. Serial.print(" \t");
  85. Serial.print(istRichtung);
  86. Serial.print("\t");
  87. Serial.print(sollRichtung);
  88. Serial.print("\t");
  89. Serial.println(output);
  90. }
  91. //istRichtung = mpu.winkelZ;
  92. //sollRichtung = spm.data[1] * 180;
  93. if(spm.hasData()) {
  94. setMotor(spm.data[2]*0.2);
  95. output = (((int)sollRichtung - (int)istRichtung + 180) % 360 - 180) / 180.0 * 0.3;
  96. setServo(output);
  97. }
  98. }
  99. #endif