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.

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