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.

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