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.

137 lines
6.0 KiB

  1. /*
  2. Dec 2014 - TMRh20 - Updated
  3. Derived from examples by J. Coliz <maniacbug@ymail.com>
  4. */
  5. /**
  6. * Example for efficient call-response using ack-payloads
  7. *
  8. * This example continues to make use of all the normal functionality of the radios including
  9. * the auto-ack and auto-retry features, but allows ack-payloads to be written optionlly as well.
  10. * This allows very fast call-response communication, with the responding radio never having to
  11. * switch out of Primary Receiver mode to send back a payload, but having the option to switch to
  12. * primary transmitter if wanting to initiate communication instead of respond to a commmunication.
  13. */
  14. #include <SPI.h>
  15. #include "RF24.h"
  16. /****************** User Config ***************************/
  17. /*** Set this radio as radio number 0 or 1 ***/
  18. bool radioNumber = 0;
  19. /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
  20. RF24 radio(7,8);
  21. /**********************************************************/
  22. // Topology
  23. byte addresses[][6] = {"1Node","2Node"}; // Radio pipe addresses for the 2 nodes to communicate.
  24. // Role management: Set up role. This sketch uses the same software for all the nodes
  25. // in this system. Doing so greatly simplifies testing.
  26. typedef enum { role_ping_out = 1, role_pong_back } role_e; // The various roles supported by this sketch
  27. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"}; // The debug-friendly names of those roles
  28. role_e role = role_pong_back; // The role of the current running sketch
  29. byte counter = 1; // A single byte to keep track of the data being sent back and forth
  30. void setup(){
  31. Serial.begin(115200);
  32. Serial.println(F("RF24/examples/GettingStarted_CallResponse"));
  33. Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  34. // Setup and configure radio
  35. radio.begin();
  36. radio.enableAckPayload(); // Allow optional ack payloads
  37. radio.enableDynamicPayloads(); // Ack payloads are dynamic payloads
  38. if(radioNumber){
  39. radio.openWritingPipe(addresses[1]); // Both radios listen on the same pipes by default, but opposite addresses
  40. radio.openReadingPipe(1,addresses[0]); // Open a reading pipe on address 0, pipe 1
  41. }else{
  42. radio.openWritingPipe(addresses[0]);
  43. radio.openReadingPipe(1,addresses[1]);
  44. }
  45. radio.startListening(); // Start listening
  46. radio.writeAckPayload(1,&counter,1); // Pre-load an ack-paylod into the FIFO buffer for pipe 1
  47. //radio.printDetails();
  48. }
  49. void loop(void) {
  50. /****************** Ping Out Role ***************************/
  51. if (role == role_ping_out){ // Radio is in ping mode
  52. byte gotByte; // Initialize a variable for the incoming response
  53. radio.stopListening(); // First, stop listening so we can talk.
  54. Serial.print(F("Now sending ")); // Use a simple byte counter as payload
  55. Serial.println(counter);
  56. unsigned long time = micros(); // Record the current microsecond count
  57. if ( radio.write(&counter,1) ){ // Send the counter variable to the other radio
  58. if(!radio.available()){ // If nothing in the buffer, we got an ack but it is blank
  59. Serial.print(F("Got blank response. round-trip delay: "));
  60. Serial.print(micros()-time);
  61. Serial.println(F(" microseconds"));
  62. }else{
  63. while(radio.available() ){ // If an ack with payload was received
  64. radio.read( &gotByte, 1 ); // Read it, and display the response time
  65. unsigned long timer = micros();
  66. Serial.print(F("Got response "));
  67. Serial.print(gotByte);
  68. Serial.print(F(" round-trip delay: "));
  69. Serial.print(timer-time);
  70. Serial.println(F(" microseconds"));
  71. counter++; // Increment the counter variable
  72. }
  73. }
  74. }else{ Serial.println(F("Sending failed.")); } // If no ack response, sending failed
  75. delay(1000); // Try again later
  76. }
  77. /****************** Pong Back Role ***************************/
  78. if ( role == role_pong_back ) {
  79. byte pipeNo, gotByte; // Declare variables for the pipe and the byte received
  80. while( radio.available(&pipeNo)){ // Read all available payloads
  81. radio.read( &gotByte, 1 );
  82. // Since this is a call-response. Respond directly with an ack payload.
  83. gotByte += 1; // Ack payloads are much more efficient than switching to transmit mode to respond to a call
  84. radio.writeAckPayload(pipeNo,&gotByte, 1 ); // This can be commented out to send empty payloads.
  85. Serial.print(F("Loaded next response "));
  86. Serial.println(gotByte);
  87. }
  88. }
  89. /****************** Change Roles via Serial Commands ***************************/
  90. if ( Serial.available() )
  91. {
  92. char c = toupper(Serial.read());
  93. if ( c == 'T' && role == role_pong_back ){
  94. Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  95. role = role_ping_out; // Become the primary transmitter (ping out)
  96. counter = 1;
  97. }else
  98. if ( c == 'R' && role == role_ping_out ){
  99. Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  100. role = role_pong_back; // Become the primary receiver (pong back)
  101. radio.startListening();
  102. counter = 1;
  103. radio.writeAckPayload(1,&counter,1);
  104. }
  105. }
  106. }