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.

122 lines
4.5 KiB

  1. /*
  2. // March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
  3. // Parts derived from examples by J. Coliz <maniacbug@ymail.com>
  4. */
  5. /**
  6. * Example for efficient call-response using ack-payloads
  7. *
  8. * This example continues to make use of all the normal functionality of the radios including
  9. * the auto-ack and auto-retry features, but allows ack-payloads to be written optionally as well.
  10. * This allows very fast call-response communication, with the responding radio never having to
  11. * switch out of Primary Receiver mode to send back a payload, but having the option to if wanting
  12. * to initiate communication instead of respond to a commmunication.
  13. */
  14. #include <SPI.h>
  15. #include "nRF24L01.h"
  16. #include "RF24.h"
  17. #include "printf.h"
  18. // Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8
  19. RF24 radio(7,8);
  20. // Topology
  21. const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; // Radio pipe addresses for the 2 nodes to communicate.
  22. // Role management: Set up role. This sketch uses the same software for all the nodes
  23. // in this system. Doing so greatly simplifies testing.
  24. typedef enum { role_ping_out = 1, role_pong_back } role_e; // The various roles supported by this sketch
  25. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"}; // The debug-friendly names of those roles
  26. role_e role = role_pong_back; // The role of the current running sketch
  27. // A single byte to keep track of the data being sent back and forth
  28. byte counter = 1;
  29. void setup(){
  30. Serial.begin(115200);
  31. printf_begin();
  32. Serial.print(F("\n\rRF24/examples/pingpair_ack/\n\rROLE: "));
  33. Serial.println(role_friendly_name[role]);
  34. Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  35. // Setup and configure rf radio
  36. radio.begin();
  37. radio.setAutoAck(1); // Ensure autoACK is enabled
  38. radio.enableAckPayload(); // Allow optional ack payloads
  39. radio.setRetries(0,15); // Smallest time between retries, max no. of retries
  40. radio.setPayloadSize(1); // Here we are sending 1-byte payloads to test the call-response speed
  41. radio.openWritingPipe(pipes[1]); // Both radios listen on the same pipes by default, and switch when writing
  42. radio.openReadingPipe(1,pipes[0]);
  43. radio.startListening(); // Start listening
  44. radio.printDetails(); // Dump the configuration of the rf unit for debugging
  45. }
  46. void loop(void) {
  47. if (role == role_ping_out){
  48. radio.stopListening(); // First, stop listening so we can talk.
  49. printf("Now sending %d as payload. ",counter);
  50. byte gotByte;
  51. unsigned long time = micros(); // Take the time, and send it. This will block until complete
  52. //Called when STANDBY-I mode is engaged (User is finished sending)
  53. if (!radio.write( &counter, 1 )){
  54. Serial.println(F("failed."));
  55. }else{
  56. if(!radio.available()){
  57. Serial.println(F("Blank Payload Received."));
  58. }else{
  59. while(radio.available() ){
  60. unsigned long tim = micros();
  61. radio.read( &gotByte, 1 );
  62. printf("Got response %d, round-trip delay: %lu microseconds\n\r",gotByte,tim-time);
  63. counter++;
  64. }
  65. }
  66. }
  67. // Try again later
  68. delay(1000);
  69. }
  70. // Pong back role. Receive each packet, dump it out, and send it back
  71. if ( role == role_pong_back ) {
  72. byte pipeNo;
  73. byte gotByte; // Dump the payloads until we've gotten everything
  74. while( radio.available(&pipeNo)){
  75. radio.read( &gotByte, 1 );
  76. radio.writeAckPayload(pipeNo,&gotByte, 1 );
  77. }
  78. }
  79. // Change roles
  80. if ( Serial.available() )
  81. {
  82. char c = toupper(Serial.read());
  83. if ( c == 'T' && role == role_pong_back )
  84. {
  85. Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  86. role = role_ping_out; // Become the primary transmitter (ping out)
  87. radio.openWritingPipe(pipes[0]);
  88. radio.openReadingPipe(1,pipes[1]);
  89. }
  90. else if ( c == 'R' && role == role_ping_out )
  91. {
  92. Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  93. role = role_pong_back; // Become the primary receiver (pong back)
  94. radio.openWritingPipe(pipes[1]);
  95. radio.openReadingPipe(1,pipes[0]);
  96. radio.startListening();
  97. }
  98. }
  99. }