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.

185 lines
4.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  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. const byte address2[6] = "00002";
  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 getDistance 10 //Abstand zu Objekten
  32. //Motortreiber
  33. #include <L298N.h>
  34. #define BEEP 14 //Motoren drehen bei diesem PWM Wert nicht, aber fiepen
  35. L298N drive;
  36. volatile int pwmA = 0;
  37. volatile int pwmB = 0;
  38. bool forwardA = true;
  39. bool forwardB = true;
  40. volatile bool driveOn = false;
  41. volatile uint32_t driveTimeout = 0;
  42. volatile uint32_t driveTimeDiff = 0;
  43. volatile bool startNewMeasurement = true;
  44. volatile uint32_t pulseStart = 0;
  45. volatile uint32_t pulseLength = 1;
  46. volatile bool newResult = false;
  47. uint32_t timer = 0;
  48. int16_t temperature = 0;
  49. uint16_t distance = 50;
  50. void setup() {
  51. Serial.begin(115200);
  52. // motorA.setPWM16(2,RESOLUTION);
  53. // motorB.setPWM16(2,RESOLUTION);
  54. radio.begin();
  55. radio.openWritingPipe(address2);
  56. radio.openReadingPipe(1, address);
  57. radio.setPALevel(RF24_PA_MAX);
  58. radio.startListening();
  59. clearCommands();
  60. //Temperatur- und Abstandsmessung
  61. setEchoPins(A2, 6); //16: A2, 6: D6
  62. tempDistSetup();
  63. timer = millis();
  64. }
  65. void loop() {
  66. //Temperatur- und Abstandsmessung
  67. if((millis() - timer) >= 100){
  68. temperature = dallas(4, 0);
  69. if(startNewMeasurement) {
  70. measureDistance();
  71. }
  72. timer = millis();
  73. }
  74. if(newResult) {
  75. // Serial.println("berechnen");
  76. distance = calculateDistance();
  77. }
  78. //wurden Kommandos empfangen?
  79. if (radio.available()) {
  80. radio.read(&commands, sizeof(commands));
  81. commandInterpretation();
  82. }
  83. //Antrieb abschalten wenn kein neuer Fahrbefehl kommt
  84. if(((millis() - driveTimeDiff) > driveTimeout)) {
  85. pwmA = 0;
  86. pwmB = 0;
  87. }
  88. //Wenn Mindestdistanz unterschritten, stopp
  89. if(distance < 20){
  90. if(pwmA < 0 && pwmB < 0){
  91. pwmA = 0;
  92. pwmB = 0;
  93. }
  94. }
  95. drive.setPWM_A(pwmA);
  96. drive.setPWM_B(pwmB);
  97. }
  98. void commandInterpretation() {
  99. for(uint8_t i = 0; i < 28; i += 3) {
  100. switch(commands[i]) {
  101. case nothing : {
  102. pwmA = 0;
  103. pwmB = 0;
  104. forwardA = true;
  105. forwardB = true;
  106. driveOn = false;
  107. break;
  108. }
  109. case speedA : {
  110. int temp1;
  111. temp1 = (0xFF00 & (commands[i+1] << 8));
  112. temp1 |= (0x00FF & commands[i+2]);
  113. pwmA = temp1;
  114. break;
  115. }
  116. case dirA : {
  117. bool temp2 = commands[i+2];
  118. break;
  119. }
  120. case speedB : {
  121. int temp3;
  122. temp3 = (0xFF00 & (commands[i+1] << 8));
  123. temp3 |= (0x00FF & commands[i+2]);
  124. pwmB = temp3;
  125. break;
  126. }
  127. case dirB : {
  128. bool temp4;
  129. temp4 = commands[i+2];
  130. break;
  131. }
  132. case goDrive : {
  133. driveOn = true;
  134. break;
  135. }
  136. case stopDrive : {
  137. driveOn = false;
  138. break;
  139. }
  140. case getTemp : {
  141. //Serial.println("Senden!");
  142. radio.stopListening();
  143. int16_t sendData = temperature;
  144. radio.write(&sendData, sizeof(int16_t));
  145. radio.startListening();
  146. break;
  147. }
  148. case timeToDrive : {
  149. uint16_t driveTime = 0;
  150. driveTime = (0xFF00 & (commands[i+1] << 8));
  151. driveTime |= (0x00FF & commands[i+2]);
  152. driveTimeout = (uint32_t)driveTime;
  153. driveTimeDiff = millis();
  154. //Serial.println(driveTimeout);
  155. break;
  156. }
  157. default : {
  158. break;
  159. }
  160. }
  161. }
  162. clearCommands();
  163. }