Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit 22e29ec

Browse files
jnuryTylerLeonhardt
authored andcommitted
Adding an ADXL345 example (#39)
* Adding ADXL345 example * Fix documentation * Add copyright to psd1 * Add copyright to psm1 * Update examples table * Allow call to Get-ADXL345Data without device * Better error handling/logging
1 parent 2479eef commit 22e29ec

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
@{
5+
6+
# Root module file
7+
RootModule = 'Microsoft.PowerShell.IoT.ADXL345.psm1'
8+
9+
# Version number of this module.
10+
ModuleVersion = '0.1.0'
11+
12+
# ID used to uniquely identify this module
13+
GUID = '78f2d4bb-195e-4143-8f54-0a6d8c68612e'
14+
15+
# Author of this module
16+
Author = 'Julien Nury'
17+
18+
# Description of the functionality provided by this module
19+
Description = 'A set of functions to interact with ADXL345s accelerometer thru I2C'
20+
21+
# Modules that must be imported into the global environment prior to importing this module
22+
RequiredModules = @('Microsoft.PowerShell.IoT')
23+
24+
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
25+
FunctionsToExport = 'Get-ADXL345Device', 'Get-ADXL345Data'
26+
27+
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
28+
CmdletsToExport = @()
29+
30+
# Variables to export from this module
31+
VariablesToExport = @()
32+
33+
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
34+
AliasesToExport = @()
35+
36+
}
37+
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
# Inspired from https://github.com/sparkfun/SparkFun_ADXL345_Arduino_Library
5+
6+
# Device registers and parameters
7+
8+
$script:ADXL345_ADDRESS = 0x53 # I2c address
9+
$script:ADXL345_POWER_CTL = 0x2D # Power-Saving Features Control
10+
$script:ADXL345_DATAX0 = 0x32 # X-Axis Data 0
11+
$script:ADXL345_DATAX1 = 0x33 # X-Axis Data 1
12+
$script:ADXL345_DATAY0 = 0x34 # Y-Axis Data 0
13+
$script:ADXL345_DATAY1 = 0x35 # Y-Axis Data 1
14+
$script:ADXL345_DATAZ0 = 0x36 # Z-Axis Data 0
15+
$script:ADXL345_DATAZ1 = 0x37 # Z-Axis Data 1
16+
$script:ADXL345_GAINX = 0.00376390 # Gain to convert X value in g
17+
$script:ADXL345_GAINY = 0.00376009 # Gain to convert Y value in g
18+
$script:ADXL345_GAINZ = 0.00349265 # Gain to convert Z value in g
19+
20+
# Published functions
21+
function Get-ADXL345Device {
22+
[CmdletBinding()]
23+
param
24+
(
25+
[ValidateNotNullOrEmpty()]
26+
[int]
27+
$Id = $script:ADXL345_ADDRESS,
28+
29+
[ValidateNotNullOrEmpty()]
30+
[string]
31+
$FriendlyName = "ADXL345"
32+
)
33+
34+
$Device = Get-I2CDevice -Id $Id -FriendlyName $FriendlyName
35+
InitializeDevice -Device $Device
36+
return $Device
37+
}
38+
39+
function Get-ADXL345Data {
40+
param (
41+
[Parameter(ValueFromPipeline)]
42+
[ValidateNotNullOrEmpty()]
43+
[Microsoft.PowerShell.IoT.I2CDevice]
44+
$Device = (Get-ADXL345Device),
45+
46+
[Parameter()]
47+
[Double]
48+
$Limit,
49+
50+
[Switch]
51+
$Raw
52+
)
53+
54+
try {
55+
$xValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAX0 -ByteCount 1
56+
$xValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAX1 -ByteCount 1
57+
$xValue = [int16]($xValue1.Data[0]) -shl 8 -bor [int16]($xValue0.Data[0])
58+
59+
$yValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAY0 -ByteCount 1
60+
$yValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAY1 -ByteCount 1
61+
$yValue = [int16]($yValue1.Data[0]) -shl 8 -bor [int16]($yValue0.Data[0])
62+
63+
$zValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAZ0 -ByteCount 1
64+
$zValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAZ1 -ByteCount 1
65+
$zValue = [int16]($zValue1.Data[0]) -shl 8 -bor [int16]($zValue0.Data[0])
66+
}
67+
catch {
68+
Throw "Unable to retreive data from device '$($Device.FriendlyName)'. Message: $($_.Exception.Message)"
69+
}
70+
71+
if (-not $Raw) {
72+
$xValue = $xValue * $script:ADXL345_GAINX
73+
$yValue = $yValue * $script:ADXL345_GAINY
74+
$zValue = $zValue * $script:ADXL345_GAINZ
75+
}
76+
77+
if ($Limit) {
78+
$xValue = FilterValue -Value $xValue -Maximum $Limit
79+
$yValue = FilterValue -Value $yValue -Maximum $Limit
80+
$zValue = FilterValue -Value $zValue -Maximum $Limit
81+
}
82+
83+
return [PSObject]@{x = $xValue; y = $yValue; z = $zvalue}
84+
}
85+
86+
# Internal functions
87+
function InitializeDevice {
88+
Param (
89+
[Parameter(Mandatory)]
90+
[Microsoft.PowerShell.IoT.I2CDevice]
91+
$Device
92+
)
93+
94+
try {
95+
Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x00
96+
Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x10
97+
Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x08
98+
}
99+
catch {
100+
Throw "Unable to initialize device '$($Device.FriendlyName)'. Message: $($_.Exception.Message)"
101+
}
102+
}
103+
104+
function FilterValue {
105+
param (
106+
[Parameter(Mandatory)]
107+
[Double]
108+
$Value,
109+
110+
[Parameter(Mandatory)]
111+
[Double]
112+
$Maximum
113+
)
114+
115+
if ($Value -ge 0) {
116+
return [Math]::Min($value, $Maximum)
117+
} else {
118+
return [Math]::Max($Value, -$Maximum)
119+
}
120+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Example module Microsoft.PowerShell.IoT.ADXL345
2+
3+
This PowerShell module is to interact with [ADXL345 accelerometer](http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf) for reading acceleration on 3 axis.
4+
5+
## Hardware setup
6+
7+
Several vendors have breakout boards with ADXL345 sensor. In this example we'll use [SparkFun Triple Axis Accelerometer Breakout](https://www.sparkfun.com/products/9836).
8+
9+
ADXL345 sensor supports both I2C and SPI interfaces; here we'll use I2C.
10+
11+
Wiring diagram with Raspberry Pi 3 looks like this:
12+
13+
![ADXL345_Wiring](https://user-images.githubusercontent.com/9315492/39673576-40f7e8b4-513f-11e8-8b69-314237f99bd1.png)
14+
15+
## Software setup
16+
17+
### Install PowerShell Core on Raspberry Pi
18+
19+
Installation instructions can be found [here](https://github.com/PowerShell/PowerShell/tree/master/docs/installation/linux.md#raspbian).
20+
21+
### Enable I2C interface on Raspberry Pi
22+
23+
1. `sudo raspi-config`
24+
2. `5 Interfacing options`
25+
3. `P5 I2C`
26+
4. `Would you like ARM I2C interface to be enabled -> Yes`
27+
28+
### Start Powershell and install modules
29+
30+
**Don't forget to start PowerShell with sudo** or you'll be unable to access I2C bus.
31+
32+
```powershell
33+
sudo pwsh
34+
Install-Module -Name Microsoft.PowerShell.IoT
35+
git clone https://github.com/PowerShell/PowerShell-IoT.git
36+
Import-Module ./PowerShell-IoT/Examples/Microsoft.PowerShell.IoT.ADXL345
37+
```
38+
39+
### Collect Data
40+
41+
To simply collect acceleration values in g:
42+
43+
```powershell
44+
PS /home/pi> $accelerometer = Get-ADXL345Device
45+
PS /home/pi> Get-ADXL345Data -Device $accelerometer
46+
47+
Name Value
48+
---- -----
49+
y -0.03008072
50+
x 0.0828058
51+
z 0.86966985
52+
```
53+
54+
To represent current acceleration on the 3 axis with bargraphs:
55+
56+
```powershell
57+
PS /home/pi> $accelerometer = Get-ADXL345Device
58+
PS /home/pi> while ($true) {
59+
$data = Get-ADXL345Data -Device $accelerometer -Limit 1
60+
Write-Progress -id 1 -Activity 'X axis' -Status 'Acceleration' -PercentComplete ($data.x * 50 + 50)
61+
Write-Progress -id 2 -Activity 'Y axis' -Status 'Acceleration' -PercentComplete ($data.y * 50 + 50)
62+
Write-Progress -id 3 -Activity 'Z axis' -Status 'Acceleration' -PercentComplete ($data.z * 50 + 50)
63+
}
64+
65+
X axis
66+
Acceleration
67+
[ooooooooooooooooooooooooooooooooooooooooooooooooooo ]
68+
Y axis
69+
Acceleration
70+
[ooooooooooooooooooooooooooooooooooooooooooooooooo ]
71+
Z axis
72+
Acceleration
73+
[ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ]
74+
```

Examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Example modules that leverage PowerShell IoT in a specific way.
99
| [Microsoft.PowerShell.IoT.LED](/Examples/Microsoft.PowerShell.IoT.LED) | Easy | GPIO |
1010
| [Microsoft.PowerShell.IoT.BME280](/Examples/Microsoft.PowerShell.IoT.BME280) | Medium | I2C |
1111
| [Microsoft.PowerShell.IoT.SSD1306](/Examples/Microsoft.PowerShell.IoT.SSD1306) | Medium | I2C |
12+
| [Microsoft.PowerShell.IoT.ADXL345](/Examples/Microsoft.PowerShell.IoT.ADXL345) | Medium | I2C |
1213
| [Microsoft.PowerShell.Plant](/Examples/Microsoft.PowerShell.IoT.Plant) | Hard | GPIO |

0 commit comments

Comments
 (0)