Skip to content

Commit c412d10

Browse files
committed
Issue arduino#149: Provide scaled IMU data
Add implementations of readAccelerometer and readGyro which return values that are scaled according to the range set with setAccelerometerRange and setGyroRange
1 parent bdaec19 commit c412d10

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

Diff for: libraries/CurieIMU/keywords.txt

+3
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ setStepCountEnabled KEYWORD1
5959
resetStepCount KEYWORD1
6060

6161
readMotionSensor KEYWORD1
62+
readMotionSensorScaled KEYWORD1
6263
readAcceleration KEYWORD1
6364
readRotation KEYWORD1
6465

6566
readAccelerometer KEYWORD1
67+
readAccelerometerScaled KEYWORD1
6668
readGyro KEYWORD1
69+
readGyroScaled KEYWORD1
6770
readTemperature KEYWORD1
6871

6972
shockDetected KEYWORD1

Diff for: libraries/CurieIMU/src/BMI160.h

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ THE SOFTWARE.
3535

3636
#include "Arduino.h"
3737

38+
#define BMI160_SENSOR_RANGE 65535.0f
39+
#define BMI160_SENSOR_LOW 32768.0f
40+
3841
#define BMI160_SPI_READ_BIT 7
3942

4043
#define BMI160_RA_CHIP_ID 0x00

Diff for: libraries/CurieIMU/src/CurieIMU.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,19 @@ void CurieIMUClass::setGyroRange(int range)
232232

233233
if (range >= 2000) {
234234
bmiRange = BMI160_GYRO_RANGE_2000;
235+
gyro_range = 2000.0f;
235236
} else if (range >= 1000) {
236237
bmiRange = BMI160_GYRO_RANGE_1000;
238+
gyro_range = 1000.0f;
237239
} else if (range >= 500) {
238240
bmiRange = BMI160_GYRO_RANGE_500;
241+
gyro_range = 500.0f;
239242
} else if (range >= 250) {
240243
bmiRange = BMI160_GYRO_RANGE_250;
244+
gyro_range = 250.0f;
241245
} else {
242246
bmiRange = BMI160_GYRO_RANGE_125;
247+
gyro_range = 125.0f;
243248
}
244249

245250
setFullScaleGyroRange(bmiRange);
@@ -277,12 +282,16 @@ void CurieIMUClass::setAccelerometerRange(int range)
277282

278283
if (range <= 2) {
279284
bmiRange = BMI160_ACCEL_RANGE_2G;
285+
accel_range = 2.0f;
280286
} else if (range <= 4) {
281287
bmiRange = BMI160_ACCEL_RANGE_4G;
288+
accel_range = 4.0f;
282289
} else if (range <= 8) {
283290
bmiRange = BMI160_ACCEL_RANGE_8G;
291+
accel_range = 8.0f;
284292
} else {
285293
bmiRange = BMI160_ACCEL_RANGE_16G;
294+
accel_range = 16.0f;
286295
}
287296

288297
setFullScaleAccelRange(bmiRange);
@@ -1556,6 +1565,16 @@ void CurieIMUClass::setStepDetectionMode(int mode)
15561565
BMI160Class::setStepDetectionMode((BMI160StepMode)mode);
15571566
}
15581567

