File Coverage

blib/lib/Device/Chip/BNO055.pm
Criterion Covered Total %
statement 187 199 93.9
branch 18 32 56.2
condition 10 21 47.6
subroutine 31 31 100.0
pod 13 19 68.4
total 259 302 85.7


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2019-2020 -- leonerd@leonerd.org.uk
5              
6 4     4   274781 use 5.026;
  4         37  
7 4     4   1709 use Object::Pad 0.19;
  4         25752  
  4         18  
8              
9             package Device::Chip::BNO055 0.02;
10             class Device::Chip::BNO055
11 1     1   483 extends Device::Chip;
  1         12174  
  1         45  
12              
13 4     4   1762 use utf8;
  4         18  
  4         22  
14              
15 4     4   111 use Carp;
  4         5  
  4         200  
16 4     4   460 use Future::AsyncAwait;
  4         790  
  4         19  
17              
18 4     4   1873 use Data::Bitfield 0.03 qw( bitfield enumfield );
  4         8414  
  4         270  
19              
20 4     4   28 use constant PROTOCOL => "I2C";
  4         7  
  4         440  
21              
22             =encoding UTF-8
23              
24             =cut
25              
26             =head1 NAME
27              
28             C - chip driver for F
29              
30             =head1 SYNOPSIS
31              
32             use Device::Chip::BNO055;
33             use Future::AsyncAwait;
34              
35             my $chip = Device::Chip::BNO055->new;
36             await $chip->mount( Device::Chip::Adapter::...->new );
37              
38             =head1 DESCRIPTION
39              
40             This L subclass provides specific communications to a
41             F F orientation sensor chip.
42              
43             The reader is presumed to be familiar with the general operation of this chip;
44             the documentation here will not attempt to explain or define chip-specific
45             concepts or features, only the use of this module to access them.
46              
47             =cut
48              
49             sub I2C_options
50             {
51             return (
52 3     3 0 1739 addr => 0x29,
53             max_bitrate => 400E3,
54             );
55             }
56              
57             =head1 METHODS
58              
59             The following methods documented in an C expression return L
60             instances.
61              
62             =cut
63              
64             # Actual chip hardware uses "paged" registers. We'll fake this for the cache
65             # by using the 8th bit to store the page number
66              
67 4     4   24 use constant REGPAGE_1 => 0x80;
  4         7  
  4         338  
68              
69             use constant {
70 4         3807 REG_CHIP_ID => 0x00,
71             REG_PAGE_ID => 0x07,
72             REG_ACC_DATA_X_LSB => 0x08,
73             REG_MAG_DATA_X_LSB => 0x0E,
74             REG_GYR_DATA_X_LSB => 0x14,
75             REG_EUL_Heading_LSB => 0x1A,
76             REG_QUA_Data_w_LSB => 0x20,
77             REG_LIA_Data_X_LSB => 0x28,
78             REG_GRV_Data_X_LSB => 0x2E,
79             REG_UNIT_SEL => 0x3B,
80             REG_OPR_MODE => 0x3D,
81              
82             REG_ACC_Config => REGPAGE_1 | 0x08,
83 4     4   22 };
  4         7  
