67
67
68
68
69
69
# Calibrated Acceleration (m/s2)
70
- _BNO_REPORT_ACCELEROMETER = const (0x01 )
70
+ BNO_REPORT_ACCELEROMETER = const (0x01 )
71
71
# Calibrated gyroscope (rad/s).
72
- _BNO_REPORT_GYROSCOPE = const (0x02 )
72
+ BNO_REPORT_GYROSCOPE = const (0x02 )
73
73
# Magnetic field calibrated (in µTesla). The fully calibrated magnetic field measurement.
74
- _BNO_REPORT_MAGNETIC_FIELD = const (0x03 )
74
+ BNO_REPORT_MAGNETIC_FIELD = const (0x03 )
75
75
# Linear acceleration (m/s2). Acceleration of the device with gravity removed
76
- _BNO_REPORT_LINEAR_ACCELERATION = const (0x04 )
76
+ BNO_REPORT_LINEAR_ACCELERATION = const (0x04 )
77
77
# Rotation Vector
78
- _BNO_REPORT_ROTATION_VECTOR = const (0x05 )
79
- _BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR = const (0x09 )
80
- _BNO_REPORT_STEP_COUNTER = const (0x11 )
81
- _BNO_REPORT_SHAKE_DETECTOR = const (0x19 )
78
+ BNO_REPORT_ROTATION_VECTOR = const (0x05 )
79
+ BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR = const (0x09 )
80
+ BNO_REPORT_STEP_COUNTER = const (0x11 )
81
+ BNO_REPORT_SHAKE_DETECTOR = const (0x19 )
82
82
83
83
84
84
_DEFAULT_REPORT_INTERVAL = const (50000 ) # in microseconds = 50ms
112
112
_BNO_CMD_TIMESTAMP_REBASE : 5 ,
113
113
}
114
114
# length is probably deterministic, like axes * 2 +4
115
- _ENABLED_SENSOR_REPORTS = {
116
- _BNO_REPORT_ACCELEROMETER : (_Q_POINT_8_SCALAR , 3 , 10 ),
117
- _BNO_REPORT_GYROSCOPE : (_Q_POINT_9_SCALAR , 3 , 10 ),
118
- _BNO_REPORT_MAGNETIC_FIELD : (_Q_POINT_4_SCALAR , 3 , 10 ),
119
- _BNO_REPORT_LINEAR_ACCELERATION : (_Q_POINT_8_SCALAR , 3 , 10 ),
120
- _BNO_REPORT_ROTATION_VECTOR : (_Q_POINT_14_SCALAR , 4 , 14 ,),
121
- _BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR : (_Q_POINT_12_SCALAR , 4 , 14 ),
122
- _BNO_REPORT_STEP_COUNTER : (1 , 1 , 12 ),
123
- _BNO_REPORT_SHAKE_DETECTOR : (1 , 1 , 6 ),
115
+ _AVAIL_SENSOR_REPORTS = {
116
+ BNO_REPORT_ACCELEROMETER : (_Q_POINT_8_SCALAR , 3 , 10 ),
117
+ BNO_REPORT_GYROSCOPE : (_Q_POINT_9_SCALAR , 3 , 10 ),
118
+ BNO_REPORT_MAGNETIC_FIELD : (_Q_POINT_4_SCALAR , 3 , 10 ),
119
+ BNO_REPORT_LINEAR_ACCELERATION : (_Q_POINT_8_SCALAR , 3 , 10 ),
120
+ BNO_REPORT_ROTATION_VECTOR : (_Q_POINT_14_SCALAR , 4 , 14 ,),
121
+ BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR : (_Q_POINT_12_SCALAR , 4 , 14 ),
122
+ BNO_REPORT_STEP_COUNTER : (1 , 1 , 12 ),
123
+ BNO_REPORT_SHAKE_DETECTOR : (1 , 1 , 6 ),
124
124
}
125
125
126
126
DATA_BUFFER_SIZE = const (512 ) # data buffer size. obviously eats ram
@@ -158,7 +158,7 @@ def wrapper_timer(*args, **kwargs):
158
158
def _parse_sensor_report_data (report_bytes ):
159
159
data_offset = 4 # this may not always be true
160
160
report_id = report_bytes [0 ]
161
- scalar , count , _report_length = _ENABLED_SENSOR_REPORTS [report_id ]
161
+ scalar , count , _report_length = _AVAIL_SENSOR_REPORTS [report_id ]
162
162
163
163
results = []
164
164
@@ -204,7 +204,7 @@ def parse_sensor_id(buffer):
204
204
205
205
def _report_length (report_id ):
206
206
if report_id < 0xF0 : # it's a sensor report
207
- return _ENABLED_SENSOR_REPORTS [report_id ][2 ]
207
+ return _AVAIL_SENSOR_REPORTS [report_id ][2 ]
208
208
209
209
return _REPORT_LENGTHS [report_id ]
210
210
@@ -362,53 +362,62 @@ def initialize(self):
362
362
self .soft_reset ()
363
363
if not self ._check_id ():
364
364
raise RuntimeError ("Could not read ID" )
365
- for report_type in _ENABLED_SENSOR_REPORTS :
366
- self ._enable_feature (report_type )
367
365
368
366
@property
369
367
def magnetic (self ):
370
368
"""A tuple of the current magnetic field measurements on the X, Y, and Z axes"""
371
369
self ._process_available_packets () # decorator?
372
- return self ._readings [_BNO_REPORT_MAGNETIC_FIELD ]
373
-
370
+ try :
371
+ return self ._readings [BNO_REPORT_MAGNETIC_FIELD ]
372
+ except KeyError :
373
+ raise RuntimeError ("No magfield report found, is it enabled?" ) from None
374
374
@property
375
375
def quaternion (self ):
376
376
"""A quaternion representing the current rotation vector"""
377
377
self ._process_available_packets ()
378
- return self ._readings [_BNO_REPORT_ROTATION_VECTOR ]
378
+ return self ._readings [BNO_REPORT_ROTATION_VECTOR ]
379
379
380
380
@property
381
381
def geomagnetic_quaternion (self ):
382
382
"""A quaternion representing the current geomagnetic rotation vector"""
383
383
self ._process_available_packets ()
384
- return self ._readings [_BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR ]
384
+ return self ._readings [BNO_REPORT_GEOMAGNETIC_ROTATION_VECTOR ]
385
385
386
386
@property
387
387
def steps (self ):
388
388
"""The number of steps detected since the sensor was initialized"""
389
389
self ._process_available_packets ()
390
- return self ._readings [_BNO_REPORT_STEP_COUNTER ]
390
+ return self ._readings [BNO_REPORT_STEP_COUNTER ]
391
391
392
392
@property
393
393
def linear_acceleration (self ):
394
394
"""A tuple representing the current linear acceleration values on the X, Y, and Z
395
395
axes in meters per second squared"""
396
396
self ._process_available_packets ()
397
- return self ._readings [_BNO_REPORT_LINEAR_ACCELERATION ]
397
+ try :
398
+ return self ._readings [BNO_REPORT_LINEAR_ACCELERATION ]
399
+ except KeyError :
400
+ raise RuntimeError ("No lin. accel report found, is it enabled?" ) from None
398
401
399
402
@property
400
403
def acceleration (self ):
401
404
"""A tuple representing the acceleration measurements on the X, Y, and Z
402
405
axes in meters per second squared"""
403
406
self ._process_available_packets ()
404
- return self ._readings [_BNO_REPORT_ACCELEROMETER ]
407
+ try :
408
+ return self ._readings [BNO_REPORT_ACCELEROMETER ]
409
+ except KeyError :
410
+ raise RuntimeError ("No accel report found, is it enabled?" ) from None
405
411
406
412
@property
407
413
def gyro (self ):
408
414
"""A tuple representing Gyro's rotation measurements on the X, Y, and Z
409
415
axes in radians per second"""
410
416
self ._process_available_packets ()
411
- return self ._readings [_BNO_REPORT_GYROSCOPE ]
417
+ try :
418
+ return self ._readings [BNO_REPORT_GYROSCOPE ]
419
+ except KeyError :
420
+ raise RuntimeError ("No gyro report found, is it enabled?" ) from None
412
421
413
422
@property
414
423
def shake (self ):
@@ -419,20 +428,24 @@ def shake(self):
419
428
this property is not guaranteed to reflect the shake state at the moment it is read
420
429
"""
421
430
self ._process_available_packets ()
422
- shake_detected = self ._readings [_BNO_REPORT_SHAKE_DETECTOR ]
431
+ shake_detected = self ._readings [BNO_REPORT_SHAKE_DETECTOR ]
423
432
# clear on read
424
433
if shake_detected :
425
- self ._readings [_BNO_REPORT_SHAKE_DETECTOR ] = False
434
+ self ._readings [BNO_REPORT_SHAKE_DETECTOR ] = False
426
435
427
436
# # decorator?
428
437
def _process_available_packets (self ):
429
438
processed_count = 0
430
439
while self ._data_ready :
431
- new_packet = self ._read_packet ()
440
+ print ("reading a packet" )
441
+ try :
442
+ new_packet = self ._read_packet ()
443
+ except PacketError :
444
+ continue
432
445
self ._handle_packet (new_packet )
433
446
processed_count += 1
434
447
self ._dbg ("" )
435
- self . _dbg ( "Processesd " , processed_count , "packets" )
448
+ #print("Processed ", processed_count, "packets")
436
449
self ._dbg ("" )
437
450
# we'll probably need an exit here for fast sensor rates
438
451
self ._dbg ("" )
@@ -511,14 +524,14 @@ def _process_report(self, report_id, report_bytes):
511
524
print (outstr )
512
525
self ._dbg ("" )
513
526
514
- if report_id == _BNO_REPORT_STEP_COUNTER :
527
+ if report_id == BNO_REPORT_STEP_COUNTER :
515
528
self ._readings [report_id ] = _parse_step_couter_report (report_bytes )
516
529
return
517
- if report_id == _BNO_REPORT_SHAKE_DETECTOR :
530
+ if report_id == BNO_REPORT_SHAKE_DETECTOR :
518
531
shake_detected = _parse_shake_report (report_bytes )
519
532
# shake not previously detected - auto cleared by 'shake' property
520
- if not self ._readings [_BNO_REPORT_SHAKE_DETECTOR ]:
521
- self ._readings [_BNO_REPORT_SHAKE_DETECTOR ] = shake_detected
533
+ if not self ._readings [BNO_REPORT_SHAKE_DETECTOR ]:
534
+ self ._readings [BNO_REPORT_SHAKE_DETECTOR ] = shake_detected
522
535
return
523
536
524
537
sensor_data = _parse_sensor_report_data (report_bytes )
@@ -541,10 +554,11 @@ def _get_feature_enable_report(
541
554
pack_into ("<I" , set_feature_report , 5 , report_interval )
542
555
return set_feature_report
543
556
544
- def _enable_feature (self , feature_id ):
557
+ def enable_feature (self , feature_id ):
545
558
self ._dbg ("\n ********** Enabling feature id:" , feature_id , "**********" )
546
559
547
560
set_feature_report = self ._get_feature_enable_report (feature_id )
561
+ print ("Enabling" , feature_id )
548
562
self ._send_packet (_BNO_CHANNEL_CONTROL , set_feature_report )
549
563
while True :
550
564
packet = self ._wait_for_packet_type (
@@ -553,15 +567,16 @@ def _enable_feature(self, feature_id):
553
567
554
568
if packet .data [1 ] == feature_id :
555
569
if (
556
- feature_id == _BNO_REPORT_ROTATION_VECTOR
570
+ feature_id == BNO_REPORT_ROTATION_VECTOR
557
571
): # check for other vector types as well
558
572
self ._readings [feature_id ] = (0.0 , 0.0 , 0.0 , 0.0 )
559
573
else :
560
574
self ._readings [feature_id ] = (0.0 , 0.0 , 0.0 )
561
- self ._dbg ("Enabled" )
562
- return True
575
+ print ("Enabled" , feature_id )
576
+ break
577
+ else :
578
+ raise RuntimeError ("Was not able to enable feature" , feature_id )
563
579
564
- return False
565
580
566
581
def _check_id (self ):
567
582
self ._dbg ("\n ********** READ ID **********" )
@@ -613,13 +628,6 @@ def _get_data(self, index, fmt_string):
613
628
data_index = index + 4
614
629
return unpack_from (fmt_string , self ._data_buffer , offset = data_index )[0 ]
615
630
616
- def _read_header (self ):
617
- """Reads the first 4 bytes available as a header"""
618
- with self .bus_device_obj as bus_dev : # pylint:disable=no-member
619
- bus_dev .readinto (self ._data_buffer , end = 4 )
620
- packet_header = Packet .header_from_buffer (self ._data_buffer )
621
- self ._dbg (packet_header )
622
- return packet_header
623
631
624
632
# pylint:disable=no-self-use
625
633
@property
0 commit comments