Skip to content

Commit a4ea582

Browse files
author
microbuilder
committed
Initial Unified Sensor plumbing
1 parent df31805 commit a4ea582

File tree

4 files changed

+305
-7
lines changed

4 files changed

+305
-7
lines changed

Adafruit_LSM9DS0.cpp

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@
1818
CONSTRUCTOR
1919
***************************************************************************/
2020
// default
21-
Adafruit_LSM9DS0::Adafruit_LSM9DS0() {
21+
Adafruit_LSM9DS0::Adafruit_LSM9DS0( int32_t sensorID ) {
2222
_i2c = true;
23+
_lsm9dso_sensorid_accel = sensorID + 1;
24+
_lsm9dso_sensorid_mag = sensorID + 2;
25+
_lsm9dso_sensorid_gyro = sensorID + 3;
26+
_lsm9dso_sensorid_temp = sensorID + 4;
2327
}
2428

25-
Adafruit_LSM9DS0::Adafruit_LSM9DS0(int8_t xmcs, int8_t gcs) {
29+
Adafruit_LSM9DS0::Adafruit_LSM9DS0(int8_t xmcs, int8_t gcs, int32_t sensorID ) {
2630
_i2c = false;
2731
// hardware SPI!
2832
_csg = gcs;
2933
_csxm = xmcs;
3034
_mosi = _miso = _clk = -1;
35+
_lsm9dso_sensorid_accel = sensorID + 1;
36+
_lsm9dso_sensorid_mag = sensorID + 2;
37+
_lsm9dso_sensorid_gyro = sensorID + 3;
38+
_lsm9dso_sensorid_temp = sensorID + 4;
3139
}
3240

33-
Adafruit_LSM9DS0::Adafruit_LSM9DS0(int8_t clk, int8_t miso, int8_t mosi, int8_t xmcs, int8_t gcs) {
41+
Adafruit_LSM9DS0::Adafruit_LSM9DS0(int8_t clk, int8_t miso, int8_t mosi, int8_t xmcs, int8_t gcs, int32_t sensorID ) {
3442
_i2c = false;
3543
// software SPI!
3644
_csg = gcs;
3745
_csxm = xmcs;
3846
_mosi = mosi;
3947
_miso = miso;
4048
_clk = clk;
49+
_lsm9dso_sensorid_accel = sensorID + 1;
50+
_lsm9dso_sensorid_mag = sensorID + 2;
51+
_lsm9dso_sensorid_gyro = sensorID + 3;
52+
_lsm9dso_sensorid_temp = sensorID + 4;
4153
}
4254

4355
bool Adafruit_LSM9DS0::begin()
@@ -277,6 +289,124 @@ void Adafruit_LSM9DS0::setupGyro ( lsm9ds0GyroScale_t scale )
277289
}
278290
}
279291

292+
/***************************************************************************
293+
UNIFIED SENSOR FUNCTIONS
294+
***************************************************************************/
295+
296+
/**************************************************************************/
297+
/*!
298+
@brief Gets the most recent accel sensor event
299+
*/
300+
/**************************************************************************/
301+
void Adafruit_LSM9DS0::getEvent(sensors_event_t *accelEvent,
302+
sensors_event_t *magEvent,
303+
sensors_event_t *gyroEvent,
304+
sensors_event_t *tempEvent )
305+
{
306+
/* Clear the events */
307+
memset(accelEvent, 0, sizeof(sensors_event_t));
308+
memset(magEvent, 0, sizeof(sensors_event_t));
309+
memset(gyroEvent, 0, sizeof(sensors_event_t));
310+
memset(tempEvent, 0, sizeof(sensors_event_t));
311+
312+
/* update the sensor data */
313+
read();
314+
uint32_t timestamp = millis();
315+
316+
/* Update the accelerometer data */
317+
accelEvent->version = sizeof(sensors_event_t);
318+
accelEvent->sensor_id = _lsm9dso_sensorid_accel;
319+
accelEvent->type = SENSOR_TYPE_ACCELEROMETER;
320+
accelEvent->timestamp = timestamp;
321+
accelEvent->acceleration.x = accelData.x;
322+
accelEvent->acceleration.y = accelData.y;
323+
accelEvent->acceleration.z = accelData.z;
324+
325+
/* Update the magnetometer data */
326+
magEvent->version = sizeof(sensors_event_t);
327+
magEvent->sensor_id = _lsm9dso_sensorid_mag;
328+
magEvent->type = SENSOR_TYPE_MAGNETIC_FIELD;
329+
magEvent->timestamp = timestamp;
330+
magEvent->magnetic.x = magData.x;
331+
magEvent->magnetic.y = magData.y;
332+
magEvent->magnetic.z = magData.z;
333+
334+
/* Update the gyroscope data */
335+
gyroEvent->version = sizeof(sensors_event_t);
336+
gyroEvent->sensor_id = _lsm9dso_sensorid_accel;
337+
gyroEvent->type = SENSOR_TYPE_GYROSCOPE;
338+
gyroEvent->timestamp = timestamp;
339+
gyroEvent->gyro.x = gyroData.x;
340+
gyroEvent->gyro.y = gyroData.y;
341+
gyroEvent->gyro.z = gyroData.z;
342+
343+
/* Update the temperature data */
344+
tempEvent->version = sizeof(sensors_event_t);
345+
tempEvent->sensor_id = _lsm9dso_sensorid_temp;
346+
tempEvent->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
347+
tempEvent->timestamp = timestamp;
348+
tempEvent->temperature = temperature;
349+
}
350+
351+
/**************************************************************************/
352+
/*!
353+
@brief Gets the sensor_t data
354+
*/
355+
/**************************************************************************/
356+
void Adafruit_LSM9DS0::getSensor(sensor_t *accel, sensor_t *mag,
357+
sensor_t *gyro, sensor_t *temp )
358+
{
359+
/* Clear the sensor_t objects */
360+
memset(accel, 0, sizeof(sensor_t));
361+
memset(mag, 0, sizeof(sensor_t));
362+
memset(gyro, 0, sizeof(sensor_t));
363+
memset(temp, 0, sizeof(sensor_t));
364+
365+
/* Insert the sensor name in the fixed length char array */
366+
strncpy (accel->name, "LSM9DS0_A", sizeof(accel->name) - 1);
367+
accel->name[sizeof(accel->name)- 1] = 0;
368+
accel->version = 1;
369+
accel->sensor_id = _lsm9dso_sensorid_accel;
370+
accel->type = SENSOR_TYPE_ACCELEROMETER;
371+
accel->min_delay = 0;
372+
accel->max_value = 0.0; // ToDo
373+
accel->min_value = 0.0; // ToDo
374+
accel->resolution = 0.0; // ToDo
375+
376+
/* Insert the sensor name in the fixed length char array */
377+
strncpy (mag->name, "LSM9DS0_M", sizeof(mag->name) - 1);
378+
mag->name[sizeof(mag->name)- 1] = 0;
379+
mag->version = 1;
380+
mag->sensor_id = _lsm9dso_sensorid_mag;
381+
mag->type = SENSOR_TYPE_MAGNETIC_FIELD;
382+
mag->min_delay = 0;
383+
mag->max_value = 0.0; // ToDo
384+
mag->min_value = 0.0; // ToDo
385+
mag->resolution = 0.0; // ToDo
386+
387+
/* Insert the sensor name in the fixed length char array */
388+
strncpy (gyro->name, "LSM9DS0_G", sizeof(gyro->name) - 1);
389+
gyro->name[sizeof(gyro->name)- 1] = 0;
390+
gyro->version = 1;
391+
gyro->sensor_id = _lsm9dso_sensorid_gyro;
392+
gyro->type = SENSOR_TYPE_GYROSCOPE;
393+
gyro->min_delay = 0;
394+
gyro->max_value = 0.0; // ToDo
395+
gyro->min_value = 0.0; // ToDo
396+
gyro->resolution = 0.0; // ToDo
397+
398+
/* Insert the sensor name in the fixed length char array */
399+
strncpy (temp->name, "LSM9DS0_T", sizeof(temp->name) - 1);
400+
temp->name[sizeof(temp->name)- 1] = 0;
401+
temp->version = 1;
402+
temp->sensor_id = _lsm9dso_sensorid_temp;
403+
temp->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
404+
temp->min_delay = 0;
405+
temp->max_value = 0.0; // ToDo
406+
temp->min_value = 0.0; // ToDo
407+
temp->resolution = 0.0; // ToDo
408+
}
409+
280410
/***************************************************************************
281411
PRIVATE FUNCTIONS
282412
***************************************************************************/

Adafruit_LSM9DS0.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#endif
2323
#include "Wire.h"
2424
#include <SPI.h>
25+
#include <Adafruit_Sensor.h>
2526

2627
#define LSM9DS0_ADDRESS_ACCELMAG (0x1D) // 3B >> 1 = 7bit default
2728
#define LSM9DS0_ADDRESS_GYRO (0x6B) // D6 >> 1 = 7bit default
@@ -55,9 +56,9 @@
5556
class Adafruit_LSM9DS0
5657
{
5758
public:
58-
Adafruit_LSM9DS0 ( );
59-
Adafruit_LSM9DS0 ( int8_t xmcs, int8_t gcs );
60-
Adafruit_LSM9DS0 ( int8_t clk, int8_t miso, int8_t mosi, int8_t xmcs, int8_t gcs );
59+
Adafruit_LSM9DS0 ( int32_t sensorID = 0 );
60+
Adafruit_LSM9DS0 ( int8_t xmcs, int8_t gcs, int32_t sensorID = 0 );
61+
Adafruit_LSM9DS0 ( int8_t clk, int8_t miso, int8_t mosi, int8_t xmcs, int8_t gcs, int32_t sensorID = 0 );
6162

6263
typedef enum
6364
{
@@ -169,14 +170,24 @@ class Adafruit_LSM9DS0
169170
void write8 ( boolean type, byte reg, byte value );
170171
byte read8 ( boolean type, byte reg );
171172
uint8_t spixfer ( uint8_t data );
173+
174+
/* Adafruit Unified Sensor Functions (not standard yet ... the current base class only */
175+
/* supports one sensor type, and we need to update the unified base class to support */
176+
/* multiple sensors in a single driver, returning an array */
177+
void getEvent ( sensors_event_t* accel, sensors_event_t* mag, sensors_event_t* gyro, sensors_event_t* temp );
178+
void getSensor ( sensor_t* accel, sensor_t* mag, sensor_t* gyro, sensor_t* temp );
172179

173180
private:
174181
boolean _i2c;
175182
int8_t _csg, _csxm, _mosi, _miso, _clk;
176183
uint8_t mySPCR, SPCRback;
177184
float _accel_mg_lsb;
178185
float _mag_mgauss_lsb;
179-
float _gyro_dps_digit;
186+
float _gyro_dps_digit;
187+
int32_t _lsm9dso_sensorid_accel;
188+
int32_t _lsm9dso_sensorid_mag;
189+
int32_t _lsm9dso_sensorid_gyro;
190+
int32_t _lsm9dso_sensorid_temp;
180191
};
181192

182193
#endif

examples/lsm9doftest/lsm9doftest.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <Wire.h>
22
#include <SPI.h>
33
#include <Adafruit_LSM9DS0.h>
4+
#include <Adafruit_Sensor.h>
45

56
// i2c
67
Adafruit_LSM9DS0 lsm;

examples/sensorapi/sensorapi.pde

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include <SPI.h>
2+
#include <Wire.h>
3+
#include <Adafruit_Sensor.h>
4+
#include <Adafruit_LSM9DS0.h>
5+
6+
/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
7+
which provides a common 'type' for sensor data and some helper functions.
8+
9+
To use this driver you will also need to download the Adafruit_Sensor
10+
library and include it in your libraries folder.
11+
12+
You should also assign a unique ID to this sensor for use with
13+
the Adafruit Sensor API so that you can identify this particular
14+
sensor in any data logs, etc. To assign a unique ID, simply
15+
provide an appropriate value in the constructor below (12345
16+
is used by default in this example).
17+
18+
Connections
19+
===========
20+
Connect SCL to analog 5
21+
Connect SDA to analog 4
22+
Connect VDD to 3.3V DC
23+
Connect GROUND to common ground
24+
25+
History
26+
=======
27+
2014/JULY/25 - First version (KTOWN)
28+
*/
29+
30+
/* Assign a unique base ID for this sensor */
31+
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(1000);
32+
33+
/**************************************************************************/
34+
/*
35+
Displays some basic information on this sensor from the unified
36+
sensor API sensor_t type (see Adafruit_Sensor for more information)
37+
*/
38+
/**************************************************************************/
39+
void displaySensorDetails(void)
40+
{
41+
sensor_t accel, mag, gyro, temp;
42+
43+
lsm.getSensor(&accel, &mag, &gyro, &temp);
44+
45+
Serial.println(F("------------------------------------"));
46+
Serial.print (F("Sensor: ")); Serial.println(accel.name);
47+
Serial.print (F("Driver Ver: ")); Serial.println(accel.version);
48+
Serial.print (F("Unique ID: ")); Serial.println(accel.sensor_id);
49+
Serial.print (F("Max Value: ")); Serial.print(accel.max_value); Serial.println(F(" m/s^2"));
50+
Serial.print (F("Min Value: ")); Serial.print(accel.min_value); Serial.println(F(" m/s^2"));
51+
Serial.print (F("Resolution: ")); Serial.print(accel.resolution); Serial.println(F(" m/s^2"));
52+
Serial.println(F("------------------------------------"));
53+
Serial.println(F(""));
54+
55+
Serial.println(F("------------------------------------"));
56+
Serial.print (F("Sensor: ")); Serial.println(mag.name);
57+
Serial.print (F("Driver Ver: ")); Serial.println(mag.version);
58+
Serial.print (F("Unique ID: ")); Serial.println(mag.sensor_id);
59+
Serial.print (F("Max Value: ")); Serial.print(mag.max_value); Serial.println(F(" uT"));
60+
Serial.print (F("Min Value: ")); Serial.print(mag.min_value); Serial.println(F(" uT"));
61+
Serial.print (F("Resolution: ")); Serial.print(mag.resolution); Serial.println(F(" uT"));
62+
Serial.println(F("------------------------------------"));
63+
Serial.println(F(""));
64+
65+
Serial.println(F("------------------------------------"));
66+
Serial.print (F("Sensor: ")); Serial.println(gyro.name);
67+
Serial.print (F("Driver Ver: ")); Serial.println(gyro.version);
68+
Serial.print (F("Unique ID: ")); Serial.println(gyro.sensor_id);
69+
Serial.print (F("Max Value: ")); Serial.print(gyro.max_value); Serial.println(F(" rad/s"));
70+
Serial.print (F("Min Value: ")); Serial.print(gyro.min_value); Serial.println(F(" rad/s"));
71+
Serial.print (F("Resolution: ")); Serial.print(gyro.resolution); Serial.println(F(" rad/s"));
72+
Serial.println(F("------------------------------------"));
73+
Serial.println(F(""));
74+
75+
Serial.println(F("------------------------------------"));
76+
Serial.print (F("Sensor: ")); Serial.println(temp.name);
77+
Serial.print (F("Driver Ver: ")); Serial.println(temp.version);
78+
Serial.print (F("Unique ID: ")); Serial.println(temp.sensor_id);
79+
Serial.print (F("Max Value: ")); Serial.print(temp.max_value); Serial.println(F(" C"));
80+
Serial.print (F("Min Value: ")); Serial.print(temp.min_value); Serial.println(F(" C"));
81+
Serial.print (F("Resolution: ")); Serial.print(temp.resolution); Serial.println(F(" C"));
82+
Serial.println(F("------------------------------------"));
83+
Serial.println(F(""));
84+
85+
delay(500);
86+
}
87+
88+
/**************************************************************************/
89+
/*
90+
Configures the gain and integration time for the TSL2561
91+
*/
92+
/**************************************************************************/
93+
void configureSensor(void)
94+
{
95+
// 1.) Set the accelerometer range
96+
lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
97+
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G);
98+
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G);
99+
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G);
100+
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G);
101+
102+
// 2.) Set the magnetometer sensitivity
103+
lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
104+
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS);
105+
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS);
106+
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS);
107+
108+
// 3.) Setup the gyroscope
109+
lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
110+
//lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS);
111+
//lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS);
112+
}
113+
114+
/**************************************************************************/
115+
/*
116+
Arduino setup function (automatically called at startup)
117+
*/
118+
/**************************************************************************/
119+
void setup(void)
120+
{
121+
Serial.begin(9600);
122+
Serial.println(F("LSM9DS0 9DOF Sensor Test")); Serial.println("");
123+
124+
/* Initialise the sensor */
125+
// if(!lsm.begin())
126+
// {
127+
// /* There was a problem detecting the LSM9DS0 ... check your connections */
128+
// Serial.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!"));
129+
// while(1);
130+
// }
131+
// Serial.println(F("Found LMS9DS0 9DOF"));
132+
133+
/* Display some basic information on this sensor */
134+
displaySensorDetails();
135+
136+
/* Setup the sensor gain and integration time */
137+
configureSensor();
138+
139+
/* We're ready to go! */
140+
Serial.println("");
141+
}
142+
143+
/**************************************************************************/
144+
/*
145+
Arduino loop function, called once 'setup' is complete (your own code
146+
should go here)
147+
*/
148+
/**************************************************************************/
149+
void loop(void)
150+
{
151+
/* Get a new sensor event */
152+
sensors_event_t accel, mag, gyro, temp;
153+
lsm.getEvent(&accel, &mag, &gyro, &temp);
154+
155+
delay(250);
156+
}

0 commit comments

Comments
 (0)