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.

187 lines
5.5 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. /** General Data Transfer Rate Test
  8. * This example demonstrates basic data transfer functionality with the
  9. updated library. This example will display the transfer rates acheived using
  10. the slower form of high-speed transfer using blocking-writes.
  11. */
  12. #include <cstdlib>
  13. #include <iostream>
  14. #include <sstream>
  15. #include <string>
  16. #include <RF24/RF24.h>
  17. #include <unistd.h>
  18. using namespace std;
  19. //
  20. // Hardware configuration
  21. //
  22. /****************** Raspberry Pi ***********************/
  23. // Radio CE Pin, CSN Pin, SPI Speed
  24. // See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.
  25. // Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
  26. //RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
  27. // NEW: Setup for RPi B+
  28. //RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
  29. // Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
  30. RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
  31. /*** RPi Alternate ***/
  32. //Note: Specify SPI BUS 0 or 1 instead of CS pin number.
  33. // See http://tmrh20.github.io/RF24/RPi.html for more information on usage
  34. //RPi Alternate, with MRAA
  35. //RF24 radio(15,0);
  36. //RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
  37. //RF24 radio(22,0);
  38. /****************** Linux (BBB,x86,etc) ***********************/
  39. // See http://tmrh20.github.io/RF24/pages.html for more information on usage
  40. // See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
  41. // See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
  42. // Setup for ARM(Linux) devices like BBB using spidev (default is "/dev/spidev1.0" )
  43. //RF24 radio(115,0);
  44. //BBB Alternate, with mraa
  45. // CE pin = (Header P9, Pin 13) = 59 = 13 + 46
  46. //Note: Specify SPI BUS 0 or 1 instead of CS pin number.
  47. //RF24 radio(59,0);
  48. /**************************************************************/
  49. // Radio pipe addresses for the 2 nodes to communicate.
  50. const uint64_t addresses[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
  51. uint8_t data[32];
  52. unsigned long startTime, stopTime, counter, rxTimer=0;
  53. int main(int argc, char** argv){
  54. bool role_ping_out = 1, role_pong_back = 0;
  55. bool role = 0;
  56. // Print preamble:
  57. cout << "RF24/examples/Transfer/\n";
  58. radio.begin(); // Setup and configure rf radio
  59. radio.setChannel(1);
  60. radio.setPALevel(RF24_PA_MAX);
  61. radio.setDataRate(RF24_1MBPS);
  62. radio.setAutoAck(1); // Ensure autoACK is enabled
  63. radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
  64. radio.setCRCLength(RF24_CRC_8); // Use 8-bit CRC for performance
  65. radio.printDetails();
  66. /********* Role chooser ***********/
  67. printf("\n ************ Role Setup ***********\n");
  68. string input = "";
  69. char myChar = {0};
  70. cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit)\n>";
  71. getline(cin,input);
  72. if(input.length() == 1) {
  73. myChar = input[0];
  74. if(myChar == '0'){
  75. cout << "Role: Pong Back, awaiting transmission " << endl << endl;
  76. }else{ cout << "Role: Ping Out, starting transmission " << endl << endl;
  77. role = role_ping_out;
  78. }
  79. }
  80. /***********************************/
  81. if ( role == role_ping_out ) {
  82. radio.openWritingPipe(addresses[1]);
  83. radio.openReadingPipe(1,addresses[0]);
  84. radio.stopListening();
  85. } else {
  86. radio.openWritingPipe(addresses[0]);
  87. radio.openReadingPipe(1,addresses[1]);
  88. radio.startListening();
  89. }
  90. for(int i=0; i<32; i++){
  91. data[i] = rand() % 255; //Load the buffer with random data
  92. }
  93. // forever loop
  94. while (1){
  95. if (role == role_ping_out){
  96. sleep(2);
  97. printf("Initiating Basic Data Transfer\n\r");
  98. long int cycles = 10000; //Change this to a higher or lower number.
  99. // unsigned long pauseTime = millis(); //Uncomment if autoAck == 1 ( NOACK )
  100. startTime = millis();
  101. for(int i=0; i<cycles; i++){ //Loop through a number of cycles
  102. data[0] = i; //Change the first byte of the payload for identification
  103. if(!radio.writeFast(&data,32)){ //Write to the FIFO buffers
  104. counter++; //Keep count of failed payloads
  105. }
  106. //This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
  107. /* if(millis() - pauseTime > 3){ // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
  108. pauseTime = millis();
  109. radio.txStandBy(); // This gives the PLL time to sync back up
  110. }
  111. */
  112. }
  113. stopTime = millis();
  114. if(!radio.txStandBy()){ counter+=3; }
  115. float numBytes = cycles*32;
  116. float rate = numBytes / (stopTime - startTime);
  117. printf("Transfer complete at %.2f KB/s \n\r",rate);
  118. printf("%lu of %lu Packets Failed to Send\n\r",counter,cycles);
  119. counter = 0;
  120. }
  121. if(role == role_pong_back){
  122. while(radio.available()){
  123. radio.read(&data,32);
  124. counter++;
  125. }
  126. if(millis() - rxTimer > 1000){
  127. rxTimer = millis();
  128. printf("Rate: ");
  129. float numBytes = counter*32;
  130. printf("%.2f KB/s \n\r",numBytes/1000);
  131. printf("Payload Count: %lu \n\r", counter);
  132. counter = 0;
  133. }
  134. }
  135. } // loop
  136. } // main