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.

157 lines
3.9 KiB

  1. /*
  2. * modularer Mini Roboter mit diversen Sensoren
  3. *
  4. *
  5. */
  6. //Funk
  7. #include <SPI.h>
  8. #include <nRF24L01.h>
  9. #include <RF24.h>
  10. #define CE A0
  11. #define CSN 3
  12. RF24 radio(A0, 3); // CE, CSN
  13. byte commands[32]; //byte 0 = command
  14. void inline clearCommands() {
  15. for(uint8_t i=0; i<32; i++) {
  16. commands[i] = 0xFF;
  17. }
  18. }
  19. const byte address[6] = "00001";
  20. //Kommandos
  21. #define nothing 9 //reset/nichts tun
  22. #define speedA 1 // set speed A + speed
  23. #define dirA 2 // set direction A + dir
  24. #define speedB 3 // set speed B + speed
  25. #define dirB 4 // set direction B + dir
  26. #define goDrive 5 //go + time to go
  27. #define stopDrive 6 //stop
  28. #define getTemp 7 //get temperature
  29. #define timeToDrive 8 //Zeitdauer des fahrens
  30. #define distance 10 //Abstand zu Objekten
  31. //Motortreiber
  32. //#include <MX1508.h>
  33. #include <L298N.h>
  34. #define BEEP 14
  35. #define PWM_PINA 10
  36. #define PINA 8
  37. #define PWM_PINB 9
  38. #define PINB 7
  39. #define NUMPWM 1
  40. #define RESOLUTION 255
  41. //MX1508 motorA(PWM_PINA,PINA, FAST_DECAY, NUMPWM);
  42. //MX1508 motorB(PWM_PINB,PINB, FAST_DECAY, NUMPWM);
  43. L298N drive;
  44. volatile int pwmA = 0;
  45. volatile int pwmB = 0;
  46. bool forwardA = true;
  47. bool forwardB = true;
  48. volatile bool driveOn = false;
  49. int temperatur = 0;
  50. volatile long driveTimeout = 0;
  51. volatile long driveTimeDiff = 0;
  52. void setup() {
  53. Serial.begin(115200);
  54. // motorA.setPWM16(2,RESOLUTION);
  55. // motorB.setPWM16(2,RESOLUTION);
  56. radio.begin();
  57. radio.openReadingPipe(0, address);
  58. radio.setPALevel(RF24_PA_MAX);
  59. radio.startListening();
  60. clearCommands();
  61. }
  62. void loop() {
  63. if (radio.available()) {
  64. radio.read(&commands, sizeof(commands));
  65. commandInterpretation();
  66. }
  67. //Serial.println(driveOn);
  68. if(((millis() - driveTimeDiff) > driveTimeout)) {
  69. pwmA = 0;
  70. pwmB = 0;
  71. }
  72. drive.setPWM_A(pwmA);
  73. drive.setPWM_B(pwmB);
  74. }
  75. void commandInterpretation() {
  76. for(uint8_t i = 0; i < 28; i += 3) {
  77. switch(commands[i]) {
  78. case nothing : {
  79. pwmA = 0;
  80. pwmB = 0;
  81. forwardA = true;
  82. forwardB = true;
  83. driveOn = false;
  84. break;
  85. }
  86. case speedA : {
  87. int temp1;
  88. temp1 = (0xFF00 & (commands[i+1] << 8));
  89. temp1 |= (0x00FF & commands[i+2]);
  90. pwmA = temp1;
  91. break;
  92. }
  93. case dirA : {
  94. bool temp2 = commands[i+2];
  95. break;
  96. }
  97. case speedB : {
  98. int temp3;
  99. temp3 = (0xFF00 & (commands[i+1] << 8));
  100. temp3 |= (0x00FF & commands[i+2]);
  101. pwmB = temp3;
  102. break;
  103. }
  104. case dirB : {
  105. bool temp4;
  106. temp4 = commands[i+2];
  107. break;
  108. }
  109. case goDrive : {
  110. driveOn = true;
  111. break;
  112. }
  113. case stopDrive : {
  114. driveOn = false;
  115. break;
  116. }
  117. case getTemp : {
  118. temperatur = (0xFF00 & (commands[i+1] << 8));
  119. temperatur |= (0x00FF & commands[i+2]);
  120. break;
  121. }
  122. case timeToDrive : {
  123. uint16_t driveTime = 0;
  124. driveTime = (0xFF00 & (commands[i+1] << 8));
  125. driveTime |= (0x00FF & commands[i+2]);
  126. driveTimeout = (long)driveTime;
  127. driveTimeDiff = millis();
  128. Serial.println(driveTimeout);
  129. break;
  130. }
  131. default : { /* pwmA = 0;
  132. pwmB = 0;
  133. forwardA = true;
  134. forwardB = true;
  135. driveOn = false; */
  136. break;
  137. }
  138. }
  139. }
  140. clearCommands();
  141. }