summaryrefslogtreecommitdiffstats
path: root/sensors
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2012-06-07 19:54:38 +0200
committerSebastian Reichel <sre@ring0.de>2012-06-07 19:54:38 +0200
commitdfbb403fcd4d3d6ceec22d2e70091686c53606f3 (patch)
tree430ea41f647fba88dcf26cb2a4f9b3eace8fcc61 /sensors
downloadmicrocopterd-dfbb403fcd4d3d6ceec22d2e70091686c53606f3.tar.bz2
initial code import
Diffstat (limited to 'sensors')
-rw-r--r--sensors/accelerometer.vala18
-rw-r--r--sensors/accelerometer/accelerometer-adxl345.vala44
-rw-r--r--sensors/barometer.vala23
-rw-r--r--sensors/barometer/barometer-bmp085.vala128
-rw-r--r--sensors/compass.vala18
-rw-r--r--sensors/compass/compass-hmc5843.vala26
-rw-r--r--sensors/gyroscope.vala18
-rw-r--r--sensors/gyroscope/gyroscope-itg3200.vala48
8 files changed, 323 insertions, 0 deletions
diff --git a/sensors/accelerometer.vala b/sensors/accelerometer.vala
new file mode 100644
index 0000000..27b47f0
--- /dev/null
+++ b/sensors/accelerometer.vala
@@ -0,0 +1,18 @@
+/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public interface Accelerometer : Device {
+ public abstract void get_data(out uint16 x, out uint16 y, out uint16 z) throws Error;
+}
diff --git a/sensors/accelerometer/accelerometer-adxl345.vala b/sensors/accelerometer/accelerometer-adxl345.vala
new file mode 100644
index 0000000..ed9947f
--- /dev/null
+++ b/sensors/accelerometer/accelerometer-adxl345.vala
@@ -0,0 +1,44 @@
+/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public class ADXL345 : I2CDevice, Accelerometer {
+ public ADXL345(uint8 dev, uint8 addr = 0x53) throws I2CError {
+ base(dev, addr);
+ }
+
+ public uint8 get_address() throws I2CError {
+ var addr = get_byte(0x00);
+ assert(addr == 0xe5);
+ return addr;
+ }
+
+ public void get_offset(out uint8 x, out uint8 y, out uint8 z) throws I2CError {
+ x = get_byte(0x1E);
+ y = get_byte(0x1F);
+ z = get_byte(0x20);
+ }
+
+ public void set_offset(uint8 x, uint8 y, uint8 z) throws I2CError {
+ set_byte(0x1E, x);
+ set_byte(0x1F, y);
+ set_byte(0x20, z);
+ }
+
+ public void get_data(out uint16 x, out uint16 y, out uint16 z) throws I2CError {
+ x = get_big_word(0x32);
+ y = get_big_word(0x34);
+ z = get_big_word(0x36);
+ }
+}
diff --git a/sensors/barometer.vala b/sensors/barometer.vala
new file mode 100644
index 0000000..5c1f4f9
--- /dev/null
+++ b/sensors/barometer.vala
@@ -0,0 +1,23 @@
+/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public interface Barometer : Device {
+ public abstract int32 get_pressure() throws Error;
+
+ public double get_altitude(int32 base_pressure) throws Error {
+ int32 p = get_pressure();
+ return (44330.0 * (1.0 - Posix.pow(p / base_pressure, 1.0 / 5.255)));
+ }
+}
diff --git a/sensors/barometer/barometer-bmp085.vala b/sensors/barometer/barometer-bmp085.vala
new file mode 100644
index 0000000..92ea917
--- /dev/null
+++ b/sensors/barometer/barometer-bmp085.vala
@@ -0,0 +1,128 @@
+/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public class BMP085 : I2CDevice, Barometer {
+ public uint8 oversampling_setting = 0x03;
+ private Calibration cal = Calibration();
+
+ private int64 next_temp_measurement;
+ private int raw_temp;
+ private int raw_pressure;
+
+ private int64 temp_correction_coefficient;
+
+ struct Calibration {
+ int16 AC1;
+ int16 AC2;
+ int16 AC3;
+ uint16 AC4;
+ uint16 AC5;
+ uint16 AC6;
+ int16 B1;
+ int16 B2;
+ int16 MB;
+ int16 MC;
+ int16 MD;
+ }
+
+ public BMP085(uint8 dev, uint8 addr = 0x77) throws I2CError {
+ base(dev, addr);
+ read_calibration_values();
+ }
+
+ private void read_calibration_values() throws I2CError {
+ var data = get_block(0xaa, 22);
+ cal.AC1 = data[0] << 8 | data[1];
+ cal.AC2 = data[2] << 8 | data[3];
+ cal.AC3 = data[4] << 8 | data[5];
+ cal.AC4 = data[6] << 8 | data[7];
+ cal.AC5 = data[8] << 8 | data[9];
+ cal.AC6 = data[10] << 8 | data[11];
+ cal.B1 = data[12] << 8 | data[13];
+ cal.B2 = data[14] << 8 | data[15];
+ cal.MB = data[16] << 8 | data[17];
+ cal.MC = data[18] << 8 | data[19];
+
+ cal.MD = data[20] << 8 | data[21];
+ }
+
+ private void msleep(uint msecs) {
+ Posix.usleep(msecs*1000);
+ }
+
+ private void update_raw_temperature() throws I2CError {
+ set_byte(0xf4, 0x2e);
+ msleep(5);
+ var data = get_block(0xf6, 2);
+ raw_temp = data[0] << 8 | data[1];
+ next_temp_measurement = time_t() + 1;
+ }
+
+ private void update_raw_pressure() throws I2CError {
+ set_byte(0xf4, 0x34 + (oversampling_setting<<6));
+ msleep(2+(3 << oversampling_setting<<1));
+ var data = get_block(0xf6, 3);
+ raw_pressure = data[0] << 16 | data[1] << 8 | data[2];
+ raw_pressure >>= 8-oversampling_setting;
+ }
+
+ public int32 get_temperature() throws I2CError {
+ if(next_temp_measurement+1 < time_t())
+ update_raw_temperature();
+
+ int64 x1 = ((raw_temp - cal.AC6) * cal.AC5) >> 15;
+ int64 x2 = (cal.MC << 11) / (x1 + cal.MD);
+ temp_correction_coefficient = x1 + x2 - 4000;
+
+ return (int32) (x1+x2+8) >> 4;
+ }
+
+ public int32 get_pressure() throws I2CError {
+ int64 x1, x2, x3, b3, p;
+ uint64 b4, b7;
+
+ if(next_temp_measurement+1 < time_t())
+ get_temperature();
+
+ update_raw_pressure();
+
+ x1 = (temp_correction_coefficient * temp_correction_coefficient) >> 12;
+ x1 *= cal.B2;
+ x1 >>= 11;
+
+ x2 = cal.AC2 * temp_correction_coefficient;
+ x2 >>= 11;
+
+ x3 = x1 + x2;
+
+ b3 = (((((int64)cal.AC1) * 4 + x3) << oversampling_setting) + 2) >> 2;
+
+ x1 = (cal.AC3 * temp_correction_coefficient) >> 13;
+ x2 = (cal.B1 * ((temp_correction_coefficient * temp_correction_coefficient) >> 12)) >> 16;
+ x3 = (x1 + x2 + 2) >> 2;
+ b4 = (cal.AC4 * (uint64)(x3 + 32768)) >> 15;
+
+ b7 = ((uint64)raw_pressure - b3) * (50000 >> oversampling_setting);
+ p = (int64) ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));
+
+ x1 = p >> 8;
+ x1 *= x1;
+ x1 = (x1 * 3038) >> 16;
+ x2 = (-7357 * p) >> 16;
+ p += (x1 + x2 + 3791) >> 4;
+
+ return (int32) p;
+ }
+}
diff --git a/sensors/compass.vala b/sensors/compass.vala
new file mode 100644
index 0000000..b62cb3c
--- /dev/null
+++ b/sensors/compass.vala
@@ -0,0 +1,18 @@
+/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public interface Compass : Device {
+ public abstract void get_data(out uint16 x, out uint16 y, out uint16 z) throws Error;
+}
diff --git a/sensors/compass/compass-hmc5843.vala b/sensors/compass/compass-hmc5843.vala
new file mode 100644
index 0000000..b09b054
--- /dev/null
+++ b/sensors/compass/compass-hmc5843.vala
@@ -0,0 +1,26 @@
+/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public class HMC5843 : I2CDevice, Compass {
+ public HMC5843(uint8 dev, uint8 addr = 0x1e) throws I2CError {
+ base(dev, addr);
+ }
+
+ public void get_data(out uint16 x, out uint16 y, out uint16 z) throws I2CError {
+ x = get_big_word(0x03);
+ y = get_big_word(0x05);
+ z = get_big_word(0x07);
+ }
+}
diff --git a/sensors/gyroscope.vala b/sensors/gyroscope.vala
new file mode 100644
index 0000000..165df73
--- /dev/null
+++ b/sensors/gyroscope.vala
@@ -0,0 +1,18 @@
+/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public interface Gyroscope : Device {
+ public abstract void get_data(out int16 x, out int16 y, out int16 z) throws Error;
+}
diff --git a/sensors/gyroscope/gyroscope-itg3200.vala b/sensors/gyroscope/gyroscope-itg3200.vala
new file mode 100644
index 0000000..c57aa55
--- /dev/null
+++ b/sensors/gyroscope/gyroscope-itg3200.vala
@@ -0,0 +1,48 @@
+/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public class ITG3200 : I2CDevice, Gyroscope {
+ public ITG3200(uint8 dev, uint8 addr = 0x68) throws I2CError {
+ base(dev, addr);
+ }
+
+ public uint8 get_address() throws I2CError {
+ return (uint8) get_byte(0x00);
+ }
+
+ /* TODO: add method for register 0x16 (frequency + range) */
+ /* TODO: add method for register 0x17 (interrupt config) */
+ /* TODO: add method for register 0x3E (power management) */
+
+ public uint8 get_sample_rate_divider() throws I2CError {
+ /* sample rate = internal frequency / (divider + 1) */
+ return (uint8) get_byte(0x15);
+ }
+
+ public void set_sample_rate_divider(uint8 val) throws I2CError {
+ set_byte(0x15, val);
+ }
+
+ public int16 get_temperature() throws I2CError {
+ int16 raw = (int16) get_big_word(0x1b);
+ return 350 + ((raw + 13200) / 28);
+ }
+
+ public void get_data(out int16 x, out int16 y, out int16 z) throws I2CError {
+ x = (int16) get_big_word(0x1d);
+ y = (int16) get_big_word(0x1f);
+ z = (int16) get_big_word(0x21);
+ }
+}