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.

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