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.

121 lines
3.7 KiB

  1. /*
  2. This program is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU General Public License
  4. version 2 as published by the Free Software Foundation.
  5. Created Dec 2014 - TMRh20
  6. */
  7. /**
  8. * Example of using interrupts
  9. *
  10. * This is a very simple example of using two devices to communicate using interrupts.
  11. * With multiple devices, each device would need to have a separate reading pipe
  12. */
  13. #include <SPI.h>
  14. #include "RF24.h"
  15. #include <printf.h>
  16. // Hardware configuration
  17. // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
  18. RF24 radio(7,8);
  19. // Use the same address for both devices
  20. uint8_t address[] = { "radio" };
  21. // Simple messages to represent a 'ping' and 'pong'
  22. uint8_t ping = 111;
  23. uint8_t pong = 222;
  24. volatile uint32_t round_trip_timer = 0;
  25. /********************** Setup *********************/
  26. void setup(){
  27. Serial.begin(115200);
  28. Serial.println(F("Simple pingpair example"));
  29. Serial.println(F("Send a 'T' via Serial to transmit a single 'ping' "));
  30. //printf_begin();
  31. // Setup and configure rf radio
  32. radio.begin();
  33. // Use dynamic payloads to improve response time
  34. radio.enableDynamicPayloads();
  35. radio.openWritingPipe(address); // communicate back and forth. One listens on it, the other talks to it.
  36. radio.openReadingPipe(1,address);
  37. radio.startListening();
  38. //radio.printDetails(); // Dump the configuration of the rf unit for debugging
  39. attachInterrupt(0, check_radio, LOW); // Attach interrupt handler to interrupt #0 (using pin 2) on BOTH the sender and receiver
  40. }
  41. /********************** Main Loop *********************/
  42. void loop() {
  43. if(Serial.available()){
  44. switch(toupper(Serial.read())){
  45. case 'T':
  46. // Only allow 1 transmission per 45ms to prevent overlapping IRQ/reads/writes
  47. // Default retries = 5,15 = ~20ms per transmission max
  48. while(micros() - round_trip_timer < 45000){
  49. //delay between writes
  50. }
  51. Serial.print(F("Sending Ping"));
  52. radio.stopListening();
  53. round_trip_timer = micros();
  54. radio.startWrite( &ping, sizeof(uint8_t),0 );
  55. break;
  56. }
  57. }
  58. }
  59. /********************** Interrupt *********************/
  60. void check_radio(void) // Receiver role: Does nothing! All the work is in IRQ
  61. {
  62. bool tx,fail,rx;
  63. radio.whatHappened(tx,fail,rx); // What happened?
  64. // If data is available, handle it accordingly
  65. if ( rx ){
  66. if(radio.getDynamicPayloadSize() < 1){
  67. // Corrupt payload has been flushed
  68. return;
  69. }
  70. // Read in the data
  71. uint8_t received;
  72. radio.read(&received,sizeof(received));
  73. // If this is a ping, send back a pong
  74. if(received == ping){
  75. radio.stopListening();
  76. // Normal delay will not work here, so cycle through some no-operations (16nops @16mhz = 1us delay)
  77. for(uint32_t i=0; i<130;i++){
  78. __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
  79. }
  80. radio.startWrite(&pong,sizeof(pong),0);
  81. Serial.print("pong");
  82. }else
  83. // If this is a pong, get the current micros()
  84. if(received == pong){
  85. round_trip_timer = micros() - round_trip_timer;
  86. Serial.print(F("Received Pong, Round Trip Time: "));
  87. Serial.println(round_trip_timer);
  88. }
  89. }
  90. // Start listening if transmission is complete
  91. if( tx || fail ){
  92. radio.startListening();
  93. Serial.println(tx ? F(":OK") : F(":Fail"));
  94. }
  95. }