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.

226 lines
7.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. TMRh20 2014 - Updates to the library allow sleeping both in TX and RX modes:
  7. TX Mode: The radio can be powered down (.9uA current) and the Arduino slept using the watchdog timer
  8. RX Mode: The radio can be left in standby mode (22uA current) and the Arduino slept using an interrupt pin
  9. */
  10. /**
  11. * Example RF Radio Ping Pair which Sleeps between Sends
  12. *
  13. * This is an example of how to use the RF24 class to create a battery-
  14. * efficient system. It is just like the GettingStarted_CallResponse example, but the
  15. * ping node powers down the radio and sleeps the MCU after every
  16. * ping/pong cycle, and the receiver sleeps between payloads.
  17. *
  18. * Write this sketch to two different nodes,
  19. * connect the role_pin to ground on one. The ping node sends the current
  20. * time to the pong node, which responds by sending the value back. The ping
  21. * node can then see how long the whole cycle took.
  22. */
  23. #include <SPI.h>
  24. #include <avr/sleep.h>
  25. #include <avr/power.h>
  26. #include "nRF24L01.h"
  27. #include "RF24.h"
  28. #include "printf.h"
  29. // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
  30. RF24 radio(7,8);
  31. // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
  32. // Leave open to be the 'ping' transmitter
  33. const int role_pin = 5;
  34. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL }; // Radio pipe addresses for the 2 nodes to communicate.
  35. // Role management
  36. // Set up role. This sketch uses the same software for all the nodes
  37. // in this system. Doing so greatly simplifies testing. The hardware itself specifies
  38. // which node it is.
  39. // The various roles supported by this sketch
  40. typedef enum { role_ping_out = 1, role_pong_back } role_e;
  41. // The debug-friendly names of those roles
  42. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
  43. // The role of the current running sketch
  44. role_e role;
  45. // Sleep declarations
  46. typedef enum { wdt_16ms = 0, wdt_32ms, wdt_64ms, wdt_128ms, wdt_250ms, wdt_500ms, wdt_1s, wdt_2s, wdt_4s, wdt_8s } wdt_prescalar_e;
  47. void setup_watchdog(uint8_t prescalar);
  48. void do_sleep(void);
  49. const short sleep_cycles_per_transmission = 4;
  50. volatile short sleep_cycles_remaining = sleep_cycles_per_transmission;
  51. void setup(){
  52. // set up the role pin
  53. pinMode(role_pin, INPUT);
  54. digitalWrite(role_pin,HIGH);
  55. delay(20); // Just to get a solid reading on the role pin
  56. // read the address pin, establish our role
  57. if ( digitalRead(role_pin) )
  58. role = role_ping_out;
  59. else
  60. role = role_pong_back;
  61. Serial.begin(115200);
  62. printf_begin();
  63. Serial.print(F("\n\rRF24/examples/pingpair_sleepy/\n\rROLE: "));
  64. Serial.println(role_friendly_name[role]);
  65. // Prepare sleep parameters
  66. // Only the ping out role uses WDT. Wake up every 4s to send a ping
  67. //if ( role == role_ping_out )
  68. setup_watchdog(wdt_4s);
  69. // Setup and configure rf radio
  70. radio.begin();
  71. // Open pipes to other nodes for communication
  72. // This simple sketch opens two pipes for these two nodes to communicate
  73. // back and forth.
  74. // Open 'our' pipe for writing
  75. // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
  76. if ( role == role_ping_out ) {
  77. radio.openWritingPipe(pipes[0]);
  78. radio.openReadingPipe(1,pipes[1]);
  79. } else {
  80. radio.openWritingPipe(pipes[1]);
  81. radio.openReadingPipe(1,pipes[0]);
  82. }
  83. // Start listening
  84. radio.startListening();
  85. // Dump the configuration of the rf unit for debugging
  86. //radio.printDetails();
  87. }
  88. void loop(){
  89. if (role == role_ping_out) { // Ping out role. Repeatedly send the current time
  90. radio.powerUp(); // Power up the radio after sleeping
  91. radio.stopListening(); // First, stop listening so we can talk.
  92. unsigned long time = millis(); // Take the time, and send it.
  93. Serial.print(F("Now sending... "));
  94. Serial.println(time);
  95. radio.write( &time, sizeof(unsigned long) );
  96. radio.startListening(); // Now, continue listening
  97. unsigned long started_waiting_at = millis(); // Wait here until we get a response, or timeout (250ms)
  98. bool timeout = false;
  99. while ( ! radio.available() ){
  100. if (millis() - started_waiting_at > 250 ){ // Break out of the while loop if nothing available
  101. timeout = true;
  102. break;
  103. }
  104. }
  105. if ( timeout ) { // Describe the results
  106. Serial.println(F("Failed, response timed out."));
  107. } else {
  108. unsigned long got_time; // Grab the response, compare, and send to debugging spew
  109. radio.read( &got_time, sizeof(unsigned long) );
  110. printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
  111. }
  112. // Shut down the system
  113. delay(500); // Experiment with some delay here to see if it has an effect
  114. // Power down the radio.
  115. radio.powerDown(); // NOTE: The radio MUST be powered back up again manually
  116. // Sleep the MCU.
  117. do_sleep();
  118. }
  119. // Pong back role. Receive each packet, dump it out, and send it back
  120. if ( role == role_pong_back ) {
  121. if ( radio.available() ) { // if there is data ready
  122. unsigned long got_time;
  123. while (radio.available()) { // Dump the payloads until we've gotten everything
  124. radio.read( &got_time, sizeof(unsigned long) ); // Get the payload, and see if this was the last one.
  125. // Spew it. Include our time, because the ping_out millis counter is unreliable
  126. printf("Got payload %lu @ %lu...",got_time,millis()); // due to it sleeping
  127. }
  128. radio.stopListening(); // First, stop listening so we can talk
  129. radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
  130. Serial.println(F("Sent response."));
  131. radio.startListening(); // Now, resume listening so we catch the next packets.
  132. } else {
  133. Serial.println(F("Sleeping"));
  134. delay(50); // Delay so the serial data can print out
  135. do_sleep();
  136. }
  137. }
  138. }
  139. void wakeUp(){
  140. sleep_disable();
  141. }
  142. // Sleep helpers
  143. //Prescaler values
  144. // 0=16ms, 1=32ms,2=64ms,3=125ms,4=250ms,5=500ms
  145. // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
  146. void setup_watchdog(uint8_t prescalar){
  147. uint8_t wdtcsr = prescalar & 7;
  148. if ( prescalar & 8 )
  149. wdtcsr |= _BV(WDP3);
  150. MCUSR &= ~_BV(WDRF); // Clear the WD System Reset Flag
  151. WDTCSR = _BV(WDCE) | _BV(WDE); // Write the WD Change enable bit to enable changing the prescaler and enable system reset
  152. WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE); // Write the prescalar bits (how long to sleep, enable the interrupt to wake the MCU
  153. }
  154. ISR(WDT_vect)
  155. {
  156. //--sleep_cycles_remaining;
  157. Serial.println(F("WDT"));
  158. }
  159. void do_sleep(void)
  160. {
  161. set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
  162. sleep_enable();
  163. attachInterrupt(0,wakeUp,LOW);
  164. WDTCSR |= _BV(WDIE);
  165. sleep_mode(); // System sleeps here
  166. // The WDT_vect interrupt wakes the MCU from here
  167. sleep_disable(); // System continues execution here when watchdog timed out
  168. detachInterrupt(0);
  169. WDTCSR &= ~_BV(WDIE);
  170. }