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.

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