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.

197 lines
4.7 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. 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 <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. volatile long driveTimeout = 0;
  52. volatile long driveTimeDiff = 0;
  53. volatile bool startNewMeasurement = true;
  54. volatile uint32_t pulseStart = 0;
  55. volatile uint32_t pulseLength = 1;
  56. volatile bool newResult = false;
  57. uint32_t timer = 0;
  58. int16_t temperature = 0;
  59. uint16_t distance = 50;
  60. void setup() {
  61. Serial.begin(115200);
  62. // motorA.setPWM16(2,RESOLUTION);
  63. // motorB.setPWM16(2,RESOLUTION);
  64. radio.begin();
  65. radio.openWritingPipe(address2);
  66. radio.openReadingPipe(1, address);
  67. radio.setPALevel(RF24_PA_MAX);
  68. radio.startListening();
  69. clearCommands();
  70. //Temperatur- und Abstandsmessung
  71. setEchoPins(A2, 6); //16: A2, 6: D6
  72. tempDistSetup();
  73. timer = millis();
  74. }
  75. void loop() {
  76. //Temperatur- und Abstandsmessung
  77. if((millis() - timer) >= 100){
  78. temperature = dallas(4, 0);
  79. if(startNewMeasurement) {
  80. measureDistance();
  81. }
  82. timer = millis();
  83. }
  84. if(newResult) {
  85. // Serial.println("berechnen");
  86. distance = calculateDistance();
  87. }
  88. if (radio.available()) {
  89. radio.read(&commands, sizeof(commands));
  90. commandInterpretation();
  91. }
  92. //Antrieb abschalten wenn kein neuer Fahrbefehl kommt
  93. if(((millis() - driveTimeDiff) > driveTimeout)) {
  94. pwmA = 0;
  95. pwmB = 0;
  96. }
  97. //Wenn Mindestdistanz unterschritten, stopp
  98. if(distance < 20){
  99. if(pwmA < 0 && pwmB < 0){
  100. pwmA = 0;
  101. pwmB = 0;
  102. }
  103. }
  104. drive.setPWM_A(pwmA);
  105. drive.setPWM_B(pwmB);
  106. }
  107. void commandInterpretation() {
  108. for(uint8_t i = 0; i < 28; i += 3) {
  109. switch(commands[i]) {
  110. case nothing : {
  111. pwmA = 0;
  112. pwmB = 0;
  113. forwardA = true;
  114. forwardB = true;
  115. driveOn = false;
  116. break;
  117. }
  118. case speedA : {
  119. int temp1;
  120. temp1 = (0xFF00 & (commands[i+1] << 8));
  121. temp1 |= (0x00FF & commands[i+2]);
  122. pwmA = temp1;
  123. break;
  124. }
  125. case dirA : {
  126. bool temp2 = commands[i+2];
  127. break;
  128. }
  129. case speedB : {
  130. int temp3;
  131. temp3 = (0xFF00 & (commands[i+1] << 8));
  132. temp3 |= (0x00FF & commands[i+2]);
  133. pwmB = temp3;
  134. break;
  135. }
  136. case dirB : {
  137. bool temp4;
  138. temp4 = commands[i+2];
  139. break;
  140. }
  141. case goDrive : {
  142. driveOn = true;
  143. break;
  144. }
  145. case stopDrive : {
  146. driveOn = false;
  147. break;
  148. }
  149. case getTemp : {
  150. //Serial.println("Senden!");
  151. radio.stopListening();
  152. int16_t sendData = temperature;
  153. radio.write(&sendData, sizeof(int16_t));
  154. radio.startListening();
  155. break;
  156. }
  157. case timeToDrive : {
  158. uint16_t driveTime = 0;
  159. driveTime = (0xFF00 & (commands[i+1] << 8));
  160. driveTime |= (0x00FF & commands[i+2]);
  161. driveTimeout = (long)driveTime;
  162. driveTimeDiff = millis();
  163. //Serial.println(driveTimeout);
  164. break;
  165. }
  166. default : {
  167. break;
  168. }
  169. }
  170. }
  171. clearCommands();
  172. }