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.

216 lines
5.7 KiB

  1. /*
  2. Copyright (C) 2011 J. Coliz <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. 03/17/2013 : Charles-Henri Hallard (http://hallard.me)
  7. Modified to use with Arduipi board http://hallard.me/arduipi
  8. Changed to use modified bcm2835 and RF24 library
  9. TMRh20 2014 - Updated to work with optimized RF24 Arduino library
  10. */
  11. /**
  12. * Example RF Radio Ping Pair
  13. *
  14. * This is an example of how to use the RF24 class on RPi, communicating to an Arduino running
  15. * the GettingStarted sketch.
  16. */
  17. #include <cstdlib>
  18. #include <iostream>
  19. #include <sstream>
  20. #include <string>
  21. #include <unistd.h>
  22. #include <RF24/RF24.h>
  23. using namespace std;
  24. //
  25. // Hardware configuration
  26. // Configure the appropriate pins for your connections
  27. /****************** Raspberry Pi ***********************/
  28. // Radio CE Pin, CSN Pin, SPI Speed
  29. // See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.
  30. // Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
  31. //RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
  32. // NEW: Setup for RPi B+
  33. //RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
  34. // Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
  35. //RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
  36. // RPi generic:
  37. RF24 radio(22,0);
  38. /*** RPi Alternate ***/
  39. //Note: Specify SPI BUS 0 or 1 instead of CS pin number.
  40. // See http://tmrh20.github.io/RF24/RPi.html for more information on usage
  41. //RPi Alternate, with MRAA
  42. //RF24 radio(15,0);
  43. //RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
  44. //RF24 radio(22,0);
  45. /****************** Linux (BBB,x86,etc) ***********************/
  46. // See http://tmrh20.github.io/RF24/pages.html for more information on usage
  47. // See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
  48. // See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
  49. // Setup for ARM(Linux) devices like BBB using spidev (default is "/dev/spidev1.0" )
  50. //RF24 radio(115,0);
  51. //BBB Alternate, with mraa
  52. // CE pin = (Header P9, Pin 13) = 59 = 13 + 46
  53. //Note: Specify SPI BUS 0 or 1 instead of CS pin number.
  54. //RF24 radio(59,0);
  55. /********** User Config *********/
  56. // Assign a unique identifier for this node, 0 or 1
  57. bool radioNumber = 1;
  58. /********************************/
  59. // Radio pipe addresses for the 2 nodes to communicate.
  60. const uint8_t pipes[][6] = {"1Node","2Node"};
  61. int main(int argc, char** argv){
  62. bool role_ping_out = true, role_pong_back = false;
  63. bool role = role_pong_back;
  64. cout << "RF24/examples/GettingStarted/\n";
  65. // Setup and configure rf radio
  66. radio.begin();
  67. // optionally, increase the delay between retries & # of retries
  68. radio.setRetries(15,15);
  69. // Dump the configuration of the rf unit for debugging
  70. radio.printDetails();
  71. /********* Role chooser ***********/
  72. printf("\n ************ Role Setup ***********\n");
  73. string input = "";
  74. char myChar = {0};
  75. cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (CTRL+C to exit) \n>";
  76. getline(cin,input);
  77. if(input.length() == 1) {
  78. myChar = input[0];
  79. if(myChar == '0'){
  80. cout << "Role: Pong Back, awaiting transmission " << endl << endl;
  81. }else{ cout << "Role: Ping Out, starting transmission " << endl << endl;
  82. role = role_ping_out;
  83. }
  84. }
  85. /***********************************/
  86. // This simple sketch opens two pipes for these two nodes to communicate
  87. // back and forth.
  88. if ( !radioNumber ) {
  89. radio.openWritingPipe(pipes[0]);
  90. radio.openReadingPipe(1,pipes[1]);
  91. } else {
  92. radio.openWritingPipe(pipes[1]);
  93. radio.openReadingPipe(1,pipes[0]);
  94. }
  95. radio.startListening();
  96. // forever loop
  97. while (1)
  98. {
  99. if (role == role_ping_out)
  100. {
  101. // First, stop listening so we can talk.
  102. radio.stopListening();
  103. // Take the time, and send it. This will block until complete
  104. printf("Now sending...\n");
  105. unsigned long time = millis();
  106. bool ok = radio.write( &time, sizeof(unsigned long) );
  107. if (!ok){
  108. printf("failed.\n");
  109. }
  110. // Now, continue listening
  111. radio.startListening();
  112. // Wait here until we get a response, or timeout (250ms)
  113. unsigned long started_waiting_at = millis();
  114. bool timeout = false;
  115. while ( ! radio.available() && ! timeout ) {
  116. if (millis() - started_waiting_at > 200 )
  117. timeout = true;
  118. }
  119. // Describe the results
  120. if ( timeout )
  121. {
  122. printf("Failed, response timed out.\n");
  123. }
  124. else
  125. {
  126. // Grab the response, compare, and send to debugging spew
  127. unsigned long got_time;
  128. radio.read( &got_time, sizeof(unsigned long) );
  129. // Spew it
  130. printf("Got response %lu, round-trip delay: %lu\n",got_time,millis()-got_time);
  131. }
  132. sleep(1);
  133. }
  134. //
  135. // Pong back role. Receive each packet, dump it out, and send it back
  136. //
  137. if ( role == role_pong_back )
  138. {
  139. // if there is data ready
  140. if ( radio.available() )
  141. {
  142. // Dump the payloads until we've gotten everything
  143. unsigned long got_time;
  144. // Fetch the payload, and see if this was the last one.
  145. while(radio.available()){
  146. radio.read( &got_time, sizeof(unsigned long) );
  147. }
  148. radio.stopListening();
  149. radio.write( &got_time, sizeof(unsigned long) );
  150. // Now, resume listening so we catch the next packets.
  151. radio.startListening();
  152. // Spew it
  153. printf("Got payload(%d) %lu...\n",sizeof(unsigned long), got_time);
  154. delay(925); //Delay after payload responded to, minimize RPi CPU time
  155. }
  156. }
  157. } // forever loop
  158. return 0;
  159. }