From 4f19f18dd955e488e161c1f4baca9314767d05a5 Mon Sep 17 00:00:00 2001 From: eloylp Date: Tue, 22 Aug 2017 12:55:55 +0200 Subject: [PATCH 01/24] ignoring idea files. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1dbc687..35d20f8 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,4 @@ target/ #Ipython Notebook .ipynb_checkpoints +.idea \ No newline at end of file From bb80b5cf603a3aae88cae4681e18404bae23edf8 Mon Sep 17 00:00:00 2001 From: eloylp Date: Tue, 22 Aug 2017 13:08:14 +0200 Subject: [PATCH 02/24] fixed missed class referencing in member method. --- Adafruit_LSM303/LSM303.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_LSM303/LSM303.py b/Adafruit_LSM303/LSM303.py index d5d08de..fc3c240 100644 --- a/Adafruit_LSM303/LSM303.py +++ b/Adafruit_LSM303/LSM303.py @@ -85,7 +85,7 @@ def read(self): mag = struct.unpack('>hhh', mag_raw) return (accel, mag) - def set_mag_gain(gain=LSM303_MAGGAIN_1_3): + def set_mag_gain(self, gain=LSM303_MAGGAIN_1_3): """Set the magnetometer gain. Gain should be one of the following constants: - LSM303_MAGGAIN_1_3 = +/- 1.3 (default) From b8b16035065d36336fcbbca105801d671abc8963 Mon Sep 17 00:00:00 2001 From: eloylp Date: Tue, 22 Aug 2017 22:20:05 +0200 Subject: [PATCH 03/24] Added example setting gain (commented) --- examples/simpletest.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/simpletest.py b/examples/simpletest.py index e27a73c..eb5373f 100644 --- a/examples/simpletest.py +++ b/examples/simpletest.py @@ -12,6 +12,9 @@ # Create a LSM303 instance. lsm303 = Adafruit_LSM303.LSM303() +# Setting gain +# lsm303.set_mag_gain(Adafruit_LSM303.LSM303_MAGGAIN_4_7) + # Alternatively you can specify the I2C bus with a bus parameter: #lsm303 = Adafruit_LSM303.LSM303(busum=2) From e128bb614badbe52184c4a1bdc655210e59eebc7 Mon Sep 17 00:00:00 2001 From: eloylp Date: Tue, 22 Aug 2017 22:38:19 +0200 Subject: [PATCH 04/24] split read in two. Magnet and accel data. Preserving original interface read() --- Adafruit_LSM303/LSM303.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Adafruit_LSM303/LSM303.py b/Adafruit_LSM303/LSM303.py index fc3c240..f31e57a 100644 --- a/Adafruit_LSM303/LSM303.py +++ b/Adafruit_LSM303/LSM303.py @@ -21,7 +21,6 @@ # SOFTWARE. import struct - # Minimal constants carried over from Arduino library: LSM303_ADDRESS_ACCEL = (0x32 >> 1) # 0011001x LSM303_ADDRESS_MAG = (0x3C >> 1) # 0011110x @@ -75,15 +74,29 @@ def read(self): be returned with: ((accel X, accel Y, accel Z), (mag X, mag Y, mag Z)) """ + return self.read_accelerometer(), self.read_magnetometer() + + def read_accelerometer(self): + + """ + :rtype: (accel X, accel Y, accel Z) + """ # Read the accelerometer as signed 16-bit little endian values. accel_raw = self._accel.readList(LSM303_REGISTER_ACCEL_OUT_X_L_A | 0x80, 6) accel = struct.unpack('> 4, accel[1] >> 4, accel[2] >> 4) - # Read the magnetometer. + return accel + + def read_magnetometer(self): + + """ + :rtype: (mag X, mag Y, mag Z) + """ mag_raw = self._mag.readList(LSM303_REGISTER_MAG_OUT_X_H_M, 6) mag = struct.unpack('>hhh', mag_raw) - return (accel, mag) + + return mag def set_mag_gain(self, gain=LSM303_MAGGAIN_1_3): """Set the magnetometer gain. Gain should be one of the following From 58535f0a8f03c47fa9a474327a621b4f01d89cd5 Mon Sep 17 00:00:00 2001 From: eloylp Date: Tue, 22 Aug 2017 22:38:39 +0200 Subject: [PATCH 05/24] formatting file. --- Adafruit_LSM303/LSM303.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Adafruit_LSM303/LSM303.py b/Adafruit_LSM303/LSM303.py index f31e57a..13cdd0f 100644 --- a/Adafruit_LSM303/LSM303.py +++ b/Adafruit_LSM303/LSM303.py @@ -23,23 +23,23 @@ # Minimal constants carried over from Arduino library: LSM303_ADDRESS_ACCEL = (0x32 >> 1) # 0011001x -LSM303_ADDRESS_MAG = (0x3C >> 1) # 0011110x - # Default Type -LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20 # 00000111 rw -LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23 # 00000000 rw -LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28 -LSM303_REGISTER_MAG_CRB_REG_M = 0x01 -LSM303_REGISTER_MAG_MR_REG_M = 0x02 -LSM303_REGISTER_MAG_OUT_X_H_M = 0x03 +LSM303_ADDRESS_MAG = (0x3C >> 1) # 0011110x +# Default Type +LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20 # 00000111 rw +LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23 # 00000000 rw +LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28 +LSM303_REGISTER_MAG_CRB_REG_M = 0x01 +LSM303_REGISTER_MAG_MR_REG_M = 0x02 +LSM303_REGISTER_MAG_OUT_X_H_M = 0x03 # Gain settings for set_mag_gain() -LSM303_MAGGAIN_1_3 = 0x20 # +/- 1.3 -LSM303_MAGGAIN_1_9 = 0x40 # +/- 1.9 -LSM303_MAGGAIN_2_5 = 0x60 # +/- 2.5 -LSM303_MAGGAIN_4_0 = 0x80 # +/- 4.0 -LSM303_MAGGAIN_4_7 = 0xA0 # +/- 4.7 -LSM303_MAGGAIN_5_6 = 0xC0 # +/- 5.6 -LSM303_MAGGAIN_8_1 = 0xE0 # +/- 8.1 +LSM303_MAGGAIN_1_3 = 0x20 # +/- 1.3 +LSM303_MAGGAIN_1_9 = 0x40 # +/- 1.9 +LSM303_MAGGAIN_2_5 = 0x60 # +/- 2.5 +LSM303_MAGGAIN_4_0 = 0x80 # +/- 4.0 +LSM303_MAGGAIN_4_7 = 0xA0 # +/- 4.7 +LSM303_MAGGAIN_5_6 = 0xC0 # +/- 5.6 +LSM303_MAGGAIN_8_1 = 0xE0 # +/- 8.1 class LSM303(object): From 06b34144d753e000de8e69ae7fd8f4283fb43eaf Mon Sep 17 00:00:00 2001 From: eloylp Date: Tue, 22 Aug 2017 23:03:55 +0200 Subject: [PATCH 06/24] updated and refactored example. --- examples/simpletest.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/examples/simpletest.py b/examples/simpletest.py index eb5373f..d41ab46 100644 --- a/examples/simpletest.py +++ b/examples/simpletest.py @@ -3,29 +3,34 @@ # second. # Author: Tony DiCola # License: Public Domain + import time +import sys -# Import the LSM303 module. import Adafruit_LSM303 - -# Create a LSM303 instance. lsm303 = Adafruit_LSM303.LSM303() -# Setting gain +# Setting magnetic gain. you can check it at http://www.magnetic-declination.com/ # lsm303.set_mag_gain(Adafruit_LSM303.LSM303_MAGGAIN_4_7) # Alternatively you can specify the I2C bus with a bus parameter: -#lsm303 = Adafruit_LSM303.LSM303(busum=2) +# lsm303 = Adafruit_LSM303.LSM303(busum=2) print('Printing accelerometer & magnetometer X, Y, Z axis values, press Ctrl-C to quit...') while True: - # Read the X, Y, Z axis acceleration values and print them. - accel, mag = lsm303.read() - # Grab the X, Y, Z components from the reading and print them out. + accel = lsm303.read_accelerometer() + mag = lsm303.read_magnetometer() + + # You also can get a accel + mag tuple with this function + # total_data = lsm303.read() + accel_x, accel_y, accel_z = accel mag_x, mag_z, mag_y = mag - print('Accel X={0}, Accel Y={1}, Accel Z={2}, Mag X={3}, Mag Y={4}, Mag Z={5}'.format( - accel_x, accel_y, accel_z, mag_x, mag_y, mag_z)) - # Wait half a second and repeat. - time.sleep(0.5) + + data = 'Accel X={0}, Accel Y={1}, Accel Z={2}, Mag X={3}, Mag Y={4}, Mag Z={5}'.format(accel_x, accel_y, accel_z, + mag_x, mag_y, mag_z) + sys.stdout.write(data) + sys.stdout.flush() + + time.sleep(0.1) From 8e4d6c6a8e29879aa318b840d47fa6d095d0a349 Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 11:35:51 +0200 Subject: [PATCH 07/24] adding compass helper class and tests. --- Adafruit_LSM303/helper.py | 17 +++++++++++++++++ test/__init__.py | 0 test/test_compass.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 Adafruit_LSM303/helper.py create mode 100644 test/__init__.py create mode 100644 test/test_compass.py diff --git a/Adafruit_LSM303/helper.py b/Adafruit_LSM303/helper.py new file mode 100644 index 0000000..1dcee13 --- /dev/null +++ b/Adafruit_LSM303/helper.py @@ -0,0 +1,17 @@ +from math import atan2, pi + + +class Compass: + def __init__(self, lsm303_sensor): + """ + :type lsm303_sensor : Adafruit_LSM303.LSM303.LSM303 + """ + self.lsm303_sensor = lsm303_sensor + + def get_heading(self): + magnet_axis_data = self.lsm303_sensor.read_magnetometer() + heading_radians = atan2(magnet_axis_data[1], magnet_axis_data[0]) + heading_degrees = heading_radians * 180 / pi + if heading_degrees < 0: + heading_degrees = 360 + heading_degrees + return heading_degrees diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_compass.py b/test/test_compass.py new file mode 100644 index 0000000..72ef2ae --- /dev/null +++ b/test/test_compass.py @@ -0,0 +1,28 @@ +from unittest import TestCase + +from ddt import ddt, data, unpack +from mock import MagicMock + +from Adafruit_LSM303.helper import Compass + + +@ddt +class TestCompass(TestCase): + @data( + (0, 0, 0), + (1, 0, 0), + (1, 1, 45), + (0, 1, 90), + (-1, 0, 180), + (-1, -1, 225), + (0, -1, 270), + (1, -0.0000000000000001, 360), + ) + @unpack + def test_get_heading(self, x, y, expected_degrees): + + lsm303 = MagicMock() + lsm303.read_magnetometer = MagicMock(return_value=(x, y)) + compass = Compass(lsm303) + heading = compass.get_heading() + self.assertEqual(expected_degrees, heading) From 0c2ef9a989830002b19d91d9d81c98b69ee1d1dd Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 12:08:40 +0200 Subject: [PATCH 08/24] Added units to return types. --- Adafruit_LSM303/LSM303.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_LSM303/LSM303.py b/Adafruit_LSM303/LSM303.py index 13cdd0f..d9aa45e 100644 --- a/Adafruit_LSM303/LSM303.py +++ b/Adafruit_LSM303/LSM303.py @@ -79,7 +79,7 @@ def read(self): def read_accelerometer(self): """ - :rtype: (accel X, accel Y, accel Z) + :rtype: (accel X, accel Y, accel Z) (meters per second squared) """ # Read the accelerometer as signed 16-bit little endian values. accel_raw = self._accel.readList(LSM303_REGISTER_ACCEL_OUT_X_L_A | 0x80, 6) @@ -91,7 +91,7 @@ def read_accelerometer(self): def read_magnetometer(self): """ - :rtype: (mag X, mag Y, mag Z) + :rtype: (mag X, mag Y, mag Z) (micro-Teslas) """ mag_raw = self._mag.readList(LSM303_REGISTER_MAG_OUT_X_H_M, 6) mag = struct.unpack('>hhh', mag_raw) From 5f080f4a0a391fc25533996ba14c5549ce49807d Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 12:09:39 +0200 Subject: [PATCH 09/24] changed sample file name --- examples/{simpletest.py => simple.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{simpletest.py => simple.py} (100%) diff --git a/examples/simpletest.py b/examples/simple.py similarity index 100% rename from examples/simpletest.py rename to examples/simple.py From 27800f124691b82ecf64f09af5ad0f9a6c11e442 Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 12:16:09 +0200 Subject: [PATCH 10/24] cleaning stdout properly in simple example. --- examples/simple.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple.py b/examples/simple.py index d41ab46..91f80ef 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -28,7 +28,7 @@ accel_x, accel_y, accel_z = accel mag_x, mag_z, mag_y = mag - data = 'Accel X={0}, Accel Y={1}, Accel Z={2}, Mag X={3}, Mag Y={4}, Mag Z={5}'.format(accel_x, accel_y, accel_z, + data = '\rAccel X={0}, Accel Y={1}, Accel Z={2}, Mag X={3}, Mag Y={4}, Mag Z={5}'.format(accel_x, accel_y, accel_z, mag_x, mag_y, mag_z) sys.stdout.write(data) sys.stdout.flush() From 42449609d76323b229203876ef9a565c1f8b50c9 Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 12:54:01 +0200 Subject: [PATCH 11/24] Updating param order in compass (test-prod) --- Adafruit_LSM303/helper.py | 2 +- test/test_compass.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_LSM303/helper.py b/Adafruit_LSM303/helper.py index 1dcee13..ab5b6f5 100644 --- a/Adafruit_LSM303/helper.py +++ b/Adafruit_LSM303/helper.py @@ -10,7 +10,7 @@ def __init__(self, lsm303_sensor): def get_heading(self): magnet_axis_data = self.lsm303_sensor.read_magnetometer() - heading_radians = atan2(magnet_axis_data[1], magnet_axis_data[0]) + heading_radians = atan2(magnet_axis_data[2], magnet_axis_data[0]) heading_degrees = heading_radians * 180 / pi if heading_degrees < 0: heading_degrees = 360 + heading_degrees diff --git a/test/test_compass.py b/test/test_compass.py index 72ef2ae..dfed8f3 100644 --- a/test/test_compass.py +++ b/test/test_compass.py @@ -22,7 +22,7 @@ class TestCompass(TestCase): def test_get_heading(self, x, y, expected_degrees): lsm303 = MagicMock() - lsm303.read_magnetometer = MagicMock(return_value=(x, y)) + lsm303.read_magnetometer = MagicMock(return_value=(x, 666, y)) compass = Compass(lsm303) heading = compass.get_heading() self.assertEqual(expected_degrees, heading) From e20e5e4a1aa578a0a81cfef3f6d04736cfbc2065 Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 12:55:14 +0200 Subject: [PATCH 12/24] Adding heading example file. --- examples/compass.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 examples/compass.py diff --git a/examples/compass.py b/examples/compass.py new file mode 100644 index 0000000..3cc4786 --- /dev/null +++ b/examples/compass.py @@ -0,0 +1,23 @@ + +import time +import sys + +import Adafruit_LSM303 +from Adafruit_LSM303.helper import Compass + +lsm303 = Adafruit_LSM303.LSM303() + +# Setting magnetic gain. you can check it at http://www.magnetic-declination.com/ +#lsm303.set_mag_gain(Adafruit_LSM303.LSM303_MAGGAIN_4_7) + +compass = Compass(lsm303) + +print('Printing heading ..') + +while True: + + heading = compass.get_heading() + data = '\rHeading (Degrees): {0} '.format(heading) + sys.stdout.write(data) + sys.stdout.flush() + time.sleep(0.1) From 18bf8370116e9a356fd6567bad16ad12be4b3bb3 Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 12:57:01 +0200 Subject: [PATCH 13/24] choosing better name for file. --- Adafruit_LSM303/{helper.py => instruments.py} | 0 examples/compass.py | 2 +- test/test_compass.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename Adafruit_LSM303/{helper.py => instruments.py} (100%) diff --git a/Adafruit_LSM303/helper.py b/Adafruit_LSM303/instruments.py similarity index 100% rename from Adafruit_LSM303/helper.py rename to Adafruit_LSM303/instruments.py diff --git a/examples/compass.py b/examples/compass.py index 3cc4786..179910d 100644 --- a/examples/compass.py +++ b/examples/compass.py @@ -3,7 +3,7 @@ import sys import Adafruit_LSM303 -from Adafruit_LSM303.helper import Compass +from Adafruit_LSM303.instruments import Compass lsm303 = Adafruit_LSM303.LSM303() diff --git a/test/test_compass.py b/test/test_compass.py index dfed8f3..c448065 100644 --- a/test/test_compass.py +++ b/test/test_compass.py @@ -3,7 +3,7 @@ from ddt import ddt, data, unpack from mock import MagicMock -from Adafruit_LSM303.helper import Compass +from Adafruit_LSM303.instruments import Compass @ddt From 80eb1d6dd764d778a21c40f8ea1e00197c322b6d Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 13:08:49 +0200 Subject: [PATCH 14/24] refactoring instrument --- Adafruit_LSM303/instruments.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Adafruit_LSM303/instruments.py b/Adafruit_LSM303/instruments.py index ab5b6f5..240550e 100644 --- a/Adafruit_LSM303/instruments.py +++ b/Adafruit_LSM303/instruments.py @@ -1,13 +1,15 @@ from math import atan2, pi -class Compass: +class Instrument: def __init__(self, lsm303_sensor): """ :type lsm303_sensor : Adafruit_LSM303.LSM303.LSM303 """ self.lsm303_sensor = lsm303_sensor + +class Compass(Instrument): def get_heading(self): magnet_axis_data = self.lsm303_sensor.read_magnetometer() heading_radians = atan2(magnet_axis_data[2], magnet_axis_data[0]) From 37f87b0927e7d58af556c9962d565d72709d7d30 Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 13:18:17 +0200 Subject: [PATCH 15/24] refactoring instrument, ep2 --- Adafruit_LSM303/instruments.py | 7 +++++++ test/test_compass.py | 18 +++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Adafruit_LSM303/instruments.py b/Adafruit_LSM303/instruments.py index 240550e..befb0c9 100644 --- a/Adafruit_LSM303/instruments.py +++ b/Adafruit_LSM303/instruments.py @@ -8,6 +8,13 @@ def __init__(self, lsm303_sensor): """ self.lsm303_sensor = lsm303_sensor + def vector_2_degrees(self, x, y): + radians = atan2(y, x) + degrees = radians * 180 / pi + if degrees < 0: + degrees = 360 + degrees + return degrees + class Compass(Instrument): def get_heading(self): diff --git a/test/test_compass.py b/test/test_compass.py index c448065..8cfbef2 100644 --- a/test/test_compass.py +++ b/test/test_compass.py @@ -3,11 +3,15 @@ from ddt import ddt, data, unpack from mock import MagicMock -from Adafruit_LSM303.instruments import Compass +from Adafruit_LSM303.instruments import Compass, Instrument @ddt -class TestCompass(TestCase): +class TestInstrument(TestCase): + def setUp(self): + lsm303 = MagicMock() + self.instrument = Instrument(lsm303) + @data( (0, 0, 0), (1, 0, 0), @@ -19,10 +23,6 @@ class TestCompass(TestCase): (1, -0.0000000000000001, 360), ) @unpack - def test_get_heading(self, x, y, expected_degrees): - - lsm303 = MagicMock() - lsm303.read_magnetometer = MagicMock(return_value=(x, 666, y)) - compass = Compass(lsm303) - heading = compass.get_heading() - self.assertEqual(expected_degrees, heading) + def test_vector_2_degrees(self, x, y, expected_degrees): + degrees = self.instrument.vector_2_degrees(x, y) + self.assertEqual(expected_degrees, degrees) From 3694bee3f0e42483229fb881e54082c017c9064d Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 13:42:14 +0200 Subject: [PATCH 16/24] refactoring instrument, ep3 (typo) --- test/test_compass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_compass.py b/test/test_compass.py index 8cfbef2..c037ed4 100644 --- a/test/test_compass.py +++ b/test/test_compass.py @@ -3,7 +3,7 @@ from ddt import ddt, data, unpack from mock import MagicMock -from Adafruit_LSM303.instruments import Compass, Instrument +from Adafruit_LSM303.instruments import Instrument @ddt From f74e054ca06a664b0e45f4ade8737c38a6b246b1 Mon Sep 17 00:00:00 2001 From: eloylp Date: Thu, 24 Aug 2017 13:50:19 +0200 Subject: [PATCH 17/24] refactoring instrument, making use of inherited function. --- Adafruit_LSM303/instruments.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Adafruit_LSM303/instruments.py b/Adafruit_LSM303/instruments.py index befb0c9..e6265f9 100644 --- a/Adafruit_LSM303/instruments.py +++ b/Adafruit_LSM303/instruments.py @@ -19,8 +19,4 @@ def vector_2_degrees(self, x, y): class Compass(Instrument): def get_heading(self): magnet_axis_data = self.lsm303_sensor.read_magnetometer() - heading_radians = atan2(magnet_axis_data[2], magnet_axis_data[0]) - heading_degrees = heading_radians * 180 / pi - if heading_degrees < 0: - heading_degrees = 360 + heading_degrees - return heading_degrees + return self.vector_2_degrees(magnet_axis_data[0], magnet_axis_data[2]) From c47fcf964a87db57a499bf0bb6634cef9220dabf Mon Sep 17 00:00:00 2001 From: eloylp Date: Fri, 25 Aug 2017 00:36:01 +0200 Subject: [PATCH 18/24] fixed bad comment. --- Adafruit_LSM303/LSM303.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_LSM303/LSM303.py b/Adafruit_LSM303/LSM303.py index d9aa45e..98d2502 100644 --- a/Adafruit_LSM303/LSM303.py +++ b/Adafruit_LSM303/LSM303.py @@ -79,7 +79,7 @@ def read(self): def read_accelerometer(self): """ - :rtype: (accel X, accel Y, accel Z) (meters per second squared) + :rtype: (accel X, accel Z, accel Y ) (meters per second squared) """ # Read the accelerometer as signed 16-bit little endian values. accel_raw = self._accel.readList(LSM303_REGISTER_ACCEL_OUT_X_L_A | 0x80, 6) From 841e256cb156ccba4230f00c839d3e572031438f Mon Sep 17 00:00:00 2001 From: eloylp Date: Fri, 25 Aug 2017 00:36:34 +0200 Subject: [PATCH 19/24] Added inclinometer instrument , example , and test. --- Adafruit_LSM303/instruments.py | 31 +++++++++++++++++++ examples/inclinometer.py | 26 ++++++++++++++++ test/test_compass.py | 28 ----------------- test/test_instrument.py | 56 ++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 28 deletions(-) create mode 100644 examples/inclinometer.py delete mode 100644 test/test_compass.py create mode 100644 test/test_instrument.py diff --git a/Adafruit_LSM303/instruments.py b/Adafruit_LSM303/instruments.py index e6265f9..96d1dc0 100644 --- a/Adafruit_LSM303/instruments.py +++ b/Adafruit_LSM303/instruments.py @@ -20,3 +20,34 @@ class Compass(Instrument): def get_heading(self): magnet_axis_data = self.lsm303_sensor.read_magnetometer() return self.vector_2_degrees(magnet_axis_data[0], magnet_axis_data[2]) + + +class Inclinometer(Instrument): + REFERENCE_POINT_DEGREES = 90 + + def get_inclination(self): + return self.get_inclination_respect_x(), self.get_inclination_respect_y() + + def get_inclination_respect_x(self): + accel_axis_data = self.get_data() + + return self.adapt_measure( + self.vector_2_degrees(accel_axis_data[0], accel_axis_data[2]) + ) + + def get_inclination_respect_y(self): + accel_axis_data = self.get_data() + return self.adapt_measure( + self.vector_2_degrees(accel_axis_data[1], accel_axis_data[2]) + ) + + def adapt_measure(self, degrees): + if degrees < self.REFERENCE_POINT_DEGREES: + return self.REFERENCE_POINT_DEGREES + degrees + elif self.REFERENCE_POINT_DEGREES <= degrees < 270: + return (degrees - self.REFERENCE_POINT_DEGREES) * -1 + elif degrees >= 270: + return degrees - (self.REFERENCE_POINT_DEGREES * 3) + + def get_data(self): + return self.lsm303_sensor.read_accelerometer() diff --git a/examples/inclinometer.py b/examples/inclinometer.py new file mode 100644 index 0000000..7972375 --- /dev/null +++ b/examples/inclinometer.py @@ -0,0 +1,26 @@ +import time +import sys + +import Adafruit_LSM303 +from Adafruit_LSM303.instruments import Inclinometer + +lsm303 = Adafruit_LSM303.LSM303() + +# Setting magnetic gain. you can check it at http://www.magnetic-declination.com/ +# lsm303.set_mag_gain(Adafruit_LSM303.LSM303_MAGGAIN_4_7) + +inclinometer = Inclinometer(lsm303) + +print('Printing Inclination (Degrees) based on x-y-z respective angle ...') + +""" + Information printed is adapted to positive and negative degrees per axis. From 0 to 179 and from 0 to -179 + This means that if you are on pitch down movement you will get a negative number until + you reach the noise of the sensor shooting again the sky. +""" +while True: + inclination = inclinometer.get_inclination() + data = '\rX : {0} Y: {1}'.format(inclination[0], inclination[1]) + sys.stdout.write(data) + sys.stdout.flush() + time.sleep(0.1) diff --git a/test/test_compass.py b/test/test_compass.py deleted file mode 100644 index c037ed4..0000000 --- a/test/test_compass.py +++ /dev/null @@ -1,28 +0,0 @@ -from unittest import TestCase - -from ddt import ddt, data, unpack -from mock import MagicMock - -from Adafruit_LSM303.instruments import Instrument - - -@ddt -class TestInstrument(TestCase): - def setUp(self): - lsm303 = MagicMock() - self.instrument = Instrument(lsm303) - - @data( - (0, 0, 0), - (1, 0, 0), - (1, 1, 45), - (0, 1, 90), - (-1, 0, 180), - (-1, -1, 225), - (0, -1, 270), - (1, -0.0000000000000001, 360), - ) - @unpack - def test_vector_2_degrees(self, x, y, expected_degrees): - degrees = self.instrument.vector_2_degrees(x, y) - self.assertEqual(expected_degrees, degrees) diff --git a/test/test_instrument.py b/test/test_instrument.py new file mode 100644 index 0000000..1b61334 --- /dev/null +++ b/test/test_instrument.py @@ -0,0 +1,56 @@ +from unittest import TestCase + +from ddt import ddt, data, unpack +from mock import MagicMock + +from Adafruit_LSM303.instruments import Instrument, Inclinometer + + +@ddt +class TestInstrument(TestCase): + def setUp(self): + lsm303 = MagicMock() + self.instrument = Instrument(lsm303) + + @data( + (0, 0, 0), + (1, 0, 0), + (1, 1, 45), + (0, 1, 90), + (-1, 0, 180), + (-1, -1, 225), + (0, -1, 270), + (1, -0.0000000000000001, 360), + ) + @unpack + def test_vector_2_degrees(self, x, y, expected_degrees): + degrees = self.instrument.vector_2_degrees(x, y) + self.assertEqual(expected_degrees, degrees) + + + +@ddt +class TestInclinometer(TestCase): + + @data( + (10, 100), + (89, 179), + (90, 0), + (180, -90), + (269, -179), + (270, 0), + (271, 1), + (360, 90), + ) + @unpack + def test_inclination_deviation_is_corrected(self, degrees, expected_degrees): + + lsm303 = MagicMock() + inclinometer = Inclinometer(lsm303) + inclinometer.vector_2_degrees = MagicMock(return_value=(degrees)) + + degrees = inclinometer.get_inclination() + self.assertEqual(expected_degrees, degrees[0]) + self.assertEqual(expected_degrees, degrees[1]) + + From 1cbf416080d0115052a599b48602bc3a62da0e3e Mon Sep 17 00:00:00 2001 From: eloylp Date: Fri, 25 Aug 2017 01:42:15 +0200 Subject: [PATCH 20/24] refactored var in test. --- test/test_instrument.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_instrument.py b/test/test_instrument.py index 1b61334..ccf76b4 100644 --- a/test/test_instrument.py +++ b/test/test_instrument.py @@ -49,8 +49,8 @@ def test_inclination_deviation_is_corrected(self, degrees, expected_degrees): inclinometer = Inclinometer(lsm303) inclinometer.vector_2_degrees = MagicMock(return_value=(degrees)) - degrees = inclinometer.get_inclination() - self.assertEqual(expected_degrees, degrees[0]) - self.assertEqual(expected_degrees, degrees[1]) + inclination_degrees_per_axis = inclinometer.get_inclination() + self.assertEqual(expected_degrees, inclination_degrees_per_axis[0]) + self.assertEqual(expected_degrees, inclination_degrees_per_axis[1]) From 11a32645572d23d3bf7300b8187c8301a37162a1 Mon Sep 17 00:00:00 2001 From: eloylp Date: Fri, 25 Aug 2017 09:26:45 +0200 Subject: [PATCH 21/24] refactored name in test. --- test/test_instrument.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/test_instrument.py b/test/test_instrument.py index ccf76b4..26132c9 100644 --- a/test/test_instrument.py +++ b/test/test_instrument.py @@ -28,10 +28,8 @@ def test_vector_2_degrees(self, x, y, expected_degrees): self.assertEqual(expected_degrees, degrees) - @ddt class TestInclinometer(TestCase): - @data( (10, 100), (89, 179), @@ -43,8 +41,7 @@ class TestInclinometer(TestCase): (360, 90), ) @unpack - def test_inclination_deviation_is_corrected(self, degrees, expected_degrees): - + def test_measure_is_adapted(self, degrees, expected_degrees): lsm303 = MagicMock() inclinometer = Inclinometer(lsm303) inclinometer.vector_2_degrees = MagicMock(return_value=(degrees)) @@ -52,5 +49,3 @@ def test_inclination_deviation_is_corrected(self, degrees, expected_degrees): inclination_degrees_per_axis = inclinometer.get_inclination() self.assertEqual(expected_degrees, inclination_degrees_per_axis[0]) self.assertEqual(expected_degrees, inclination_degrees_per_axis[1]) - - From 7b5ee4ab3c6fd48a1d4a835c40ee58a7aa996eb6 Mon Sep 17 00:00:00 2001 From: eloylp Date: Fri, 25 Aug 2017 09:27:35 +0200 Subject: [PATCH 22/24] added new line at the end of gitignore. --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 35d20f8..82dc2cd 100644 --- a/.gitignore +++ b/.gitignore @@ -60,4 +60,4 @@ target/ #Ipython Notebook .ipynb_checkpoints -.idea \ No newline at end of file +.idea From ab4391597ea58714fb74b53bf86807505d931fe1 Mon Sep 17 00:00:00 2001 From: eloylp Date: Wed, 22 Nov 2017 22:30:46 +0100 Subject: [PATCH 23/24] improved use of python math functions. --- Adafruit_LSM303/instruments.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Adafruit_LSM303/instruments.py b/Adafruit_LSM303/instruments.py index 96d1dc0..7ba81b5 100644 --- a/Adafruit_LSM303/instruments.py +++ b/Adafruit_LSM303/instruments.py @@ -1,4 +1,5 @@ -from math import atan2, pi +from math import atan2, degrees + class Instrument: @@ -10,10 +11,10 @@ def __init__(self, lsm303_sensor): def vector_2_degrees(self, x, y): radians = atan2(y, x) - degrees = radians * 180 / pi - if degrees < 0: - degrees = 360 + degrees - return degrees + degrees_calc = degrees(radians) + if degrees_calc < 0: + degrees_calc = 360 + degrees_calc + return degrees_calc class Compass(Instrument): From 7de6b1132ee84570b537da66e16427de54aad7d8 Mon Sep 17 00:00:00 2001 From: eloylp Date: Wed, 6 Dec 2017 01:00:01 +0100 Subject: [PATCH 24/24] removing measure adapter. --- Adafruit_LSM303/instruments.py | 19 ++----------------- test/test_instrument.py | 23 ----------------------- 2 files changed, 2 insertions(+), 40 deletions(-) diff --git a/Adafruit_LSM303/instruments.py b/Adafruit_LSM303/instruments.py index 7ba81b5..5b1c93b 100644 --- a/Adafruit_LSM303/instruments.py +++ b/Adafruit_LSM303/instruments.py @@ -1,7 +1,6 @@ from math import atan2, degrees - class Instrument: def __init__(self, lsm303_sensor): """ @@ -24,31 +23,17 @@ def get_heading(self): class Inclinometer(Instrument): - REFERENCE_POINT_DEGREES = 90 def get_inclination(self): return self.get_inclination_respect_x(), self.get_inclination_respect_y() def get_inclination_respect_x(self): accel_axis_data = self.get_data() - - return self.adapt_measure( - self.vector_2_degrees(accel_axis_data[0], accel_axis_data[2]) - ) + return self.vector_2_degrees(accel_axis_data[0], accel_axis_data[2]) def get_inclination_respect_y(self): accel_axis_data = self.get_data() - return self.adapt_measure( - self.vector_2_degrees(accel_axis_data[1], accel_axis_data[2]) - ) - - def adapt_measure(self, degrees): - if degrees < self.REFERENCE_POINT_DEGREES: - return self.REFERENCE_POINT_DEGREES + degrees - elif self.REFERENCE_POINT_DEGREES <= degrees < 270: - return (degrees - self.REFERENCE_POINT_DEGREES) * -1 - elif degrees >= 270: - return degrees - (self.REFERENCE_POINT_DEGREES * 3) + return self.vector_2_degrees(accel_axis_data[1], accel_axis_data[2]) def get_data(self): return self.lsm303_sensor.read_accelerometer() diff --git a/test/test_instrument.py b/test/test_instrument.py index 26132c9..ab41119 100644 --- a/test/test_instrument.py +++ b/test/test_instrument.py @@ -26,26 +26,3 @@ def setUp(self): def test_vector_2_degrees(self, x, y, expected_degrees): degrees = self.instrument.vector_2_degrees(x, y) self.assertEqual(expected_degrees, degrees) - - -@ddt -class TestInclinometer(TestCase): - @data( - (10, 100), - (89, 179), - (90, 0), - (180, -90), - (269, -179), - (270, 0), - (271, 1), - (360, 90), - ) - @unpack - def test_measure_is_adapted(self, degrees, expected_degrees): - lsm303 = MagicMock() - inclinometer = Inclinometer(lsm303) - inclinometer.vector_2_degrees = MagicMock(return_value=(degrees)) - - inclination_degrees_per_axis = inclinometer.get_inclination() - self.assertEqual(expected_degrees, inclination_degrees_per_axis[0]) - self.assertEqual(expected_degrees, inclination_degrees_per_axis[1])