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

Expose MPU-6050 FIFO and some helper functions #39

Merged
merged 1 commit into from
Jan 7, 2025

Conversation

CTho9305
Copy link
Contributor

@CTho9305 CTho9305 commented Jan 3, 2025

I'm using the MPU-6050 to measure vibration, which requires rapid acceleration sampling. I achieve that by configuring the FIFO to store acceleration samples at 200Hz, accumulate 128 samples (power of 2 for FFT; 256 doesn't fit in the 1024b FIFO since each sample is 6B), then reading them out of the FIFO for processing (calculate magnitude, then FFT or actually ulab.utils.spectrogram). The reading needs to happen fast so that the FIFO doesn't overflow during the readout.

The FIFO itself is highly configurable, so it isn't reasonable for the library to track exactly how the user has configured it.

I also expose helper functions for scaling the samples that are read out of the FIFO, though the user still has to know how to unpack the raw bytes.

My surrounding code, very roughly, is:

mpu = adafruit_mpu6050.MPU6050(i2c)
# We want to sample 200Hz, so divide 8kHz by 40 to get 200Hz
mpu.sample_rate_divisor = 39
mpu.fiforst = True
mpu.accel_fifo_en = 1
mpu.fifo_en = 1 # I'm not sure this is strictly necessary

hz_per_bin = 128/200
while mpu.fifo_count < (6*128):
    time.sleep(0.01)
fifo_bytes = mpu.read_whole_fifo()[:128*6]
data = [None] * 128
for i in range(0, 128):
    # annoying user-side unpack,
    (x, y, z) = unpack_from(">hhh", fifo_bytes, i*6)
    data[i] = mpu.scale_accel([x,y,z])
def root_of_sum_of_squares(vals):
    sum_of_squares = sum(x**2 for x in vals)
    return math.sqrt(sum_of_squares)
mags = list(root_of_sum_of_squares(x) for x in data)
arr = ulab.numpy.array(mags)
spec = ulab.utils.spectrogram(arr)
ispec = ulab.numpy.array(spec, dtype=ulab.numpy.int16)
# the second half is a mirror of the first half
#print(f'{ispec.tolist()[:65]=}')
max_bin = ulab.numpy.argmax(ispec[1:65])+1
max_power = ispec[max_bin]
max_bin_hz = max_bin * hz_per_bin
print(f'%3.1f Hz, %3d RPM, %3d' % (max_bin_hz, max_bin_hz*60, max_power))

@CTho9305 CTho9305 marked this pull request as ready for review January 4, 2025 01:46
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! One possible bug.

adafruit_mpu6050.py Show resolved Hide resolved
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thank you!

@tannewt tannewt merged commit 8844efe into adafruit:main Jan 7, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants