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.

85 lines
1.7 KiB

  1. #define xPin A5
  2. #define yPin A6
  3. int16_t xValue = 0;
  4. int16_t yValue = 0;
  5. int16_t leftPWM = 0;
  6. int16_t rightPWM = 0;
  7. const int16_t deadZone = 10;
  8. void setup() {
  9. Serial.begin(115200);
  10. }
  11. void loop() {
  12. koordinaten(analogRead(xPin), analogRead(yPin));
  13. motorPWM();
  14. Serial.print("X: ");
  15. Serial.println(xValue);
  16. Serial.print("Y: ");
  17. Serial.println(yValue);
  18. Serial.print("links: ");
  19. Serial.println(leftPWM);
  20. Serial.print("rechts: ");
  21. Serial.println(rightPWM);
  22. delay(200);
  23. }
  24. void koordinaten(uint16_t x, uint16_t y) {
  25. //9-bit reichen, der ADC schafft bestenfalls 8-bit praezision
  26. x = x >> 1;
  27. y = y >> 1;
  28. xValue = map(x, 0, 511, -255, 255);
  29. yValue = map(y, 0, 511, 255, -255);
  30. }
  31. void motorPWM() {
  32. if((abs(xValue) > deadZone) || (abs(yValue) > deadZone)) {
  33. if(yValue >= 0) {
  34. if(xValue >= 0) {
  35. //+y , +x
  36. leftPWM = yValue;
  37. rightPWM = yValue - xValue;
  38. if(xValue >= yValue) {
  39. leftPWM = 255;
  40. rightPWM = -255;
  41. }
  42. } else {
  43. //+y , -x
  44. leftPWM = yValue;
  45. rightPWM = yValue + xValue;
  46. if(abs(xValue) >= yValue) {
  47. leftPWM = -255;
  48. rightPWM = 255;
  49. }
  50. }
  51. } else {
  52. if(xValue >= 0) {
  53. //-y , +x
  54. leftPWM = yValue;
  55. rightPWM = yValue + xValue;
  56. if(xValue >= abs(yValue)) {
  57. leftPWM = 255;
  58. rightPWM = -255;
  59. }
  60. } else {
  61. //-y , -x
  62. leftPWM = yValue;
  63. rightPWM = yValue - xValue;
  64. if(abs(xValue) >= abs(yValue)) {
  65. leftPWM = -255;
  66. rightPWM = 255;
  67. }
  68. }
  69. }
  70. if(abs(xValue) < deadZone) {
  71. leftPWM = yValue;
  72. rightPWM = yValue;
  73. }
  74. } else {
  75. leftPWM = 0;
  76. rightPWM = 0;
  77. }
  78. }