Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.7 KiB

4 years ago
4 years ago
4 years ago
  1. #define xPin A5
  2. #define yPin A6
  3. #define leftMax 255//215
  4. #define leftMin -255//-100
  5. #define rightMax 255
  6. #define rightMin -255
  7. volatile int16_t xValue = 0;
  8. volatile int16_t yValue = 0;
  9. volatile int16_t leftPWM = 0;
  10. volatile int16_t rightPWM = 0;
  11. const int16_t deadZone = 10;
  12. /*
  13. void setup() {
  14. Serial.begin(115200);
  15. }
  16. void loop() {
  17. koordinaten(analogRead(xPin), analogRead(yPin));
  18. motorPWM();
  19. Serial.print("X: ");
  20. Serial.println(xValue);
  21. Serial.print("Y: ");
  22. Serial.println(yValue);
  23. Serial.print("links: ");
  24. Serial.println(leftPWM);
  25. Serial.print("rechts: ");
  26. Serial.println(rightPWM);
  27. delay(200);
  28. }
  29. */
  30. void motorMapping() {
  31. static long temp = millis();
  32. koordinaten(analogRead(xPin), analogRead(yPin));
  33. motorPWM();
  34. pwmB = map(leftPWM, -255,255,leftMin,leftMax);
  35. pwmA = map(rightPWM, -255,255,rightMin,rightMax);
  36. if((millis() - temp) > 100) {
  37. //lcd.clear();
  38. lcd.println("Links: ");
  39. lcd.println(pwmB);
  40. lcd.gotoXY(0,2);
  41. lcd.println("Rechts: ");
  42. lcd.println(pwmA);
  43. }
  44. senden();
  45. }
  46. void senden() {
  47. commands[0] = speedA;
  48. commands[1] = highByte(pwmA);
  49. commands[2] = lowByte(pwmA);
  50. commands[3] = speedB;
  51. commands[4] = highByte(pwmB);
  52. commands[5] = lowByte(pwmB);
  53. commands[6] = timeToDrive;
  54. commands[7] = highByte(driveTimeout);
  55. commands[8] = lowByte(driveTimeout);
  56. commands[9] = goDrive;
  57. commands[10] = getDistance;
  58. radio.write(&commands, sizeof(commands));
  59. }
  60. void koordinaten(uint16_t x, uint16_t y) {
  61. //9-bit reichen, der ADC schafft bestenfalls 8-bit praezision
  62. x = x >> 1;
  63. y = y >> 1;
  64. xValue = map(x, 0, 511, -255, 255);
  65. yValue = map(y, 0, 511, 255, -255);
  66. }
  67. void motorPWM() {
  68. if((abs(xValue) > deadZone) || (abs(yValue) > deadZone)) {
  69. if(yValue >= 0) {
  70. if(xValue >= 0) {
  71. //+y , +x
  72. leftPWM = yValue;
  73. rightPWM = yValue - xValue;
  74. if(xValue >= yValue) {
  75. leftPWM = 255;
  76. rightPWM = -255;
  77. }
  78. } else {
  79. //+y , -x
  80. leftPWM = yValue;
  81. rightPWM = yValue + xValue;
  82. if(abs(xValue) >= yValue) {
  83. leftPWM = -255;
  84. rightPWM = 255;
  85. }
  86. }
  87. } else {
  88. if(xValue >= 0) {
  89. //-y , +x
  90. leftPWM = yValue;
  91. rightPWM = yValue + xValue;
  92. if(xValue >= abs(yValue)) {
  93. leftPWM = 255;
  94. rightPWM = -255;
  95. }
  96. } else {
  97. //-y , -x
  98. leftPWM = yValue;
  99. rightPWM = yValue - xValue;
  100. if(abs(xValue) >= abs(yValue)) {
  101. leftPWM = -255;
  102. rightPWM = 255;
  103. }
  104. }
  105. }
  106. if(abs(xValue) < deadZone) {
  107. leftPWM = yValue;
  108. rightPWM = yValue;
  109. }
  110. } else {
  111. leftPWM = 0;
  112. rightPWM = 0;
  113. }
  114. }