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.

246 lines
5.6 KiB

  1. /*
  2. Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. version 2 as published by the Free Software Foundation.
  6. */
  7. /**
  8. * Example using Dynamic Payloads
  9. *
  10. * This is an example of how to use payloads of a varying (dynamic) size.
  11. */
  12. #include <SPI.h>
  13. #include "nRF24L01.h"
  14. #include "RF24.h"
  15. //
  16. // Hardware configuration
  17. //
  18. // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
  19. RF24 radio(7,8);
  20. // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
  21. // Leave open to be the 'ping' transmitter
  22. const int role_pin = 5;
  23. //
  24. // Topology
  25. //
  26. // Radio pipe addresses for the 2 nodes to communicate.
  27. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  28. //
  29. // Role management
  30. //
  31. // Set up role. This sketch uses the same software for all the nodes
  32. // in this system. Doing so greatly simplifies testing. The hardware itself specifies
  33. // which node it is.
  34. //
  35. // This is done through the role_pin
  36. //
  37. // The various roles supported by this sketch
  38. typedef enum { role_ping_out = 1, role_pong_back } role_e;
  39. // The debug-friendly names of those roles
  40. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
  41. // The role of the current running sketch
  42. role_e role;
  43. //
  44. // Payload
  45. //
  46. const int min_payload_size = 4;
  47. const int max_payload_size = 32;
  48. const int payload_size_increments_by = 1;
  49. int next_payload_size = min_payload_size;
  50. char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
  51. void setup(void)
  52. {
  53. //
  54. // Role
  55. //
  56. // set up the role pin
  57. pinMode(role_pin, INPUT);
  58. digitalWrite(role_pin,HIGH);
  59. delay(20); // Just to get a solid reading on the role pin
  60. // read the address pin, establish our role
  61. if ( digitalRead(role_pin) )
  62. role = role_ping_out;
  63. else
  64. role = role_pong_back;
  65. //
  66. // Print preamble
  67. //
  68. Serial.begin(115200);
  69. Serial.println(F("RF24/examples/pingpair_dyn/"));
  70. Serial.print(F("ROLE: "));
  71. Serial.println(role_friendly_name[role]);
  72. //
  73. // Setup and configure rf radio
  74. //
  75. radio.begin();
  76. // enable dynamic payloads
  77. radio.enableDynamicPayloads();
  78. // optionally, increase the delay between retries & # of retries
  79. radio.setRetries(5,15);
  80. //
  81. // Open pipes to other nodes for communication
  82. //
  83. // This simple sketch opens two pipes for these two nodes to communicate
  84. // back and forth.
  85. // Open 'our' pipe for writing
  86. // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
  87. if ( role == role_ping_out )
  88. {
  89. radio.openWritingPipe(pipes[0]);
  90. radio.openReadingPipe(1,pipes[1]);
  91. }
  92. else
  93. {
  94. radio.openWritingPipe(pipes[1]);
  95. radio.openReadingPipe(1,pipes[0]);
  96. }
  97. //
  98. // Start listening
  99. //
  100. radio.startListening();
  101. //
  102. // Dump the configuration of the rf unit for debugging
  103. //
  104. radio.printDetails();
  105. }
  106. void loop(void)
  107. {
  108. //
  109. // Ping out role. Repeatedly send the current time
  110. //
  111. if (role == role_ping_out)
  112. {
  113. // The payload will always be the same, what will change is how much of it we send.
  114. static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
  115. // First, stop listening so we can talk.
  116. radio.stopListening();
  117. // Take the time, and send it. This will block until complete
  118. Serial.print(F("Now sending length "));
  119. Serial.println(next_payload_size);
  120. radio.write( send_payload, next_payload_size );
  121. // Now, continue listening
  122. radio.startListening();
  123. // Wait here until we get a response, or timeout
  124. unsigned long started_waiting_at = millis();
  125. bool timeout = false;
  126. while ( ! radio.available() && ! timeout )
  127. if (millis() - started_waiting_at > 500 )
  128. timeout = true;
  129. // Describe the results
  130. if ( timeout )
  131. {
  132. Serial.println(F("Failed, response timed out."));
  133. }
  134. else
  135. {
  136. // Grab the response, compare, and send to debugging spew
  137. uint8_t len = radio.getDynamicPayloadSize();
  138. // If a corrupt dynamic payload is received, it will be flushed
  139. if(!len){
  140. return;
  141. }
  142. radio.read( receive_payload, len );
  143. // Put a zero at the end for easy printing
  144. receive_payload[len] = 0;
  145. // Spew it
  146. Serial.print(F("Got response size="));
  147. Serial.print(len);
  148. Serial.print(F(" value="));
  149. Serial.println(receive_payload);
  150. }
  151. // Update size for next time.
  152. next_payload_size += payload_size_increments_by;
  153. if ( next_payload_size > max_payload_size )
  154. next_payload_size = min_payload_size;
  155. // Try again 1s later
  156. delay(100);
  157. }
  158. //
  159. // Pong back role. Receive each packet, dump it out, and send it back
  160. //
  161. if ( role == role_pong_back )
  162. {
  163. // if there is data ready
  164. while ( radio.available() )
  165. {
  166. // Fetch the payload, and see if this was the last one.
  167. uint8_t len = radio.getDynamicPayloadSize();
  168. // If a corrupt dynamic payload is received, it will be flushed
  169. if(!len){
  170. continue;
  171. }
  172. radio.read( receive_payload, len );
  173. // Put a zero at the end for easy printing
  174. receive_payload[len] = 0;
  175. // Spew it
  176. Serial.print(F("Got response size="));
  177. Serial.print(len);
  178. Serial.print(F(" value="));
  179. Serial.println(receive_payload);
  180. // First, stop listening so we can talk
  181. radio.stopListening();
  182. // Send the final one back.
  183. radio.write( receive_payload, len );
  184. Serial.println(F("Sent response."));
  185. // Now, resume listening so we catch the next packets.
  186. radio.startListening();
  187. }
  188. }
  189. }
  190. // vim:cin:ai:sts=2 sw=2 ft=cpp