Auto_Robotik_Datenfusion_Final.ino 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <Arduino.h>
  2. #include <avr/wdt.h>
  3. #include "spm4648.h"
  4. #include "MPU6050.h"
  5. #include "globals.h"
  6. //#include "driveCircle.h"
  7. //#include "driveRegler.h"
  8. #include "drivefusion.h"
  9. bool lastSwitch = true;
  10. uint32_t mainLoopDelay = 16000;
  11. uint32_t mainLoopStart = 0;
  12. void driveRemote() {
  13. if(spm.hasData()) {
  14. setMotor(spm.data[2]*0.3);
  15. setServo(spm.data[1]);
  16. Serial.print("Manual: \t");
  17. Serial.print(gasVal);
  18. Serial.print(" \t");
  19. Serial.print(servoVal);
  20. Serial.print(" \t");
  21. Serial.print(spm.data[2]*0.2);
  22. Serial.print("\t");
  23. Serial.println(spm.data[1]);
  24. }
  25. }
  26. void setup() {
  27. Serial.begin(115200);
  28. Serial1.begin(115200);
  29. Serial2.begin(38400);
  30. Serial3.begin(9600);
  31. Serial.println("boot");
  32. mpu.setUpMPU();
  33. mpu.callibrateGyroValues();
  34. initServos();
  35. Serial.println("setup finished");
  36. wdt_enable(WDTO_250MS);
  37. }
  38. void loop() {
  39. wdt_reset();
  40. spm.read(Serial1);
  41. mpu.readAndProcessGyroData();
  42. mpu.readAndProcessAccelData();
  43. mpu.printData();
  44. if(spm.isConnected()) {
  45. bool sw = spm.data[4] > 0;
  46. if(sw == false && lastSwitch == true) {
  47. initAuto();
  48. }
  49. if(sw) {
  50. msAuto = millis();
  51. driveRemote();
  52. } else {
  53. driveAuto();
  54. }
  55. lastSwitch = sw;
  56. } else {
  57. //safe mode
  58. Serial.println(F("No connection!"));
  59. setMotor(0);
  60. setServo(0);
  61. }
  62. mainLoopDelay = micros() - mainLoopStart;
  63. if(mainLoopDelay < 10000)
  64. delayMicroseconds(10000 - mainLoopDelay); //max. 16ms
  65. mainLoopDelay = micros() - mainLoopStart;
  66. //Serial.println(mainLoopDelay/1000.0);
  67. mainLoopStart = micros();
  68. }