This repository was archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Update to method names and linting #3
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,8 @@ | |
_LSM303ACCEL_MG_LSB = 16704.0 | ||
_GRAVITY_STANDARD = 9.80665 # Earth's gravity in m/s^2 | ||
_GAUSS_TO_MICROTESLA = 100.0 # Gauss to micro-Tesla multiplier | ||
# pylint: enable=bad-whitespace | ||
|
||
|
||
class LSM303(object): | ||
"""Driver for the LSM303 accelerometer/magnetometer.""" | ||
|
@@ -128,122 +130,111 @@ class LSM303(object): | |
def __init__(self, i2c): | ||
self._accel_device = I2CDevice(i2c, _ADDRESS_ACCEL) | ||
self._mag_device = I2CDevice(i2c, _ADDRESS_MAG) | ||
self._write_u8(self._accel_device, _REG_ACCEL_CTRL_REG1_A, 0x27) # Enable the accelerometer | ||
self._write_u8(self._mag_device, _REG_MAG_MR_REG_M, 0x00) # Enable the magnetometer | ||
self._write_u8(self._accel_device, _REG_ACCEL_CTRL_REG1_A, 0x27) # Enable the accelerometer | ||
self._write_u8(self._mag_device, _REG_MAG_MR_REG_M, 0x00) # Enable the magnetometer | ||
self._lsm303mag_gauss_lsb_xy = 1100.0 | ||
self._lsm303mag_gauss_lsb_z = 980.0 | ||
self._mag_gain = MAGGAIN_1_3 | ||
self._mag_rate = MAGRATE_0_7 | ||
|
||
@property | ||
def raw_accelerometer(self): | ||
def raw_acceleration(self): | ||
"""The raw accelerometer sensor values. | ||
A 3-tuple of X, Y, Z axis values that are 16-bit signed integers. | ||
""" | ||
self._read_bytes(self._accel_device, _REG_ACCEL_OUT_X_L_A | 0x80, 6, self._BUFFER) | ||
return struct.unpack_from('<hhh', self._BUFFER[0:6]) | ||
|
||
|
||
@property | ||
def accelerometer(self): | ||
def acceleration(self): | ||
"""The processed accelerometer sensor values. | ||
A 3-tuple of X, Y, Z axis values in meters per second squared that are signed floats. | ||
""" | ||
raw_accel_data = self.raw_accelerometer | ||
return [n / _LSM303ACCEL_MG_LSB * _GRAVITY_STANDARD for n in raw_accel_data] | ||
|
||
raw_accel_data = self.raw_acceleration | ||
return tuple([n / _LSM303ACCEL_MG_LSB * _GRAVITY_STANDARD for n in raw_accel_data]) | ||
|
||
@property | ||
def raw_magnetometer(self): | ||
def raw_magnetic(self): | ||
"""The raw magnetometer sensor values. | ||
A 3-tuple of X, Y, Z axis values that are 16-bit signed integers. | ||
""" | ||
self._read_bytes(self._mag_device, _REG_MAG_OUT_X_H_M, 6, self._BUFFER) | ||
raw_values = struct.unpack_from('>hhh', self._BUFFER[0:6]) | ||
return [n >> 4 for n in raw_values] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to return a tuple also for consistency. |
||
|
||
|
||
@property | ||
def magnetometer(self): | ||
def magnetic(self): | ||
"""The processed magnetometer sensor values. | ||
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats. | ||
""" | ||
mag_x, mag_y, mag_z = self.raw_magnetometer | ||
mag_x, mag_y, mag_z = self.raw_magnetic | ||
return (mag_x / self._lsm303mag_gauss_lsb_xy * _GAUSS_TO_MICROTESLA, | ||
mag_y / self._lsm303mag_gauss_lsb_xy * _GAUSS_TO_MICROTESLA, | ||
mag_z / self._lsm303mag_gauss_lsb_z * _GAUSS_TO_MICROTESLA) | ||
|
||
|
||
@property | ||
def mag_gain(self): | ||
"""The magnetometer's gain.""" | ||
return self._mag_gain | ||
|
||
|
||
@mag_gain.setter | ||
def mag_gain(self, value): | ||
# pylint: disable=line-too-long | ||
assert value in (MAGGAIN_1_3, MAGGAIN_1_9, MAGGAIN_2_5, MAGGAIN_4_0, MAGGAIN_4_7, MAGGAIN_5_6, MAGGAIN_8_1) | ||
# pylint: enable=line-too-long | ||
assert value in (MAGGAIN_1_3, MAGGAIN_1_9, MAGGAIN_2_5, MAGGAIN_4_0, MAGGAIN_4_7, | ||
MAGGAIN_5_6, MAGGAIN_8_1) | ||
|
||
self._mag_gain = value | ||
self._write_u8(self._mag_device, _REG_MAG_CRB_REG_M, self._mag_gain) | ||
if self._mag_gain == MAGGAIN_1_3: | ||
self._lsm303mag_gauss_lsb_xy = 1100.0 | ||
self._lsm303mag_gauss_lsb_z = 980.0 | ||
self._lsm303mag_gauss_lsb_z = 980.0 | ||
elif self._mag_gain == MAGGAIN_1_9: | ||
self._lsm303mag_gauss_lsb_xy = 855.0 | ||
self._lsm303mag_gauss_lsb_z = 760.0 | ||
self._lsm303mag_gauss_lsb_z = 760.0 | ||
elif self._mag_gain == MAGGAIN_2_5: | ||
self._lsm303mag_gauss_lsb_xy = 670.0 | ||
self._lsm303mag_gauss_lsb_z = 600.0 | ||
self._lsm303mag_gauss_lsb_z = 600.0 | ||
elif self._mag_gain == MAGGAIN_4_0: | ||
self._lsm303mag_gauss_lsb_xy = 450.0 | ||
self._lsm303mag_gauss_lsb_z = 400.0 | ||
self._lsm303mag_gauss_lsb_z = 400.0 | ||
elif self._mag_gain == MAGGAIN_4_7: | ||
self._lsm303mag_gauss_lsb_xy = 400.0 | ||
self._lsm303mag_gauss_lsb_z = 355.0 | ||
self._lsm303mag_gauss_lsb_z = 355.0 | ||
elif self._mag_gain == MAGGAIN_5_6: | ||
self._lsm303mag_gauss_lsb_xy = 330.0 | ||
self._lsm303mag_gauss_lsb_z = 295.0 | ||
self._lsm303mag_gauss_lsb_z = 295.0 | ||
elif self._mag_gain == MAGGAIN_8_1: | ||
self._lsm303mag_gauss_lsb_xy = 230.0 | ||
self._lsm303mag_gauss_lsb_z = 205.0 | ||
|
||
self._lsm303mag_gauss_lsb_z = 205.0 | ||
|
||
@property | ||
def mag_rate(self): | ||
"""The magnetometer update rate.""" | ||
return self._mag_rate | ||
|
||
|
||
@mag_rate.setter | ||
def mag_rate(self, value): | ||
# pylint: disable=line-too-long | ||
assert value in (MAGRATE_0_7, MAGRATE_1_5, MAGRATE_3_0, MAGRATE_7_5, MAGRATE_15, MAGRATE_30, MAGRATE_75, MAGRATE_220) | ||
# pylint: enable=line-too-long | ||
assert value in (MAGRATE_0_7, MAGRATE_1_5, MAGRATE_3_0, MAGRATE_7_5, MAGRATE_15, MAGRATE_30, | ||
MAGRATE_75, MAGRATE_220) | ||
|
||
self._mag_rate = value | ||
reg_m = ((value & 0x07) << 2) & 0xFF | ||
self._write_u8(self._mag_device, _REG_MAG_CRA_REG_M, reg_m) | ||
|
||
|
||
def _read_u8(self, device, address): | ||
with device as i2c: | ||
self._BUFFER[0] = address & 0xFF | ||
i2c.write(self._BUFFER, end=1, stop=False) | ||
i2c.readinto(self._BUFFER, end=1) | ||
return self._BUFFER[0] | ||
|
||
|
||
def _write_u8(self, device, address, val): | ||
with device as i2c: | ||
self._BUFFER[0] = address & 0xFF | ||
self._BUFFER[1] = val & 0xFF | ||
i2c.write(self._BUFFER, end=2) | ||
|
||
# pylint: disable=no-self-use | ||
def _read_bytes(self, device, address, count, buf): | ||
@staticmethod | ||
def _read_bytes(device, address, count, buf): | ||
with device as i2c: | ||
buf[0] = address & 0xFF | ||
i2c.write(buf, end=1, stop=False) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this meant to be left in? (surprised that's not a default check)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's meant to be there, it's needed because of the style choice to have all the registers line up, however, at the start it was disabled for the entire file which isn't consistent with the way we've been doing it, so I resolved the white space issues present in the rest of the file and enabled pylint again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the things we do to keep the lint monster happy