Skip to content
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

MPU6050 #35

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ devices
#'PharoThings-Devices-ADXL345' -> #(#'PharoThings-Devices-I2C').
#'PharoThings-Devices-MCP9808' -> #(#'PharoThings-Devices-I2C').
#'PharoThings-Devices-BME280' -> #(#'PharoThings-Devices-I2C').
#'PharoThings-Devices-HD44780' -> #(#'PharoThings-Devices-I2C')
#'PharoThings-Devices-HD44780' -> #(#'PharoThings-Devices-I2C').
#'PharoThings-Devices-MPU6050' -> #(#'PharoThings-Devices-I2C')
}
5 changes: 5 additions & 0 deletions src/PharoThings-Devices-MPU6050.package/.filetree
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"separateMethodMetaAndSource" : false,
"noMethodMetaData" : true,
"useCypressPropertiesFile" : true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
I provide implementation of Gyroscope/Accelerator I2C sensor MPU6050.

The code for initialization and sensors reading is based from example:
https://tutorials-raspberrypi.com/measuring-rotation-and-acceleration-raspberry-pi/

Register map:
https://www.i2cdevlib.com/devices/mpu6050#registers

To use it:
sensor := (RpiBoard3B current) installDevice: PotMPU6050Device new.
or on inspector:
sensor := board installDevice: PotMPU6050Device new.

API:
readGyroscope. "#(1540 -9 -211)"
readGyroscopeSkaliert. "#(13.526718 -1.916031 -1.526718)"
readAccelerometer. "#(748 -892 15676)"
readAccelerometerSkaliert. "#(0.033691 -0.051025 0.974121)"
readRotationXY. "#(-3.224775339993247 -2.4755961852044157)"

sensor := (RpiBoard3B current) installDevice: PotMPU6050Device new.
sensor readRollPitchYaw
sensor printRollPitchYaw.
sensor finishRollPitchYaw.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
instance creation
defaultI2CAddress
^16r68
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
controlling
calculateDistanceA: anAccelerometerScaledA B: anAccelerometerScaledB
^ ((anAccelerometerScaledA*anAccelerometerScaledA)+(anAccelerometerScaledB*anAccelerometerScaledB)) sqrt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
controlling
calculateRotationX: anAccelerometerScaledX Y: anAccelerometerScaledY Z: anAccelerometerScaledZ
| radiansX radiansY rotationX rotationY |
radiansX := anAccelerometerScaledY arcTan: (self calculateDistanceA: anAccelerometerScaledX B: anAccelerometerScaledZ).
radiansY := anAccelerometerScaledX arcTan: (self calculateDistanceA: anAccelerometerScaledY B: anAccelerometerScaledZ).
rotationX := radiansX radiansToDegrees.
rotationY := radiansY radiansToDegrees * -1.
^ { rotationX . rotationY }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
controlling
connect
super connect.
self wakeUpSensor

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
controlling
readAccelerometer
| accX accY accZ |
accX := self readRegisters2C: 16r3b.
accY := self readRegisters2C: 16r3d.
accZ := self readRegisters2C: 16r3f.
^ { accX . accY . accZ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
controlling
readAccelerometerScaled
| acc accX accY accZ |
acc := self readAccelerometer.
accX := ((acc at: 1)/16384) asFloat round: 6.
accY := ((acc at: 2)/16384) asFloat round: 6.
accZ := ((acc at: 3)/16384) asFloat round: 6.
^ { accX . accY . accZ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
controlling
readGyroscope
| gyroX gyroY gyroZ |
gyroX := self readRegisters2C: 16r43.
gyroY := self readRegisters2C: 16r45.
gyroZ := self readRegisters2C: 16r47.
^ { gyroX . gyroY . gyroZ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
controlling
readGyroscopeScaled
| gyro gyroX gyroY gyroZ |
gyro := self readGyroscope.
gyroX := (gyro at: 1)/131 asFloat round: 6.
gyroY := (gyro at: 2)/131 asFloat round: 6.
gyroZ := (gyro at: 3)/131 asFloat round: 6.
^ { gyroX . gyroY . gyroZ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
controlling
readRegisters: anHex
| h l value |
h := i2cConnection read8BitsAt: anHex.
l := i2cConnection read8BitsAt: anHex + 1.
(h == 0 & l == 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have more meaningful names for h and l it would be much better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to improve it. I rewrote this drive from scratch, I'm just finishing some adjusts to push it again.

ifTrue: [ self wakeUpSensor. 1 milliSeconds wait.
h := i2cConnection read8BitsAt: anHex.
l := i2cConnection read8BitsAt: anHex + 1 ].
value := (h bitShift: 8) + l.
^ value
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
controlling
readRegisters2C: anHex
| value |
value := self readRegisters: anHex.
value >= 16r8000
ifTrue: [ ^ ((65535 - value) + 1) * -1]
ifFalse: [^ value ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
controlling
readRotationXY
| accelerometerScaled rotation rotationX rotationY |
accelerometerScaled := self readAccelerometerScaled.
rotation := self calculateRotationX: (accelerometerScaled at: 1) Y: (accelerometerScaled at: 2) Z: (accelerometerScaled at: 3).
rotationX := rotation at: 1.
rotationY := rotation at: 2.
^ { rotationX . rotationY }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
controlling
wakeUpSensor
i2cConnection write8BitsAt: 16r6b data: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"commentStamp" : "AllexOliveira 8/7/2019 18:28",
"super" : "PotI2CDevice",
"category" : "PharoThings-Devices-MPU6050",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"readProcess"
],
"name" : "PotMPU6050Device",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SystemOrganization addCategory: #'PharoThings-Devices-MPU6050'!
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(name 'PharoThings-Devices-MPU6050')
1 change: 1 addition & 0 deletions src/PharoThings-Devices-MPU6050.package/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }