Skip to content

Commit

Permalink
Added bias adjustments for gyroscrope
Browse files Browse the repository at this point in the history
  • Loading branch information
jackbogatz committed Dec 4, 2024
1 parent c692591 commit d9b4e5f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 5 deletions.
15 changes: 14 additions & 1 deletion hardware/IMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ IMU::IMU()
ifstream.close();
}

void IMU::SetGyroBiasX(double x)
{
gyroBiasX = x;
}
void IMU::SetGyroBiasY(double y)
{
gyroBiasY = y;
}
void IMU::SetGyroBiasZ(double z)
{
gyroBiasZ = z;
}

std::tuple<double, double, double> IMU::GetBodyAcceleration()
{
std::fstream ifstream(accelPath + "/in_accel_x_raw");
Expand Down Expand Up @@ -76,6 +89,6 @@ std::tuple<double, double, double> IMU::GetBodyAngularRate()
ifstream >> nAnglVelZ;
ifstream.close();

return std::make_tuple(nAnglVelX * 0.000266, -nAnglVelY * 0.000266, -nAnglVelZ * 0.000266);
return std::make_tuple(nAnglVelX * 0.000266+ gyroBiasX, -nAnglVelY * 0.000266 + gyroBiasY, -nAnglVelZ * 0.000266 + gyroBiasZ);

}
16 changes: 15 additions & 1 deletion hardware_simulation/IMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@

IMU::IMU() {}


void IMU::SetGyroBiasX(double x)
{
gyroBiasX = x;
}
void IMU::SetGyroBiasY(double y)
{
gyroBiasY = y;
}
void IMU::SetGyroBiasZ(double z)
{
gyroBiasZ = z;
}

std::tuple<double, double, double> IMU::GetBodyAngularRate() {
return UDPClient::GetInstance().GetBodyAngularRate();
}

std::tuple<double, double, double> IMU::GetBodyAcceleration() {
return UDPClient::GetInstance().GetBodyAcceleration();
}
}
13 changes: 13 additions & 0 deletions hardware_test/IMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ std::tuple<double, double, double> IMU::GetBodyAcceleration()
{
return std::make_tuple(0,0,10.0);
}

void IMU::SetGyroBiasX(double x)
{
gyroBiasX = x;
}
void IMU::SetGyroBiasY(double y)
{
gyroBiasY = y;
}
void IMU::SetGyroBiasZ(double z)
{
gyroBiasZ = z;
}
12 changes: 12 additions & 0 deletions include/IMU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ class IMU
IMU();
std::tuple<double, double, double> GetBodyAngularRate(); // Returns angular rate, p, q, and r in order
std::tuple<double, double, double> GetBodyAcceleration(); // Returns linear acceleration, x, y, z
void SetGyroBiasX(double x);
void SetGyroBiasY(double y);
void SetGyroBiasZ(double z);
double gyroBiasX;
double gyroBiasY;
double gyroBiasZ;

// private:
// double gyroBiasX;
// double gyroBiasY;
// double gyroBiasZ;

};
4 changes: 3 additions & 1 deletion include/Mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Mode
enum Phase
{
Calibration,
GyroBiasOffset,
TestTVC,
Idle,
Launch,
Expand All @@ -23,11 +24,12 @@ class Mode
};

Mode(Mode::Phase eInitialMode);
bool Update(Navigation& navigation, Controller& controller, Igniter& igniter);
bool Update(Navigation& navigation, Controller& controller, Igniter& igniter, IMU& imu);
private:
Mode::Phase eCurrentMode;

Mode::Phase UpdateCalibration(Navigation& navigation, Controller& controller, double currentTime);
Mode::Phase GetGyroBiasOffset(Navigation& navigation, Controller& controller, IMU& imu, double currentTime);
Mode::Phase UpdateTestTVC(Navigation& navigation, Controller& controller, double currentTime);
Mode::Phase UpdateIdle(Navigation& navigation, Controller& controller, double currentTime);
Mode::Phase UpdateLaunch(Navigation& navigation, Controller& controller, Igniter& igniter, double current_time);
Expand Down
2 changes: 1 addition & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main()



while(mode.Update(navigation, controller, igniter)) {}
while(mode.Update(navigation, controller, igniter, imu)) {}

//#ifdef NDEBUG
// gpioTerminate();
Expand Down
38 changes: 37 additions & 1 deletion src/Mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,38 @@ Mode::Phase Mode::UpdateCalibration(Navigation& navigation, Controller& controll
return Mode::Calibration;
}

Mode::Phase Mode::GetGyroBiasOffset(Navigation& navigation, Controller& controller, IMU& imu, double currentTime)
{
static int loops = 1;
std::tuple<double,double,double> gyro = imu.GetBodyAngularRate();
static double gyro_x = 0;
static double gyro_y = 0;
static double gyro_z = 0;

gyro_x += std::get<0>(gyro);
gyro_y += std::get<1>(gyro);
gyro_z += std::get<2>(gyro);


if(loops == 100)
{
gyro_x /= loops;
gyro_y /= loops;
gyro_z /= loops;

imu.SetGyroBiasX(-gyro_x);
imu.SetGyroBiasY(-gyro_y);
imu.SetGyroBiasZ(-gyro_z);
return Mode::Idle;
}

++loops;
return Mode::GyroBiasOffset;



}

Mode::Phase Mode::UpdateTestTVC(Navigation& navigation, Controller& controller, double currentTime) {


Expand Down Expand Up @@ -233,7 +265,7 @@ Mode::Phase Mode::UpdateSafeMode(Navigation& navigation, Controller& controller,
}


bool Mode::Update(Navigation& navigation, Controller& controller, Igniter& igniter) {
bool Mode::Update(Navigation& navigation, Controller& controller, Igniter& igniter, IMU& imu) {

// Track total elapsed time and delta time
static double currentTime = 0;
Expand All @@ -258,6 +290,10 @@ bool Mode::Update(Navigation& navigation, Controller& controller, Igniter& ignit
// Telemetry::GetInstance().RunTelemetry(navigation, controller, 0.05, 0.08);
this->eCurrentMode = UpdateCalibration(navigation, controller, currentTime);
break;
case GyroBiasOffset:
// Telemetry::GetInstance().RunTelemetry(navigation, controller, 0.05, 0.08);
this->eCurrentMode = GetGyroBiasOffset(navigation, controller, imu, currentTime);
break;
case TestTVC:
Telemetry::GetInstance().RunTelemetry(navigation, controller, 0.05, 0.08);
this->eCurrentMode = UpdateTestTVC(navigation, controller, currentTime);
Expand Down

0 comments on commit d9b4e5f

Please sign in to comment.