84              
85             my @OPR_MODES = qw(
86             CONFIGMODE ACCONLY MAGONLY GYROONLY ACCMAG ACCGYRO MAGGYRO AMG
87             IMU COMPASS M4G NDOF_FMC_OFF NDOF
88             );
89              
90             bitfield { format => "bytes-LE" }, OPR_MODE =>
91             OPR_MODE => enumfield(0, @OPR_MODES);
92              
93             bitfield { format => "bytes-LE" }, CONFIG =>
94             # Page 0
95             #
96             # REG_UNIT_SEL
97             ACC_Unit => enumfield( 0, qw( m/s² mg )),
98             GYR_Unit => enumfield( 1, qw( dps rps )),
99             EUL_Unit => enumfield( 2, qw( degrees radians )),
100             TEMP_Unit => enumfield( 4, qw( Celsius Farenheit )),
101             ORI_Android_Windows => enumfield( 7, qw( Windows Android )),
102              
103             # REG_OPR_MODE
104             OPR_MODE => enumfield(2*8+0, @OPR_MODES),
105              
106             # REG_PWR_MODE
107             PWR_MODE => enumfield(3*8+0, qw( normal low-power suspend . )),
108              
109             # REG_TEMP_SOURCE
110             TEMP_Source => enumfield(5*8+0, qw( accelerometer gyroscope . . )),
111              
112             # REG_AXIS_MAP_CONFIG
113             X_AXIS_MAP => enumfield(6*8+0, qw( X Y Z . )),
114             Y_AXIS_MAP => enumfield(6*8+2, qw( X Y Z . )),
115             Z_AXIS_MAP => enumfield(6*8+4, qw( X Y Z . )),
116             # REG_AXIS_MAP_SIGN
117             X_AXIS_SIGN => enumfield(7*8+0, qw( positive negative )),
118             Y_AXIS_SIGN => enumfield(7*8+1, qw( positive negative )),
119             Z_AXIS_SIGN => enumfield(7*8+2, qw( positive negative )),
120              
121             # Page 1
122             #
123             # REG_ACC_Config
124             ACC_Range => enumfield(8*8+0, qw( 2G 4G 8G 16G )),
125             ACC_BW => enumfield(8*8+2, qw( 7.81Hz 15.63Hz 31.25Hz 62.5Hz 125Hz 250Hz 500Hz 1000Hz )),
126             ACC_PWR_Mode => enumfield(8*8+5, qw( normal suspend low-power-1 standby low-power-2 deep-suspend . . )),
127             # REG_MAG_Config
128             MAG_Data_output_rate => enumfield(9*8+0, qw( 2Hz 6Hz 8Hz 10Hz 15Hz 20Hz 25Hz 30Hz )),
129             MAG_OPR_Mode => enumfield(9*8+3, qw( low-power regular enhanced-regular high-accuracy )),
130             MAG_Power_mode => enumfield(9*8+6, qw( normal sleep suspend force-mode )),
131             # GYR_Config_0
132             GYR_Range => enumfield(10*8+0, qw( 2000dps 1000dps 500dps 250dps 125dps . . . )),
133             GYR_Bandwidth => enumfield(10*8+3, qw( 523Hz 230Hz 116Hz 47Hz 23Hz 12Hz 64Hz 32Hz )),
134             # GYR_Config_1
135             GYR_Power_Mode => enumfield(11*8+0, qw( normal fast-powerup deep-suspend suspend advanced-powersave . . . )),
136             ;
137              
138             has $_reg_page = 0;
139             has $_regcache = "";
140              
141             has $_ACC_Unit;
142             has $_GYR_Unit;
143             has $_EUL_Unit;
144              
145 12         15 async method read_reg ( $reg, $len )
  12         14  
  12         16  
  12         16  
146 12         26 {
147 12         35 my $protocol = $self->protocol;
148              
149 12         61 my $page = 0 + ( $reg >= REGPAGE_1 ); $reg &= ~REGPAGE_1;
  12         19  
150              
151 12 100       29 if( $_reg_page != $page ) {
152 3         19 await $protocol->write( pack "C C", REG_PAGE_ID, $page );
153 3         2850 $_reg_page = $page;
154             }
155              
156 12         56 return await $protocol->write_then_read( pack( "C", $reg ), $len );
157 12     12 0 19 }
158              
159 14         16 async method cached_read_reg ( $reg, $len )
  14         21  
  14         24  
  14         18  
160 14         42 {
161 4     4   28 no warnings 'numeric'; # quiet the warning about negative repeat count
  4         13  
  4         1825  
162              
163 14 100       35 if( length $_regcache >= $reg + $len ) {
164 10         56 return substr( $_regcache, $reg, $len );
165             }
166              
167 4         13 my $bytes = await $self->read_reg( $reg, $len );
168 4         18340 $_regcache .= "\0" x ( $reg + $len - length $_regcache );
169 4         12 substr( $_regcache, $reg, $len ) = $bytes;
170              
171 4         14 return $bytes;
172 14     14 0 138 }
173              
174 2         4 async method write_reg ( $reg, $bytes )
  2         3  
  2         3  
  2         2  
