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.

273 lines
6.0 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. #include <SPI.h>
  8. #include "nRF24L01.h"
  9. #include "RF24.h"
  10. #include "printf.h"
  11. //
  12. // Test version of RF24, exposes some protected interface
  13. //
  14. class RF24Test: public RF24
  15. {
  16. public: RF24Test(int a, int b): RF24(a,b) {}
  17. };
  18. //
  19. // Hardware configuration
  20. //
  21. // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
  22. RF24Test radio(48,49);
  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 = 5;
  26. //
  27. // Topology
  28. //
  29. // Radio pipe addresses for the 2 nodes to communicate.
  30. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  31. //
  32. // Role management
  33. //
  34. // Set up role. This sketch uses the same software for all the nodes
  35. // in this system. Doing so greatly simplifies testing. The hardware itself specifies
  36. // which node it is.
  37. //
  38. // This is done through the role_pin
  39. //
  40. // The various roles supported by this sketch
  41. typedef enum { role_ping_out = 1, role_pong_back } role_e;
  42. // The debug-friendly names of those roles
  43. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
  44. // The role of the current running sketch
  45. role_e role;
  46. //
  47. // Test state
  48. //
  49. bool done; //*< Are we done with the test? */
  50. bool passed; //*< Have we passed the test? */
  51. bool notified; //*< Have we notified the user we're done? */
  52. const int num_needed = 10; //*< How many success/failures until we're done? */
  53. int receives_remaining = num_needed; //*< How many ack packets until we declare victory? */
  54. int failures_remaining = num_needed; //*< How many more failed sends until we declare failure? */
  55. const int interval = 100; //*< ms to wait between sends */
  56. char configuration = '1'; //*< Configuration key, one char sent in by the test framework to tell us how to configure, this is the default */
  57. void one_ok(void)
  58. {
  59. // Have we received enough yet?
  60. if ( ! --receives_remaining )
  61. {
  62. done = true;
  63. passed = true;
  64. }
  65. }
  66. void one_failed(void)
  67. {
  68. // Have we failed enough yet?
  69. if ( ! --failures_remaining )
  70. {
  71. done = true;
  72. passed = false;
  73. }
  74. }
  75. void setup(void)
  76. {
  77. //
  78. // Role
  79. //
  80. // set up the role pin
  81. pinMode(role_pin, INPUT);
  82. digitalWrite(role_pin,HIGH);
  83. delay(20); // Just to get a solid reading on the role pin
  84. // read the address pin, establish our role
  85. if ( digitalRead(role_pin) )
  86. role = role_ping_out;
  87. else
  88. role = role_pong_back;
  89. //
  90. // Print preamble
  91. //
  92. Serial.begin(115200);
  93. printf_begin();
  94. printf("\n\rRF24/tests/pingpair_blocking/\n\r");
  95. printf("ROLE: %s\n\r",role_friendly_name[role]);
  96. //
  97. // get test config
  98. //
  99. printf("+READY press any key to start\n\r\n\r");
  100. while (! Serial.available() ) {}
  101. configuration = Serial.read();
  102. printf("Configuration\t = %c\n\r",configuration);
  103. //
  104. // Setup and configure rf radio
  105. //
  106. radio.begin();
  107. //
  108. // Open pipes to other nodes for communication
  109. //
  110. // This simple sketch opens two pipes for these two nodes to communicate
  111. // back and forth.
  112. // Open 'our' pipe for writing
  113. // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
  114. if ( role == role_ping_out )
  115. {
  116. radio.openWritingPipe(pipes[0]);
  117. radio.openReadingPipe(1,pipes[1]);
  118. }
  119. else
  120. {
  121. radio.openWritingPipe(pipes[1]);
  122. radio.openReadingPipe(1,pipes[0]);
  123. }
  124. //
  125. // Start listening
  126. //
  127. radio.startListening();
  128. //
  129. // Dump the configuration of the rf unit for debugging
  130. //
  131. radio.printDetails();
  132. if ( role == role_pong_back )
  133. printf("\n\r+OK ");
  134. }
  135. void loop(void)
  136. {
  137. //
  138. // Ping out role. Repeatedly send the current time
  139. //
  140. if (role == role_ping_out)
  141. {
  142. // First, stop listening so we can talk.
  143. radio.stopListening();
  144. // Take the time, and send it. This will block until complete
  145. unsigned long time = millis();
  146. printf("Now sending %lu...",time);
  147. radio.write( &time, sizeof(unsigned long) );
  148. // Now, continue listening
  149. radio.startListening();
  150. // Wait here until we get a response, or timeout (250ms)
  151. unsigned long started_waiting_at = millis();
  152. bool timeout = false;
  153. while ( ! radio.available() && ! timeout )
  154. if (millis() - started_waiting_at > 200 )
  155. timeout = true;
  156. // Describe the results
  157. if ( timeout )
  158. {
  159. printf("Failed, response timed out.\n\r");
  160. one_failed();
  161. }
  162. else
  163. {
  164. // Grab the response, compare, and send to debugging spew
  165. unsigned long got_time;
  166. radio.read( &got_time, sizeof(unsigned long) );
  167. // Spew it
  168. printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
  169. one_ok();
  170. }
  171. // Try again later
  172. delay(250);
  173. }
  174. //
  175. // Pong back role. Receive each packet, dump it out, and send it back
  176. //
  177. if ( role == role_pong_back )
  178. {
  179. // if there is data ready
  180. if ( radio.available() )
  181. {
  182. // Dump the payloads until we've gotten everything
  183. unsigned long got_time;
  184. bool done = false;
  185. while (radio.available())
  186. {
  187. // Fetch the payload, and see if this was the last one.
  188. radio.read( &got_time, sizeof(unsigned long) );
  189. }
  190. // Delay just a little bit to let the other unit
  191. // make the transition to receiver
  192. //delay(20);
  193. //}
  194. // First, stop listening so we can talk
  195. radio.stopListening();
  196. // Spew it
  197. printf("Got payload %lu...",got_time);
  198. // Send the final one back.
  199. radio.write( &got_time, sizeof(unsigned long) );
  200. // Now, resume listening so we catch the next packets.
  201. radio.startListening();
  202. printf("Sent response.\n\r");
  203. }
  204. }
  205. //
  206. // Stop the test if we're done and report results
  207. //
  208. if ( done && ! notified )
  209. {
  210. notified = true;
  211. printf("\n\r+OK ");
  212. if ( passed )
  213. printf("PASS\n\r\n\r");
  214. else
  215. printf("FAIL\n\r\n\r");
  216. }
  217. }
  218. // vim:cin:ai:sts=2 sw=2 ft=cpp