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.

242 lines
5.5 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 RF Radio Ping Pair ... for Maple
  9. *
  10. * This is an example of how to use the RF24 class. Write this sketch to two different nodes,
  11. * connect the role_pin to ground on one. The ping node sends the current time to the pong node,
  12. * which responds by sending the value back. The ping node can then see how long the whole cycle
  13. * took.
  14. */
  15. #include "WProgram.h"
  16. #include <SPI.h>
  17. #include "nRF24L01.h"
  18. #include "RF24.h"
  19. //
  20. // Maple specific setup. Other than this section, the sketch is the same on Maple as on
  21. // Arduino
  22. //
  23. #ifdef MAPLE_IDE
  24. // External startup function
  25. extern void board_start(const char* program_name);
  26. // Use SPI #2.
  27. HardwareSPI SPI(2);
  28. #else
  29. #define board_startup printf
  30. #define toggleLED(x) (x)
  31. #endif
  32. //
  33. // Hardware configuration
  34. //
  35. // Set up nRF24L01 radio on SPI bus plus pins 7 & 6
  36. // (This works for the Getting Started board plugged into the
  37. // Maple Native backwards.)
  38. RF24 radio(7,6);
  39. // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
  40. // Leave open to be the 'ping' transmitter
  41. const int role_pin = 10;
  42. //
  43. // Topology
  44. //
  45. // Radio pipe addresses for the 2 nodes to communicate.
  46. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  47. //
  48. // Role management
  49. //
  50. // Set up role. This sketch uses the same software for all the nodes
  51. // in this system. Doing so greatly simplifies testing. The hardware itself specifies
  52. // which node it is.
  53. //
  54. // This is done through the role_pin
  55. //
  56. // The various roles supported by this sketch
  57. typedef enum { role_ping_out = 1, role_pong_back } role_e;
  58. // The debug-friendly names of those roles
  59. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
  60. // The role of the current running sketch
  61. role_e role;
  62. void setup(void)
  63. {
  64. //
  65. // Role
  66. //
  67. // set up the role pin
  68. pinMode(role_pin, INPUT);
  69. digitalWrite(role_pin,HIGH);
  70. delay(20); // Just to get a solid reading on the role pin
  71. // read the address pin, establish our role
  72. if ( digitalRead(role_pin) )
  73. role = role_ping_out;
  74. else
  75. role = role_pong_back;
  76. //
  77. // Print preamble
  78. //
  79. board_start("\n\rRF24/examples/pingpair/\n\r");
  80. printf("ROLE: %s\n\r",role_friendly_name[role]);
  81. //
  82. // Setup and configure rf radio
  83. //
  84. radio.begin();
  85. // optionally, increase the delay between retries & # of retries
  86. radio.setRetries(15,15);
  87. // optionally, reduce the payload size. seems to
  88. // improve reliability
  89. radio.setPayloadSize(8);
  90. //
  91. // Open pipes to other nodes for communication
  92. //
  93. // This simple sketch opens two pipes for these two nodes to communicate
  94. // back and forth.
  95. // Open 'our' pipe for writing
  96. // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
  97. if ( role == role_ping_out )
  98. {
  99. radio.openWritingPipe(pipes[0]);
  100. radio.openReadingPipe(1,pipes[1]);
  101. }
  102. else
  103. {
  104. radio.openWritingPipe(pipes[1]);
  105. radio.openReadingPipe(1,pipes[0]);
  106. }
  107. //
  108. // Start listening
  109. //
  110. radio.startListening();
  111. //
  112. // Dump the configuration of the rf unit for debugging
  113. //
  114. radio.printDetails();
  115. }
  116. void loop(void)
  117. {
  118. //
  119. // Ping out role. Repeatedly send the current time
  120. //
  121. if (role == role_ping_out)
  122. {
  123. toggleLED();
  124. // First, stop listening so we can talk.
  125. radio.stopListening();
  126. // Take the time, and send it. This will block until complete
  127. unsigned long time = millis();
  128. printf("Now sending %lu...",time);
  129. bool ok = radio.write( &time, sizeof(unsigned long) );
  130. if (ok)
  131. printf("ok...\r\n");
  132. else
  133. printf("failed.\r\n");
  134. // Now, continue listening
  135. radio.startListening();
  136. // Wait here until we get a response, or timeout (250ms)
  137. unsigned long started_waiting_at = millis();
  138. bool timeout = false;
  139. while ( ! radio.available() && ! timeout )
  140. if (millis() - started_waiting_at > 200 )
  141. timeout = true;
  142. // Describe the results
  143. if ( timeout )
  144. {
  145. printf("Failed, response timed out.\r\n");
  146. }
  147. else
  148. {
  149. // Grab the response, compare, and send to debugging spew
  150. unsigned long got_time;
  151. radio.read( &got_time, sizeof(unsigned long) );
  152. // Spew it
  153. printf("Got response %lu, round-trip delay: %lu\r\n",got_time,millis()-got_time);
  154. }
  155. toggleLED();
  156. // Try again 1s later
  157. delay(1000);
  158. }
  159. //
  160. // Pong back role. Receive each packet, dump it out, and send it back
  161. //
  162. if ( role == role_pong_back )
  163. {
  164. // if there is data ready
  165. if ( radio.available() )
  166. {
  167. // Dump the payloads until we've gotten everything
  168. unsigned long got_time;
  169. bool done = false;
  170. while (!done)
  171. {
  172. // Fetch the payload, and see if this was the last one.
  173. done = radio.read( &got_time, sizeof(unsigned long) );
  174. // Spew it
  175. printf("Got payload %lu...",got_time);
  176. // Delay just a little bit to let the other unit
  177. // make the transition to receiver
  178. delay(20);
  179. }
  180. // First, stop listening so we can talk
  181. radio.stopListening();
  182. // Send the final one back.
  183. radio.write( &got_time, sizeof(unsigned long) );
  184. printf("Sent response.\r\n");
  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