175 2         4 {
176 2         6 my $protocol = $self->protocol;
177              
178 2         11 my $page = 0 + ( $reg >= REGPAGE_1 ); $reg &= ~REGPAGE_1;
  2         3  
179              
180 2 100       5 if( $_reg_page != $page ) {
181 1         14 await $protocol->write( pack "C C", REG_PAGE_ID, $page );
182 1         968 $_reg_page = $page;
183             }
184              
185 2         11 return await $protocol->write( pack "C a*", $reg, $bytes );
186 2     2 0 4 }
187              
188 3         6 async method cached_write_reg ( $reg, $bytes )
  3         4  
  3         5  
  3         4  
189 3         8 {
190 4     4   29 no warnings 'numeric'; # quiet the warning about negative repeat count
  4         4  
  4         11487  
191              
192             # Trim common prefix/suffix
193 3   100     17 while( length $bytes and substr( $bytes, 0, 1 ) eq substr( $_regcache, $reg, 1 ) ) {
194 11         26 $bytes =~ s/^.//s;
195 11         27 $reg++;
196             }
197              
198 3         5 my $len = length $bytes;
199 3   66     13 while( $len and substr( $bytes, $len - 1, 1 ) eq substr( $_regcache, $reg + $len - 1, 1 ) ) {
200 0         0 $bytes =~ s/.$//s;
201 0         0 $len--;
202             }
203              
204 3 100       9 return unless $len;
205              
206 2         5 await $self->write_reg( $reg, $bytes );
207              
208 2         1839 $_regcache .= "\0" x ( $reg + $len - length $_regcache );
209 2         13 substr( $_regcache, $reg, $len ) = $bytes;
210 3     3 0 57 }
211              
212             =head2 read_ids
213              
214             $ids = await $chip->read_ids;
215              
216             Returns an 8-character string composed of the four ID registers. For a
217             C chip this should be the string
218              
219             "A0FB320F"
220              
221             =cut
222              
223 1         4 async method read_ids ()
  1         1  
224 1         28 {
225 1         5 return uc unpack "H*", await $self->read_reg( REG_CHIP_ID, 4 );
226 1     1 1 306 }
227              
228             =head2 read_config
229              
230             $config = await $chip->read_config;
231              
232             Returns the current chip configuration.
233              
234             =cut
235              
236 7         13 async method read_config ()
  7         10  
237 7         17 {
238             return {
239 7         30 unpack_CONFIG( join "", await Future->needs_all(
240             $self->cached_read_reg( REG_UNIT_SEL, 8 ),
241             $self->cached_read_reg( REG_ACC_Config, 4 ),
242             ) ),
243             };
244 7     7 1 2674 }
245              
246             =head2 change_config
247              
248             await $chip->change_config( %changes );
249              
250             Changes the configuration. Any field names not mentioned will be preserved at
251             their existing values.
252              
253             This method can only be used while the chip is in config mode, and cannot
254             itself be used to set C. For that, use L.
255              
256             =cut
257              
258 1         1 async method change_config ( %changes )
  1         4  
  1         2  
259 1         5 {
260             $changes{OPR_MODE} and
261 1 50       4 croak "->change_config cannot change OPR_MODE; use ->set_opr_mode\n";
262              
263 1         4 my $config = await $self->read_config;
264              
265 1 50       317 $config->{OPR_MODE} eq "CONFIGMODE" or
266             croak "Can only ->change_config when in CONFIGMODE";
267              
268 1         6 $config->{$_} = $changes{$_} for keys %changes;
269              
270 1         7 my ( $page0, $page1 ) = unpack "a8 a4", pack_CONFIG( %$config );
271              
272 1         447 await Future->needs_all(
273             $self->cached_write_reg( REG_UNIT_SEL, $page0 ),
274             $self->cached_write_reg( REG_ACC_Config, $page1 ),
275             );
276 1     1 1 4537 }
277              
278             =head2 set_opr_mode
279              
280             await $chip->set_opr_mode( $mode );
281              
282             Sets the C register.
283              
284             =cut
285              
286 1         2 async method set_opr_mode ( $mode )
  1         2  
  1         1  
