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.

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