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.

223 lines
4.7 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. * Interrupt-driven test for native target
  9. *
  10. * This example is the friendliest for the native target because it doesn't do
  11. * any polling. Made a slight change to call done() at the end of setup.
  12. */
  13. #include <SPI.h>
  14. #include "nRF24L01.h"
  15. #include "RF24.h"
  16. #include "printf.h"
  17. //
  18. // Hardware configuration
  19. //
  20. // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
  21. RF24 radio(8,9);
  22. // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
  23. // Leave open to be the 'ping' transmitter
  24. const short role_pin = 7;
  25. //
  26. // Topology
  27. //
  28. // Single radio pipe address for the 2 nodes to communicate.
  29. const uint64_t pipe = 0xE8E8F0F0E1LL;
  30. //
  31. // Role management
  32. //
  33. // Set up role. This sketch uses the same software for all the nodes in this
  34. // system. Doing so greatly simplifies testing. The hardware itself specifies
  35. // which node it is.
  36. //
  37. // This is done through the role_pin
  38. //
  39. // The various roles supported by this sketch
  40. typedef enum { role_sender = 1, role_receiver } role_e;
  41. // The debug-friendly names of those roles
  42. const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
  43. // The role of the current running sketch
  44. role_e role;
  45. // Interrupt handler, check the radio because we got an IRQ
  46. void check_radio(void);
  47. void setup(void)
  48. {
  49. //
  50. // Role
  51. //
  52. // set up the role pin
  53. pinMode(role_pin, INPUT);
  54. digitalWrite(role_pin,HIGH);
  55. delay(20); // Just to get a solid reading on the role pin
  56. // read the address pin, establish our role
  57. if ( digitalRead(role_pin) )
  58. role = role_sender;
  59. else
  60. role = role_receiver;
  61. //
  62. // Print preamble
  63. //
  64. Serial.begin(115200);
  65. printf_begin();
  66. printf("\n\rRF24/examples/pingpair_irq/\n\r");
  67. printf("ROLE: %s\n\r",role_friendly_name[role]);
  68. //
  69. // Setup and configure rf radio
  70. //
  71. radio.begin();
  72. // We will be using the Ack Payload feature, so please enable it
  73. radio.enableAckPayload();
  74. //
  75. // Open pipes to other nodes for communication
  76. //
  77. // This simple sketch opens a single pipe for these two nodes to communicate
  78. // back and forth. One listens on it, the other talks to it.
  79. if ( role == role_sender )
  80. {
  81. radio.openWritingPipe(pipe);
  82. }
  83. else
  84. {
  85. radio.openReadingPipe(1,pipe);
  86. }
  87. //
  88. // Start listening
  89. //
  90. if ( role == role_receiver )
  91. radio.startListening();
  92. //
  93. // Dump the configuration of the rf unit for debugging
  94. //
  95. radio.printDetails();
  96. //
  97. // Attach interrupt handler to interrupt #0 (using pin 2)
  98. // on BOTH the sender and receiver
  99. //
  100. attachInterrupt(0, check_radio, FALLING);
  101. //
  102. // On the native target, this is as far as we get
  103. //
  104. #if NATIVE
  105. done();
  106. #endif
  107. }
  108. static uint32_t message_count = 0;
  109. void loop(void)
  110. {
  111. //
  112. // Sender role. Repeatedly send the current time
  113. //
  114. if (role == role_sender)
  115. {
  116. // Take the time, and send it.
  117. unsigned long time = millis();
  118. printf("Now sending %lu\n\r",time);
  119. radio.startWrite( &time, sizeof(unsigned long) );
  120. // Try again soon
  121. delay(2000);
  122. }
  123. //
  124. // Receiver role: Does nothing! All the work is in IRQ
  125. //
  126. }
  127. void check_radio(void)
  128. {
  129. // What happened?
  130. bool tx,fail,rx;
  131. radio.whatHappened(tx,fail,rx);
  132. // Have we successfully transmitted?
  133. if ( tx )
  134. {
  135. if ( role == role_sender )
  136. printf("Send:OK\n\r");
  137. if ( role == role_receiver )
  138. printf("Ack Payload:Sent\n\r");
  139. }
  140. // Have we failed to transmit?
  141. if ( fail )
  142. {
  143. if ( role == role_sender )
  144. printf("Send:Failed\n\r");
  145. if ( role == role_receiver )
  146. printf("Ack Payload:Failed\n\r");
  147. }
  148. // Transmitter can power down for now, because
  149. // the transmission is done.
  150. if ( ( tx || fail ) && ( role == role_sender ) )
  151. radio.powerDown();
  152. // Did we receive a message?
  153. if ( rx )
  154. {
  155. // If we're the sender, we've received an ack payload
  156. if ( role == role_sender )
  157. {
  158. radio.read(&message_count,sizeof(message_count));
  159. printf("Ack:%lu\n\r",(unsigned long)message_count);
  160. }
  161. // If we're the receiver, we've received a time message
  162. if ( role == role_receiver )
  163. {
  164. // Get this payload and dump it
  165. static unsigned long got_time;
  166. radio.read( &got_time, sizeof(got_time) );
  167. printf("Got payload %lu\n\r",got_time);
  168. // Add an ack packet for the next time around. This is a simple
  169. // packet counter
  170. radio.writeAckPayload( 1, &message_count, sizeof(message_count) );
  171. ++message_count;
  172. }
  173. }
  174. }
  175. // vim:ai:cin:sts=2 sw=2 ft=cpp