Skip to content

Commit bb07eb2

Browse files
Rocketctfacchinm
authored andcommitted
added new IMU library support for new revision
1 parent 6d9721b commit bb07eb2

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

src/IMUClass.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
This file is part of the Arduino_LSM6DSOX library.
3+
Copyright (c) 2021 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "IMUClass.h"
21+
22+
// sets function called on slave write
23+
IMUClass::IMUClass( getRev_t getRevision)
24+
{
25+
//If board_revision = 0, IMU module is LSM6DSOX, otherwise is LSM6DS3
26+
board_revision = getRevision;
27+
}
28+
29+
IMUClass::~IMUClass()
30+
{
31+
}
32+
33+
int IMUClass::begin()
34+
{
35+
_revision = board_revision();
36+
if (_revision == BOARD_REVISION_2) {
37+
LSM6DSOX = new LSM6DSOXClass(Wire, LSM6DSOX_ADDRESS);
38+
if (LSM6DSOX == nullptr) return 0;
39+
return LSM6DSOX->begin();
40+
} else {
41+
LSM6DS3 = new LSM6DS3Class(Wire, LSM6DS3_ADDRESS);
42+
if (LSM6DS3 == nullptr) return 0;
43+
return LSM6DS3->begin();
44+
}
45+
46+
}
47+
48+
void IMUClass::end()
49+
{
50+
if (_revision == BOARD_REVISION_2) {
51+
LSM6DSOX->end();
52+
delete LSM6DSOX;
53+
LSM6DSOX = nullptr;
54+
} else {
55+
LSM6DS3->end();
56+
delete LSM6DS3;
57+
LSM6DS3 = nullptr;
58+
}
59+
}
60+
61+
int IMUClass::readAcceleration(float& x, float& y, float& z)
62+
{
63+
if (_revision == BOARD_REVISION_2) {
64+
return LSM6DSOX->readAcceleration(x,y,z);
65+
} else {
66+
return LSM6DS3->readAcceleration(x,y,z);
67+
}
68+
}
69+
70+
int IMUClass::accelerationAvailable()
71+
{
72+
if (_revision == BOARD_REVISION_2) {
73+
return LSM6DSOX->accelerationAvailable();
74+
} else {
75+
return LSM6DS3->accelerationAvailable();
76+
}
77+
78+
}
79+
80+
float IMUClass::accelerationSampleRate()
81+
{
82+
if (_revision == BOARD_REVISION_2) {
83+
return LSM6DSOX->accelerationSampleRate();
84+
} else {
85+
return LSM6DS3->accelerationSampleRate();
86+
}
87+
}
88+
89+
int IMUClass::readGyroscope(float& x, float& y, float& z)
90+
{
91+
if (_revision == BOARD_REVISION_2) {
92+
return LSM6DSOX->readGyroscope(x,y,z);
93+
} else {
94+
return LSM6DS3->readGyroscope(x,y,z);
95+
}
96+
}
97+
98+
int IMUClass::gyroscopeAvailable()
99+
{
100+
if (_revision == BOARD_REVISION_2) {
101+
return LSM6DSOX->gyroscopeAvailable();
102+
} else {
103+
return LSM6DS3->gyroscopeAvailable();
104+
}
105+
}
106+
107+
float IMUClass::gyroscopeSampleRate()
108+
{
109+
if (_revision == BOARD_REVISION_2) {
110+
return LSM6DSOX->gyroscopeSampleRate();
111+
} else {
112+
return LSM6DS3->gyroscopeSampleRate();
113+
}
114+
}

src/IMUClass.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef _IMUCLASS_H_INCLUDED
2+
#define _IMUCLASS_H_INCLUDED
3+
4+
#include <Arduino.h>
5+
#include <MKRIoTCarrierDefines.h>
6+
7+
8+
class IMUClass {
9+
public:
10+
IMUClass(int (*)(void));
11+
~IMUClass();
12+
13+
int begin();
14+
void end();
15+
16+
// Accelerometer
17+
int readAcceleration(float& x, float& y, float& z); // Results are in g (earth gravity).
18+
float accelerationSampleRate(); // Sampling rate of the sensor.
19+
int accelerationAvailable(); // Check for available data from accelerometer
20+
21+
// Gyroscope
22+
int readGyroscope(float& x, float& y, float& z); // Results are in degrees/second.
23+
float gyroscopeSampleRate(); // Sampling rate of the sensor.
24+
int gyroscopeAvailable(); // Check for available data from gyroscope
25+
26+
27+
28+
private:
29+
LSM6DSOXClass* LSM6DSOX;
30+
LSM6DS3Class* LSM6DS3;
31+
private:
32+
33+
int (*board_revision)(void);
34+
int _revision;
35+
};
36+
37+
#endif //_IMUCLASS_H_INCLUDED

0 commit comments

Comments
 (0)