-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New sensor MPU6050 #421
Closed
Closed
New sensor MPU6050 #421
Changes from all commits
Commits
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 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 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 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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
""" | ||
MPU-6050 Gyroscope / Accelerometer Sensor | ||
|
||
Mandatory: | ||
- chip_addr | ||
|
||
Output: | ||
- x (in m*s²) | ||
- y (in m*s²) | ||
- z (in m*s²) | ||
""" | ||
|
||
from json import dumps | ||
from typing import cast | ||
import smbus | ||
import math | ||
import json | ||
|
||
from ...types import CerberusSchemaType, ConfigType, SensorValueType | ||
from . import GenericSensor | ||
from ...exceptions import RuntimeConfigError | ||
import logging | ||
|
||
_LOG = logging.getLogger(__name__) | ||
|
||
REQUIREMENTS = ("mpu6050",) | ||
CONFIG_SCHEMA: CerberusSchemaType = { | ||
"chip_addr": {"type": 'integer', "required": True, "empty": False}, | ||
} | ||
|
||
# MPU6050 Registers | ||
|
||
MPU6050_PWR_MGMT_1 = 0x6B | ||
MPU6050_TEMP_OUT_H = 0x41 | ||
MPU6050_ACCEL_XOUT_H = 0x3B | ||
MPU6050_ACCEL_YOUT_H = 0x3D | ||
MPU6050_ACCEL_ZOUT_H = 0x3F | ||
MPU6050_GYRO_XOUT_H = 0x43 | ||
MPU6050_GYRO_YOUT_H = 0x45 | ||
MPU6050_GYRO_ZOUT_H = 0x47 | ||
|
||
# ============================================================================= | ||
|
||
class Sensor(GenericSensor): | ||
""" | ||
Implementation of Sensor class for the MPU6050 sensor. | ||
""" | ||
|
||
SENSOR_SCHEMA: CerberusSchemaType = { | ||
"type": { | ||
"type": 'list', | ||
"required": False, | ||
"empty": False, | ||
"default": ['gyro', "angle"], | ||
"allowed": ['gyro', 'angle', 'accel', 'temp', 'all'], | ||
}, | ||
} | ||
|
||
def setup_module(self) -> None: | ||
self.i2c_addr: int = self.config["chip_addr"] | ||
self.sensor = smbus.SMBus(1) # or 0 for RPi 1 | ||
self.sensor.write_byte_data(self.i2c_addr, MPU6050_PWR_MGMT_1, 0) | ||
|
||
def get_value(self, sens_conf: ConfigType) -> SensorValueType: | ||
sens_type = sens_conf["type"] | ||
|
||
# Read raw data from the sensor | ||
def read_raw_data(addr): | ||
# Read raw data in a single transaction | ||
high = self.sensor.read_i2c_block_data(self.i2c_addr, addr, 2) | ||
value = (high[0] << 8) | high[1] | ||
if value > 32768: | ||
value -= 65536 | ||
return value | ||
|
||
# Read sensor data | ||
accel_x, accel_y, accel_z = [read_raw_data(addr) for addr in (MPU6050_ACCEL_XOUT_H, MPU6050_ACCEL_YOUT_H, MPU6050_ACCEL_ZOUT_H)] | ||
gyro_x, gyro_y, gyro_z = [read_raw_data(addr) for addr in (MPU6050_GYRO_XOUT_H, MPU6050_GYRO_YOUT_H, MPU6050_GYRO_ZOUT_H)] | ||
temp = read_raw_data(MPU6050_TEMP_OUT_H) | ||
|
||
# Calculate angles | ||
x_angle = math.atan(accel_x / 16384.0) * (180 / math.pi) | ||
y_angle = math.atan(accel_y / 16384.0) * (180 / math.pi) | ||
|
||
# Prepare data dictionary | ||
data_gyro = { | ||
"gyro_x": gyro_x, | ||
"gyro_y": gyro_y, | ||
"gyro_z": gyro_z, | ||
} | ||
|
||
data_angle = { | ||
"angle_x": x_angle, | ||
"angle_y": y_angle, | ||
} | ||
|
||
data_accel= { | ||
"accel_x": accel_x, | ||
"accel_y": accel_y, | ||
"accel_z": accel_z, | ||
} | ||
|
||
data_temp= { | ||
"temp": (temp / 340.0) + 36.53 # Temperature formula for MPU6050 | ||
} | ||
|
||
data = {} | ||
if "gyro" in sens_type or "all" in sens_type: | ||
data.update(data_gyro) | ||
if "angle" in sens_type or "all" in sens_type: | ||
data.update(data_angle) | ||
if "accel" in sens_type or "all" in sens_type: | ||
data.update(data_accel) | ||
if "temp" in sens_type or "all" in sens_type: | ||
data.update(data_temp) | ||
|
||
return data |
This file contains 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
Oops, something went wrong.
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.
the smbus module should be moved to the setup_modul method and in the REQUIREMENTS, so it is only required, when this module is really used. (see e.g. lm75 sensor)