[Swlug] An interesting time with an accelerometer
Rhys Sage
rhys_sage at yahoo.com
Sat Jun 25 18:36:29 UTC 2022
After hunting around a bit I found some C++ code that uses accelerometer data (but curiously not gyroscopic data) to produce roll and pitch data. I had to convert from C++ to Python which was not that challenging. I can't test the original as I have Python on my Pi Pico and not C++. I really don't want to have to rig up a Pi Pico with C++ as it's a little out of the scope of what I'm doing so I'm presuming the C++ code actually works. Having said that I'm skeptical because my conversion produced angles in the tens of thousands. This is the original...
https://github.com/mahengunawardena/BeagleboneBlack_I2C_ADXL345/blob/master/ADXL345Accelerometer.cpp
My Python conversion is
from imu import MPU6050
from time import sleep
from machine import Pin, I2C
import math
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
imu = MPU6050(i2c)
while True:
#input('rotate some, hit enter')
ax=round(imu.accel.x,2)
ay=round(imu.accel.y,2)
az=round(imu.accel.z,2)
AccelerationX = ax * 3.9
AccelerationY = ay * 3.9
AccelerationZ = az * 3.9
Pitch = 180 * math.atan (AccelerationX/math.sqrt(AccelerationY*AccelerationY + AccelerationZ*AccelerationZ))/math.pi;
Roll = 180 * math.atan (AccelerationY/math.sqrt(AccelerationX*AccelerationX + AccelerationZ*AccelerationZ))/math.pi;
print("Roll", int(Roll), " Pitch", int(Pitch),end="\r")
I don't see obviously any difference - I think I have encompassed all the necessary code which in C++ looked like this section:
accelerationX = (signed int)(((signed int)rawData_X) * 3.9);
accelerationY = (signed int)(((signed int)rawData_Y) * 3.9);
accelerationZ = (signed int)(((signed int)rawData_Z) * 3.9);
pitch = 180 * atan (accelerationX/sqrt(accelerationY*accelerationY + accelerationZ*accelerationZ))/M_PI;
roll = 180 * atan (accelerationY/sqrt(accelerationX*accelerationX + accelerationZ*accelerationZ))/M_PI;
The output is not within the anticipated range and looks to be somewhat strange....
MicroPython v1.19 on 2022-06-16; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT
Roll -1.067217e-07 Pitch 80.44571
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
MicroPython v1.19 on 2022-06-16; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT
Roll 0 Pitch 80977
Has anybody any suggestions? I rather suspect the original github code might be bad since there's no inclusion of gyroscopic data and nothing that looks particularly Kalman.
Rhys Sage
More information about the Swlug
mailing list