diff options
author | Sebastian Reichel <sre@ring0.de> | 2012-10-15 00:36:43 +0200 |
---|---|---|
committer | Sebastian Reichel <sre@ring0.de> | 2012-10-15 00:36:43 +0200 |
commit | 1523ee7ee712d19c33c360b81328ae871e54caa4 (patch) | |
tree | 542e17c9e4a6584d1dae0ad79ebb337c476a74b5 | |
parent | 93a10edb6fef5d127d6395d0af5e238c26a2f2ca (diff) | |
download | microcopterd-1523ee7ee712d19c33c360b81328ae871e54caa4.tar.bz2 |
ADXL345: normalize data
-rw-r--r-- | main.vala | 4 | ||||
-rw-r--r-- | sensors/accelerometer.vala | 2 | ||||
-rw-r--r-- | sensors/accelerometer/adxl345.vala | 11 |
3 files changed, 10 insertions, 7 deletions
@@ -27,8 +27,8 @@ Kinematics kinematics; bool loop() { try { /* get sensor data */ - double gx, gy, gz; - int16 ax, ay, az, mx, my, mz; + double gx, gy, gz, ax, ay, az; + int16 mx, my, mz; gyroscope.get_data(out gx, out gy, out gz); accelerometer.get_data(out ax, out ay, out az); compass.get_data(out mx, out my, out mz); diff --git a/sensors/accelerometer.vala b/sensors/accelerometer.vala index b1f9607..95345ae 100644 --- a/sensors/accelerometer.vala +++ b/sensors/accelerometer.vala @@ -15,5 +15,5 @@ public interface Accelerometer : Device { public abstract void init(KeyFile cfg) throws Error; - public abstract void get_data(out int16 x, out int16 y, out int16 z) throws Error; + public abstract void get_data(out double x, out double y, out double z) throws Error; } diff --git a/sensors/accelerometer/adxl345.vala b/sensors/accelerometer/adxl345.vala index 9c2c58c..3cc8be7 100644 --- a/sensors/accelerometer/adxl345.vala +++ b/sensors/accelerometer/adxl345.vala @@ -14,6 +14,9 @@ */ public class ADXL345 : I2CDevice, Accelerometer { + /* ADXL345 3.9mG per LSB */ + private static const double scale_factor = (3.9 / 1000.0); + public ADXL345(uint8 dev, uint8 addr = 0x53) throws I2CError { setup(dev, addr); devsetup(); @@ -56,11 +59,11 @@ public class ADXL345 : I2CDevice, Accelerometer { set_byte(0x20, z); } - public void get_data(out int16 x, out int16 y, out int16 z) throws I2CError { + public void get_data(out double x, out double y, out double z) throws I2CError { var data = get_block(0x32, 6); - x = data[0] | data[1] << 8; - y = data[2] | data[3] << 8; - z = data[4] | data[5] << 8; + x = (data[0] | data[1] << 8) * scale_factor; + y = (data[2] | data[3] << 8) * scale_factor; + z = (data[4] | data[5] << 8) * scale_factor; } } |