This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Adding an ADXL345 example #39
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
97a5e73
Adding ADXL345 example
e1aa8f8
Fix documentation
217fbfe
Add copyright to psd1
TylerLeonhardt ead7bf1
Add copyright to psm1
TylerLeonhardt 8e410a1
Update examples table
0d53864
Allow call to Get-ADXL345Data without device
ce69655
Better error handling/logging
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
37 changes: 37 additions & 0 deletions
37
Examples/Microsoft.PowerShell.IoT.ADXL345/Microsoft.PowerShell.IoT.ADXL345.psd1
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
@{ | ||
|
||
# Root module file | ||
RootModule = 'Microsoft.PowerShell.IoT.ADXL345.psm1' | ||
|
||
# Version number of this module. | ||
ModuleVersion = '0.1.0' | ||
|
||
# ID used to uniquely identify this module | ||
GUID = '78f2d4bb-195e-4143-8f54-0a6d8c68612e' | ||
|
||
# Author of this module | ||
Author = 'Julien Nury' | ||
|
||
# Description of the functionality provided by this module | ||
Description = 'A set of functions to interact with ADXL345s accelerometer thru I2C' | ||
|
||
# Modules that must be imported into the global environment prior to importing this module | ||
RequiredModules = @('Microsoft.PowerShell.IoT') | ||
|
||
# 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. | ||
FunctionsToExport = 'Get-ADXL345Device', 'Get-ADXL345Data' | ||
|
||
# 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. | ||
CmdletsToExport = @() | ||
|
||
# Variables to export from this module | ||
VariablesToExport = @() | ||
|
||
# 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. | ||
AliasesToExport = @() | ||
|
||
} | ||
|
120 changes: 120 additions & 0 deletions
120
Examples/Microsoft.PowerShell.IoT.ADXL345/Microsoft.PowerShell.IoT.ADXL345.psm1
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
# Inspired from https://github.com/sparkfun/SparkFun_ADXL345_Arduino_Library | ||
|
||
# Device registers and parameters | ||
|
||
$script:ADXL345_ADDRESS = 0x53 # I2c address | ||
$script:ADXL345_POWER_CTL = 0x2D # Power-Saving Features Control | ||
$script:ADXL345_DATAX0 = 0x32 # X-Axis Data 0 | ||
$script:ADXL345_DATAX1 = 0x33 # X-Axis Data 1 | ||
$script:ADXL345_DATAY0 = 0x34 # Y-Axis Data 0 | ||
$script:ADXL345_DATAY1 = 0x35 # Y-Axis Data 1 | ||
$script:ADXL345_DATAZ0 = 0x36 # Z-Axis Data 0 | ||
$script:ADXL345_DATAZ1 = 0x37 # Z-Axis Data 1 | ||
$script:ADXL345_GAINX = 0.00376390 # Gain to convert X value in g | ||
$script:ADXL345_GAINY = 0.00376009 # Gain to convert Y value in g | ||
$script:ADXL345_GAINZ = 0.00349265 # Gain to convert Z value in g | ||
|
||
# Published functions | ||
function Get-ADXL345Device { | ||
[CmdletBinding()] | ||
param | ||
( | ||
[ValidateNotNullOrEmpty()] | ||
[int] | ||
$Id = $script:ADXL345_ADDRESS, | ||
|
||
[ValidateNotNullOrEmpty()] | ||
[string] | ||
$FriendlyName = "ADXL345" | ||
) | ||
|
||
$Device = Get-I2CDevice -Id $Id -FriendlyName $FriendlyName | ||
InitializeDevice -Device $Device | ||
return $Device | ||
} | ||
|
||
function Get-ADXL345Data { | ||
param ( | ||
[Parameter(ValueFromPipeline)] | ||
[ValidateNotNullOrEmpty()] | ||
[Microsoft.PowerShell.IoT.I2CDevice] | ||
$Device = (Get-ADXL345Device), | ||
|
||
[Parameter()] | ||
[Double] | ||
$Limit, | ||
|
||
[Switch] | ||
$Raw | ||
) | ||
|
||
try { | ||
$xValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAX0 -ByteCount 1 | ||
$xValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAX1 -ByteCount 1 | ||
$xValue = [int16]($xValue1.Data[0]) -shl 8 -bor [int16]($xValue0.Data[0]) | ||
|
||
$yValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAY0 -ByteCount 1 | ||
$yValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAY1 -ByteCount 1 | ||
$yValue = [int16]($yValue1.Data[0]) -shl 8 -bor [int16]($yValue0.Data[0]) | ||
|
||
$zValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAZ0 -ByteCount 1 | ||
$zValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAZ1 -ByteCount 1 | ||
$zValue = [int16]($zValue1.Data[0]) -shl 8 -bor [int16]($zValue0.Data[0]) | ||
} | ||
catch { | ||
Throw "Unable to retreive data from device '$($Device.FriendlyName)'. Message: $($_.Exception.Message)" | ||
} | ||
|
||
if (-not $Raw) { | ||
$xValue = $xValue * $script:ADXL345_GAINX | ||
$yValue = $yValue * $script:ADXL345_GAINY | ||
$zValue = $zValue * $script:ADXL345_GAINZ | ||
} | ||
|
||
if ($Limit) { | ||
$xValue = FilterValue -Value $xValue -Maximum $Limit | ||
$yValue = FilterValue -Value $yValue -Maximum $Limit | ||
$zValue = FilterValue -Value $zValue -Maximum $Limit | ||
} | ||
|
||
return [PSObject]@{x = $xValue; y = $yValue; z = $zvalue} | ||
} | ||
|
||
# Internal functions | ||
function InitializeDevice { | ||
Param ( | ||
[Parameter(Mandatory)] | ||
[Microsoft.PowerShell.IoT.I2CDevice] | ||
$Device | ||
) | ||
|
||
try { | ||
Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x00 | ||
Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x10 | ||
Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x08 | ||
} | ||
catch { | ||
Throw "Unable to initialize device '$($Device.FriendlyName)'. Message: $($_.Exception.Message)" | ||
} | ||
} | ||
|
||
function FilterValue { | ||
param ( | ||
[Parameter(Mandatory)] | ||
[Double] | ||
$Value, | ||
|
||
[Parameter(Mandatory)] | ||
[Double] | ||
$Maximum | ||
) | ||
|
||
if ($Value -ge 0) { | ||
return [Math]::Min($value, $Maximum) | ||
} else { | ||
return [Math]::Max($Value, -$Maximum) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Example module Microsoft.PowerShell.IoT.ADXL345 | ||
|
||
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. | ||
|
||
## Hardware setup | ||
|
||
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). | ||
|
||
ADXL345 sensor supports both I2C and SPI interfaces; here we'll use I2C. | ||
|
||
Wiring diagram with Raspberry Pi 3 looks like this: | ||
|
||
 | ||
|
||
## Software setup | ||
|
||
### Install PowerShell Core on Raspberry Pi | ||
|
||
Installation instructions can be found [here](https://github.com/PowerShell/PowerShell/tree/master/docs/installation/linux.md#raspbian). | ||
|
||
### Enable I2C interface on Raspberry Pi | ||
|
||
1. `sudo raspi-config` | ||
2. `5 Interfacing options` | ||
3. `P5 I2C` | ||
4. `Would you like ARM I2C interface to be enabled -> Yes` | ||
|
||
### Start Powershell and install modules | ||
|
||
**Don't forget to start PowerShell with sudo** or you'll be unable to access I2C bus. | ||
|
||
```powershell | ||
sudo pwsh | ||
Install-Module -Name Microsoft.PowerShell.IoT | ||
git clone https://github.com/PowerShell/PowerShell-IoT.git | ||
Import-Module ./PowerShell-IoT/Examples/Microsoft.PowerShell.IoT.ADXL345 | ||
``` | ||
|
||
### Collect Data | ||
|
||
To simply collect acceleration values in g: | ||
|
||
```powershell | ||
PS /home/pi> $accelerometer = Get-ADXL345Device | ||
PS /home/pi> Get-ADXL345Data -Device $accelerometer | ||
|
||
Name Value | ||
---- ----- | ||
y -0.03008072 | ||
x 0.0828058 | ||
z 0.86966985 | ||
``` | ||
|
||
To represent current acceleration on the 3 axis with bargraphs: | ||
|
||
```powershell | ||
PS /home/pi> $accelerometer = Get-ADXL345Device | ||
PS /home/pi> while ($true) { | ||
$data = Get-ADXL345Data -Device $accelerometer -Limit 1 | ||
Write-Progress -id 1 -Activity 'X axis' -Status 'Acceleration' -PercentComplete ($data.x * 50 + 50) | ||
Write-Progress -id 2 -Activity 'Y axis' -Status 'Acceleration' -PercentComplete ($data.y * 50 + 50) | ||
Write-Progress -id 3 -Activity 'Z axis' -Status 'Acceleration' -PercentComplete ($data.z * 50 + 50) | ||
} | ||
|
||
X axis | ||
Acceleration | ||
[ooooooooooooooooooooooooooooooooooooooooooooooooooo ] | ||
Y axis | ||
Acceleration | ||
[ooooooooooooooooooooooooooooooooooooooooooooooooo ] | ||
Z axis | ||
Acceleration | ||
[ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ] | ||
``` |
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.
A comment I won't block on... but I think it'd be good practice to have
Get-ADXL345Data
callGet-ADXL345Device
if$Device
was not passed in. It save's just a little bit of typing if they're using the default settings. 😄