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.

183 lines
4.4 KiB

  1. /*
  2. TMRh20 2014 - Optimized RF24 Library Fork
  3. */
  4. /**
  5. * Example using Dynamic Payloads
  6. *
  7. * This is an example of how to use payloads of a varying (dynamic) size.
  8. */
  9. #include <cstdlib>
  10. #include <iostream>
  11. #include <sstream>
  12. #include <string>
  13. #include <RF24/RF24.h>
  14. using namespace std;
  15. //
  16. // Hardware configuration
  17. // Configure the appropriate pins for your connections
  18. /****************** Raspberry Pi ***********************/
  19. RF24 radio(22,0); // CE GPIO, CSN SPI-BUS
  20. int interruptPin = 23; // GPIO pin for interrupts
  21. /**************************************************************/
  22. // Radio pipe addresses for the 2 nodes to communicate.
  23. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  24. const int min_payload_size = 4;
  25. const int max_payload_size = 32;
  26. const int payload_size_increments_by = 1;
  27. int next_payload_size = min_payload_size;
  28. char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
  29. bool role_ping_out = 1, role_pong_back = 0;
  30. bool role = 0;
  31. void intHandler(){
  32. //
  33. // Pong back role. Receive each packet, dump it out, and send it back
  34. //
  35. if ( role == role_pong_back )
  36. {
  37. // if there is data ready
  38. if ( radio.available() )
  39. {
  40. // Dump the payloads until we've gotten everything
  41. uint8_t len=0;
  42. while (radio.available())
  43. {
  44. // Fetch the payload, and see if this was the last one.
  45. len = radio.getDynamicPayloadSize();
  46. radio.read( receive_payload, len );
  47. // Put a zero at the end for easy printing
  48. receive_payload[len] = 0;
  49. // Spew it
  50. printf("Got payload size=%i value=%s\n\r",len,receive_payload);
  51. }
  52. // First, stop listening so we can talk
  53. radio.stopListening();
  54. // Send the final one back.
  55. radio.write( receive_payload, len );
  56. printf("Sent response.\n\r");
  57. // Now, resume listening so we catch the next packets.
  58. radio.startListening();
  59. }
  60. }
  61. }
  62. int main(int argc, char** argv){
  63. // Print preamble:
  64. cout << "RF24/examples/pingpair_dyn/\n";
  65. // Setup and configure rf radio
  66. radio.begin();
  67. radio.enableDynamicPayloads();
  68. radio.setRetries(5,15);
  69. radio.printDetails();
  70. /********* Role chooser ***********/
  71. printf("\n ************ Role Setup ***********\n");
  72. string input = "";
  73. char myChar = {0};
  74. cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) \n>";
  75. getline(cin,input);
  76. if(input.length() == 1) {
  77. myChar = input[0];
  78. if(myChar == '0'){
  79. cout << "Role: Pong Back, awaiting transmission " << endl << endl;
  80. }else{ cout << "Role: Ping Out, starting transmission " << endl << endl;
  81. role = role_ping_out;
  82. }
  83. }
  84. /***********************************/
  85. if ( role == role_ping_out ) {
  86. radio.openWritingPipe(pipes[0]);
  87. radio.openReadingPipe(1,pipes[1]);
  88. } else {
  89. radio.openWritingPipe(pipes[1]);
  90. radio.openReadingPipe(1,pipes[0]);
  91. radio.startListening();
  92. }
  93. attachInterrupt(interruptPin, INT_EDGE_FALLING, intHandler); //Attach interrupt to bcm pin 23
  94. // forever loop
  95. while (1)
  96. {
  97. if (role == role_ping_out)
  98. {
  99. // The payload will always be the same, what will change is how much of it we send.
  100. static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
  101. // First, stop listening so we can talk.
  102. radio.stopListening();
  103. // Take the time, and send it. This will block until complete
  104. printf("Now sending length %i...",next_payload_size);
  105. radio.write( send_payload, next_payload_size );
  106. // Now, continue listening
  107. radio.startListening();
  108. // Wait here until we get a response, or timeout
  109. unsigned long started_waiting_at = millis();
  110. bool timeout = false;
  111. while ( ! radio.available() && ! timeout )
  112. if (millis() - started_waiting_at > 500 )
  113. timeout = true;
  114. // Describe the results
  115. if ( timeout )
  116. {
  117. printf("Failed, response timed out.\n\r");
  118. }
  119. else
  120. {
  121. // Grab the response, compare, and send to debugging spew
  122. uint8_t len = radio.getDynamicPayloadSize();
  123. radio.read( receive_payload, len );
  124. // Put a zero at the end for easy printing
  125. receive_payload[len] = 0;
  126. // Spew it
  127. printf("Got response size=%i value=%s\n\r",len,receive_payload);
  128. }
  129. // Update size for next time.
  130. next_payload_size += payload_size_increments_by;
  131. if ( next_payload_size > max_payload_size )
  132. next_payload_size = min_payload_size;
  133. // Try again 1s later
  134. delay(100);
  135. }
  136. }
  137. }