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.

143 lines
5.9 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. Update 2014 - TMRh20
  7. */
  8. /**
  9. * Example of using interrupts
  10. *
  11. * This is an example of how to user interrupts to interact with the radio, and a demonstration
  12. * of how to use them to sleep when receiving, and not miss any payloads.
  13. * The pingpair_sleepy example expands on sleep functionality with a timed sleep option for the transmitter.
  14. * Sleep functionality is built directly into my fork of the RF24Network library
  15. */
  16. #include <SPI.h>
  17. #include "nRF24L01.h"
  18. #include "RF24.h"
  19. #include "printf.h"
  20. // Hardware configuration
  21. RF24 radio(7,8); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
  22. const short role_pin = 5; // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
  23. // Leave open to be the 'ping' transmitter
  24. // Demonstrates another method of setting up the addresses
  25. byte address[][5] = { 0xCC,0xCE,0xCC,0xCE,0xCC , 0xCE,0xCC,0xCE,0xCC,0xCE};
  26. // Role management
  27. // Set up role. This sketch uses the same software for all the nodes in this
  28. // system. Doing so greatly simplifies testing. The hardware itself specifies
  29. // which node it is.
  30. // This is done through the role_pin
  31. typedef enum { role_sender = 1, role_receiver } role_e; // The various roles supported by this sketch
  32. const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"}; // The debug-friendly names of those roles
  33. role_e role; // The role of the current running sketch
  34. static uint32_t message_count = 0;
  35. /********************** Setup *********************/
  36. void setup(){
  37. pinMode(role_pin, INPUT); // set up the role pin
  38. digitalWrite(role_pin,HIGH); // Change this to LOW/HIGH instead of using an external pin
  39. delay(20); // Just to get a solid reading on the role pin
  40. if ( digitalRead(role_pin) ) // read the address pin, establish our role
  41. role = role_sender;
  42. else
  43. role = role_receiver;
  44. Serial.begin(115200);
  45. printf_begin();
  46. Serial.print(F("\n\rRF24/examples/pingpair_irq\n\rROLE: "));
  47. Serial.println(role_friendly_name[role]);
  48. // Setup and configure rf radio
  49. radio.begin();
  50. //radio.setPALevel(RF24_PA_LOW);
  51. radio.enableAckPayload(); // We will be using the Ack Payload feature, so please enable it
  52. radio.enableDynamicPayloads(); // Ack payloads are dynamic payloads
  53. // Open pipes to other node for communication
  54. if ( role == role_sender ) { // This simple sketch opens a pipe on a single address for these two nodes to
  55. radio.openWritingPipe(address[0]); // communicate back and forth. One listens on it, the other talks to it.
  56. radio.openReadingPipe(1,address[1]);
  57. }else{
  58. radio.openWritingPipe(address[1]);
  59. radio.openReadingPipe(1,address[0]);
  60. radio.startListening();
  61. radio.writeAckPayload( 1, &message_count, sizeof(message_count) ); // Add an ack packet for the next time around. This is a simple
  62. ++message_count;
  63. }
  64. radio.printDetails(); // Dump the configuration of the rf unit for debugging
  65. delay(50);
  66. attachInterrupt(0, check_radio, LOW); // Attach interrupt handler to interrupt #0 (using pin 2) on BOTH the sender and receiver
  67. }
  68. /********************** Main Loop *********************/
  69. void loop() {
  70. if (role == role_sender) { // Sender role. Repeatedly send the current time
  71. unsigned long time = millis(); // Take the time, and send it.
  72. Serial.print(F("Now sending "));
  73. Serial.println(time);
  74. radio.startWrite( &time, sizeof(unsigned long) ,0);
  75. delay(2000); // Try again soon
  76. }
  77. if(role == role_receiver){ // Receiver does nothing except in IRQ
  78. }
  79. }
  80. /********************** Interrupt *********************/
  81. void check_radio(void) // Receiver role: Does nothing! All the work is in IRQ
  82. {
  83. bool tx,fail,rx;
  84. radio.whatHappened(tx,fail,rx); // What happened?
  85. if ( tx ) { // Have we successfully transmitted?
  86. if ( role == role_sender ){ Serial.println(F("Send:OK")); }
  87. if ( role == role_receiver ){ Serial.println(F("Ack Payload:Sent")); }
  88. }
  89. if ( fail ) { // Have we failed to transmit?
  90. if ( role == role_sender ){ Serial.println(F("Send:Failed")); }
  91. if ( role == role_receiver ){ Serial.println(F("Ack Payload:Failed")); }
  92. }
  93. if ( rx || radio.available()){ // Did we receive a message?
  94. if ( role == role_sender ) { // If we're the sender, we've received an ack payload
  95. radio.read(&message_count,sizeof(message_count));
  96. Serial.print(F("Ack: "));
  97. Serial.println(message_count);
  98. }
  99. if ( role == role_receiver ) { // If we're the receiver, we've received a time message
  100. static unsigned long got_time; // Get this payload and dump it
  101. radio.read( &got_time, sizeof(got_time) );
  102. Serial.print(F("Got payload "));
  103. Serial.println(got_time);
  104. radio.writeAckPayload( 1, &message_count, sizeof(message_count) ); // Add an ack packet for the next time around. This is a simple
  105. ++message_count; // packet counter
  106. }
  107. }
  108. }