Commit d032e3a6 authored by Marlene Böhmer's avatar Marlene Böhmer

Bridge: rename variables; do not check for connection

parent 2932a150
......@@ -3,6 +3,7 @@
import sys
import threading
import signal
import time
import cflib.crtp as crtp
from cflib.crtp.crtpstack import CRTPPacket, CRTPPort
......@@ -13,16 +14,17 @@ class ForwardBridge(threading.Thread):
threading.Thread.__init__(self)
self._crazyflie_connection = crazyflie_connection
self._client_connection = client_connection
self.stop_running = False
self._stop_running = False
def run(self):
while not self.stop_running:
while not self._stop_running:
pk = self._client_connection.receive_packet(-1)
if pk and not self.stop_running:
if pk and not self._stop_running:
self._crazyflie_connection.send_packet(pk)
time.sleep(0.001)
def stop(self):
self.stop_running = True
self._stop_running = True
class BackwardBridge(threading.Thread):
......@@ -30,58 +32,60 @@ class BackwardBridge(threading.Thread):
threading.Thread.__init__(self)
self._crazyflie_connection = crazyflie_connection
self._client_connection = client_connection
self.stop_running = False
self._stop_running = False
def run(self):
while not self.stop_running:
while not self._stop_running:
pk = self._crazyflie_connection.receive_packet(-1)
if pk and not self.stop_running:
if pk and not self._stop_running:
self._client_connection.send_packet(pk)
time.sleep(0.001)
def stop(self):
self.stop_running = True
self._stop_running = True
class Bridge:
def __init__(self, crazyflie_uri, client_uri):
print('Initializing Bridge ...')
self.crazyflie_uri = crazyflie_uri
self.client_uri = client_uri
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 = crtp.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 diver found for Crazyflie URI ' + self._crazyflie_uri)
self._client_connection = crtp.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 diver found for Client URI ' + self._client_uri)
self.forward_thread = None
self.backward_thread = None
self._forward_thread = None
self._backward_thread = None
print('Bridge initialized.')
def check_for_crazyflie_connection(self):
print('Connecting to ' + str(self.crazyflie_uri) + ' ...')
print('Connecting to ' + str(self._crazyflie_uri) + ' ...')
packet = CRTPPacket()
packet.port = CRTPPort.LINKCTRL
packet.channel = 1
self._crazyflie_connection.send_packet(packet)
self._crazyflie_connection.receive_packet(-1)
print('Connected to ' + str(self.crazyflie_uri) + '.')
print('Connected to ' + str(self._crazyflie_uri) + '.')
def start(self):
self.forward_thread = ForwardBridge(self._crazyflie_connection, self._client_connection)
self.backward_thread = BackwardBridge(self._crazyflie_connection, self._client_connection)
self.forward_thread.daemon = True
self.backward_thread.daemon = True
self.forward_thread.start()
self.backward_thread.start()
self._forward_thread = ForwardBridge(self._crazyflie_connection, self._client_connection)
self._backward_thread = BackwardBridge(self._crazyflie_connection, self._client_connection)
self._forward_thread.daemon = True
self._backward_thread.daemon = True
self._forward_thread.start()
self._backward_thread.start()
def stop(self):
print('\nStopping threads ...')
self.forward_thread.stop()
self.backward_thread.stop()
self._forward_thread.stop()
self._backward_thread.stop()
print('Closing connections ...')
self._crazyflie_connection.close()
self._client_connection.close()
......@@ -95,11 +99,11 @@ if __name__ == '__main__':
sys.exit(0)
serial_uri = 'serial://pi'
prrt_uri = sys.argv[1]
prrt_uri = 'prrt://' + sys.argv[1] + ':5000'
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