Skip to content

Add rate limiter to Radio link #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions cflib/crtp/radiodriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import re
import struct
import threading
import time
from enum import Enum
from queue import Queue
from threading import Semaphore
Expand All @@ -46,8 +47,11 @@
from typing import Dict
from typing import Iterable
from typing import List
from typing import Optional
from typing import Tuple
from typing import Union
from urllib.parse import parse_qs
from urllib.parse import urlparse

import cflib.drivers.crazyradio as crazyradio
from .crtpstack import CRTPPacket
Expand Down Expand Up @@ -258,7 +262,7 @@ def connect(self, uri, link_quality_callback, link_error_callback):
an error message.
"""

devid, channel, datarate, address = self.parse_uri(uri)
devid, channel, datarate, address, rate_limit = self.parse_uri(uri)
self.uri = uri

if self._radio is None:
Expand All @@ -285,56 +289,59 @@ def connect(self, uri, link_quality_callback, link_error_callback):
self.out_queue,
link_quality_callback,
link_error_callback,
self)
self,
rate_limit)
self._thread.start()

self.link_error_callback = link_error_callback

@staticmethod
def parse_uri(uri):
def parse_uri(uri: str):
# check if the URI is a radio URI
if not re.search('^radio://', uri):
if not uri.startswith('radio://'):
raise WrongUriType('Not a radio URI')

# Open the USB dongle
if not re.search('^radio://([0-9a-fA-F]+)((/([0-9]+))'
'((/(250K|1M|2M))?(/([A-F0-9]+))?)?)?(\\?.+)?$', uri):
raise WrongUriType('Wrong radio URI format!')

uri_data = re.search('^radio://([0-9a-fA-F]+)((/([0-9]+))'
'((/(250K|1M|2M))?(/([A-F0-9]+))?)?)?(\\?.+)?$', uri)
parsed_uri = urlparse(uri)
parsed_query = parse_qs(parsed_uri.query)
parsed_path = parsed_uri.path.strip('/').split('/')

if len(uri_data.group(1)) < 10 and uri_data.group(1).isdigit():
devid = int(uri_data.group(1))
# Open the USB dongle
if len(parsed_uri.netloc) < 10 and parsed_uri.netloc.isdigit():
devid = int(parsed_uri.netloc)
else:
try:
devid = crazyradio.get_serials().index(
uri_data.group(1).upper())
parsed_uri.netloc.upper())
except ValueError:
raise Exception('Cannot find radio with serial {}'.format(
uri_data.group(1)))
parsed_uri.netloc))

channel = 2
if uri_data.group(4):
channel = int(uri_data.group(4))
if len(parsed_path) > 0:
channel = int(parsed_path[0])

datarate = Crazyradio.DR_2MPS
if uri_data.group(7) == '250K':
datarate = Crazyradio.DR_250KPS
if uri_data.group(7) == '1M':
datarate = Crazyradio.DR_1MPS
if uri_data.group(7) == '2M':
datarate = Crazyradio.DR_2MPS
if len(parsed_path) > 1:
if parsed_path[1] == '250K':
datarate = Crazyradio.DR_250KPS
if parsed_path[1] == '1M':
datarate = Crazyradio.DR_1MPS
if parsed_path[1] == '2M':
datarate = Crazyradio.DR_2MPS

address = DEFAULT_ADDR_A
if uri_data.group(9):
if len(parsed_path) > 2:
# We make sure to pad the address with zeros if we do not have the
# correct length.
addr = '{:0>10}'.format(uri_data.group(9))
addr = '{:0>10}'.format(parsed_path[2])
new_addr = struct.unpack('<BBBBB', binascii.unhexlify(addr))
address = new_addr

return devid, channel, datarate, address
rate_limit = None
if 'rate_limit' in parsed_query:
rate_limit = int(parsed_query['rate_limit'][0])

return devid, channel, datarate, address, rate_limit

def receive_packet(self, wait=0):
"""
Expand Down Expand Up @@ -517,7 +524,7 @@ class _RadioDriverThread(threading.Thread):
Crazyradio USB driver. """

def __init__(self, radio, inQueue, outQueue,
link_quality_callback, link_error_callback, link):
link_quality_callback, link_error_callback, link, rate_limit: Optional[int]):
""" Create the object """
threading.Thread.__init__(self)
self._radio = radio
Expand All @@ -529,6 +536,7 @@ def __init__(self, radio, inQueue, outQueue,
self._retry_before_disconnect = _nr_of_retries
self._retries = collections.deque()
self._retry_sum = 0
self.rate_limit = rate_limit

self._curr_up = 0
self._curr_down = 1
Expand Down Expand Up @@ -641,6 +649,11 @@ def run(self):
else:
waitTime = 0

# If there is a rate limit setup, sleep here to force the rate limit
if self.rate_limit:
time.sleep(1.0/self.rate_limit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought, nowhere in the code does anything state that rate_limit is in Hz ... should this be more explicit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An SI rate is normally Hz so I did not think about precising it. Are you envisioning a comment or renaming the variable with "_hz" in the name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, perhaps just some cognitive help while reading this part:

            # If there is a rate limit setup, sleep here to force the rate limit (Hz)
            if self.rate_limit:
                time.sleep(1.0/self.rate_limit)

waitTime = 0

# get the next packet to send of relaxation (wait 10ms)
outPacket = None
try:
Expand Down