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.

177 lines
4.3 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. int temperature;
  16. int 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. //Kommandos
  24. #define nothing 9 //reset/nichts tun
  25. #define speedA 1 // set speed A + speed
  26. #define dirA 2 // set direction A + dir
  27. #define speedB 3 // set speed B + speed
  28. #define dirB 4 // set direction B + dir
  29. #define goDrive 5 //go + time to go
  30. #define stopDrive 6 //stop
  31. #define getTemp 7 //get temperature
  32. #define timeToDrive 8 //Zeitdauer des fahrens
  33. #define getDistance 10 //Abstand zu Objekten
  34. //Motortreiber
  35. //#include <MX1508.h>
  36. #include <L298N.h>
  37. #define BEEP 14
  38. /*
  39. #define PWM_PINA 10
  40. #define PINA 8
  41. #define PWM_PINB 9
  42. #define PINB 7
  43. #define NUMPWM 1
  44. #define RESOLUTION 255 */
  45. //MX1508 motorA(PWM_PINA,PINA, FAST_DECAY, NUMPWM);
  46. //MX1508 motorB(PWM_PINB,PINB, FAST_DECAY, NUMPWM);
  47. L298N drive;
  48. volatile int pwmA = 0;
  49. volatile int pwmB = 0;
  50. bool forwardA = true;
  51. bool forwardB = true;
  52. volatile bool driveOn = false;
  53. int temperatur = 0;
  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.openReadingPipe(0, address);
  62. radio.setPALevel(RF24_PA_MAX);
  63. radio.startListening();
  64. clearCommands();
  65. //Temperatur- und Abstandsmessung
  66. tempDistSetup();
  67. setEchoPins(0, 0); //Setze die pins für den Abstandsensor aus denen gelesenw erden soll das erster ist der Trigger-, das zweite der Echopin
  68. timer = millis();
  69. }
  70. void loop() {
  71. if (radio.available()) {
  72. radio.read(&commands, sizeof(commands));
  73. commandInterpretation();
  74. }
  75. //Serial.println(driveOn);
  76. if(((millis() - driveTimeDiff) > driveTimeout)) {
  77. pwmA = 0;
  78. pwmB = 0;
  79. }
  80. drive.setPWM_A(pwmA);
  81. drive.setPWM_B(pwmB);
  82. //Temperatur- und Abstandsmessung
  83. temperature = dallas(4, 0);
  84. if(millis() - timer >= 100){
  85. measureDistance();
  86. timer = millis();
  87. }
  88. distance = calculateDistance();
  89. }
  90. void commandInterpretation() {
  91. for(uint8_t i = 0; i < 28; i += 3) {
  92. switch(commands[i]) {
  93. case nothing : {
  94. pwmA = 0;
  95. pwmB = 0;
  96. forwardA = true;
  97. forwardB = true;
  98. driveOn = false;
  99. break;
  100. }
  101. case speedA : {
  102. int temp1;
  103. temp1 = (0xFF00 & (commands[i+1] << 8));
  104. temp1 |= (0x00FF & commands[i+2]);
  105. pwmA = temp1;
  106. break;
  107. }
  108. case dirA : {
  109. bool temp2 = commands[i+2];
  110. break;
  111. }
  112. case speedB : {
  113. int temp3;
  114. temp3 = (0xFF00 & (commands[i+1] << 8));
  115. temp3 |= (0x00FF & commands[i+2]);
  116. pwmB = temp3;
  117. break;
  118. }
  119. case dirB : {
  120. bool temp4;
  121. temp4 = commands[i+2];
  122. break;
  123. }
  124. case goDrive : {
  125. driveOn = true;
  126. break;
  127. }
  128. case stopDrive : {
  129. driveOn = false;
  130. break;
  131. }
  132. case getTemp : {
  133. int temp5;
  134. temp5 = (0xFF00 & (commands[i+1] << 8));
  135. temp5 |= (0x00FF & commands[i+2]);
  136. break;
  137. }
  138. case timeToDrive : {
  139. uint16_t driveTime = 0;
  140. driveTime = (0xFF00 & (commands[i+1] << 8));
  141. driveTime |= (0x00FF & commands[i+2]);
  142. driveTimeout = (long)driveTime;
  143. driveTimeDiff = millis();
  144. Serial.println(driveTimeout);
  145. break;
  146. }
  147. default : { /* pwmA = 0;
  148. pwmB = 0;
  149. forwardA = true;
  150. forwardB = true;
  151. driveOn = false; */
  152. break;
  153. }
  154. }
  155. }
  156. clearCommands();
  157. }