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.

161 lines
5.0 KiB

  1. /*
  2. * Getting Started example sketch for nRF24L01+ radios
  3. * This is an example of how to send data from one node to another using data structures
  4. * Updated: Dec 2014 by TMRh20
  5. */
  6. #include <SPI.h>
  7. #include "RF24.h"
  8. byte addresses[][6] = {"1Node","2Node"};
  9. /****************** User Config ***************************/
  10. /*** Set this radio as radio number 0 or 1 ***/
  11. bool radioNumber = 1;
  12. /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
  13. RF24 radio(7,8);
  14. /**********************************************************/
  15. // Used to control whether this node is sending or receiving
  16. bool role = 0;
  17. /**
  18. * Create a data structure for transmitting and receiving data
  19. * This allows many variables to be easily sent and received in a single transmission
  20. * See http://www.cplusplus.com/doc/tutorial/structures/
  21. */
  22. struct dataStruct{
  23. unsigned long _micros;
  24. float value;
  25. }myData;
  26. void setup() {
  27. Serial.begin(115200);
  28. Serial.println(F("RF24/examples/GettingStarted_HandlingData"));
  29. Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  30. radio.begin();
  31. // Set the PA Level low to prevent power supply related issues since this is a
  32. // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  33. radio.setPALevel(RF24_PA_LOW);
  34. // Open a writing and reading pipe on each radio, with opposite addresses
  35. if(radioNumber){
  36. radio.openWritingPipe(addresses[1]);
  37. radio.openReadingPipe(1,addresses[0]);
  38. }else{
  39. radio.openWritingPipe(addresses[0]);
  40. radio.openReadingPipe(1,addresses[1]);
  41. }
  42. myData.value = 1.22;
  43. // Start the radio listening for data
  44. radio.startListening();
  45. }
  46. void loop() {
  47. /****************** Ping Out Role ***************************/
  48. if (role == 1) {
  49. radio.stopListening(); // First, stop listening so we can talk.
  50. Serial.println(F("Now sending"));
  51. myData._micros = micros();
  52. if (!radio.write( &myData, sizeof(myData) )){
  53. Serial.println(F("failed"));
  54. }
  55. radio.startListening(); // Now, continue listening
  56. unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
  57. boolean timeout = false; // Set up a variable to indicate if a response was received or not
  58. while ( ! radio.available() ){ // While nothing is received
  59. if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
  60. timeout = true;
  61. break;
  62. }
  63. }
  64. if ( timeout ){ // Describe the results
  65. Serial.println(F("Failed, response timed out."));
  66. }else{
  67. // Grab the response, compare, and send to debugging spew
  68. radio.read( &myData, sizeof(myData) );
  69. unsigned long time = micros();
  70. // Spew it
  71. Serial.print(F("Sent "));
  72. Serial.print(time);
  73. Serial.print(F(", Got response "));
  74. Serial.print(myData._micros);
  75. Serial.print(F(", Round-trip delay "));
  76. Serial.print(time-myData._micros);
  77. Serial.print(F(" microseconds Value "));
  78. Serial.println(myData.value);
  79. }
  80. // Try again 1s later
  81. delay(1000);
  82. }
  83. /****************** Pong Back Role ***************************/
  84. if ( role == 0 )
  85. {
  86. if( radio.available()){
  87. // Variable for the received timestamp
  88. while (radio.available()) { // While there is data ready
  89. radio.read( &myData, sizeof(myData) ); // Get the payload
  90. }
  91. radio.stopListening(); // First, stop listening so we can talk
  92. myData.value += 0.01; // Increment the float value
  93. radio.write( &myData, sizeof(myData) ); // Send the final one back.
  94. radio.startListening(); // Now, resume listening so we catch the next packets.
  95. Serial.print(F("Sent response "));
  96. Serial.print(myData._micros);
  97. Serial.print(F(" : "));
  98. Serial.println(myData.value);
  99. }
  100. }
  101. /****************** Change Roles via Serial Commands ***************************/
  102. if ( Serial.available() )
  103. {
  104. char c = toupper(Serial.read());
  105. if ( c == 'T' && role == 0 ){
  106. Serial.print(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  107. role = 1; // Become the primary transmitter (ping out)
  108. }else
  109. if ( c == 'R' && role == 1 ){
  110. Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  111. role = 0; // Become the primary receiver (pong back)
  112. radio.startListening();
  113. }
  114. }
  115. } // Loop