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.

218 lines
7.5 KiB

  1. /*
  2. * Getting Started example sketch for nRF24L01+ radios
  3. * This is a very basic example of how to send data from one node to another
  4. * but modified to include failure handling.
  5. *
  6. * The nrf24l01+ radios are fairly reliable devices, but on breadboards etc, with inconsistent wiring, failures may
  7. * occur randomly after many hours to days or weeks. This sketch demonstrates how to handle the various failures and
  8. * keep the radio operational.
  9. *
  10. * The three main failure modes of the radio include:
  11. * Writing to radio: Radio unresponsive - Fixed internally by adding a timeout to the internal write functions in RF24 (failure handling)
  12. * Reading from radio: Available returns true always - Fixed by adding a timeout to available functions by the user. This is implemented internally in RF24Network.
  13. * Radio configuration settings are lost - Fixed by monitoring a value that is different from the default, and re-configuring the radio if this setting reverts to the default.
  14. *
  15. * The printDetails output should appear as follows for radio #0:
  16. *
  17. * STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
  18. * RX_ADDR_P0-1 = 0x65646f4e31 0x65646f4e32
  19. * RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
  20. * TX_ADDR = 0x65646f4e31
  21. * RX_PW_P0-6 = 0x20 0x20 0x00 0x00 0x00 0x00
  22. * EN_AA = 0x3f
  23. * EN_RXADDR = 0x02
  24. * RF_CH = 0x4c
  25. * RF_SETUP = 0x03
  26. * CONFIG = 0x0f
  27. * DYNPD/FEATURE = 0x00 0x00
  28. * Data Rate = 1MBPS
  29. * Model = nRF24L01+
  30. * CRC Length = 16 bits
  31. * PA Power = PA_LOW
  32. *
  33. *Users can use this sketch to troubleshoot radio module wiring etc. as it makes the radios hot-swapable
  34. *
  35. * Updated: 2019 by TMRh20
  36. */
  37. #include <SPI.h>
  38. #include "RF24.h"
  39. #include "printf.h"
  40. /****************** User Config ***************************/
  41. /*** Set this radio as radio number 0 or 1 ***/
  42. bool radioNumber = 0;
  43. /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
  44. RF24 radio(7,8);
  45. /**********************************************************/
  46. byte addresses[][6] = {"1Node","2Node"};
  47. // Used to control whether this node is sending or receiving
  48. bool role = 0;
  49. /**********************************************************/
  50. //Function to configure the radio
  51. void configureRadio(){
  52. radio.begin();
  53. // Set the PA Level low to prevent power supply related issues since this is a
  54. // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  55. radio.setPALevel(RF24_PA_LOW);
  56. // Open a writing and reading pipe on each radio, with opposite addresses
  57. if(radioNumber){
  58. radio.openWritingPipe(addresses[1]);
  59. radio.openReadingPipe(1,addresses[0]);
  60. }else{
  61. radio.openWritingPipe(addresses[0]);
  62. radio.openReadingPipe(1,addresses[1]);
  63. }
  64. // Start the radio listening for data
  65. radio.startListening();
  66. radio.printDetails();
  67. }
  68. /**********************************************************/
  69. void setup() {
  70. Serial.begin(115200);
  71. Serial.println(F("RF24/examples/GettingStarted"));
  72. Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  73. printf_begin();
  74. configureRadio();
  75. }
  76. uint32_t configTimer = millis();
  77. void loop() {
  78. if(radio.failureDetected){
  79. radio.failureDetected = false;
  80. delay(250);
  81. Serial.println("Radio failure detected, restarting radio");
  82. configureRadio();
  83. }
  84. //Every 5 seconds, verify the configuration of the radio. This can be done using any
  85. //setting that is different from the radio defaults.
  86. if(millis() - configTimer > 5000){
  87. configTimer = millis();
  88. if(radio.getDataRate() != RF24_1MBPS){
  89. radio.failureDetected = true;
  90. Serial.print("Radio configuration error detected");
  91. }
  92. }
  93. /****************** Ping Out Role ***************************/
  94. if (role == 1) {
  95. radio.stopListening(); // First, stop listening so we can talk.
  96. Serial.println(F("Now sending"));
  97. unsigned long start_time = micros(); // Take the time, and send it. This will block until complete
  98. if (!radio.write( &start_time, sizeof(unsigned long) )){
  99. Serial.println(F("failed"));
  100. }
  101. radio.startListening(); // Now, continue listening
  102. unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
  103. boolean timeout = false; // Set up a variable to indicate if a response was received or not
  104. while ( ! radio.available() ){ // While nothing is received
  105. if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
  106. timeout = true;
  107. break;
  108. }
  109. }
  110. if ( timeout ){ // Describe the results
  111. Serial.println(F("Failed, response timed out."));
  112. }else{
  113. unsigned long got_time; // Grab the response, compare, and send to debugging spew
  114. //Failure Handling:
  115. uint32_t failTimer = millis();
  116. while(radio.available()){ //If available always returns true, there is a problem
  117. if(millis() - failTimer > 250){
  118. radio.failureDetected = true;
  119. Serial.println("Radio available failure detected");
  120. break;
  121. }
  122. radio.read( &got_time, sizeof(unsigned long) );
  123. }
  124. unsigned long end_time = micros();
  125. // Spew it
  126. Serial.print(F("Sent "));
  127. Serial.print(start_time);
  128. Serial.print(F(", Got response "));
  129. Serial.print(got_time);
  130. Serial.print(F(", Round-trip delay "));
  131. Serial.print(end_time-start_time);
  132. Serial.println(F(" microseconds"));
  133. }
  134. // Try again 1s later
  135. delay(1000);
  136. }
  137. /****************** Pong Back Role ***************************/
  138. if ( role == 0 )
  139. {
  140. unsigned long got_time;
  141. if( radio.available()){
  142. uint32_t failTimer = millis(); // Variable for the received timestamp
  143. while (radio.available()) { // While there is data ready
  144. if(millis()-failTimer > 500){
  145. Serial.println("Radio available failure detected");
  146. radio.failureDetected = true;
  147. break;
  148. }
  149. radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
  150. }
  151. radio.stopListening(); // First, stop listening so we can talk
  152. radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
  153. radio.startListening(); // Now, resume listening so we catch the next packets.
  154. Serial.print(F("Sent response "));
  155. Serial.println(got_time);
  156. }
  157. }
  158. /****************** Change Roles via Serial Commands ***************************/
  159. if ( Serial.available() )
  160. {
  161. char c = toupper(Serial.read());
  162. if ( c == 'T' && role == 0 ){
  163. Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  164. role = 1; // Become the primary transmitter (ping out)
  165. }else
  166. if ( c == 'R' && role == 1 ){
  167. Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  168. role = 0; // Become the primary receiver (pong back)
  169. radio.startListening();
  170. }
  171. }
  172. } // Loop