31
31
from collections import namedtuple
32
32
import time
33
33
from micropython import const
34
- import digitalio
35
34
36
35
# TODO: Remove on release
37
36
from .debug import channels , reports
38
37
39
38
# TODO: shorten names
40
39
# Channel 0: the SHTP command channel
41
- _BNO_CHANNEL_SHTP_COMMAND = const (0 )
40
+ BNO_CHANNEL_SHTP_COMMAND = const (0 )
42
41
BNO_CHANNEL_EXE = const (1 )
43
42
_BNO_CHANNEL_CONTROL = const (2 )
44
43
_BNO_CHANNEL_INPUT_SENSOR_REPORTS = const (3 )
134
133
135
134
class PacketError (Exception ):
136
135
"""Raised when the packet couldnt be parsed"""
137
- pass
136
+
137
+ pass # pylint:disable=unnecessary-pass
138
138
139
139
140
140
def _elapsed (start_time ):
@@ -145,11 +145,11 @@ def elapsed_time(func):
145
145
"""Print the runtime of the decorated function"""
146
146
147
147
def wrapper_timer (* args , ** kwargs ):
148
- start_time = time .monotonic_ns () # 1
148
+ start_time = time .monotonic () # 1
149
149
value = func (* args , ** kwargs )
150
- end_time = time .monotonic_ns () # 2
150
+ end_time = time .monotonic () # 2
151
151
run_time = end_time - start_time # 3
152
- print ("Finished" , func .__name__ , "in" , (run_time / 1000000 .0 ), "ms" )
152
+ print ("Finished" , func .__name__ , "in" , (run_time * 1000 .0 ), "ms" )
153
153
return value
154
154
155
155
return wrapper_timer
@@ -388,7 +388,9 @@ def geomagnetic_quaternion(self):
388
388
try :
389
389
return self ._readings [BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR ]
390
390
except KeyError :
391
- raise RuntimeError ("No geomag quaternion report found, is it enabled?" ) from None
391
+ raise RuntimeError (
392
+ "No geomag quaternion report found, is it enabled?"
393
+ ) from None
392
394
393
395
@property
394
396
def steps (self ):
@@ -450,15 +452,15 @@ def shake(self):
450
452
def _process_available_packets (self ):
451
453
processed_count = 0
452
454
while self ._data_ready :
453
- print ("reading a packet" )
455
+ # print("reading a packet")
454
456
try :
455
457
new_packet = self ._read_packet ()
456
458
except PacketError :
457
459
continue
458
460
self ._handle_packet (new_packet )
459
461
processed_count += 1
460
462
self ._dbg ("" )
461
- #print("Processed", processed_count, "packets")
463
+ # print("Processed", processed_count, "packets")
462
464
self ._dbg ("" )
463
465
# we'll probably need an exit here for fast sensor rates
464
466
self ._dbg ("" )
@@ -480,8 +482,12 @@ def _wait_for_packet_type(self, channel_number, report_id=None, timeout=5.0):
480
482
return new_packet
481
483
else :
482
484
return new_packet
483
- self ._dbg ("passing packet to handler for de-slicing" )
484
- self ._handle_packet (new_packet )
485
+ if new_packet .channel_number not in (
486
+ BNO_CHANNEL_EXE ,
487
+ BNO_CHANNEL_SHTP_COMMAND ,
488
+ ):
489
+ self ._dbg ("passing packet to handler for de-slicing" )
490
+ self ._handle_packet (new_packet )
485
491
486
492
raise RuntimeError ("Timed out waiting for a packet on channel" , channel_number )
487
493
@@ -503,11 +509,14 @@ def _update_sequence_number(self, new_packet):
503
509
self ._sequence_number [channel ] = seq
504
510
505
511
def _handle_packet (self , packet ):
506
-
507
512
# split out reports first
508
- _separate_batch (packet , self ._packet_slices )
509
- while len (self ._packet_slices ) > 0 :
510
- self ._process_report (* self ._packet_slices .pop ())
513
+ try :
514
+ _separate_batch (packet , self ._packet_slices )
515
+ while len (self ._packet_slices ) > 0 :
516
+ self ._process_report (* self ._packet_slices .pop ())
517
+ except Exception as error :
518
+ print (packet )
519
+ raise error
511
520
512
521
def _handle_control_report (self , report_id , report_bytes ):
513
522
if report_id == _SHTP_REPORT_PRODUCT_ID_RESPONSE :
@@ -543,8 +552,11 @@ def _process_report(self, report_id, report_bytes):
543
552
if report_id == BNO_REPORT_SHAKE_DETECTOR :
544
553
shake_detected = _parse_shake_report (report_bytes )
545
554
# shake not previously detected - auto cleared by 'shake' property
546
- if not self ._readings [BNO_REPORT_SHAKE_DETECTOR ]:
547
- self ._readings [BNO_REPORT_SHAKE_DETECTOR ] = shake_detected
555
+ try :
556
+ if not self ._readings [BNO_REPORT_SHAKE_DETECTOR ]:
557
+ self ._readings [BNO_REPORT_SHAKE_DETECTOR ] = shake_detected
558
+ except KeyError :
559
+ pass
548
560
return
549
561
550
562
sensor_data = _parse_sensor_report_data (report_bytes )
@@ -567,11 +579,13 @@ def _get_feature_enable_report(
567
579
pack_into ("<I" , set_feature_report , 5 , report_interval )
568
580
return set_feature_report
569
581
582
+ # TODO: add docs for available features
570
583
def enable_feature (self , feature_id ):
584
+ """Used to enable a given feature of the BNO080"""
571
585
self ._dbg ("\n ********** Enabling feature id:" , feature_id , "**********" )
572
586
573
587
set_feature_report = self ._get_feature_enable_report (feature_id )
574
- print ("Enabling" , feature_id )
588
+ # print("Enabling", feature_id)
575
589
self ._send_packet (_BNO_CHANNEL_CONTROL , set_feature_report )
576
590
while True :
577
591
packet = self ._wait_for_packet_type (
@@ -585,11 +599,9 @@ def enable_feature(self, feature_id):
585
599
self ._readings [feature_id ] = (0.0 , 0.0 , 0.0 , 0.0 )
586
600
else :
587
601
self ._readings [feature_id ] = (0.0 , 0.0 , 0.0 )
588
- print ("Enabled" , feature_id )
602
+ # print("Enabled", feature_id)
589
603
break
590
- else :
591
- raise RuntimeError ("Was not able to enable feature" , feature_id )
592
-
604
+ raise RuntimeError ("Was not able to enable feature" , feature_id )
593
605
594
606
def _check_id (self ):
595
607
self ._dbg ("\n ********** READ ID **********" )
@@ -641,7 +653,6 @@ def _get_data(self, index, fmt_string):
641
653
data_index = index + 4
642
654
return unpack_from (fmt_string , self ._data_buffer , offset = data_index )[0 ]
643
655
644
-
645
656
# pylint:disable=no-self-use
646
657
@property
647
658
def _data_ready (self ):
@@ -651,7 +662,9 @@ def hard_reset(self):
651
662
"""Hardware reset the sensor to an initial unconfigured state"""
652
663
if not self ._reset :
653
664
return
654
- #print("Hard resetting...")
665
+ # print("Hard resetting...")
666
+ import digitalio # pylint:disable=import-outside-toplevel
667
+
655
668
self ._reset .direction = digitalio .Direction .OUTPUT
656
669
self ._reset .value = True
657
670
time .sleep (0.01 )
@@ -665,25 +678,18 @@ def soft_reset(self):
665
678
print ("Soft resetting..." , end = "" )
666
679
data = bytearray (1 )
667
680
data [0 ] = 1
668
- seq = self ._send_packet (BNO_CHANNEL_EXE , data )
681
+ _seq = self ._send_packet (BNO_CHANNEL_EXE , data )
669
682
time .sleep (0.5 )
670
-
671
- for i in range (3 ):
672
- while True : # retry reading packets until ready!
673
- try :
674
- packet = self ._read_packet ()
675
- break
676
- except PacketError :
677
- time .sleep (0.1 )
678
-
679
- #print(packet)
680
- if i == 0 and packet .channel_number != _BNO_CHANNEL_SHTP_COMMAND :
681
- raise RuntimeError ("Expected an SHTP announcement" )
682
- if i == 1 and packet .channel_number != BNO_CHANNEL_EXE :
683
- raise RuntimeError ("Expected a reset reply" )
684
- if i == 2 and packet .channel_number != _BNO_CHANNEL_CONTROL :
685
- raise RuntimeError ("Expected a control announcement" )
686
- print ("OK!" );
683
+ _seq = self ._send_packet (BNO_CHANNEL_EXE , data )
684
+ time .sleep (0.5 )
685
+
686
+ for _i in range (3 ):
687
+ try :
688
+ _packet = self ._read_packet ()
689
+ except PacketError :
690
+ time .sleep (0.5 )
691
+
692
+ print ("OK!" )
687
693
# all is good!
688
694
689
695
def _send_packet (self , channel , data ):
0 commit comments