287 1         5 {
288             # TODO: Only allow state transitions when one of old or new mode is CONFIGMODE
289              
290 1         5 await $self->cached_write_reg( REG_OPR_MODE, pack_OPR_MODE( OPR_MODE => $mode ) );
291 1     1 1 2383 }
292              
293             =head2 read_accelerometer_raw
294              
295             ( $x, $y, $z ) = await $chip->read_accelerometer_raw;
296              
297             Returns the most recent accelerometer readings in raw 16bit signed integers
298              
299             =cut
300              
301 1         2 async method read_accelerometer_raw ()
  1         1  
302 1         3 {
303 1         3 return unpack "s< s< s<", await $self->read_reg( REG_ACC_DATA_X_LSB, 6 );
304 1     1 1 2 }
305              
306             =head2 read_accelerometer
307              
308             ( $x, $y, $z ) = await $chip->read_accelerometer;
309              
310             Returns the most recent accelerometer readings in converted units, either
311             C or C depending on the chip's C configuration.
312              
313             =cut
314              
315 1         3 async method read_accelerometer ()
  1         2  
316 1         4 {
317 1   33     6 my $units = $_ACC_Unit //= ( await $self->read_config )->{ACC_Unit};
318              
319 1 50       324 if( $units eq "mg" ) {
    50          
320 0         0 return map { $_ / 1000 } await $self->read_accelerometer_raw;
  0         0  
321             }
322             elsif( $units eq "m/s²" ) {
323 1         5 return map { $_ / 100 } await $self->read_accelerometer_raw;
  3         939  
324             }
325 1     1 1 3690 }
326              
327             =head2 read_magnetometer_raw
328              
329             ( $x, $y, $z ) = await $chip->read_magnetometer_raw;
330              
331             Returns the most recent magnetometer readings in raw 16bit signed integers
332              
333             =cut
334              
335 1         2 async method read_magnetometer_raw ()
  1         2  
336 1         3 {
337 1         3 return unpack "s< s< s<", await $self->read_reg( REG_MAG_DATA_X_LSB, 6 );
338 1     1 1 2 }
339              
340             =head2 read_magnetometer
341              
342             ( $x, $y, $z ) = await $chip->read_magnetometer;
343              
344             Returns the most recent magnetometer readings in converted units of C<µT>.
345              
346             =cut
347              
348 1         2 async method read_magnetometer ()
  1         1  
349 1         5 {
350 1         4 return map { $_ / 16 } await $self->read_magnetometer_raw;
  3         978  
351 1     1 1 2864 }
352              
353             =head2 read_gyroscope_raw
354              
355             ( $x, $y, $z ) = await $chip->read_gyroscope_raw;
356              
357             Returns the most recent gyroscope readings in raw 16bit signed integers
358              
359             =cut
360              
361 1         2 async method read_gyroscope_raw ()
  1         1  
362 1         3 {
363 1         4 return unpack "s< s< s<", await $self->read_reg( REG_GYR_DATA_X_LSB, 6 );
364 1     1 1 2 }
365              
366             =head2 read_gyroscope
367              
368             ( $x, $y, $z ) = await $chip->read_gyroscope;
369              
370             Returns the most recent gyroscope readings in converted units, either
371             C or C depending on the chip's C configuration.
372              
373             =cut
374              
375 1         3 async method read_gyroscope ()
  1         1  
