File Coverage

blib/lib/Device/Gyroscope/L3GD20.pm
Criterion Covered Total %
statement 24 39 61.5
branch n/a
condition n/a
subroutine 8 14 57.1
pod 5 5 100.0
total 37 58 63.7


line stmt bran cond sub pod time code
1 1     1   4 use strict;
  1         1  
  1         26  
2 1     1   3 use warnings;
  1         2  
  1         37  
3              
4             package Device::Gyroscope::L3GD20;
5              
6             # PODNAME: Device::Gyroscope::L3GD20
7             # ABSTRACT: I2C interface to Gyroscope on the L3GD20 using Device::SMBus
8             #
9             # This file is part of Device-L3GD20
10             #
11             # This software is copyright (c) 2015 by Shantanu Bhadoria.
12             #
13             # This is free software; you can redistribute it and/or modify it under
14             # the same terms as the Perl 5 programming language system itself.
15             #
16             our $VERSION = '0.009'; # VERSION
17              
18             # Dependencies
19 1     1   20 use 5.010;
  1         2  
  1         24  
20 1     1   4 use POSIX;
  1         1  
  1         9  
21              
22 1     1   5927 use Math::Trig qw(deg2rad);
  1         12040  
  1         68  
23              
24 1     1   9 use Moose;
  1         1  
  1         8  
25             extends 'Device::SMBus';
26              
27              
28             has '+I2CDeviceAddress' => (
29             is => 'ro',
30             default => 0x6b,
31             );
32              
33              
34             has gyroscopeGain => (
35             is => 'rw',
36             default => 0.07,
37             );
38              
39              
40             has xZero => (
41             is => 'rw',
42             default => 0,
43             );
44              
45              
46             has yZero => (
47             is => 'rw',
48             default => 0,
49             );
50              
51              
52             has zZero => (
53             is => 'rw',
54             default => 0,
55             );
56              
57              
58             # Registers for the Gyroscope
59             use constant {
60 1         82 CTRL_REG1 => 0x20,
61             CTRL_REG4 => 0x23,
62 1     1   5414 };
  1         2  
63              
64              
65             # X, Y and Z Axis Gyroscope Data value in 2's complement
66             use constant {
67 1         330 OUT_X_H => 0x29,
68             OUT_X_L => 0x28,
69              
70             OUT_Y_H => 0x2b,
71             OUT_Y_L => 0x2a,
72              
73             OUT_Z_H => 0x2d,
74             OUT_Z_L => 0x2c,
75 1     1   3 };
  1         2  
76              
77             #use integer; # Use arithmetic right shift instead of unsigned binary right shift with >> 4
78              
79              
80             sub enable {
81 0     0 1   my ($self) = @_;
82 0           $self->writeByteData( CTRL_REG1, 0b00001111 );
83 0           $self->writeByteData( CTRL_REG4, 0b00110000 );
84             }
85              
86              
87             sub getRawReading {
88 0     0 1   my ($self) = @_;
89              
90             return {
91 0           x =>
92             ( $self->_typecast_int_to_int16( $self->readNBytes( OUT_X_L, 2 ) ) )
93             - $self->xZero,
94             y =>
95             ( $self->_typecast_int_to_int16( $self->readNBytes( OUT_Y_L, 2 ) ) )
96             - $self->yZero,
97             z =>
98             ( $self->_typecast_int_to_int16( $self->readNBytes( OUT_Z_L, 2 ) ) )
99             - $self->zZero,
100             };
101             }
102              
103              
104             sub getReadingDegreesPerSecond {
105 0     0 1   my ($self) = @_;
106              
107 0           my $gain = $self->gyroscopeGain;
108 0           my $gyro = $self->getRawReading;
109             return {
110 0           x => ( $gyro->{x} * $gain ),
111             y => ( $gyro->{y} * $gain ),
112             z => ( $gyro->{z} * $gain ),
113             };
114             }
115              
116              
117             sub getReadingRadiansPerSecond {
118 0     0 1   my ($self) = @_;
119              
120 0           my $gain = $self->gyroscopeGain;
121 0           my $gyro = $self->getRawReading;
122             return {
123 0           x => deg2rad( $gyro->{x} * $gain ),
124             y => deg2rad( $gyro->{y} * $gain ),
125             z => deg2rad( $gyro->{z} * $gain ),
126             };
127             }
128              
129             sub _typecast_int_to_int16 {
130 0     0     return unpack 's' => pack 'S' => $_[1];
131             }
132              
133              
134             sub calibrate {
135 0     0 1   my ($self) = @_;
136              
137             }
138              
139             1;
140              
141             __END__
142              
143             =pod
144              
145             =head1 NAME
146              
147             Device::Gyroscope::L3GD20 - I2C interface to Gyroscope on the L3GD20 using Device::SMBus
148              
149             =head1 VERSION
150              
151             version 0.009
152              
153             =head1 ATTRIBUTES
154              
155             =head2 I2CDeviceAddress
156              
157             Contains the I2CDevice Address for the bus on which your gyroscope is connected. It would look like 0x6b. Default is 0x6b.
158              
159             =head2 gyroscopeGain
160              
161             Unless you are modifying gyroscope setup you must not change this. This contains the Gyroscope gain value which helps in converting the raw measurements from gyroscope register in to degrees per second.
162              
163             =head2 xZero
164              
165             This is the raw value for the X axis when the gyro is stationary. This is a part of gyro calibration to get more accurate values for rotation.
166              
167             =head2 yZero
168              
169             This is the raw value for the Y axis when the gyro is stationary. This is a part of gyro calibration to get more accurate values for rotation.
170              
171             =head2 zZero
172              
173             This is the raw value for the Z axis when the gyro is stationary. This is a part of gyro calibration to get more accurate values for rotation.
174              
175             =head1 METHODS
176              
177             =head2 enable
178              
179             $self->enable()
180              
181             Initializes the device, Call this before you start using the device. This function sets up the appropriate default registers.
182             The Device will not work properly unless you call this function
183              
184             =head2 getRawReading
185              
186             $self->getRawReading()
187              
188             Return raw readings from registers. Note that if xZero,yZero or zZero are set, this function returns the values adjusted from the values at default non rotating state of the gyroscope. Its recommended that you set these values to achieve accurate results from the gyroscope.
189              
190             =head2 getReadingDegreesPerSecond
191              
192             Return gyroscope readings in degrees per second
193              
194             =head2 getReadingRadiansPerSecond
195              
196             Return gyroscope readings in radians per second
197              
198             =head2 calibrate
199              
200             Placeholder for documentation on calibration
201              
202             =head1 REGISTERS
203              
204             =head2 CTRL_REG1
205              
206             =head2 CTRL_REG4
207              
208             =head2 OUT_X_H
209              
210             =head2 OUT_X_L
211              
212             =head2 OUT_Y_H
213              
214             =head2 OUT_Y_L
215              
216             =head2 OUT_Z_H
217              
218             =head2 OUT_Z_L
219              
220             =head1 AUTHOR
221              
222             Shantanu Bhadoria <shantanu at cpan dott org>
223              
224             =head1 COPYRIGHT AND LICENSE
225              
226             This software is copyright (c) 2015 by Shantanu Bhadoria.
227              
228             This is free software; you can redistribute it and/or modify it under
229             the same terms as the Perl 5 programming language system itself.
230              
231             =cut