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.

211 lines
5.7 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.h"
  14. using namespace std;
  15. //
  16. // Hardware configuration
  17. // Configure the appropriate pins for your connections
  18. /****************** Raspberry Pi ***********************/
  19. // Radio CE Pin, CSN Pin, SPI Speed
  20. // See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.
  21. // Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
  22. //RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
  23. // NEW: Setup for RPi B+
  24. //RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
  25. // Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
  26. RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
  27. /*** RPi Alternate ***/
  28. //Note: Specify SPI BUS 0 or 1 instead of CS pin number.
  29. // See http://tmrh20.github.io/RF24/RPi.html for more information on usage
  30. //RPi Alternate, with MRAA
  31. //RF24 radio(15,0);
  32. //RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
  33. //RF24 radio(22,0);
  34. /****************** Linux (BBB,x86,etc) ***********************/
  35. // See http://tmrh20.github.io/RF24/pages.html for more information on usage
  36. // See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
  37. // See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
  38. // Setup for ARM(Linux) devices like BBB using spidev (default is "/dev/spidev1.0" )
  39. //RF24 radio(115,0);
  40. //BBB Alternate, with mraa
  41. // CE pin = (Header P9, Pin 13) = 59 = 13 + 46
  42. //Note: Specify SPI BUS 0 or 1 instead of CS pin number.
  43. //RF24 radio(59,0);
  44. /**************************************************************/
  45. // Radio pipe addresses for the 2 nodes to communicate.
  46. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  47. const int min_payload_size = 4;
  48. const int max_payload_size = 32;
  49. const int payload_size_increments_by = 1;
  50. int next_payload_size = min_payload_size;
  51. char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
  52. int main(int argc, char** argv){
  53. bool role_ping_out = 1, role_pong_back = 0;
  54. bool role = 0;
  55. // Print preamble:
  56. cout << "RF24/examples/pingpair_dyn/\n";
  57. // Setup and configure rf radio
  58. radio.begin();
  59. radio.enableDynamicPayloads();
  60. radio.setRetries(5,15);
  61. radio.printDetails();
  62. /********* Role chooser ***********/
  63. printf("\n ************ Role Setup ***********\n");
  64. string input = "";
  65. char myChar = {0};
  66. cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) \n>";
  67. getline(cin,input);
  68. if(input.length() == 1) {
  69. myChar = input[0];
  70. if(myChar == '0'){
  71. cout << "Role: Pong Back, awaiting transmission " << endl << endl;
  72. }else{ cout << "Role: Ping Out, starting transmission " << endl << endl;
  73. role = role_ping_out;
  74. }
  75. }
  76. /***********************************/
  77. if ( role == role_ping_out ) {
  78. radio.openWritingPipe(pipes[0]);
  79. radio.openReadingPipe(1,pipes[1]);
  80. } else {
  81. radio.openWritingPipe(pipes[1]);
  82. radio.openReadingPipe(1,pipes[0]);
  83. radio.startListening();
  84. }
  85. // forever loop
  86. while (1)
  87. {
  88. if (role == role_ping_out)
  89. {
  90. // The payload will always be the same, what will change is how much of it we send.
  91. static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
  92. // First, stop listening so we can talk.
  93. radio.stopListening();
  94. // Take the time, and send it. This will block until complete
  95. printf("Now sending length %i...",next_payload_size);
  96. radio.write( send_payload, next_payload_size );
  97. // Now, continue listening
  98. radio.startListening();
  99. // Wait here until we get a response, or timeout
  100. unsigned long started_waiting_at = millis();
  101. bool timeout = false;
  102. while ( ! radio.available() && ! timeout )
  103. if (millis() - started_waiting_at > 500 )
  104. timeout = true;
  105. // Describe the results
  106. if ( timeout )
  107. {
  108. printf("Failed, response timed out.\n\r");
  109. }
  110. else
  111. {
  112. // Grab the response, compare, and send to debugging spew
  113. uint8_t len = radio.getDynamicPayloadSize();
  114. radio.read( receive_payload, len );
  115. // Put a zero at the end for easy printing
  116. receive_payload[len] = 0;
  117. // Spew it
  118. printf("Got response size=%i value=%s\n\r",len,receive_payload);
  119. }
  120. // Update size for next time.
  121. next_payload_size += payload_size_increments_by;
  122. if ( next_payload_size > max_payload_size )
  123. next_payload_size = min_payload_size;
  124. // Try again 1s later
  125. delay(100);
  126. }
  127. //
  128. // Pong back role. Receive each packet, dump it out, and send it back
  129. //
  130. if ( role == role_pong_back )
  131. {
  132. // if there is data ready
  133. if ( radio.available() )
  134. {
  135. // Dump the payloads until we've gotten everything
  136. uint8_t len;
  137. while (radio.available())
  138. {
  139. // Fetch the payload, and see if this was the last one.
  140. len = radio.getDynamicPayloadSize();
  141. radio.read( receive_payload, len );
  142. // Put a zero at the end for easy printing
  143. receive_payload[len] = 0;
  144. // Spew it
  145. printf("Got payload size=%i value=%s\n\r",len,receive_payload);
  146. }
  147. // First, stop listening so we can talk
  148. radio.stopListening();
  149. // Send the final one back.
  150. radio.write( receive_payload, len );
  151. printf("Sent response.\n\r");
  152. // Now, resume listening so we catch the next packets.
  153. radio.startListening();
  154. }
  155. }
  156. }
  157. }