Skip to content

Commit 3614293

Browse files
committed
Updated read-ow example
1 parent 3e03e9d commit 3614293

File tree

1 file changed

+31
-99
lines changed

1 file changed

+31
-99
lines changed

examples/memory/read-ow.py

Lines changed: 31 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -22,131 +22,63 @@
2222
# You should have received a copy of the GNU General Public License
2323
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2424
"""
25-
Simple example that connects to the first Crazyflie found, looks for
25+
Simple example that connects to a Crazyflie, looks for
2626
1-wire memories and lists its contents.
2727
"""
2828
import logging
2929
import sys
30-
import time
30+
from threading import Event
3131

3232
import cflib.crtp # noqa
3333
from cflib.crazyflie import Crazyflie
3434
from cflib.crazyflie.mem import MemoryElement
35+
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
3536
from cflib.utils import uri_helper
3637

3738
uri = uri_helper.uri_from_env(default='radio://0/80/2M/E7E7E7E7E7')
3839

3940
# Only output errors from the logging framework
4041
logging.basicConfig(level=logging.ERROR)
4142

43+
update_event = Event()
4244

43-
class OWExample:
44-
"""
45-
Simple example listing the 1-wire memories found and lists its contents.
46-
"""
4745

48-
def __init__(self, link_uri):
49-
""" Initialize and run the example with the specified link_uri """
46+
def read_ow_mems(cf):
47+
mems = cf.mem.get_mems(MemoryElement.TYPE_1W)
48+
print(f'Found {len(mems)} 1-wire memories')
5049

51-
# Create a Crazyflie object without specifying any cache dirs
52-
self._cf = Crazyflie()
50+
for m in mems:
51+
update_event.clear()
5352

54-
# Keep track on if ow memory was detected
55-
self.read_ow = False
53+
print(f'Reading id={m.id}')
54+
m.update(data_updated_cb)
55+
success = update_event.wait(timeout=5.0)
56+
if not success:
57+
print(f'Mem read time out for memory {m.id}')
58+
sys.exit(1)
5659

57-
# Connect some callbacks from the Crazyflie API
58-
self._cf.connected.add_callback(self._connected)
59-
self._cf.disconnected.add_callback(self._disconnected)
60-
self._cf.connection_failed.add_callback(self._connection_failed)
61-
self._cf.connection_lost.add_callback(self._connection_lost)
6260

63-
print('Connecting to %s' % link_uri)
61+
def data_updated_cb(mem):
62+
print(f'Got id={mem.id}')
63+
print(f'\tAddr : {mem.addr}')
64+
print(f'\tType : {mem.type}')
65+
print(f'\tSize : {mem.size}')
66+
print(f'\tValid : {mem.valid}')
67+
print(f'\tName : {mem.name}')
68+
print(f'\tVID : 0x{mem.vid:02X}')
69+
print(f'\tPID : 0x{mem.pid:02X}')
70+
print(f'\tPins : 0x{mem.pins:02X}')
71+
print('\tElements : ')
6472

65-
# Try to connect to the Crazyflie
66-
self._cf.open_link(link_uri)
73+
for key, element in mem.elements.items():
74+
print(f'\t\t{key}={element}')
6775

68-
# Variable used to keep main loop occupied until disconnect
69-
self.is_connected = True
70-
self._mems_to_update = 0
71-
72-
def _connected(self, link_uri):
73-
""" This callback is called form the Crazyflie API when a Crazyflie
74-
has been connected and the TOCs have been downloaded."""
75-
print('Connected to %s' % link_uri)
76-
77-
mems = self._cf.mem.get_mems(MemoryElement.TYPE_1W)
78-
self._mems_to_update = len(mems)
79-
print('Found {} 1-wire memories'.format(len(mems)))
80-
81-
if len(mems) == 0:
82-
self.read_ow = True
83-
self._cf.close_link()
84-
85-
for m in mems:
86-
print('Updating id={}'.format(m.id))
87-
m.update(self._data_updated)
88-
89-
def _data_updated(self, mem):
90-
print('Updated id={}'.format(mem.id))
91-
print('\tAddr : {}'.format(mem.addr))
92-
print('\tType : {}'.format(mem.type))
93-
print('\tSize : {}'.format(mem.size))
94-
print('\tValid : {}'.format(mem.valid))
95-
print('\tName : {}'.format(mem.name))
96-
print('\tVID : 0x{:02X}'.format(mem.vid))
97-
print('\tPID : 0x{:02X}'.format(mem.pid))
98-
print('\tPins : 0x{:02X}'.format(mem.pins))
99-
print('\tElements : ')
100-
101-
for key in mem.elements:
102-
print('\t\t{}={}'.format(key, mem.elements[key]))
103-
104-
self._mems_to_update -= 1
105-
if self._mems_to_update == 0:
106-
self.read_ow = True
107-
self._cf.close_link()
108-
109-
def _stab_log_error(self, logconf, msg):
110-
"""Callback from the log API when an error occurs"""
111-
print('Error when logging %s: %s' % (logconf.name, msg))
112-
113-
def _stab_log_data(self, timestamp, data, logconf):
114-
"""Callback from a the log API when data arrives"""
115-
print('[%d][%s]: %s' % (timestamp, logconf.name, data))
116-
117-
def _connection_failed(self, link_uri, msg):
118-
"""Callback when connection initial connection fails (i.e no Crazyflie
119-
at the specified address)"""
120-
print('Connection to %s failed: %s' % (link_uri, msg))
121-
self.is_connected = False
122-
123-
def _connection_lost(self, link_uri, msg):
124-
"""Callback when disconnected after a connection has been made (i.e
125-
Crazyflie moves out of range)"""
126-
print('Connection to %s lost: %s' % (link_uri, msg))
127-
128-
def _disconnected(self, link_uri):
129-
"""Callback when the Crazyflie is disconnected (called in all cases)"""
130-
print('Disconnected from %s' % link_uri)
131-
self.is_connected = False
76+
update_event.set()
13277

13378

13479
if __name__ == '__main__':
13580
# Initialize the low-level drivers
13681
cflib.crtp.init_drivers()
13782

138-
le = OWExample(uri)
139-
140-
# The Crazyflie lib doesn't contain anything to keep the application alive,
141-
# so this is where your application should do something. In our case we
142-
# are just waiting until we are disconnected, or timeout.
143-
timeout = 5 # seconds
144-
ts = time.time()
145-
try:
146-
while le.is_connected and (time.time() - ts < timeout):
147-
time.sleep(1)
148-
except KeyboardInterrupt:
149-
sys.exit(1)
150-
151-
if not le.read_ow:
152-
sys.exit(1)
83+
with SyncCrazyflie(uri, cf=Crazyflie(rw_cache='./cache')) as scf:
84+
read_ow_mems(scf.cf)

0 commit comments

Comments
 (0)