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.

149 lines
5.3 KiB

  1. /*
  2. TMRh20 2014
  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. General Data Transfer Rate Test
  9. This example demonstrates basic data transfer functionality with the
  10. updated library. This example will display the transfer rates acheived
  11. using the slower form of high-speed transfer using blocking-writes.
  12. */
  13. #include <SPI.h>
  14. #include "RF24.h"
  15. /************* USER Configuration *****************************/
  16. // Hardware configuration
  17. RF24 radio(7,8); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
  18. /***************************************************************/
  19. const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; // Radio pipe addresses for the 2 nodes to communicate.
  20. byte data[32]; //Data buffer for testing data transfer speeds
  21. unsigned long counter, rxTimer; //Counter and timer for keeping track transfer info
  22. unsigned long startTime, stopTime;
  23. bool TX=1,RX=0,role=0;
  24. void setup(void) {
  25. Serial.begin(115200);
  26. radio.begin(); // Setup and configure rf radio
  27. radio.setChannel(1);
  28. radio.setPALevel(RF24_PA_MAX); // If you want to save power use "RF24_PA_MIN" but keep in mind that reduces the module's range
  29. radio.setDataRate(RF24_1MBPS);
  30. radio.setAutoAck(1); // Ensure autoACK is enabled
  31. radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
  32. radio.setCRCLength(RF24_CRC_8); // Use 8-bit CRC for performance
  33. radio.openWritingPipe(pipes[0]);
  34. radio.openReadingPipe(1,pipes[1]);
  35. radio.startListening(); // Start listening
  36. radio.printDetails(); // Dump the configuration of the rf unit for debugging
  37. Serial.println(F("\n\rRF24/examples/Transfer/"));
  38. Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  39. randomSeed(analogRead(0)); //Seed for random number generation
  40. for(int i = 0; i < 32; i++){
  41. data[i] = random(255); //Load the buffer with random data
  42. }
  43. radio.powerUp(); //Power up the radio
  44. }
  45. void loop(void){
  46. if(role == TX){
  47. delay(2000);
  48. Serial.println(F("Initiating Basic Data Transfer"));
  49. unsigned long cycles = 10000; //Change this to a higher or lower number.
  50. startTime = millis();
  51. unsigned long pauseTime = millis();
  52. for(int i=0; i<cycles; i++){ //Loop through a number of cycles
  53. data[0] = i; //Change the first byte of the payload for identification
  54. if(!radio.writeFast(&data,32)){ //Write to the FIFO buffers
  55. counter++; //Keep count of failed payloads
  56. }
  57. //This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
  58. // if(millis() - pauseTime > 3){
  59. // pauseTime = millis();
  60. // radio.txStandBy(); // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
  61. // //delayMicroseconds(130); // This gives the PLL time to sync back up
  62. // }
  63. }
  64. stopTime = millis();
  65. //This should be called to wait for completion and put the radio in standby mode after transmission, returns 0 if data still in FIFO (timed out), 1 if success
  66. if(!radio.txStandBy()){ counter+=3; } //Standby, block only until FIFO empty or auto-retry timeout. Flush TX FIFO if failed
  67. //radio.txStandBy(1000); //Standby, using extended timeout period of 1 second
  68. float numBytes = cycles*32;
  69. float rate = numBytes / (stopTime - startTime);
  70. Serial.print("Transfer complete at "); Serial.print(rate); Serial.println(" KB/s");
  71. Serial.print(counter); Serial.print(" of "); Serial.print(cycles); Serial.println(" Packets Failed to Send");
  72. counter = 0;
  73. }
  74. if(role == RX){
  75. while(radio.available()){
  76. radio.read(&data,32);
  77. counter++;
  78. }
  79. if(millis() - rxTimer > 1000){
  80. rxTimer = millis();
  81. unsigned long numBytes = counter*32;
  82. Serial.print(F("Rate: "));
  83. //Prevent dividing into 0, which will cause issues over a period of time
  84. Serial.println(numBytes > 0 ? numBytes/1000.0:0);
  85. Serial.print(F("Payload Count: "));
  86. Serial.println(counter);
  87. counter = 0;
  88. }
  89. }
  90. //
  91. // Change roles
  92. //
  93. if ( Serial.available() )
  94. {
  95. char c = toupper(Serial.read());
  96. if ( c == 'T' && role == RX )
  97. {
  98. Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  99. radio.openWritingPipe(pipes[1]);
  100. radio.openReadingPipe(1,pipes[0]);
  101. radio.stopListening();
  102. role = TX; // Become the primary transmitter (ping out)
  103. }
  104. else if ( c == 'R' && role == TX )
  105. {
  106. radio.openWritingPipe(pipes[0]);
  107. radio.openReadingPipe(1,pipes[1]);
  108. radio.startListening();
  109. Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  110. role = RX; // Become the primary receiver (pong back)
  111. }
  112. }
  113. }