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. /*
  36. #define PWM_PINA 10
  37. #define PINA 8
  38. #define PWM_PINB 9
  39. #define PINB 7
  40. #define NUMPWM 1
  41. #define RESOLUTION 255 */
  42. //MX1508 motorA(PWM_PINA,PINA, FAST_DECAY, NUMPWM);
  43. //MX1508 motorB(PWM_PINB,PINB, FAST_DECAY, NUMPWM);
  44. L298N drive;
  45. volatile int pwmA = 0;
  46. volatile int pwmB = 0;
  47. bool forwardA = true;
  48. bool forwardB = true;
  49. volatile bool driveOn = false;
  50. int temperatur = 0;
  51. volatile long driveTimeout = 0;
  52. volatile long driveTimeDiff = 0;
  53. void setup() {
  54. Serial.begin(115200);
  55. // motorA.setPWM16(2,RESOLUTION);
  56. // motorB.setPWM16(2,RESOLUTION);
  57. radio.begin();
  58. radio.openReadingPipe(0, address);
  59. radio.setPALevel(RF24_PA_MAX);
  60. radio.startListening();
  61. clearCommands();
  62. }
  63. void loop() {
  64. if (radio.available()) {
  65. radio.read(&commands, sizeof(commands));
  66. commandInterpretation();
  67. }
  68. //Serial.println(driveOn);
  69. if(((millis() - driveTimeDiff) > driveTimeout)) {
  70. pwmA = 0;
  71. pwmB = 0;
  72. }
  73. drive.setPWM_A(pwmA);
  74. drive.setPWM_B(pwmB);
  75. }
  76. void commandInterpretation() {
  77. for(uint8_t i = 0; i < 28; i += 3) {
  78. switch(commands[i]) {
  79. case nothing : {
  80. pwmA = 0;
  81. pwmB = 0;
  82. forwardA = true;
  83. forwardB = true;
  84. driveOn = false;
  85. break;
  86. }
  87. case speedA : {
  88. int temp1;
  89. temp1 = (0xFF00 & (commands[i+1] << 8));
  90. temp1 |= (0x00FF & commands[i+2]);
  91. pwmA = temp1;
  92. break;
  93. }
  94. case dirA : {
  95. bool temp2 = commands[i+2];
  96. break;
  97. }
  98. case speedB : {
  99. int temp3;
  100. temp3 = (0xFF00 & (commands[i+1] << 8));
  101. temp3 |= (0x00FF & commands[i+2]);
  102. pwmB = temp3;
  103. break;
  104. }
  105. case dirB : {
  106. bool temp4;
  107. temp4 = commands[i+2];
  108. break;
  109. }
  110. case goDrive : {
  111. driveOn = true;
  112. break;
  113. }
  114. case stopDrive : {
  115. driveOn = false;
  116. break;
  117. }
  118. case getTemp : {
  119. temperatur = (0xFF00 & (commands[i+1] << 8));
  120. temperatur |= (0x00FF & commands[i+2]);
  121. break;
  122. }
  123. case timeToDrive : {
  124. uint16_t driveTime = 0;
  125. driveTime = (0xFF00 & (commands[i+1] << 8));
  126. driveTime |= (0x00FF & commands[i+2]);
  127. driveTimeout = (long)driveTime;
  128. driveTimeDiff = millis();
  129. Serial.println(driveTimeout);
  130. break;
  131. }
  132. default : { /* pwmA = 0;
  133. pwmB = 0;
  134. forwardA = true;
  135. forwardB = true;
  136. driveOn = false; */
  137. break;
  138. }
  139. }
  140. }
  141. clearCommands();
  142. }