Skip to content

Instantly share code, notes, and snippets.

@pulsar256
Created July 11, 2015 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pulsar256/4c11569fb45eadfe81fa to your computer and use it in GitHub Desktop.
Save pulsar256/4c11569fb45eadfe81fa to your computer and use it in GitHub Desktop.
Vibration Tester
#include "U8glib.h"
#include <Wire.h>
#define TWI_FREQ 400000L
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
const int MPU=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int16_t minGZ; int16_t maxGZ; int16_t minGX;
int16_t maxGX; int16_t minGY; int16_t maxGY;
char strBuf[64];
void draw() {
uint8_t y=0;
uint8_t lh=11;
u8g.setFont(u8g_font_profont11);
y+=lh;
u8g.drawStr( 0, y, "1k gyro z samples");
y+=lh+3;
sprintf(strBuf,"gX %+6d ... %+6d", minGX, maxGX);
u8g.drawStr( 0, y, strBuf);
y+=lh;
sprintf(strBuf,"gY %+6d ... %+6d", minGY,maxGY);
u8g.drawStr( 0, y, strBuf);
y+=lh;
sprintf(strBuf,"gZ %+6d ... %+6d", minGZ, maxGZ);
u8g.drawStr( 0, y, strBuf);
y+=lh+3;
long av = (abs(maxGX - minGX) + abs(maxGY - minGY) + abs(maxGZ - minGZ)) / 3;
//sprintf(strBuf,"dt %5d %5d %5d", abs(maxGX - minGX), abs(maxGY - minGY), abs(maxGZ - minGZ) );
sprintf(strBuf,"V-Score %5d", av);
u8g.drawStr( 0, y, strBuf);
}
void readGyro(){
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}
void setup() {
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Wire.beginTransmission(MPU);
Wire.write(0x19); // see register map, starting at 0x19
Wire.write(0x00); // no filtering
Wire.write(0x00); // acc full scale
Wire.write(0x00); // gyro full scale
Wire.endTransmission(true);
}
void loop() {
minGZ=0; maxGZ=0; minGX=0;
maxGX=0; minGY=0; maxGY=0;
// capture 1k samples and determine the max peaks for the gyro sensor.
for (int i=0; i<1000; i++){
readGyro();
// offsets for my gyro sensor, too lazy to do autozeroing.
// your numbers will be different!
GyZ += 85;
GyX += 315;
minGX = GyX < minGX ? GyX : minGX;
maxGX = GyX > maxGX ? GyX : maxGX;
minGY = GyY < minGY ? GyY : minGY;
maxGY = GyY > maxGY ? GyY : maxGY;
minGZ = GyZ < minGZ ? GyZ : minGZ;
maxGZ = GyZ > maxGZ ? GyZ : maxGZ;
}
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment