first commit
This commit is contained in:
commit
a5a0434432
1126 changed files with 439481 additions and 0 deletions
26
Software/Python/grove_accelerometer_16g/LICENSE.txt
Normal file
26
Software/Python/grove_accelerometer_16g/LICENSE.txt
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Copyright (c) 2013, Pimoroni Ltd
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
- Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
- Neither the name of the copyright holders nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
24
Software/Python/grove_accelerometer_16g/README.md
Normal file
24
Software/Python/grove_accelerometer_16g/README.md
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
adxl345-python
|
||||
==============
|
||||
|
||||
Raspberry Pi Python i2c library for the ADXL3453-axis MEMS accelerometer IC which is used in breakout boards like the Adafruit ADXL345 Triple-Axis Accelerometer (http://www.adafruit.com/product/163).
|
||||
|
||||
This library is a basic implementation of the i2c protocol for the IC offering a simple way to get started with it on the Raspberry Pi.
|
||||
|
||||
You can import the module and get a sensor reading like this:
|
||||
|
||||
from adxl345 import ADXL345
|
||||
|
||||
adxl345 = ADXL345()
|
||||
|
||||
axes = adxl345.getAxes(True)
|
||||
print "ADXL345 on address 0x%x:" % (adxl345.address)
|
||||
print " x = %.3fG" % ( axes['x'] )
|
||||
print " y = %.3fG" % ( axes['y'] )
|
||||
print " z = %.3fG" % ( axes['z'] )
|
||||
|
||||
or you can run it directly from the command line like this:
|
||||
|
||||
sudo python ADXL345.py
|
||||
|
||||
which will output the current x, y, and z axis readings in Gs.
|
||||
112
Software/Python/grove_accelerometer_16g/adxl345.py
Normal file
112
Software/Python/grove_accelerometer_16g/adxl345.py
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
# ADXL345 Python library for Raspberry Pi
|
||||
#
|
||||
# author: Jonathan Williamson
|
||||
# license: BSD, see LICENSE.txt included in this package
|
||||
#
|
||||
# This is a Raspberry Pi Python implementation to help you get started with
|
||||
# the Adafruit Triple Axis ADXL345 breakout board:
|
||||
# http://shop.pimoroni.com/products/adafruit-triple-axis-accelerometer
|
||||
|
||||
import smbus
|
||||
from time import sleep
|
||||
|
||||
# select the correct i2c bus for this revision of Raspberry Pi
|
||||
with open('/proc/cpuinfo', 'r') as file:
|
||||
revision = ([l[12:-1] for l in file.readlines() if l[:8]=="Revision"]+['0000'])[0]
|
||||
bus = smbus.SMBus(1 if int(revision, 16) >= 4 else 0)
|
||||
|
||||
# ADXL345 constants
|
||||
EARTH_GRAVITY_MS2 = 9.80665
|
||||
SCALE_MULTIPLIER = 0.004
|
||||
|
||||
DATA_FORMAT = 0x31
|
||||
BW_RATE = 0x2C
|
||||
POWER_CTL = 0x2D
|
||||
|
||||
BW_RATE_1600HZ = 0x0F
|
||||
BW_RATE_800HZ = 0x0E
|
||||
BW_RATE_400HZ = 0x0D
|
||||
BW_RATE_200HZ = 0x0C
|
||||
BW_RATE_100HZ = 0x0B
|
||||
BW_RATE_50HZ = 0x0A
|
||||
BW_RATE_25HZ = 0x09
|
||||
|
||||
RANGE_2G = 0x00
|
||||
RANGE_4G = 0x01
|
||||
RANGE_8G = 0x02
|
||||
RANGE_16G = 0x03
|
||||
|
||||
MEASURE = 0x08
|
||||
AXES_DATA = 0x32
|
||||
|
||||
class ADXL345:
|
||||
|
||||
address = None
|
||||
|
||||
def __init__(self, address = 0x53):
|
||||
self.address = address
|
||||
self.setBandwidthRate(BW_RATE_100HZ)
|
||||
self.setRange(RANGE_2G)
|
||||
self.enableMeasurement()
|
||||
|
||||
def enableMeasurement(self):
|
||||
bus.write_byte_data(self.address, POWER_CTL, MEASURE)
|
||||
|
||||
def setBandwidthRate(self, rate_flag):
|
||||
bus.write_byte_data(self.address, BW_RATE, rate_flag)
|
||||
|
||||
# set the measurement range for 10-bit readings
|
||||
def setRange(self, range_flag):
|
||||
value = bus.read_byte_data(self.address, DATA_FORMAT)
|
||||
|
||||
value &= ~0x0F;
|
||||
value |= range_flag;
|
||||
value |= 0x08;
|
||||
|
||||
bus.write_byte_data(self.address, DATA_FORMAT, value)
|
||||
|
||||
# returns the current reading from the sensor for each axis
|
||||
#
|
||||
# parameter gforce:
|
||||
# False (default): result is returned in m/s^2
|
||||
# True : result is returned in gs
|
||||
def getAxes(self, gforce = False):
|
||||
bytes = bus.read_i2c_block_data(self.address, AXES_DATA, 6)
|
||||
|
||||
x = bytes[0] | (bytes[1] << 8)
|
||||
if(x & (1 << 16 - 1)):
|
||||
x = x - (1<<16)
|
||||
|
||||
y = bytes[2] | (bytes[3] << 8)
|
||||
if(y & (1 << 16 - 1)):
|
||||
y = y - (1<<16)
|
||||
|
||||
z = bytes[4] | (bytes[5] << 8)
|
||||
if(z & (1 << 16 - 1)):
|
||||
z = z - (1<<16)
|
||||
|
||||
x = x * SCALE_MULTIPLIER
|
||||
y = y * SCALE_MULTIPLIER
|
||||
z = z * SCALE_MULTIPLIER
|
||||
|
||||
if gforce == False:
|
||||
x = x * EARTH_GRAVITY_MS2
|
||||
y = y * EARTH_GRAVITY_MS2
|
||||
z = z * EARTH_GRAVITY_MS2
|
||||
|
||||
x = round(x, 4)
|
||||
y = round(y, 4)
|
||||
z = round(z, 4)
|
||||
|
||||
return {"x": x, "y": y, "z": z}
|
||||
|
||||
if __name__ == "__main__":
|
||||
# if run directly we'll just create an instance of the class and output
|
||||
# the current readings
|
||||
adxl345 = ADXL345()
|
||||
|
||||
axes = adxl345.getAxes(True)
|
||||
print("ADXL345 on address 0x%x:" % (adxl345.address))
|
||||
print(" x = %.3fG" % ( axes['x'] ))
|
||||
print(" y = %.3fG" % ( axes['y'] ))
|
||||
print(" z = %.3fG" % ( axes['z'] ))
|
||||
19
Software/Python/grove_accelerometer_16g/basic_example.py
Normal file
19
Software/Python/grove_accelerometer_16g/basic_example.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# ADXL345 Python example
|
||||
#
|
||||
# author: Jonathan Williamson
|
||||
# license: BSD, see LICENSE.txt included in this package
|
||||
#
|
||||
# This is an example to show you how to the Grove +-16g Accelerometer
|
||||
# http://www.seeedstudio.com/depot/Grove-3Axis-Digital-Accelerometer16g-p-1156.html
|
||||
|
||||
from adxl345 import ADXL345
|
||||
import time
|
||||
|
||||
adxl345 = ADXL345()
|
||||
|
||||
print("ADXL345 on address 0x%x:" % (adxl345.address))
|
||||
while True:
|
||||
axes = adxl345.getAxes(True)
|
||||
print(( axes['x'] ),"\t",( axes['y'] ),"\t",( axes['z'] ))
|
||||
time.sleep(.1)
|
||||
|
||||
17
Software/Python/grove_accelerometer_16g/example.py
Normal file
17
Software/Python/grove_accelerometer_16g/example.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# ADXL345 Python example
|
||||
#
|
||||
# author: Jonathan Williamson
|
||||
# license: BSD, see LICENSE.txt included in this package
|
||||
#
|
||||
# This is an example to show you how to the Grove +-16g Accelerometer
|
||||
# http://www.seeedstudio.com/depot/Grove-3Axis-Digital-Accelerometer16g-p-1156.html
|
||||
|
||||
from adxl345 import ADXL345
|
||||
|
||||
adxl345 = ADXL345()
|
||||
|
||||
axes = adxl345.getAxes(True)
|
||||
print("ADXL345 on address 0x%x:" % (adxl345.address))
|
||||
print(" x = %.3fG" % ( axes['x'] ))
|
||||
print(" y = %.3fG" % ( axes['y'] ))
|
||||
print(" z = %.3fG" % ( axes['z'] ))
|
||||
Loading…
Add table
Add a link
Reference in a new issue