376 1         5 {
377 1   33     6 my $units = $_GYR_Unit //= ( await $self->read_config )->{GYR_Unit};
378              
379 1 50       354 if( $units eq "dps" ) {
    0          
380 1         5 return map { $_ / 16 } await $self->read_gyroscope_raw;
  3         980  
381             }
382             elsif( $units eq "rps" ) {
383 0         0 return map { $_ / 900 } await $self->read_gyroscope_raw;
  0         0  
384             }
385 1     1 1 2597 }
386              
387             =head2 read_euler_angles
388              
389             ( $heading, $roll, $pitch ) = await $chip->read_euler_angles;
390              
391             Returns the most recent Euler angle fusion readings in converted units, either
392             degrees or radians depending on the chip's C configuration.
393              
394             =cut
395              
396 1         1 async method read_euler_angles ()
  1         2  
397 1         5 {
398 1   33     15 my $units = $_EUL_Unit //= ( await $self->read_config )->{EUL_Unit};
399              
400 1         320 my ( $heading, $roll, $pitch ) = unpack "s< s< s<", await $self->read_reg( REG_EUL_Heading_LSB, 6 );
401              
402 1 50       936 if( $units eq "degrees" ) {
    0          
403 1         3 return map { $_ / 16 } ( $heading, $roll, $pitch );
  3         9  
404             }
405             elsif( $units eq "radians" ) {
406 0         0 return map { $_ / 900 } ( $heading, $roll, $pitch );
  0         0  
407             }
408 1     1 1 2487 }
409              
410             =head2 read_quarternion
411              
412             ( $w, $x, $y, $z ) = await $chip->read_quarternion;
413              
414             Returns the most recent quarternion fusion readings in converted units as
415             scaled numbers between -1 and 1.
416              
417             =cut
418              
419 1         3 async method read_quarternion ()
  1         2  
420 1         4 {
421 1         3 my ( $w, $x, $y, $z ) = unpack "s< s< s< s<", await $self->read_reg( REG_QUA_Data_w_LSB, 8 );
422              
423 1         932 return map { $_ / 2**14 } ( $w, $x, $y, $z );
  4         11  
424 1     1 1 2437 }
425              
426             =head2 read_linear_acceleration
427              
428             ( $x, $y, $z ) = await $chip->read_linear_acceleration;
429              
430             Returns the most recent linear acceleration fusion readings in converted
431             units, either C or C depending on the chip's C
432             configuration.
433              
434             =cut
435              
436 1         2 async method read_linear_acceleration ()
  1         2  
437 1         5 {
438 1   33     6 my $units = $_ACC_Unit //= ( await $self->read_config )->{ACC_Unit};
439              
440 1         4 my ( $x, $y, $z ) = unpack "s< s< s<", await $self->read_reg( REG_LIA_Data_X_LSB, 6 );
441              
442 1 50       940 if( $units eq "mg" ) {
    50          
443 0         0 return map { $_ / 1000 } ( $x, $y, $z );
  0         0  
444             }
445             elsif( $units eq "m/s²" ) {
446 1         3 return map { $_ / 100 } ( $x, $y, $z );
  3         13  
447             }
448 1     1 1 2432 }
449              
450             =head2 read_linear_acceleration
451              
452             ( $x, $y, $z ) = await $chip->read_linear_acceleration;
453              
454             Returns the most recent gravity fusion readings in converted units, either
455             C or C depending on the chip's C configuration.
456              
457             =cut
458              
459 1         2 async method read_gravity ()
  1         1  
460 1         4 {
461 1   33     5 my $units = $_ACC_Unit //= ( await $self->read_config )->{ACC_Unit};
462              
463 1         4 my ( $x, $y, $z ) = unpack "s< s< s<", await $self->read_reg( REG_GRV_Data_X_LSB, 6 );
464              
465 1 50       1316 if( $units eq "mg" ) {
    50          
466 0         0 return map { $_ / 1000 } ( $x, $y, $z );
  0         0  
467             }
468             elsif( $units eq "m/s²" ) {
469 1         3 return map { $_ / 100 } ( $x, $y, $z );
  3         9  
470             }
471 1     1 0 2341 }
472              
473             =head1 AUTHOR
474              
475             Paul Evans
476              
477             =cut
478              
479             0x55AA;