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.

169 lines
6.3 KiB

  1. /*
  2. TMRh20 2014 - Updated to work with optimized RF24 Arduino library
  3. */
  4. /**
  5. * Example for efficient call-response using ack-payloads
  6. *
  7. * This example continues to make use of all the normal functionality of the radios including
  8. * the auto-ack and auto-retry features, but allows ack-payloads to be written optionlly as well.
  9. * This allows very fast call-response communication, with the responding radio never having to
  10. * switch out of Primary Receiver mode to send back a payload, but having the option to switch to
  11. * primary transmitter if wanting to initiate communication instead of respond to a commmunication.
  12. */
  13. #include <cstdlib>
  14. #include <iostream>
  15. #include <sstream>
  16. #include <string>
  17. #include <unistd.h>
  18. #include <RF24/RF24.h>
  19. using namespace std;
  20. //
  21. // Hardware configuration
  22. // Configure the appropriate pins for your connections
  23. /****************** Raspberry Pi ***********************/
  24. // Radio CE Pin, CSN Pin, SPI Speed
  25. // See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.
  26. // Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
  27. //RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
  28. // NEW: Setup for RPi B+
  29. //RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
  30. // Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
  31. RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
  32. /*** RPi Alternate ***/
  33. //Note: Specify SPI BUS 0 or 1 instead of CS pin number.
  34. // See http://tmrh20.github.io/RF24/RPi.html for more information on usage
  35. //RPi Alternate, with MRAA
  36. //RF24 radio(15,0);
  37. //RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
  38. //RF24 radio(22,0);
  39. /****************** Linux (BBB,x86,etc) ***********************/
  40. // See http://tmrh20.github.io/RF24/pages.html for more information on usage
  41. // See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
  42. // See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
  43. // Setup for ARM(Linux) devices like BBB using spidev (default is "/dev/spidev1.0" )
  44. //RF24 radio(115,0);
  45. //BBB Alternate, with mraa
  46. // CE pin = (Header P9, Pin 13) = 59 = 13 + 46
  47. //Note: Specify SPI BUS 0 or 1 instead of CS pin number.
  48. //RF24 radio(59,0);
  49. /********** User Config *********/
  50. // Assign a unique identifier for this node, 0 or 1. Arduino example uses radioNumber 0 by default.
  51. bool radioNumber = 1;
  52. /********************************/
  53. // Radio pipe addresses for the 2 nodes to communicate.
  54. const uint8_t addresses[][6] = {"1Node","2Node"};
  55. bool role_ping_out = 1, role_pong_back = 0, role = 0;
  56. uint8_t counter = 1; // A single byte to keep track of the data being sent back and forth
  57. int main(int argc, char** argv){
  58. cout << "RPi/RF24/examples/gettingstarted_call_response\n";
  59. radio.begin();
  60. radio.enableAckPayload(); // Allow optional ack payloads
  61. radio.enableDynamicPayloads();
  62. radio.printDetails(); // Dump the configuration of the rf unit for debugging
  63. /********* Role chooser ***********/
  64. printf("\n ************ Role Setup ***********\n");
  65. string input = "";
  66. char myChar = {0};
  67. cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (CTRL+C to exit)\n>";
  68. getline(cin,input);
  69. if(input.length() == 1) {
  70. myChar = input[0];
  71. if(myChar == '0'){
  72. cout << "Role: Pong Back, awaiting transmission " << endl << endl;
  73. }else{ cout << "Role: Ping Out, starting transmission " << endl << endl;
  74. role = role_ping_out;
  75. }
  76. }
  77. /***********************************/
  78. // This opens two pipes for these two nodes to communicate
  79. // back and forth.
  80. if ( !radioNumber ) {
  81. radio.openWritingPipe(addresses[0]);
  82. radio.openReadingPipe(1,addresses[1]);
  83. }else{
  84. radio.openWritingPipe(addresses[1]);
  85. radio.openReadingPipe(1,addresses[0]);
  86. }
  87. radio.startListening();
  88. radio.writeAckPayload(1,&counter,1);
  89. // forever loop
  90. while (1){
  91. /****************** Ping Out Role ***************************/
  92. if (role == role_ping_out){ // Radio is in ping mode
  93. uint8_t gotByte; // Initialize a variable for the incoming response
  94. radio.stopListening(); // First, stop listening so we can talk.
  95. printf("Now sending %d as payload. ",counter); // Use a simple byte counter as payload
  96. unsigned long time = millis(); // Record the current microsecond count
  97. if ( radio.write(&counter,1) ){ // Send the counter variable to the other radio
  98. if(!radio.available()){ // If nothing in the buffer, we got an ack but it is blank
  99. printf("Got blank response. round-trip delay: %lu ms\n\r",millis()-time);
  100. }else{
  101. while(radio.available() ){ // If an ack with payload was received
  102. radio.read( &gotByte, 1 ); // Read it, and display the response time
  103. printf("Got response %d, round-trip delay: %lu ms\n\r",gotByte,millis()-time);
  104. counter++; // Increment the counter variable
  105. }
  106. }
  107. }else{ printf("Sending failed.\n\r"); } // If no ack response, sending failed
  108. sleep(1); // Try again later
  109. }
  110. /****************** Pong Back Role ***************************/
  111. if ( role == role_pong_back ) {
  112. uint8_t pipeNo, gotByte; // Declare variables for the pipe and the byte received
  113. if( radio.available(&pipeNo)){ // Read all available payloads
  114. radio.read( &gotByte, 1 );
  115. // Since this is a call-response. Respond directly with an ack payload.
  116. gotByte += 1; // Ack payloads are much more efficient than switching to transmit mode to respond to a call
  117. radio.writeAckPayload(pipeNo,&gotByte, 1 ); // This can be commented out to send empty payloads.
  118. printf("Loaded next response %d \n\r", gotByte);
  119. delay(900); //Delay after a response to minimize CPU usage on RPi
  120. //Expects a payload every second
  121. }
  122. }
  123. } //while 1
  124. } //main