-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
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
Add support for the adxl345 accelerometer for the BTT EBB42 V1.2 toolhead #27635
base: bugfix-2.1.x
Are you sure you want to change the base?
Conversation
Add support for the adxl345 accelerometer
We will need to explore the methods used for sampling, motion patterns that are analyzed, and the conversion of the collected data into settings to apply to our existing input shaping features. This is a challenging frontier for the casual developer, but I believe we can tackle it! |
This is going to get a lot of people excited I think. After a very quick quick look at the code, it looks to me like a wrapping of an SPI interface for the adxl345. Have I read that correctly? Using the accelerometer to measure resonant frequencies can be done with a frequency sweep by printing the same pattern used here. Whilst this is occurring, it will be necessary to poll the appropriate |
Here's a sample of arduinoFFT code from the chatbot that should help our cause… #include <SPI.h>
#include <arduinoFFT.h>
// Constants
#define FFT_SIZE 128 // Size of FFT (power of 2)
#define SAMPLING_FREQUENCY 1000 // Sampling frequency in Hz
#define ADXL_CS_PIN 10 // Chip Select pin for ADXL
// Globals
double vReal[FFT_SIZE];
double vImag[FFT_SIZE];
arduinoFFT FFT = arduinoFFT(vReal, vImag, FFT_SIZE, SAMPLING_FREQUENCY);
void setup() {
Serial.begin(115200);
pinMode(ADXL_CS_PIN, OUTPUT);
SPI.begin();
// ADXL initialization code here
}
void loop() {
// Collect data
for (int i = 0; i < FFT_SIZE; i++) {
vReal[i] = readADXL_X(); // Replace with function to read ADXL X-axis acceleration
vImag[i] = 0; // Imaginary part is always 0
delayMicroseconds(1000000 / SAMPLING_FREQUENCY);
}
// Perform FFT
FFT.Windowing(FFT_WIN_TYP_HAMMING, FFT_FORWARD); // Apply a window function
FFT.Compute(FFT_FORWARD); // Compute FFT
FFT.ComplexToMagnitude(); // Compute magnitude
// Find dominant frequency
double peakFrequency = FFT.MajorPeak();
Serial.print("Peak Frequency: ");
Serial.println(peakFrequency, 2);
delay(1000); // Delay for visualization
}
// Example function to read X-axis acceleration
double readADXL_X() {
digitalWrite(ADXL_CS_PIN, LOW);
SPI.transfer(0x32 | 0x80); // Send read command for X-axis
uint8_t lsb = SPI.transfer(0x00);
uint8_t msb = SPI.transfer(0x00);
digitalWrite(ADXL_CS_PIN, HIGH);
int16_t raw = (msb << 8) | lsb;
return raw * 0.004; // Convert to g's (adjust based on ADXL scale)
} |
Oh that makes tons of sense. Then you don't even have to know what the input frequency is when measuring the output. |
Yep. We just need enough samples at a regular interval to get some complete waves. There's probably some dynamic methods as well, such as counting up frequency peaks in buckets and then narrowing down on each peak. |
So are we trying to FFT the "ringing" from an abrupt step-input, or should we feed in sinusoidal motion of known peak distance/acceleration, in a frequency sweep, and look for the induced over/undershoots of the target points? I agree that sharp-input ringing could potentially give a "quicker output" through fft, and may give better accuracy for resonance decay-rates, but I'd expect the second method to give better settings for exactly what amount of over/undershoot to expect, and thus how to compensate best for the initial inaccuracy caused by axis resonance. Ok, so I guess both would give different bits of information that would benefit the calibrated settings we could generate from the test. |
Thanks for the feedback. I will have a look into this.
If we don't care about the real time aspect of the data, then the sensor with its 32 level FIFO is quite powerful. |
FWIW, Nyquist says we need to sample at a minimum of 5x the frequency of the fastest wave you want to capture. |
@robherc |
May want to plot that on a sine wave. At 2x frequency, a sine wave can be read at only zero crossings, so the wave would be 100% entirely missed in all readings. |
The key word you missed here is "faster". |
I'm not interested in fighting with you here...was just trying to help avoid potentially painful "learning experiences" along the path to a working solution. What I do know for certain is that 5x sampling is the minimum sample-rate to extract enough data from a single wave to fairly reliably determine both its frequency AND its amplitude. So I may have worded it poorly before, but the thought I was trying to convey was "If we sample at 5x the highest frequency of interest, we can save some added effort in the processing, and enjoy increased reliability of our readings." |
I think we're probably not looking higher than 100Hz. Sampling at 500Hz would both be very possible and also hopefully give good data. One factor in choosing sampling rate is, I am slightly guessing, noise. Can't claim to be an expert on Nyquist but I vaguely think the faster than 2x thing might not take noise into account. |
Right, but the faster the sample rate, the more noisy the data will be. Also the amount of data will need to be considered, as the data needs to be stored and processed. Probably we can learn quite a bit from systems that already use an accelerometer. |
My default mode is to reinvent wheels. One of these days I am going to come up with The Wheel 2.0 and astound everyone. But you're right, it would be a lot quicker and easier to |
For Wheel 2.0 could you remove Pi from the calculations, make it an even 3 please? Much easier to remember and the math would be much simpler. |
Add support for the adxl345 accelerometer for the BTT EBB42 V1.2 toolhead
Requirements
BTT EBB42 V1.2 toolhead
Benefits
The accelerometer can be used for input shaping.
Configurations
Just enable
ACCELEROMETER_ADXL345
inConfiguration_adv.h
Note: This is new, and currently there is no code to process the accelerometer data.
Related Issues
#27588