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.

141 lines
4.6 KiB

  1. #!/usr/bin/env python
  2. #
  3. # Example using Dynamic Payloads
  4. #
  5. # This is an example of how to use payloads of a varying (dynamic) size.
  6. #
  7. from __future__ import print_function
  8. import time
  9. from RF24 import *
  10. import RPi.GPIO as GPIO
  11. irq_gpio_pin = None
  12. ########### USER CONFIGURATION ###########
  13. # See https://github.com/TMRh20/RF24/blob/master/pyRF24/readme.md
  14. # CE Pin, CSN Pin, SPI Speed
  15. # Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
  16. #radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ)
  17. #RPi B
  18. # Setup for GPIO 15 CE and CE1 CSN with SPI Speed @ 8Mhz
  19. #radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ)
  20. #RPi B+
  21. # Setup for GPIO 22 CE and CE0 CSN for RPi B+ with SPI Speed @ 8Mhz
  22. #radio = RF24(RPI_BPLUS_GPIO_J8_15, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)
  23. # RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
  24. radio = RF24(22, 0);
  25. # Setup for connected IRQ pin, GPIO 24 on RPi B+; uncomment to activate
  26. #irq_gpio_pin = RPI_BPLUS_GPIO_J8_18
  27. #irq_gpio_pin = 24
  28. ##########################################
  29. def try_read_data(channel=0):
  30. if radio.available():
  31. while radio.available():
  32. len = radio.getDynamicPayloadSize()
  33. receive_payload = radio.read(len)
  34. print('Got payload size={} value="{}"'.format(len, receive_payload.decode('utf-8')))
  35. # First, stop listening so we can talk
  36. radio.stopListening()
  37. # Send the final one back.
  38. radio.write(receive_payload)
  39. print('Sent response.')
  40. # Now, resume listening so we catch the next packets.
  41. radio.startListening()
  42. pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2]
  43. min_payload_size = 4
  44. max_payload_size = 32
  45. payload_size_increments_by = 1
  46. next_payload_size = min_payload_size
  47. inp_role = 'none'
  48. send_payload = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ789012'
  49. millis = lambda: int(round(time.time() * 1000))
  50. print('pyRF24/examples/pingpair_dyn/')
  51. radio.begin()
  52. radio.enableDynamicPayloads()
  53. radio.setRetries(5,15)
  54. radio.printDetails()
  55. print(' ************ Role Setup *********** ')
  56. while (inp_role !='0') and (inp_role !='1'):
  57. inp_role = str(input('Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) '))
  58. if inp_role == '0':
  59. print('Role: Pong Back, awaiting transmission')
  60. if irq_gpio_pin is not None:
  61. # set up callback for irq pin
  62. GPIO.setmode(GPIO.BCM)
  63. GPIO.setup(irq_gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  64. GPIO.add_event_detect(irq_gpio_pin, GPIO.FALLING, callback=try_read_data)
  65. radio.openWritingPipe(pipes[1])
  66. radio.openReadingPipe(1,pipes[0])
  67. radio.startListening()
  68. else:
  69. print('Role: Ping Out, starting transmission')
  70. radio.openWritingPipe(pipes[0])
  71. radio.openReadingPipe(1,pipes[1])
  72. # forever loop
  73. while 1:
  74. if inp_role == '1': # ping out
  75. # The payload will always be the same, what will change is how much of it we send.
  76. # First, stop listening so we can talk.
  77. radio.stopListening()
  78. # Take the time, and send it. This will block until complete
  79. print('Now sending length {} ... '.format(next_payload_size), end="")
  80. radio.write(send_payload[:next_payload_size])
  81. # Now, continue listening
  82. radio.startListening()
  83. # Wait here until we get a response, or timeout
  84. started_waiting_at = millis()
  85. timeout = False
  86. while (not radio.available()) and (not timeout):
  87. if (millis() - started_waiting_at) > 500:
  88. timeout = True
  89. # Describe the results
  90. if timeout:
  91. print('failed, response timed out.')
  92. else:
  93. # Grab the response, compare, and send to debugging spew
  94. len = radio.getDynamicPayloadSize()
  95. receive_payload = radio.read(len)
  96. # Spew it
  97. print('got response size={} value="{}"'.format(len, receive_payload.decode('utf-8')))
  98. # Update size for next time.
  99. next_payload_size += payload_size_increments_by
  100. if next_payload_size > max_payload_size:
  101. next_payload_size = min_payload_size
  102. time.sleep(0.1)
  103. else:
  104. # Pong back role. Receive each packet, dump it out, and send it back
  105. # if there is data ready
  106. if irq_gpio_pin is None:
  107. # no irq pin is set up -> poll it
  108. try_read_data()
  109. else:
  110. # callback routine set for irq pin takes care for reading -
  111. # do nothing, just sleeps in order not to burn cpu by looping
  112. time.sleep(1000)