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.

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