Skip to content

Commit

Permalink
Merge branch 'pr-add-calibration-parameters' of https://github.com/ab…
Browse files Browse the repository at this point in the history
…urt2/puara-gestures into pr-add-calibration-parameters
  • Loading branch information
aburt2 committed Nov 29, 2023
2 parents f961d0d + 6c31cd7 commit da82d27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
48 changes: 18 additions & 30 deletions puara_gestures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,19 @@ void PuaraGestures::updateOrientation() {
void PuaraGestures::setCalibrationParameters(calibrationParameters calParams) {
// Save calibration parameters to class
// Magnetometer Cal
sx = calParams.sx;
sy = calParams.sy;
sz = calParams.sz;
h = calParams.h;
std::copy(std::begin(sx), std::end(sx), std::begin(calParams.sx));
std::copy(std::begin(sy), std::end(sy), std::begin(calParams.sy));
std::copy(std::begin(sz), std::end(sz), std::begin(calParams.sz));
std::copy(std::begin(h), std::end(h), std::begin(calParams.h));
// Accel Cal
accel_zerog = calParams.accel_zerog;
std::copy(std::begin(accel_zerog), std::end(accel_zerog), std::begin(calParams.accel_zerog));
// Gyro Cal
gyro_zerorate = calParams.gyro_zerorate;
std::copy(std::begin(gyro_zerorate), std::end(gyro_zerorate), std::begin(calParams.gyro_zerorate));
}

void PuaraGestures::setAccelerometerValues(float accelX, float accelY, float accelZ) {
// Calibrate accelerometer
float accelCal[3];
accelCal = calibrateAccelerometer(float accelX, float accelY, float accelZ);
calibrateAccelerometer(accelX, accelY, accelZ);

// Save calibrate values for sensor fusion and puara gestures
orientation.setAccelerometerValues(accelCal[0], accelCal[1], accelCal[2]);
Expand All @@ -192,8 +191,7 @@ void PuaraGestures::setGyroscopeValues(float gyroX, float gyroY, float gyroZ) {
static long then = esp_timer_get_time();
long now = esp_timer_get_time();
// Calibrate Gyroscope
float gyroCal[3];
gyroCal = calibrateGyroscope(float gyroX, float gyroY, float gyroZ);
calibrateGyroscope(gyroX, gyroY, gyroZ);

orientation.setGyroscopeDegreeValues(gyroCal[0], gyroCal[1], gyroCal[2], (now - then) * 0.000001);
then = now;
Expand All @@ -212,8 +210,7 @@ void PuaraGestures::setGyroscopeValues(float gyroX, float gyroY, float gyroZ) {

void PuaraGestures::setMagnetometerValues(float magX, float magY, float magZ) {
// Calibrate magnetometer, sensor fusion code already assumes calibrate magnetometer, hence calibration occurs here
float magCal[3];
magCal = calibrateMagnetometer(float magX, float magY, float magZ);
calibrateMagnetometer(magX, magY, magZ);

// Set magnetometer values for sensor fusion
orientation.setMagnetometerValues(magCal[0], magCal[1], magCal[2]);
Expand All @@ -224,34 +221,25 @@ void PuaraGestures::setMagnetometerValues(float magX, float magY, float magZ) {
this->magZ = magCal[2];
}

float PuaraGestures::calibrateMagnetometer(float magX, float magY, float magZ) {
float magCal[3] = {magX,magY,magZ};

void PuaraGestures::calibrateMagnetometer(float magX, float magY, float magZ) {
// Calibrate magnetometer
magCal[0] = sx[0]*(magX-h[0]) + sx[1]*(magX-h[0]) + sx[2]*(magX-h[0]);
magCal[1] = sy[0]*(magY-h[1]) + sy[1]*(magY-h[1]) + sy[2]*(magY-h[1]);
magCal[2] = sz[0]*(magZ-h[2]) + sz[1]*(magZ-h[2]) + sz[2]*(magZ-h[2]);
return magCal;
}

float PuaraGestures::calibrateAccelerometer(float accelX, float accelY, float accelZ) {
float accelCal[3] = {accelX,accelY,accelZ};

void PuaraGestures::calibrateAccelerometer(float accelX, float accelY, float accelZ) {
// Calibrate accelerometer
accelCal[0] -= accel_zerog[0];
accelCal[1] -= accel_zerog[1];
accelCal[2] -= accel_zerog[2];
return accelCal;
accelCal[0] = accelX - accel_zerog[0];
accelCal[1] = accelY - accel_zerog[1];
accelCal[2] = accelZ - accel_zerog[2];
}

float PuaraGestures::calibrateGyroscope(float gyroX, float gyroY, float gyroZ) {
float gyroCal[3] = {gyroX,gyroY,gyroZ};

void PuaraGestures::calibrateGyroscope(float gyroX, float gyroY, float gyroZ) {
// Calibrate magnetometer
gyroCal[0]-= gyro_zerorate[0];
gyroCal[1]-= gyro_zerorate[1];
gyroCal[2]-= gyro_zerorate[2];
return gyroCal;
gyroCal[0] = gyroX - gyro_zerorate[0];
gyroCal[1] = gyroY - gyro_zerorate[1];
gyroCal[2] = gyroZ - gyro_zerorate[2];
}


Expand Down
23 changes: 13 additions & 10 deletions puara_gestures.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
// Calibration parameter input structure
struct calibrationParameters {
// Accelerometer Parameters
float accel_zerog[3],
float accel_zerog[3];

// Gyroscope Parameters
float gyro_zerorate[3],
float gyro_zerorate[3];

// Magnetometer Parameters
float sx[3],
float sy[3],
float sz[3],
float h[3]
}
float sx[3];
float sy[3];
float sz[3];
float h[3];
};

class PuaraGestures {

Expand Down Expand Up @@ -150,22 +150,25 @@ class PuaraGestures {
IMU_Orientation::Euler getOrientationEuler();

// Calibration Methods
float calibrateMagnetometer(float magX, float magY, float magZ);
float calibrateAccelerometer(float accelX, float accelY, float accelZ);
float calibrateGyroscope(float gyroX, float gyroY, float gyroZ);
void calibrateMagnetometer(float magX, float magY, float magZ);
void calibrateAccelerometer(float accelX, float accelY, float accelZ);
void calibrateGyroscope(float gyroX, float gyroY, float gyroZ);
void setCalibrationParameters(calibrationParameters calParams);

// Magnetometer Calibration Variables
float sx[3] = {0.333, 0.333, 0.333};
float sy[3] = {0.333, 0.333, 0.333};
float sz[3] = {0.333, 0.333, 0.333};
float h[3] = {0,0,0};
float magCal[3];

// Accelerometer Calibration variables
float accel_zerog[3] = {0,0,0};
float accelCal[3];

/// Gyroscope Calibration variables
float gyro_zerorate[3] = {0,0,0};
float gyroCal[3];

// touch array
void updateTouchArray (int *discrete_touch, int touchSize);
Expand Down

0 comments on commit da82d27

Please sign in to comment.