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.

262 lines
6.2 KiB

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