1568+
float convertRaw(int16_t raw, float range)
1569+
{
1570+
float slope;
1571+
float val;
1572+
1573+
val = (float) raw;
1574+
slope = (range * 2.0f) / BMI160_SENSOR_RANGE;
1575+
return -(range) + slope * (val + BMI160_SENSOR_LOW);
1576+
}
1577+
15591578
void CurieIMUClass::readMotionSensor(int& ax, int& ay, int& az, int& gx, int& gy, int& gz)
15601579
{
15611580
short sax, say, saz, sgx, sgy, sgz;
@@ -1570,6 +1589,21 @@ void CurieIMUClass::readMotionSensor(int& ax, int& ay, int& az, int& gx, int& gy
15701589
gz = sgz;
15711590
}
15721591

1592+
void CurieIMUClass::readMotionSensorScaled(float& ax, float& ay, float& az,
1593+
float& gx, float& gy, float& gz)
1594+
{
1595+
int16_t sax, say, saz, sgx, sgy, sgz;
1596+
1597+
getMotion6(&sax, &say, &saz, &sgx, &sgy, &sgz);
1598+
1599+
ax = convertRaw(sax, accel_range);
1600+
ay = convertRaw(say, accel_range);
1601+
az = convertRaw(saz, accel_range);
1602+
gx = convertRaw(sgx, gyro_range);
1603+
gy = convertRaw(sgy, gyro_range);
1604+
gz = convertRaw(sgz, gyro_range);
1605+
}
1606+
15731607
void CurieIMUClass::readAccelerometer(int& x, int& y, int& z)
15741608
{
15751609
short sx, sy, sz;
@@ -1581,6 +1615,17 @@ void CurieIMUClass::readAccelerometer(int& x, int& y, int& z)
15811615
z = sz;
15821616
}
15831617

1618+
void CurieIMUClass::readAccelerometerScaled(float& x, float& y, float& z)
1619+
{
1620+
int16_t sx, sy, sz;
1621+
1622+
getAcceleration(&sx, &sy, &sz);
1623+
1624+
x = convertRaw(sx, accel_range);
1625+
y = convertRaw(sy, accel_range);
1626+
z = convertRaw(sz, accel_range);
1627+
}
1628+
15841629
void CurieIMUClass::readGyro(int& x, int& y, int& z)
15851630
{
15861631
short sx, sy, sz;
@@ -1592,6 +1637,17 @@ void CurieIMUClass::readGyro(int& x, int& y, int& z)
15921637
z = sz;
15931638
}
15941639

1640+
void CurieIMUClass::readGyroScaled(float& x, float& y, float& z)
1641+
{
1642+
int16_t sx, sy, sz;
1643+
1644+
getRotation(&sx, &sy, &sz);
1645+
1646+
x = convertRaw(sx, gyro_range);
1647+
y = convertRaw(sy, gyro_range);
1648+
z = convertRaw(sz, gyro_range);
1649+
}
1650+
15951651
int CurieIMUClass::readAccelerometer(int axis)
15961652
{
15971653
if (axis == X_AXIS) {
@@ -1605,6 +1661,23 @@ int CurieIMUClass::readAccelerometer(int axis)
16051661
return 0;
16061662
}
16071663

1664+
float CurieIMUClass::readAccelerometerScaled(int axis)
1665+
{
1666+
int16_t raw;
1667+
1668+
if (axis == X_AXIS) {
1669+
raw = getAccelerationX();
1670+
} else if (axis == Y_AXIS) {
1671+
raw = getAccelerationY();
1672+
} else if (axis == Z_AXIS) {
1673+
raw = getAccelerationZ();
1674+
} else {
1675+
return 0;
1676+
}
1677+
1678+
return convertRaw(raw, accel_range);
1679+
}
1680+
16081681
int CurieIMUClass::readGyro(int axis)
16091682
{
16101683
if (axis == X_AXIS) {
@@ -1618,6 +1691,23 @@ int CurieIMUClass::readGyro(int axis)
16181691
return 0;
16191692
}
16201693

1694+
float CurieIMUClass::readGyroScaled(int axis)
1695+
{
1696+
int16_t raw;
1697+
1698+
if (axis == X_AXIS) {
1699+
raw = getRotationX();
1700+
} else if (axis == Y_AXIS) {
1701+
raw = getRotationY();
1702+
} else if (axis == Z_AXIS) {
1703+
raw = getRotationZ();
1704+
} else {
1705+
return 0;
1706+
}
1707+
1708+
return convertRaw(raw, gyro_range);
1709+
}
1710+
16211711
int CurieIMUClass::readTemperature()
16221712
{
16231713
return getTemperature();

Diff for: libraries/CurieIMU/src/CurieIMU.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,15 @@ class CurieIMUClass : public BMI160Class {
187187
void setStepDetectionMode(int mode);
188188

189189
void readMotionSensor(int& ax, int& ay, int& az, int& gx, int& gy, int& gz);
190+
void readMotionSensorScaled(float& ax, float& ay, float& az, float& gx, float& gy, float& gz);
190191
void readAccelerometer(int& x, int& y, int& z);
192+
void readAccelerometerScaled(float& x, float& y, float& z);
191193
void readGyro(int& x, int& y, int& z);
192-
194+
void readGyroScaled(float& x, float& y, float& z);
193195
int readAccelerometer(int axis);
196+
float readAccelerometerScaled(int axis);
194197
int readGyro(int axis);
198+
float readGyroScaled(int axis);
195199
int readTemperature();
196200

197201
bool shockDetected(int axis, int direction);
@@ -205,6 +209,9 @@ class CurieIMUClass : public BMI160Class {
205209
private:
206210
int serial_buffer_transfer(uint8_t *buf, unsigned tx_cnt, unsigned rx_cnt);
207211

212+
float accel_range;
213+
float gyro_range;
214+
208215
float getFreefallDetectionThreshold();
209216
void setFreefallDetectionThreshold(float threshold);
210217
float getShockDetectionThreshold();

0 commit comments

Comments
 (0)