Commit 4f75d9e4 authored by Andreas Schmidt's avatar Andreas Schmidt

~= optimize driver import in bridge.py

parent 7de5b270
......@@ -4,9 +4,38 @@ import sys
import threading
import signal
import cflib.crtp as crtp
from cflib.crtp.crtpstack import CRTPPacket, CRTPPort
PACKAGES = {
"prrt": "cflib.crtp.prrtdriver",
"radio": "cflib.crtp.radiodriver",
"serial": "cflib.crtp.serialdriver",
"udp": "cflib.crtp.udpdriver",
"usb": "cflib.crtp.usbdriver",
}
NAMES = {
"prrt": "PrrtDriver",
"radio": "RadioDriver",
"serial": "SerialDriver",
"udp": "UdpDriver",
"usb": "UsbDriver",
}
drivers = { }
def get_link_driver(uri, link_quality_callback=None, link_error_callback=None):
protocol = uri.split("://")[0]
if not protocol in drivers:
package = PACKAGES[protocol]
name = NAMES[protocol]
driver = getattr(__import__(package, fromlist=[name]), name)
drivers[protocol] = driver
driverClass = drivers[protocol]
instance = driverClass()
instance.connect(uri, link_quality_callback, link_error_callback)
return instance
class ForwardBridge(threading.Thread):
def __init__(self, crazyflie_connection, client_connection):
......@@ -48,14 +77,12 @@ class Bridge:
self._crazyflie_uri = crazyflie_uri
self._client_uri = client_uri
crtp.init_drivers(enable_debug_driver=False)
self._crazyflie_connection = crtp.get_link_driver(self._crazyflie_uri, None, None)
self._crazyflie_connection = get_link_driver(self._crazyflie_uri, None, None)
if not self._crazyflie_connection:
raise Exception('No diver found for Crazyflie URI ' + self._crazyflie_uri)
self._client_connection = crtp.get_link_driver(self._client_uri, None, None)
raise Exception('No driver found for Crazyflie URI ' + self._crazyflie_uri)
self._client_connection = get_link_driver(self._client_uri, None, None)
if not self._client_connection:
raise Exception('No diver found for Client URI ' + self._client_uri)
raise Exception('No driver found for Client URI ' + self._client_uri)
self._forward_thread = None
self._backward_thread = None
......@@ -99,7 +126,7 @@ if __name__ == '__main__':
bridge = Bridge(serial_uri, prrt_uri)
# bridge.check_for_crazyflie_connection() # optional
bridge.check_for_crazyflie_connection() # optional
bridge.start()
print('\nSetup finished.\nUse "Ctrl+C" to stop.\n')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment