Last active
July 14, 2020 19:45
-
-
Save jborza/4b8648ec583c2ef3b42fa1361ed4fbfa to your computer and use it in GitHub Desktop.
This file contains 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
#include "M5StickC.h" | |
float accX = 0; | |
float accY = 0; | |
float accZ = 0; | |
#define SAMPLE_COUNT 64 | |
#define BUFFER_SIZE SAMPLE_COUNT | |
float bufferX[BUFFER_SIZE]; | |
float bufferY[BUFFER_SIZE]; | |
void setup() { | |
// put your setup code here, to run once: | |
M5.begin(); | |
M5.IMU.Init(); | |
//horizontal rotation | |
M5.Lcd.setRotation(3); | |
M5.Lcd.fillScreen(DARKGREEN); | |
M5.Lcd.setTextSize(1); | |
M5.Lcd.println("See serial plotter!"); | |
M5.Lcd.printf("IMU type: %d - (2=MPU6886)\n", M5.IMU.imuType); | |
M5.Lcd.printf("aRes: %f Acscale: %d", M5.MPU6886.aRes, M5.MPU6886.Acscale); | |
} | |
char buffer[64]; | |
int x,y; | |
#define PULSE_THRESHOLD 0.8 | |
enum { MODE_DETECTING, MODE_COLLECTING, MODE_PRINTING, MODE_EVALUATING}; | |
int mode = MODE_DETECTING; | |
int collecting_idx = 0; | |
void print_buffer(){ | |
for(int i = 0; i < SAMPLE_COUNT; i++){ | |
Serial.printf("x:%f,y:%f\n",bufferX[i],bufferY[i]); | |
} | |
//separator | |
for(int i = 0; i < SAMPLE_COUNT; i++){ | |
Serial.println("x:0,y:0"); | |
} | |
} | |
#define MIN(a,b) (((a)<(b))?(a):(b)) | |
#define MAX(a,b) (((a)>(b))?(a):(b)) | |
void evaluate_sample(){ | |
//find the max and min of each axis | |
float maxX = 0, maxY = 0, minX = 0, minY = 0; | |
for(int i = 0; i < SAMPLE_COUNT; i++){ | |
maxX = MAX(maxX, bufferX[i]); | |
minX = MIN(minX, bufferX[i]); | |
maxY = MAX(maxY, bufferY[i]); | |
minY = MIN(minY, bufferY[i]); | |
} | |
M5.Lcd.setTextSize(3); | |
M5.Lcd.setCursor(0,40); | |
//M5.Lcd.printf("maxX: %f, minX: %f, maxY:%f, minY:%f", maxX, minX,maxY, minY); | |
//find the first peak | |
float maxAbsX = 0, maxAbsY = 0; | |
maxAbsX = MAX(maxX, fabs(minX)); | |
maxAbsY = MAX(maxY, fabs(minY)); | |
int dx = 0, dy = 0; | |
if(maxAbsX > maxAbsY){ | |
//x peak | |
if(maxX > fabs(minX)){ | |
dx = 1; | |
M5.Lcd.println("TOP "); | |
} | |
else{ | |
dx = -1; | |
M5.Lcd.println("BOTTOM"); | |
} | |
} | |
else{ | |
//y peak | |
if(maxY > fabs(minY)){ | |
//left | |
dy = -1; | |
M5.Lcd.println("LEFT "); | |
} | |
else{ | |
dy = 1; | |
M5.Lcd.println("RIGHT "); | |
} | |
} | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
M5.IMU.getAccelData(&accX,&accY,&accZ); | |
//detect a pulse from the threshold | |
if(mode == MODE_DETECTING){ | |
//apply threshold detection | |
if(fabs(accX) < PULSE_THRESHOLD && fabs(accY) < PULSE_THRESHOLD) | |
return; | |
mode = MODE_COLLECTING; | |
collecting_idx = 0; | |
} | |
else if(mode == MODE_COLLECTING){ | |
bufferX[collecting_idx] = accX; | |
bufferY[collecting_idx] = accY; | |
collecting_idx++; | |
if(collecting_idx == SAMPLE_COUNT) | |
{ | |
mode = MODE_PRINTING; | |
} | |
delay(1); //sleep a bit before next measurement | |
return; | |
} | |
else if(mode == MODE_PRINTING){ | |
print_buffer(); | |
mode = MODE_EVALUATING; | |
} | |
else if(mode == MODE_EVALUATING){ | |
evaluate_sample(); | |
mode = MODE_DETECTING; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment