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.

106 lines
2.5 KiB

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