-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicCrazyflie.py
130 lines (108 loc) · 4.25 KB
/
dynamicCrazyflie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
#
# || ____ _ __
# +------+ / __ )(_) /_______________ _____ ___
# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
#
# Copyright (C) 2016 Bitcraze AB
#
# Crazyflie Nano Quadcopter Client
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
"""
The synchronous Crazyflie class is a wrapper around the "normal" Crazyflie
class. It handles the asynchronous nature of the Crazyflie API and turns it
into blocking function. It is useful for simple scripts that performs tasks
as a sequence of events.
"""
import logging
from threading import Event
from cflib.crazyflie import Crazyflie
logger = logging.getLogger(__name__)
class SyncCrazyflie:
def __init__(self, link_uri, cf=None):
""" Create a synchronous Crazyflie instance with the specified
link_uri """
if cf:
self.cf = cf
else:
self.cf = Crazyflie()
self._link_uri = link_uri
self._connect_event = None
self._disconnect_event = None
self._is_link_open = False
self._error_message = None
def open_link(self):
if (self.is_link_open()):
raise Exception('Link already open')
self._add_callbacks()
logger.debug('Connecting to %s' % self._link_uri)
self._connect_event = Event()
self.cf.open_link(self._link_uri)
self._connect_event.wait()
self._connect_event = None
if not self._is_link_open:
self._remove_callbacks()
# raise Exception(self._error_message)
return False
else:
return True
def close_link(self):
if (self.is_link_open()):
self._disconnect_event = Event()
self.cf.close_link()
self._disconnect_event.wait()
self._disconnect_event = None
def __exit__(self, exc_type, exc_val, exc_tb):
self.close_link()
def is_link_open(self):
return self._is_link_open
def _connected(self, link_uri):
""" This callback is called form the Crazyflie API when a Crazyflie
has been connected and the TOCs have been downloaded."""
logger.debug('Connected to %s' % link_uri)
self._is_link_open = True
if self._connect_event:
self._connect_event.set()
def _connection_failed(self, link_uri, msg):
"""Callback when initial connection fails (i.e no Crazyflie
at the specified address)"""
logger.debug('Connection to %s failed: %s' % (link_uri, msg))
self._is_link_open = False
self._error_message = msg
if self._connect_event:
self._connect_event.set()
def _disconnected(self, link_uri):
self._remove_callbacks()
self._is_link_open = False
if self._disconnect_event:
self._disconnect_event.set()
def _add_callbacks(self):
self.cf.connected.add_callback(self._connected)
self.cf.connection_failed.add_callback(self._connection_failed)
self.cf.disconnected.add_callback(self._disconnected)
def _remove_callbacks(self):
def remove_callback(container, callback):
try:
container.remove_callback(callback)
except ValueError:
pass
remove_callback(self.cf.connected, self._connected)
remove_callback(self.cf.connection_failed, self._connection_failed)
remove_callback(self.cf.disconnected, self._disconnected)
def getLinkStatus(self):
return self._is_link_open