Package: sensational
SensationalSensational is a library for interfacing with various sensors.The intention behind this library is:To make connecting to and interacting with sensor chips a breezeTo be readily expandable while keeping things sensible and tidyExample#!/usr/bin/env python3fromsmbus2importSMBusfromsensational.sensorsimportMPU9250bus=SMBus(1)sensor=MPU9250(bus)sensor.update()print(sensor.data)AuthorDaniel 'Vector' Kerrvector@vector.id.auLicenceMIT. See theLICENSEfile for details.PerceptionsPerception classes are models used to capture something that can be perceived. Examples include:AccelerationDepthMagnetismTemperature...These classes are mostly used to provide a consistent interface that can be interrogated withisinstance()calls and such. They're not essential but they add a bit of structure.UnitsSensational makes use of typed units to allow for better interoperability
between readings from different sensors, and also to introduce some basic
sanity checks when performing operations on perceived values.This library usespintfor unit definitions by default.#!/usr/bin/env python3fromsensational.unitsimportu# Define some values with unitsthree_meters=u(3,u.meter)one_hour=u(1,u.hour)thirty_minutes=u(30,u.minute)twenty_five_degrees=u(25.0,u.celsius)# Add 30 minutes to 1 hourone_and_a_half_hours=one_hour+thirty_minutesprint(one_and_a_half_hours)# Add temperature to distance (what?!)try:this_will_fail=twenty_five_degrees+three_metersexcept:print("You can't add temperature to distance, silly!")# Compound units - accelerationgravity_earth=u(9.8,u.meter/u.second/u.second)print("Earth's gravity is",gravity_earth)# Compound Units - velocitymeters_per_hour=three_meters/one_and_a_half_hoursprint("Distance over time is",meters_per_hour)Custom Units ProviderIf you would like to use a different units provider (for example,units,numericalunits,astropy.units,scimath.units,
...), feel free to create a factory for it. Create and populate a subclass ofsensational.units.UnitsFactory, and then assign it as the default
provider by callingu.SetFactory(instance).#!/usr/bin/env python3fromsensational.unitsimportufromsensational.unitsimportUnitsFactoryclassCustomUnitsFactory(UnitsFactory):# ...custom_units_factory=CustomUnitsFactory()u.SetFactory(custom_units_factory)Units with Custom SensorsIf you decide to create your own sensor (yay!), be sure to initialise all
unit-based values withu(quantity, unit). This is to cater for differences
in implementations across unit providers, as some (coughpintcough) don't
behave very well when dealing with units that aren't plain multiplicative scales
of each other (e.g., ºK, ºC and ºF, because they don't share the same zero point or scale).If you don't do this, you or the user of your sensor might run into trouble.See the source code for the existing sensors for examples of initializing values
returned from sensor readings if you're not sure!i2c SensorsThe following I2C sensors are supported by this library.mpu9250SparkFun IMU Breakout (View Product Page)#!/usr/bin/env python3fromsensational.sensorsimportMPU9250fromsmbus2importSMBusfromtimeimportsleepdefmain():bus=SMBus(1)imu=MPU9250(bus)whileTrue:imu.update()print(imu.linear_acceleration,imu.angular_acceleration,imu.magnetism,imu.temperature,)sleep(0.100)if__name__=='__main__':main()ak89633-Axis Magnetometer (View Datasheet)#!/usr/bin/env python3fromsensational.sensorsimportMPU9250fromsmbus2importSMBusfromtimeimportsleepdefmain():bus=SMBus(1)magnetometer=AK8963(bus)whileTrue:magnetometer.update()print(magnetometer.magnetism)sleep(0.100)if__name__=='__main__':main()